Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Understanding texture objects?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 OmarShehata   Members   -  Reputation: 205

Like
0Likes
Like

Posted 03 March 2012 - 05:01 AM

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");
GShapes.CreateTexture(tex,surface);
surface2 = IMG_Load("Images/Shape2.png");
GShapes.CreateTexture(tex2,surface2);



//CreateTexture function is here:
void CreateTexture(GLuint texture,SDL_Surface* surface){
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
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 );

OR

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!

Sponsor:

#2 haegarr   Crossbones+   -  Reputation: 4438

Like
1Likes
Like

Posted 03 March 2012 - 05:18 AM

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.

#3 OmarShehata   Members   -  Reputation: 205

Like
0Likes
Like

Posted 03 March 2012 - 05:35 AM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS