Texturing in OpenGL

Started by
22 comments, last by Merowingian 16 years, 7 months ago
I think you need to set these each time you create and bind a new texture:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

Also, make doubly sure that texturing is enabled before you draw. I didn't see a glEnable(GL_TEXTURE_2D) call, so I wasn't sure.


Also, every time you draw the object you are creating the texture, using it, and deleting it. This is not the ideal solution. It will get slow really fast, because that means that the texture data has to be sent to gpu memory for every texture, every single frame! If you don't know, these transfers are not quick.

Instead, you should create the textures outside of the drawing code, and only call glTexImage2D(...) once for each texture created. Then, when you want to use a texture, just call glBindTexture(GL_TEXTURE_2D, texID). Once you no longer need the textures (most likely when the program ends, or when you load a different level, for example), then you call glDeleteTextures.

For example:
const unsigned int TexCount = 4;GLuint TextureIDs[TexCount];std::string texFiles[] =   { "tex1.bmp", "shelf.bmp", "sky.bmp", "dirt.bmp" };void initGL(){  //initialize viewport, perspective, anything else you need  //Load textures  glGenTextures(TexCount, TextureIDs);    for(int i=0;i<TexCount;++i)  {    unsigned int width, height;    glBindTexture(GL_TEXTURE_2D, TextureIDs);    char* texData = MyTextureLoadingFunction(texFiles.c_str(), width, height);    glTexEnvi(...);    glTexParameteri(...);    glTexParameteri(...);    glTexParameteri(...);    glTexParameteri(...);    glTexImage2D(..., width, height, ..., texData);  }  glEnable(GL_TEXTURE_2D);}void drawStuff(){  //.. drawing code, transforms, whatever  glBindTexture(GL_TEXTURE_2D, TextureIDs[0]);  drawMyObject();    //more transforms and whatnot  glBindTexture(GL_TEXTURE_2D, TextureIDs[1]);  drawMyShelf();  //more transforms and whatnot  glBindTexture(GL_TEXTURE_2D, TextureIDs[2]);  drawMySkyPlane();  // etc, etc...}//Then when you don't need the textures anymorevoid cleanupTextures(){  glDeleteTextures(TexCount, TextureIDs);}



Anyway... make sure texturing is enabled, and make sure you set the parameters you want for each texture (Even if they are the same! You must!). Also, don't forget to call glTexEnvi() and setup that properly for each texture.
Advertisement
Quote:Original post by NaxosAnyway... make sure texturing is enabled, and make sure you set the parameters you want for each texture (Even if they are the same! You must!). Also, don't forget to call glTexEnvi() and setup that properly for each texture.
Most of Naxos' post was spot-on but that last sentence is just slightly wrong. Texture parameters are per-texture-object state so you need to set them for each texture you create (and just to reiterate, don't recreate a texture every frame). Texture environment settings, however, are per-texture-unit state. So if the current texture unit is GL_TEXTURE0 and you bind texture object 1, then change texture environment settings, then bind texture object 2, the texture environment settings you just set are still in effect on texture unit GL_TEXTURE0. You change the current texture unit with glActiveTexture, but this gets into using multiple textures at once (multitexturing) so if you don't need that then just stick with using texture unit GL_TEXTURE0 for now.
Oops! Thanks Kalidor :) I wasn't aware of that. I'm glad somebody's paying attention ^^
Hey guys,

thanks for your help so far, I just got a new Macbook and I need to transfer all my data to that new notebook before I can continue programming.

I'll get back to ya'll later.

Cheers
Marvin

This topic is closed to new replies.

Advertisement