Texture mapping problems...

I have basic texture mapping working. I have a targa mapped onto a quad. However, if I try to enable depth testing or backface culling, the quad disappears. Does texture mapping not work with depth testing/backface culling?


well, try to “flip” your quad. Perhaps you drawn it backwards so when you enable backface culling you disable your quad.

Thanks Dixie Dude, it was there. I’m a moron.

Now I have a different problem. When I rotate the quad along the X or Y axis, the texture “stays in place.” It looks like it’s just projected through the quad, and not ON the quad. However, if I rotate along the Z axis, the texture rotates with the quad. What did I do?!!

Are you giving specific texture coordinates or using glTexGen to generate texcoords for you?

I’m using specific coordinates.

glVertex3f(-25.0f, -25.0f, 0.0f); glTexCoord2f(0.0f, 0.0f);
glVertex3f(25.0f, -25.0f, 0.0f); glTexCoord2f(1.0f, 0.0f);
glVertex3f(25.0f, 25.0f, 0.0f); glTexCoord2f(1.0f, 1.0f);
glVertex3f(-25.0f, 25.0f, 0.0f); glTexCoord2f(0.0f, 1.0f);

I haven’t had any troubles learning OpenGL up to this point. Is texturemapping that hard?

You’ve got things backwards there. You need to put the glTexCoord calls first. Think of it as a state machine… when you call glVertex it will use whatever the current texcoord/color/etc is. Since you don’t set the texcoords until AFTER the vertex, that texcoord won’t be used until the NEXT vertex call. So… on the next drawn frame your first vertex is going to have the texcoord that you used on your last line there.

Ah. Now the bitmap is rightside-up, but the other problem still exists. It still looks like the texture is just projected on to the quad, so when I rotate the quad around the Y or X axis, the texture doesn’t foreshorten. Any ideas?

There are three possibilities.

  1. You aren’t setting anything in the texture matrix, right?

  2. You may want to try glHint(GL_PERSPECTIVE_CORRECT) (I think that’s the right syntax for it).

  3. Also, though I doubt this is the cause, make sure you aren’t putting your camera matrix in your GL_PROJECTION matrix. It could call perspective correction problems.

Deiussum and Korval,
I have no idea what I’ve done. From what I can see (which ain’t much, I’m relatively new to this), I don’t see what I’m doing wrong. So, here’s the code that I do have:

#include <GL/glut.h>

#include “gtBitmap.h”

const int msecs = 5;

GLuint textureObj[1]; // Texture object to hold our texturemap
GLfloat rot = 0.5f,
xrot = 1.0f,
yrot = 0.0f,
zrot = 0.0f;

void myDisplayFunc() {
glLoadIdentity(); // Reset modelview matrix

glColor3f(1.0f, 1.0f, 1.0f);

glBindTexture(GL_TEXTURE_2D, *textureObj);
glTranslatef(125.0f, 125.0f, 0.0f);
glRotatef(rot, xrot, yrot, zrot);

    glTexCoord2f(0.0f, 0.0f);	glVertex3f(-25.0f, -25.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);	glVertex3f(25.0f, -25.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);	glVertex3f(25.0f, 25.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);	glVertex3f(-25.0f, 25.0f, 0.0f);

glutSwapBuffers();	// Swap the buffers


void myReshapeFunc(GLsizei width, GLsizei height) {
GLfloat lightPos[] = {-50.0f, 50.0f, 100.0f, 1.0f};

if (height == 0)								// Avoid divide by zero
    height = 1;

glViewport(0, 0, width, height);				// Setup the viewport

glMatrixMode(GL_PROJECTION);					// Select the projection matrix
glLoadIdentity();								// Reset the projection matrix

if (width &lt;= height) 
    glOrtho(0.0f, 250.0f, 0.0f, 250.0f*height/width, 1.0f, -1.0f);
    glOrtho(0.0f, 250.0f*width/height, 0.0f, 250.0f, 1.0f, -1.0f);

glLightfv(GL_LIGHT0, GL_POSITION, lightPos);	// Position the light

glMatrixMode(GL_MODELVIEW);						// Select the modelview matrix
glLoadIdentity();								// Reset the modelview matrix


void myTimerFunc(int val) {
// Do animation stuff here
if (rot >= 360.0f)
rot -= 360.0f;
rot += 0.5f;
glutTimerFunc(msecs, myTimerFunc, 1);

void SetupRC() {
gtTarga tempTexture(“northrim_ball.tga”); // Load up temporary env map into memory
tempTexture.reverseRows(); // Upside down, flip it
tempTexture.buildTexture(textureObj); // Build the texture object

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);		// Set the background color

//glEnable(GL_CULL_FACE);						// Enable backface culling
//glEnable(GL_DEPTH_TEST);					// Enable depth testing

glEnable(GL_COLOR_MATERIAL);				// Enable color tracking
glEnable(GL_TEXTURE_2D);					// Enable textures


glShadeModel(GL_SMOOTH);					// Set shading to smooth


int main(int argc, char* argv[]) {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutTimerFunc(msecs, myTimerFunc, 1);



return 0;


And here’s what I do to build the texture:

glGenTextures(1, &textureObj[0]);

glBindTexture(GL_TEXTURE_2D, *textureObj);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelList);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelList);

[This message has been edited by gtada (edited 06-01-2001).]

Your primary problem is that you are using an orthographic projection matrix, which does not do any perspective correction for you. What that means is, that your quad will look the same no matter how far away it. Try using gluPerspective instead.

Secondly, you have your far plane set to a lower value than your near plane. While that isn’t what was causing your issue in this case, it would probably be wise not to do that. Note that when you use gluPerspective both the near and far planes MUST > 0.0. (Note: NOT >= 0.0 but > 0.0) Using 0 for the near clip plane will cause problems, just as using a far clip plane < the near plane will cause problems.