Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Texture question

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    5

    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?

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texture question

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

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    5

    Re: Texture question

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    5

    Re: Texture question

    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?

  5. #5
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: Texture question

    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?
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    5

    Re: Texture question

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

    init images
    Code :
        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\n");
    	}
     
    	// 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\n");
    	}
     
            // 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\n");
                    // this error should not go unhandled
            }
     
    	// Have OpenGL generate a texture object handle for us
    	glGenTextures( 1, &amp;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\n", SDL_GetError());
                SDL_Quit();
        }    
     
        // Free the SDL_Surface only if it was successfully created
        if ( surface ) { 
                SDL_FreeSurface( surface );
        }

    draw a quad
    Code :
    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...

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    5

    Re: Texture question

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •