Texture question

hey, i’m new to OpenGL, and cutting my teeth on some stuff… i’m using SDL combined with OpenGL to do 2d graphics… I’ve created a sprite i can display on the screen, but i’m trying to understand how textures are handled…

from what i gather, you generate an “id” for a texture by calling glGenTextures, which returns new “blank” texture id’s, which you then draw your image on using glBindTexture, and glTexImage2D (and some other params)… after this the texture is saved somewhere, (buffer?) and you can “draw” it to a quad using glBindTexture and the glTexCoord2f functions?

mainly i’m how i should go about saving an instance of the texture… i know SFML requires you to save images in a singular library, SDL takes care of it by itself… does open gl handle this in the backend so all you’re really dealing with is the IDs?

glTexImage2D is uploading data to the texture memory which was internally allocated by the driver. You don’t need to worry about this memory as it is automatically managed by the driver and you’ll never need to access it.
All you need to use is the ID returned by glGenTexture so that you can texture map polygons using the bound texture (glBindTexture).

ok, if I were to save all the texture id’s in a list so that multiple copies of a sprite can use the same texture, how would i go about that?

I’m saving the id’s to a map<string, GLuint> (the string being the path)…

i can load the texture fine (glGenTexture and glBindTexture etc) and save the resulting GLuint to the list…

if i return the GLuint from my “loadFunction” the image display fine… but if i call it from a different function (getTexture(string path)) it comes up as a white box.

i guess i need to know if a texture can be used more than once? can i attach the same texture id to 2 different quads?

Have you tried it?

glBindTexture(GL_TEXTURE_2D, textureID);
DrawQuad1();

glBindTexture(GL_TEXTURE_2D, textureID);
DrawQuad2();

glBindTexture(GL_TEXTURE_2D, textureID);
DrawQuad3();

Does it render properly or not?

I have tried that yes, and it doesn’t seem to work… Here’s the code

init images


    GLuint gn;
    SDL_Surface* surface;
    GLenum texture_format;
    GLint nOfColors;
    
    if ( (surface = IMG_Load("zeldatest.png")) ) { 
 
	// Check that the image's width is a power of 2
	if ( (surface->w & (surface->w - 1)) != 0 ) {
		printf("warning: image width is not a power of 2
");
	}
 
	// Also check if the height is a power of 2
	if ( (surface->h & (surface->h - 1)) != 0 ) {
		printf("warning: image height is not a power of 2
");
	}
 
        // get the number of channels in the SDL surface
        nOfColors = surface->format->BytesPerPixel;
        if (nOfColors == 4)     // contains an alpha channel
        {
                if (surface->format->Rmask == 0x000000ff)
                        texture_format = GL_RGBA;
                else
                        texture_format = GL_BGRA;
        } else if (nOfColors == 3)     // no alpha channel
        {
                if (surface->format->Rmask == 0x000000ff)
                        texture_format = GL_RGB;
                else
                        texture_format = GL_BGR;
        } else {
                printf("warning: the image is not truecolor..  this will probably break
");
                // this error should not go unhandled
        }
 
	// Have OpenGL generate a texture object handle for us
	glGenTextures( 1, &gn );
 
	// Bind the texture object
	glBindTexture( GL_TEXTURE_2D, gn );
 
	// Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
	// Edit the texture object's image data using the information SDL_Surface gives us
	glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
                      texture_format, GL_UNSIGNED_BYTE, surface->pixels );
    } 
    else {
            printf("SDL could not load image %s
", SDL_GetError());
            SDL_Quit();
    }    

    // Free the SDL_Surface only if it was successfully created
    if ( surface ) { 
            SDL_FreeSurface( surface );
    }

draw a quad


void Square::Draw(GLuint texture){ 
    
    glBindTexture( GL_TEXTURE_2D, texture );
    
    glBegin(GL_QUADS);
        glTexCoord2f( 0.0f, 0.0f ); glVertex3f(0, 0, 0);
        glTexCoord2f( 0.5f, 0.0f ); glVertex3f(20, 0, 0);
        glTexCoord2f( 0.5f, 0.5f ); glVertex3f(20, 25, 0);
        glTexCoord2f( 0.0f, 0.5f ); glVertex3f(0, 25, 0);
    glEnd();
}

(admittedly not exactly like your saying, i had a “sprite” class that was working until i had this problem, so i’ve been cutting out code to try to rule it out…) basically, in the main game init function i run the load function, which saves the GLuint to gn (a private class variable)… in the render function you run the draw(GLuint texture) to draw the quad and add the texture, (i send the texture and call the glBindTexture inside the function… is that my problem?) i’m going to attempt to start over when i get home and staple down what’s going on… thanks for the help everyone… i’m quite new to C++, SDL and OpenGL…

ok, so, if i hard code everything, it works… thanks for the help, i guess i’ll just start over, trying one thing at a time.