OpenGL loads BMP files

Source: Internet
Author: User

The following code is from nehe, based on the framework of Lesson 1:

Http://download.csdn.net/source/1907052

 

 

1. define a global variable to store the BMP file:

Gluint texture [1]; // stores a texture

 

2. Define two functions to load the BMP file:

/* <Br/> * loadbmp <br/> * load bitmap image <br/> */<br/> aux_rgbimagerec * loadbmp (char * filename) <br/>{< br/> file * file = NULL; <br/> If (! Filename) <br/>{< br/> return NULL; <br/>}</P> <p> // try to open the file <br/> file = fopen (filename, "R"); <br/> If (file) <br/>{< br/> fclose (File); <br/> return auxdibimageload (filename ); // load the bitmap and return the pointer <br/>}< br/> return NULL; <br/>}</P> <p>/* <br/> * loadgltextures <br/> * load a bitmap (call the above Code) convert to texture <br/> */<br/> int loadgltextures () <br/>{< br/> int status = false; // Status Indicator <br/> aux_rgbimagerec * textureimage [1]; // create a texture bucket <br/> memset (textureimage, 0, sizeof (void *) * 1); </P> <p> // load the bitmap to check for any errors, if the bitmap is not found, exit <br/> If (textureimage [0] = loadbmp ("xiaoshe.bmp") <br/>{< br/> Status = true; </P> <p> // create texture <br/> glgentextures (1, & texture [0]); <br/> // use a typical texture generated from the bitmap data <br/> glbindtexture (gl_texture_2d, texture [0]); <br/> // generate texture <br/> glteximage2d (gl_texture_2d, 0, 3, textureimage [0]-> sizex, textureimage [0]-> sizey, 0, gl_rgb, response, textureimage [0]-> data); <br/> gltexparameteri (gl_texture_2d, cosine, gl_linear); // linear filtering <br/> gltexparameteri (gl_texture_2d, cosine, gl_linear); <br/>}</P> <p> // release bitmap data <br/> If (textureimage [0]) // whether the texture exists <br/>{< br/> If (textureimage [0]-> data) // whether the texture image exists <br/>{< br/> free (textureimage [0]-> data ); // release the memory occupied by the texture image <br/>}< br/> free (textureimage [0]); // release the image structure <br/>}</P> <p> return status; <br/>}

3. Load BMP in initgl (glvoid) and enable texture ing:

// Call the texture loading subroutine <br/> If (! Loadgltextures () <br/>{< br/> return 0; <br/>}< br/> glable (gl_texture_2d); // enable texture ing

4. Draw a quadrilateral display BMP file in drawglscence (glvoid:

 Glbindtexture (gl_texture_2d, texture [0]); // select the texture </P> <p> glbegin (gl_quads); <br/> gltexcoord2f (0.0f, 1.0f ); // top left <br/> glvertex3f (-1.0f, 1.0f, 0.0f); // top left </P> <p> gltexcoord2f (1.0f, 1.0f ); // upper right <br/> glvertex3f (1.0f, 1.0f, 0.0f); // upper right </P> <p> gltexcoord2f (1.0f, 0.0f ); // bottom right <br/> glvertex3f (1.0f,-1.0f, 0.0f); // bottom right </P> <p> gltexcoord2f (0.0f, 0.0f ); // bottom left <br/> glvertex3f (-1.0f,-1.0f, 0.0f); // bottom left <br/> glend ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.