Texturing problem, already tried resources

Hi everyone,

I’m having trouble with texturing, tried all the resources but only get a coloured quad :frowning:

My image data is stored in pz->data which works because I can draw the data using glDrawPixels. However when I try to use the glTexImage2D function all I get is a coloured quad without the texture. I have already put in the TexEnvf to disable external colour as suggested on a forum.

VOLUME_HEIGHT and VOLUME_WIDTH are the sizes of my texture and quad.

This is my InitGL();


void MyGLCanvas::InitGL()
  // Function to initialise the GL context
{
  int w, h;

  GetClientSize(&w, &h);
  SetCurrent();
glDrawBuffer(GL_BACK); 						
glClearColor(1.0, 1.0, 1.0, 0.0);
glClearDepth(1.0f);			// Depth Buffer Setup	glEnable(GL_DEPTH_TEST);	       // Enables Depth Testing
glDepthFunc(GL_LEQUAL);	      // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);			// Really Nice Perspective Calculations

  glViewport(0, 0, (GLint) w, (GLint) h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, w, 0, h, -1, 1);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

And I also create a texture ID with Gluint texture[1];
Ok, now Here is texturing part of my Render function (where I was using glDrawPixels previously)



glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture[0]);	// Create The Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, VOLUME_WIDTH, VOLUME_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, pz->data);
		
glTranslatef((w-VOLUME_WIDTH)/2,(h-VOLUME_HEIGHT)/2, 0.0f);						

glBindTexture(GL_TEXTURE_2D, texture[0]);// Select Our Texture
glTexEnvf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_REPLACE);

glLoadIdentity();
glTranslatef((w-VOLUME_WIDTH)/2,(h-VOLUME_HEIGHT)/2, 0.0f);

glBegin(GL_QUADS);
  glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f,  0.0f);	// Bottom Left Of The Texture and Quad

  glTexCoord2f(VOLUME_WIDTH, 0.0f); glVertex3f( VOLUME_WIDTH, 0.0f,  0.0f);	// Bottom Right Of The Texture and Quad

  glTexCoord2f(VOLUME_WIDTH, VOLUME_HEIGHT); glVertex3f( VOLUME_WIDTH,  VOLUME_HEIGHT,  0.0f);	// Top Right Of The Texture and Quad

  glTexCoord2f(0.0f, VOLUME_HEIGHT); glVertex3f( 0.0f, VOLUME_HEIGHT,  0.0f); // Top Left Of The Texture and Quad

glEnd();

glDisable(GL_TEXTURE_2D);

I have followed the tutorials both in the wiki and on Nehe, checked the man pages out and the forums but I can’t figure out my problem. I get my quad in the correct position but with no texture on it. It is only displayed in the colour that I use in an earlier part of my render function (to draw some other things).

If I remove the glDisable textures line at the end, my other 2d objects become all black. So I think I’m doing that right.

Any advice on the next step would help me tonnes. Please!

umm feel like a total idiot. Often while constructing my question I end up coming across my mistakes. I had a data conversion error between ints and floats. (looks sheepish). though maybe my code’ll help other newbies with texturing problems!

Typical order is as follows:

INIT:
glGenTextures
glBindTexture
glTexParameter…MIN_FILTER…LINEAR
glTexImage2D

RENDER:
//glActiveTexture() //if you use multitexturing
glBindTexture
glTexEnv //if you need something else than MODULATE
glEnable(GL_TEXTURE_2D)
(…)
glDisable(GL_TEXTURE_2D)
glTexEnv

Try rearranging it first so you don’t create new texture every frame. Then, if it’s not working, check what glGetError says here and there.

EDIT: Good point Zengar, I missed that one :slight_smile:

Also, texture coordinates are in normalized space and go from 0 to 1, you are using actual texture dimensions as coordinates!