Hmm… The mistake is a very common misunderstanding for beginners.
My tip is to look up glBindtexture() and try to understand what it really
does.
What it dose is just telling opengl: “All the current texture commands
should be applied to this(and only this) texture”
The best way to explain what is happening is probably going through your
code and seeing what happens
/////////////////////////////////
extern GLuint texture[2];
void LoadGLTextures( void )
{
SDL_Surface *image[2];
image[0] = SDL_LoadBMP( "data/reference.bmp" );
image[1] = SDL_LoadBMP( "data/thissideup.bmp" );
// This tells gl how the image data i stored in memory.
// You usually don't have to bother with it.
// glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGenTextures(2, &texture[0] );
//This tells gl to work with the texture texture[0]
glBindTexture( GL_TEXTURE_2D, texture[0] );
//This tells gl to work with the texture texture[1]
glBindTexture( GL_TEXTURE_2D, texture[1] );
//This stores the image data of image[0] in texture[1] !!!
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[0]->w, image[0]->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, image[0]->pixels );
//This stores the image data of image[1] in texture[1] !!!
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[1]->w, image[1]->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, image[1]->pixels );
//This sets the filtering of texture[1] to LINEAR
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
if ( image[0] ) SDL_FreeSurface( image[0] );
if ( image[1] ) SDL_FreeSurface( image[1] );
//Actualy This one made me a little curious.
glMatrixMode( GL_TEXTURE );
glRotatef( 180.0f, 0.0f, 0.0f, 1.0f );
glScalef( -1.0f, 1.0f, 1.0f );
glMatrixMode(GL_MODELVIEW);
}
//////////////////////////////
Solution. To fix the problem you have to move some of the funtion calls
around. First use glBindTexture() on texture 0, load the imagedata and set
the parameters.
First use glBindTexture() on texture 1, load the imagedata and set the
parameters.
Someting like this:
glGenTextures(2, &texture[0] );
//This tells gl to work with the texture texxture[0]
glBindTexture( GL_TEXTURE_2D, texture[0] );
//This sets the filtering of texture[0] to LINEAR
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//This stores the image data of image[0] in texture[0] !!!
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[0]->w, image[0]->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, image[0]->pixels );
//This tells gl to work with the texture texture[1]
glBindTexture( GL_TEXTURE_2D, texture[1] );
//This stores the image data of image[1] in texture[1] !!!
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[1]->w, image[1]->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, image[1]->pixels );
//This sets the filtering of texture[1] to LINEAR
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );> From: p at dirac.org (Peter Jay Salzman)
Subject: [SDL] Newbie: Creating an array of OpenGL textures
Hi all,
I’m displaying 2 OpenGL quads, and would like to display different
textures on them. To that end, I’m trying to create an array of
textures, but it’s simply not happening.
The second one displays correctly. The first texture is showing up as a
plain white surface. I’ve heard this can happen if you bind the texture
after you start the glBegin(). But I’m not.
What am I doing wrong? I’m enough of a newbie that I’m at a complete
loss. Here’s the function where I load the two textures:
extern GLuint texture[2];
void LoadGLTextures( void )
{
SDL_Surface *image[2];
image[0] = SDL_LoadBMP( “data/reference.bmp” );
image[1] = SDL_LoadBMP( “data/thissideup.bmp” );
// I don’t know what this does.
// glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
// Create the textures names, which are unsigned ints.
//
glGenTextures(2, &texture[0] );
// Bind the textures to a texturing target
//
glBindTexture( GL_TEXTURE_2D, texture[0] );
glBindTexture( GL_TEXTURE_2D, texture[1] );
// Generate the texture
//
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[0]->w, image[0]->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, image[0]->pixels );
glTexImage2D(GL_TEXTURE_2D, 0, 3, image[1]->w, image[1]->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, image[1]->pixels );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
if ( image[0] ) SDL_FreeSurface( image[0] );
if ( image[1] ) SDL_FreeSurface( image[1] );
// Reorient the textures.
glMatrixMode( GL_TEXTURE );
glRotatef( 180.0f, 0.0f, 0.0f, 1.0f );
glScalef( -1.0f, 1.0f, 1.0f );
glMatrixMode(GL_MODELVIEW);
}
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl