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. :slight_smile:

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

}

This is more of an OpenGL list question (off-topic that is…), but here you go:

glBindTexture() switches to the specified texture. So when you do two
consequtive glBindTexture() calls you will end up adressing the second
one. That’s probably why your first textured quad shows all white…

Good luck,

/Olof

Peter Jay Salzman: “[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. :slight_smile:

#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

This is more of an OpenGL list question (off-topic that is…), but
here you go:

glBindTexture() switches to the specified texture. So when you do two
consequtive glBindTexture() calls you will end up adressing the second
one. That’s probably why your first textured quad shows all white…

To be more specific, Peter, you were right to call glBindTexture with
GL_TEXTURE_2D because that sets their texture type (1D, 2D, or 3D.)
However it’s also used to select which texture you’re drawing with.
From the man page:

“glBindTexture lets you create or use a named texture.”

Olof, he was probably calling glBindTexture to assign a texture target
(the type of texture) to each of his textures. Even if he did do this
before drawing two quads, he wouldn’t end up with a white quad and a
textured quad, he’d end up with two similarly textured quads. Which
brings me to my next question…

Peter: Where is your code that draws your quads??? That might help us
solve you problem!

Oh, also, Peter, in your code, you have this code: &texture[0]

You may not know EXACTLY what each part of this code does. Don’t just
copy code straight out of tutorials, because if you don’t intimately
understand the programming language, you may THINK you know what’s
happening, but you don’t really know. I’m not trying to pick on you,
and certainly that code WILL WORK AS IS, but just a heads up, a lot of
newbs to OpenGL are also newbs to programming in general, and need to
learn some fundamentals. (This is my personal opinion, take it or leave
it. If you disagree, don’t bother posting about it, just forget I said
anything.)

Goo dluck

Good luck,

/Olof

Peter Jay Salzman: "[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. :slight_smile:

#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


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

  • Donny VisznekiOn Jul 30, 2004, at 4:08 PM, Olof Bjarnason wrote:

Quoth Donny Viszneki , on 2004-07-31 00:56:38 -0400:

Olof, he was probably calling glBindTexture to assign a texture target
(the type of texture) to each of his textures. Even if he did do this
before drawing two quads, he wouldn’t end up with a white quad and a
textured quad, he’d end up with two similarly textured quads.

But the original sequence would not result in that, assuming the quads
were drawn correctly. Note that a texture object having a target does
not imply that it is valid or has any data. Note also that
glBindTexture is a state-changing operation and as part of such
changes which texture object is modified by subsequent glTexImage*
calls. Look at the code again:

  // Bind the textures to a texturing target
  //
  glBindTexture( GL_TEXTURE_2D, texture[0] );

The current texture object is now texture[0], which is implicitly
created by the glBindTexture call.

  glBindTexture( GL_TEXTURE_2D, texture[1] );

The current texture object is now texture[1], which is implicitly
created by the glBindTexture call.

  // Generate the texture
  //
  glTexImage2D(GL_TEXTURE_2D, 0, 3, image[0]->w, image[0]->h, 0,
        GL_BGR, GL_UNSIGNED_BYTE, image[0]->pixels );

Some data is uploaded to texture[1].

  glTexImage2D(GL_TEXTURE_2D, 0, 3, image[1]->w, image[1]->h, 0,
        GL_BGR, GL_UNSIGNED_BYTE, image[1]->pixels );

Different data is uploaded to texture[1].

No image data is ever uploaded to texture[0], so it will not texture
primitives properly (or in this case at all) when used.

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040731/aa8d68cb/attachment.pgp

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