Hi folks,
I’m trying to build an understanding of how to use textures. I’ve been scouring the various webpages that claim to explain how to do this but many of the examples actually don’t work.
Here’s some code that I’m trying to use but for some reason it does not work. Can anyone diagnose what is wrong with this code? It’s virtually identical to the many examples I’ve seen on the web.
Thanks
GLuint texture;
glGenTextures (1, &texture);
glBindTexture (GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
bool wrap = false;
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP);
uint32_t n_bytes = 3 * textureWidth * textureHeight;
_pixels = malloc (n_bytes);
if (!_pixels) {
return false;
}
// Fill it with a random grayscale value.
memset (_pixels, n_bytes, rand()&255);
glTexImage2D (GL_TEXTURE_2D, 0, 3, textureWidth, textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, _pixels);
glEnable (GL_TEXTURE_2D); // UNINTUITIVE: This has to be here, not during initialization.
free (_pixels);
glPushMatrix();
glTexCoord2d(0.0,0.0); glVertex3d(x0,y0,depth);
glTexCoord2d(1.0,0.0); glVertex3d(x1,y0,depth);
glTexCoord2d(1.0,1.0); glVertex3d(x1,y1,depth);
glTexCoord2d(0.0,1.0); glVertex3d(x0,y1,depth);
glEnd();
glPopMatrix();
//glDeleteTextures (1, &texture);