Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: examples textures - help me understand something

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    25

    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, &amp;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);

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: examples textures - help me understand something

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    25

    Re: examples textures - help me understand something

    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, &amp;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);

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    748

    Re: examples textures - help me understand something

    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

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    25

    Re: examples textures - help me understand something

    Quote Originally Posted by zeoverlord
    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 ?

  6. #6
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    25

    Re: examples textures - help me understand something

    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.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,726

    Re: examples textures - help me understand something

    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.

  8. #8
    Junior Member Newbie
    Join Date
    Mar 2012
    Location
    Texas
    Posts
    3

    Re: examples textures - help me understand something

    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.

  9. #9
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,726

    Re: examples textures - help me understand something

    Wrong thread. Your thread is over here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •