Binding multiple textures

Member
Posts: 28
Joined: 2005.04
Post: #1
I am having a problem using multiple textures.

Let say I was to create a skybox and load in 6 textures, one for each face, the code would be this:

Code:
int textureID[6], i;

    glGenTextures(6, textureID);

    // load in all the textures here
    // the object texture contains -width, -height, bpp, -bitmap
    for ( i = 0; i < 6; i++ ) {
    glPixelStorei(GL_UNPACK_ROW_LENGTH, [textures[i] width]);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glBindTexture(GL_TEXTURE_2D, textureID[i]);
    gluBuild2DMipmaps(GL_TEXTURE_2D,
              [textures[i] bpp],
              [textures[i] width],
              [textures[i] height],
              [textures[i] bpp],
              [textures[i] datatype],
              [[textures[i] bitmap] bitmapData]);
    }

Then i were to do

Code:
glBegin(GL_QUADS);
    // Right face
    glBindTexture(GL_TEXTURE_2D, textureID[0]);
       // --- draw here

    // Left Face
    glBindTexture(GL_TEXTURE_2D, textureID[1]);
       // --- draw here

The very last texture I loaded in and bound would be on every face. I cannot see what I am missing. Any thoughts?
Quote this message in a reply
Member
Posts: 157
Joined: 2002.12
Post: #2
I think you should bind the texture before calling glBegin(GL_QUADS);

glBindTexture(GL_TEXTURE_2D, textureID[0]);
draw quad

glBindTexture(GL_TEXTURE_2D, textureID[1]);
draw quad etc..
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
Yes, binding a texture between glBegin() and glEnd() will generate a GL_INVALID_OPERATION error. As an aside, it's advisable to check for GL errors at least once per render. It can often give you clues as to why something's not working.

- Alex Diener
Quote this message in a reply
Member
Posts: 28
Joined: 2005.04
Post: #4
Thanks guys, that was the problem.

Mad lots of wasted time, I thought it was something more complex.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #5
A good rule of thumb is to do the following just after you swap:

Code:
GLuint err = glError();
if (err)
{
    printf ("GL Error: %d", err);
    exit(1);
}

That way, as soon as you introduce a GL error, you will notice, since it will crash almost immediately. You'll know that you broke something since the last build. The again, it is advisable to check for GL errors often, since they can be hard to track down. (Not with the OpenGL Profiler in place, but do this anyway.)
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
Actually, it is possible for multiple types of errors to be flagged simultaneously. The snippet Fenris posted will only report the first one. Instead, do something like:
Code:
GLenum err = glGetError();
while (err != GL_NO_ERROR) {
    printf("%s caught at %s:%u\n", (char *)gluErrorString(err), __FILE__, __LINE__);
    err = glGetError();
}
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #7
Even better would be:

Code:
GLuint err = glError();
if (err)
{
    printf ("GL Error: %s", gluErrorString(err));
    exit(1);
}

That will print out the name of the error instead of just a number.

(edit: looks like arrekusu beat me to it)
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #8
The point was while() instead of if(). The other stuff is extra.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #9
Had no idea that GLError had a list of errors, that's really cool to know. Also, I accidentally wrote glError instead of glGetError, sorry about the confusion.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Problems Binding textures kodex 1 2,345 Apr 4, 2005 01:45 PM
Last Post: kodex
  Texture binding. LongJumper 2 2,237 Oct 19, 2003 08:59 PM
Last Post: LongJumper
  Binding 16Bit textures? ChrisD 15 5,921 May 1, 2003 07:58 PM
Last Post: Mars_999