GLuint texName[2]; // replace 2 (in the next line too) with the actual number of texture objects you want to create. glGenTextures(2, texName);
glBindTexture(GL_TEXTURE_2D, texName[0]); // or texName[1] to initialize the second texture object just created above. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // clamping if s texture val out range of [0,1]. The other common choise is GL_REPEAT. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // clamping if t texture val out range of [0,1]. The other common choise is GL_REPEAT. /* these are anti-aliasing related issue, you dont have to understand .*/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /* Now bind the actual image to be texture mapped for this texture object There are a few important things you have to be VERY careful, 1. you must pass the correct data type of the color component in your image. 2. the image size must be 2^m + b and >= 64, where ususally b = 0 as below, */ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
glBindTexture(GL_TEXTURE_2D, texName[0]); glBegin(...); ... glTexCoord2f(s, t); glVertex3f(x, y, z); // this vertex is related to the texture coordinate (s,t) just given. glEnd();
Use this command to set to replace mode, in which case the pixel get color only from the texture image.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
and use this command to set to modulate mode, in which case the pixel get color from both the texture image and the color from either direct color specification or lighting computation.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Text is also a geometry, exactly the same as triangles, quads, etc. The only difference is it has a different way to specify the space position of the character to be rendered.
glRasterPos3f(-1, 0, -1); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'A'); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'B'); // you dont have to specify space position for 'B'; it at the right side of 'A'.
In display() we use this code to draw a texture mapped triangle.
glBegin(GL_TRIANGLES); { glTexCoord2f(0, 0); glVertex3f( -2, 0, -2 ); glTexCoord2f(1, 0); glVertex3f( 2, 0, -2 ); glTexCoord2f(0.5, 1); glVertex3f( 0, 2, -2 ); } glEnd();
Now you do this, it should draw texture mapped triangles of the model.
TriangleMesh msh("anobj.obj"); msh.glDraw();
1.3.6