[OpenGL] graphics Journey: texture Paster Trilogy

Source: Internet
Author: User

Next, we will introduce texture pasters in OpenGL in the following three steps:

0. Declare the data for storing textures:

# Define maxtexture 1 gluint texture [maxtexture]; // defines a texture array for storing the subsequent Image Texture Information

1. Introduce BMP bitmap:

Aux_rgbimagerec * LoadImage (char * filename) // load bitmap data {file * file = NULL; If (! Filename) {return NULL;} file = fopen (filename, "R"); If (File) {fclose (File); Return auxdibimageload (filename);} return NULL ;}

2. Convert the loaded bitmap into a texture:

Bool loadtexturegl (void) // convert the loaded bitmap to a texture {bool state = false; aux_rgbimagerec * textureimage [maxtexture]; // create a texture storage space memset (textureimage, 0, sizeof (void *) * maxtexture); // clear the image information in the texture bucket as null // load the image and check whether an error occurs, if the image does not exist, if (textureimage [0] = LoadImage ("Data/sky.bmp") {state = true; glgentextures (maxtexture, & texture [0]) is returned. // convert the loaded image into a texture and save it to the texture array. // use the image data to generate a texture for (INT I = 0; I <maxtexture; ++ I) {glbindtexture (gl_texture_2d, texture [I]); // use the typical texture generated from the bitmap data // generate the texture glubuild2dmipmaps (gl_texture_2d, 3, textureimage [I]-> sizex, textureimage [I]-> sizey, gl_rgb, gl_unsigned_byte, textureimage [I]-> data); gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear ); // when the texture is enlarged to the original texture (gl_texture_mag_filter) or smaller than the original texture (gl_texture_min_filter), gltexparameteri (gl_texture_2d, cosine, gl_linear) is used in linear filtering );}} for (INT I = 0; I <maxtexture; ++ I) // release the memory used to store bitmap information {If (textureimage [I]) {If (textureimage [I]-> data) {free (textureimage [I]-> data);} Free (textureimage [I]);} return state ;}

3. Apply texture maps to the scene:

Void gamescene: drawsky () {gl_texture_2d); // enable the texture ing glable (gl_depth_test); // enable the deep test gldisable (gl_blend ); // disable the mixed loadtexturegl (); glbindtexture (gl_texture_2d, texture [0]); // bind the texture to glbegin (gl_quads) on the object drawn below ); // The coordinate on the left is the source image. The source image must be an integer multiple of 2. The minimum value is 64*64, and the value is 128*128. The maximum value is 256*256. // The coordinate gltexcoord2f (0.0f, 0.0f), glvertex3f (0.0f, 1.0f, 1.0f), gltexcoord2f (1.0f, 0.0f), glvertex3f (1.0f, 1.0f, 1.0f, 1.0f); random (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, 0.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f (0.0f, 1.0f, 0.0f); glend (); gldisable (gl_texture_2d); // enable the texture ing gldisable (gl_depth_test); // enable the deep test}

Texture maps are completed in the preceding three steps.

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.