PDA

View Full Version : Binding multiple textures


NitroPye
2005.04.27, 12:24 PM
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:


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


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?

NYGhost
2005.04.27, 02:33 PM
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..

ThemsAllTook
2005.04.27, 03:14 PM
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

NitroPye
2005.04.27, 03:20 PM
Thanks guys, that was the problem.

:mad: lots of wasted time, I thought it was something more complex.

Fenris
2005.04.27, 05:15 PM
A good rule of thumb is to do the following just after you swap:


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.)

arekkusu
2005.04.27, 05:44 PM
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:

GLenum err = glGetError();
while (err != GL_NO_ERROR) {
printf("%s caught at %s:%u\n", (char *)gluErrorString(err), __FILE__, __LINE__);
err = glGetError();
}

Skorche
2005.04.27, 05:47 PM
Even better would be:


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)

arekkusu
2005.04.28, 12:05 AM
The point was while() instead of if(). The other stuff is extra.

Fenris
2005.04.28, 03:22 AM
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.