Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Understanding texture objects?

This topic is 2784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've implemented openGL with SDL and gotten up to 1500 moving PNG's, scaled, with transparency and not powers of two at 30 fps. Which seems very nice for what I'm doing.

However I'm having some trouble understanding binding textures.

So at first I had great lag because of creating and destroying a new texture every time I needed to draw. I fixed this by creating only one texture of each image I need and re-using that texture to draw.

So I do something like this:

GLuint tex,tex2;

surface = IMG_Load("Images/Joe.png");
surface2 = IMG_Load("Images/Shape2.png");

//CreateTexture function is here:
void CreateTexture(GLuint texture,SDL_Surface* surface){
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
assert(surface != NULL);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, (surface->w), (surface->h), 0,GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );

And then if I proceed to draw without binding any texture, it will draw the last created texture. Makes sense.

However, if I call:

glBindTexture( GL_TEXTURE_2D, tex );


glBindTexture( GL_TEXTURE_2D, tex2 );

Neither of the two PNG's appear. Just a white box.

Confused, I output tex and tex2, they are both 0's. Now if I manually bind "1" when I need tex, and bind "2" when I need tex2, it works. But aren't they supposed to store their values? I'm not sure what I'm doing wrong.

I mean I could make my own system to assign a number to each PNG I create, but I'd like to know how others do it.

Thanks in advance!

Share this post

Link to post
Share on other sites
Due to
void CreateTexture(GLuint texture,SDL_Surface* surface){
you define "texture" to be a local parameter to which the values of the global "tex" or "tex2", resp., is copied when CreateTexture is invoked. Inside CreateTexture there is no connection with the global "tex" or "tex2", resp., so the glGenTexture inside CreateTexture will not write to them. Hence the generated texture ID gets lost when CreateTexture is left.

You probably want to hand over a reference
void CreateTexture(GLuint& texture,SDL_Surface* surface){
so to enable the routine to actually overwrite the global variables.

A more clean way would be letting CreateTexture return the texture ID like this signature
GLuint CreateTexture(SDL_Surface* surface){
suggests, although the routine body has to be adapted accordingly. Then
tex = CreateTexture(surface);
tex2 = CreateTexture(surface2);

would be the correct invocations.

Share this post

Link to post
Share on other sites
Ah, that worked marvelously!

I forgot about the whole handing down a reference thing. I originally had the CreateTexture function return the new texture which I assigned the way you showed in your example, but I don't remember why I changed it.

Thank you very much haegarr!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!