Note The format problem of BMP pictures, 32-bit ARGB or 24-bit RGB. The material you use must be aware of how many bits are used. Otherwise, the wrong picture or any other error will be displayed.
The code is as follows
32-bit version
#include <GL/glut.h>StaticGlint ImageWidth;StaticGlint ImageHeight;StaticGlint Pixellength;Staticglubyte*Pixeldata; #include<stdio.h>#include<stdlib.h>voidDisplayvoid){ //clearing the screen is not necessary//each time you draw, the screen is covered//so the results are the same regardless of whether the screen is cleared//glclear (gl_color_buffer_bit); //Draw pixels//Glbitmap (imagewidth,imageheight,0,0,0.0,0.0,pixeldata);gldrawpixels (ImageWidth, ImageHeight, Gl_abgr_ext, Gl_unsigned_byte, Pixeldata); //Finish Drawingglutswapbuffers ();}intMainintargcChar*argv[]) { //Open Filefile* pFile = fopen ("/home/aerk/bbb.bmp","RB"); if(PFile = =0) Exit (0); //Read image size informationFseek (PFile,0x0012, Seek_set); Fread (&imagewidth,sizeof(ImageWidth),1, PFile); Fread (&imageheight,sizeof(ImageHeight),1, PFile); //Calculate pixel data lengthPixellength = ImageWidth *4; while(Pixellength%4!=0 ) ++pixellength; Pixellength*=ImageHeight; //reading pixel dataPixeldata = (glubyte*)malloc(pixellength); if(Pixeldata = =0) Exit (0); Fseek (PFile, Wu, Seek_set); Fread (Pixeldata, Pixellength,1, PFile); //Close Filefclose (PFile); //initialize glut and runGlutinit (&argc, argv); Glutinitdisplaymode (glut_double|Glut_rgba); Glutinitwindowposition ( -, -); Glutinitwindowsize (ImageWidth, imageheight); Glutcreatewindow ("FileName"); Glutdisplayfunc (&display); Glutmainloop (); //Freeing Memory//in fact, the Glutmainloop function never returns, and here it never arrives.//write here to release memory just for a personal habit//don't worry about memory not being freed. All memory is automatically reclaimed by the operating system at the end of the program Free(Pixeldata); return 0;}
24-bit version
#include <GL/glut.h>StaticGlint ImageWidth;StaticGlint ImageHeight;StaticGlint Pixellength;Staticglubyte*Pixeldata; #include<stdio.h>#include<stdlib.h>voidDisplayvoid){ //clearing the screen is not necessary//each time you draw, the screen is covered//so the results are the same regardless of whether the screen is cleared//glclear (gl_color_buffer_bit); //Draw pixels//Glbitmap (imagewidth,imageheight,0,0,0.0,0.0,pixeldata);gldrawpixels (ImageWidth, ImageHeight, Gl_bgr_ext, Gl_unsigned_byte, Pixeldata); //Finish Drawingglutswapbuffers ();}intMainintargcChar*argv[]) { //Open Filefile* pFile = fopen ("/home/aerk/bbb.bmp","RB"); if(PFile = =0) Exit (0); //Read image size informationFseek (PFile,0x0012, Seek_set); Fread (&imagewidth,sizeof(ImageWidth),1, PFile); Fread (&imageheight,sizeof(ImageHeight),1, PFile); //Calculate pixel data lengthPixellength = ImageWidth *3; while(Pixellength%4!=0 ) ++pixellength; Pixellength*=ImageHeight; //reading pixel dataPixeldata = (glubyte*)malloc(pixellength); if(Pixeldata = =0) Exit (0); Fseek (PFile, Wu, Seek_set); Fread (Pixeldata, Pixellength,1, PFile); //Close Filefclose (PFile); //initialize glut and runGlutinit (&argc, argv); Glutinitdisplaymode (glut_double|Glut_rgba); Glutinitwindowposition ( -, -); Glutinitwindowsize (ImageWidth, imageheight); Glutcreatewindow ("FileName"); Glutdisplayfunc (&display); Glutmainloop (); //Freeing Memory//in fact, the Glutmainloop function never returns, and here it never arrives.//write here to release memory just for a personal habit//don't worry about memory not being freed. All memory is automatically reclaimed by the operating system at the end of the program Free(Pixeldata); return 0;}
OpenGL open local BMP picture drawing