OpenGL texture Paster rendering consists of three steps:
(1) Load OpenGL texture resources before painting
A. Read BMP wide and high values and pixel Matrices
B. Call glgentextures to obtain the texture Object ID
C. Call glbindtextures to bind the texture Object ID so that subsequent OpenGL commands use the texture of this ID
D. Call glteximage2d to generate a texture
E. Set Some OpenGL texture processing parameters.
Bool loadgltextures () // load the bitmap and convert it to the texture {int status = false; // Status Indicator int W = 0; int h = 0; unsigned char * pimage = NULL; if (readbmp ("Debug/data/tree.bmp", pimage, W, H) {status = true; // set status to trueglgentextures (1, & texture [0]); // create a texture // use glbindtexture (gl_texture_2d, texture [0]), a typical texture generated from the bitmap data; // generate a texture glteximage2d (gl_texture_2d, 0, 3, W, H, 0, gl_rgb, gl_unsigned_byte, pimage/* textureimage [0]-> data */); gltexparameteri (gl_texture_2d, callback, gl_linear ); // Linear Filter gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear); // Linear Filter} If (pimage) Free (pimage); Return status; // return status}
(2) Enable texture ing
Glable (gl_texture_2d); // enable texture ing
(3) scenario Rendering
A. Bind a texture Object ID so that subsequent OpenGL commands use the texture of this ID
B. Set vertex texture coordinates
Int drawglscene (glvoid) {glclear (gl_color_buffer_bit | cached); // clear the screen and depth cache glloadidentity (); // reset the current model observation matrix glbindtexture (gl_texture_2d, texture [0]); // select the texture glbegin (gl_quads); // The Front gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f ); // The bottom left gltexcoord2f (1.0f, 0.0f) of the texture and quadrilateral; glvertex3f (1.0f,-1.0f, 1.0f); // the bottom right gltexcoord2f (1.0f, 1.0f) of the texture and quadrilateral ); glvertex3f (1.0f, 1.0f, 1.0f); // the upper right gltexcoord2f (0.0f, 1.0f) of the texture and quadrilateral; glvertex3f (-1.0f, 1.0f, 1.0f ); // texture and top left of the Quadrilateral // gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f); // texture and gltexcoord2f (1.0f, 1.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f); // The right gltexcoord2f (0.0f, 1.0f) of the texture and quadrilateral; glvertex3f (1.0f, 1.0f,-1.0f ); // texture and top left gltexcoord2f (0.0f, 0.0f) of the quadrilateral; glvertex3f (1.0f,-1.0f,-1.0f); // texture and bottom left of the Quadrilateral // top gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f); // the top left gltexcoord2f (0.0f, 0.0f) of the texture and quadrilateral; glvertex3f (-1.0f, 1.0f, 1.0f ); // texture and gltexcoord2f (1.0f, 0.0f) at the lower left of the quadrilateral; glvertex3f (1.0f, 1.0f, 1.0f); // texture and gltexcoord2f (1.0f, 1.0f) at the lower right of the quadrilateral ); glvertex3f (1.0f, 1.0f,-1.0f); // the top right of the texture and quadrilateral // gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f,-1.0f,-1.0f ); // The upper right gltexcoord2f (0.0f, 1.0f) of the texture and quadrilateral; glvertex3f (1.0f,-1.0f,-1.0f); // the upper left gltexcoord2f (0.0f, 0.0f) of the texture and quadrilateral ); glvertex3f (1.0f,-1.0f, 1.0f); // The bottom left gltexcoord2f (1.0f, 0.0f) of the texture and quadrilateral; glvertex3f (-1.0f,-1.0f, 1.0f ); // texture and right bottom of the Quadrilateral // gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f); // texture and gltexcoord2f (1.0f, 1.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f); // The right gltexcoord2f (0.0f, 1.0f) of the texture and quadrilateral; glvertex3f (1.0f, 1.0f, 1.0f ); // texture and the top left gltexcoord2f (0.0f, 0.0f) of the quadrilateral; glvertex3f (1.0f,-1.0f, 1.0f); // texture and the bottom left gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f); // The bottom left gltexcoord2f (1.0f, 0.0f) of the texture and quadrilateral; glvertex3f (-1.0f,-1.0f, 1.0f ); // texture and gltexcoord2f (1.0f, 1.0f) in the lower right of the quadrilateral; glvertex3f (-1.0f, 1.0f, 1.0f); // texture and gltexcoord2f (0.0f, 1.0f) in the upper right of the quadrilateral ); glvertex3f (-1.0f, 1.0f,-1.0f); // glend (); Return true ;}
The drawn result is as follows (cube ):
++
Note the following when reading BMP pixels (For more information about the BMP format, see this article.):
(1) The length and width of the texture must be equal, and it is the Npower of 2 [n> 0 & n <10]
(2) Remove invalid pixels that require 4-byte alignment at the end of each line in the BMP file (keep data tight)
(3) store each pixel in RGB order, while BMP stores BGR. Therefore, the R and B values must be exchanged.
The Code is as follows (taking 24-bit BMP as an example ):
bool ReadBMP(CHAR *Filename, unsigned char*& pImage, int& w, int& h){FILE* pBmpFile = fopen(Filename, "rb");if (!pBmpFile) return false;BITMAPFILEHEADER filehdr;BITMAPINFOHEADER infohdr;fread(&filehdr, 1, sizeof(filehdr), pBmpFile); fread(&infohdr, 1, sizeof(infohdr), pBmpFile); unsigned tmpImaLength = filehdr.bfSize - filehdr.bfOffBits;unsigned char* pTmpIma = new unsigned char[tmpImaLength];fseek(pBmpFile, filehdr.bfOffBits, SEEK_SET); fread(pTmpIma, 1, tmpImaLength, pBmpFile);w = infohdr.biWidth;h = infohdr.biHeight;unsigned __int64 imageLength = (unsigned __int64)3*w*h;pImage = new unsigned char[imageLength];unsigned idx = 0;unsigned bmpIdx = 0;for (long i=0; i<infohdr.biHeight; i++){for (long j=0; j<infohdr.biWidth; j++){pImage[idx] = pTmpIma[bmpIdx+2];pImage[idx+1] = pTmpIma[bmpIdx+1];pImage[idx+2] = pTmpIma[bmpIdx];bmpIdx += 3;idx += 3;}long lineLeftByte = (3*infohdr.biWidth)%4;if (lineLeftByte>0)bmpIdx += (4-lineLeftByte);}delete [] pTmpIma;fclose(pBmpFile);return true;}
To avoid these troubles, you can use auxdibimageloada provided by OpenGL to read BMP images.
Aux_rgbimagerec * textureimage [1]; // creates a texture storage space memset (textureimage, 0, sizeof (void *) * 1); textureimage [0] = auxdibimageloada (filename );
After generating the texture, remember to free up the heap memory of texureimage [0]-> data and texureimage [0!