examples textures - help me understand something

I am trying to understand what is going on with textures in the many examples I see online and why my image is blanking out. Below is a typical example of the code I find. When I uncomment the first glBindTexture call with the textureId param the image produced by glTexImage3D goes blank. I’m not sure what I’m binding to what and what that has to do with the following glTexImage3D command and why the binding is fouling the image up. Can anyone explain? Why in the many examples do you bind the texture to zero (0) after the glTexImage3D command?

— 8< —

GLuint textureId;
glGenTextures(1, &textureId);

// uncomment to black out image
//glBindTexture(GL_TEXTURE_3D, textureId);

glTexImage3D(GL_TEXTURE_3D, 0,GL_RGBA8 , WIDTH, HEIGHT, DEPTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glBindTexture(GL_TEXTURE_3D, 0);

OpenGL is a state machine. This means you have to select the state object and then set its properties. In this case glBindTexture is setting up an association with one of the texture slots in the state machine.

I understand the state machine nature of opengl.

What I don’t understand is why if I call glBindTexture(GL_TEXTURE_3D, textureId) I get an all white (blank) image. I don’t call glBindTexture in any other places in my program.


CODE1 (produces blank image):

GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_3D, textureId);

glTexImage3D(GL_TEXTURE_3D, 0,GL_RGBA8 , WIDTH, HEIGHT, DEPTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);


CODE2 (produces meaningful image):

glTexImage3D(GL_TEXTURE_3D, 0,GL_RGBA8 , WIDTH, HEIGHT, DEPTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glBindTexture is probably bound to 0 by default, so if you don’t have a glBindTexture in your code then that is what it will use.

in your code you bind the texture to textureId but then tell it to use texure 0 which results in a white image.
And the reason why most code bind to 0 like that after loading an image is because it fixes a lot of weird bugs and other problems later in the code just because it’s a state machine.

So in order for glBindTexture(GL_TEXTURE_3D, textureId); to work you also have to call it before rendering any polygons

Now we’re getting somewhere. I don’t render any polygons. I only use glTexImage3D to display what is in the data you see above. After these commands (above) are run glBindTexture is not run anywhere else in my program. If opengl is a state machine then the above state is the last state that I set before a display occurs.

Do you mean I need to call glBindTexture before the SwapBuffers ?

bingo, I looked at my code again and I had a method called “refresh” which ran before swapbuffers. I needed to glBindTexture to textureId there. Now it works. Thanx.

I don’t render any polygons. I only use glTexImage3D to display what is in the data you see above.

Then how do you draw things? Are you doing a blit from the texture or something? Textures don’t draw themselves.

Hey… I need to write a code for animating the grass. can anyone please help me how do I start it? Do you have any code snippets? I am new to opengl and using it for 1st time.

Thank you.

Wrong thread. Your thread is over here.