Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

879728 Posts in 33001 Topics- by 24376 Members - Latest Member: xnothegame1

May 24, 2013, 07:12:17 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)OpenGL Texture Objects question
Pages: [1]
Print
Author Topic: OpenGL Texture Objects question  (Read 1185 times)
bluej774
Level 1
*


It smells like wet fur in here!


View Profile WWW
« on: January 28, 2010, 02:02:17 AM »

I'm new to OpenGL and I have a question about OpenGL texture objects.  Once a texture has been loaded via glTexImage2D and you have a reference to the texture via a GLuint, is there some way I can get the height and width of the texture back from OpenGL using that GLuint?  If so, it would be really handy.  Please advise.
Logged
Saint
Level 3
***



View Profile WWW
« Reply #1 on: January 28, 2010, 02:37:23 AM »

Yes.

First, bind the texture as usual with glBindTexture, then...

Code:
glGetTexLevelParameteriv(GL_TEXTURE_2D,[Mipmap level, likely 0 in your case],GL_TEXTURE_WIDTH,[Pointer to GLint that will receive the result]);

... Or use GL_TEXTURE_HEIGHT to find the height. Read up more on this function in the GL documentation;
http://www.opengl.org/sdk/docs/man/
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see

Valaam0
View Profile
« Reply #2 on: January 28, 2010, 08:11:12 AM »

It probably makes more sense for you to store that information manually together with the texture ID somewhere.
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
bluej774
Level 1
*


It smells like wet fur in here!


View Profile WWW
« Reply #3 on: January 28, 2010, 01:00:55 PM »

Thanks for the info, Saint.  I'm still getting used to opengl.  It's not as terrible as so many claim it is, but it certainly is a paradigm shift.  It's unlike any other library I've ever worked with.

And you're right, Ivan.  I'm probably going to have to make a custom texture class or something like that.
Logged
bluej774
Level 1
*


It smells like wet fur in here!


View Profile WWW
« Reply #4 on: January 29, 2010, 03:24:01 AM »

Here's another question.  Can textures pile up in the background and max out memory?

The reason I ask is that I've written a Sprite class.  Naturally, I've written a destructor that uses the glDeleteTextures call to delete the texture being referenced in that sprite class.  But then I realized that the texture the Sprite object references can also be referenced by another variable.  So if a Sprite object is destroyed and the texture goes with it, but then another reference to that texture is used for something, it'll cause problems.  So, what's the recommended solution?  Should I not destroy textures in the Sprite class at the risk of texture buildup?  Or is there a better solution I'm not thinking of?
Logged
mewse
Level 5
*****



View Profile WWW
« Reply #5 on: January 29, 2010, 03:48:39 AM »

Here's another question.  Can textures pile up in the background and max out memory?

Yes.  If you keep creating textures and don't ever glDeleteTextures them, then eventually you'll fill up memory on the video card, and OpenGL will start swapping them in and out to the video card as they're used.  This can be a big performance problem, and OpenGL won't tell you if that's happening.  So you want to be a little careful with what and how many textures you're asking OpenGL to use.


The reason I ask is that I've written a Sprite class.  Naturally, I've written a destructor that uses the glDeleteTextures call to delete the texture being referenced in that sprite class.  But then I realized that the texture the Sprite object references can also be referenced by another variable.  So if a Sprite object is destroyed and the texture goes with it, but then another reference to that texture is used for something, it'll cause problems.  So, what's the recommended solution?  Should I not destroy textures in the Sprite class at the risk of texture buildup?  Or is there a better solution I'm not thinking of?

The correct thing to do is to have your game be aware of the textures it's asked OpenGL to use.  If a texture is used for lots of sprites, your game should only ask OpenGL to create it once, and then share that texture ID amongst all the sprites.  You should then tell OpenGL to destroy the texture only once no sprites are referencing the texture any more, and you don't expect to need the texture again.  (That is, it's worth keeping projectile textures around, even if no sprites are currently drawing with them, because you'll probably need them again soon.  No point destroying them and then immediately re-creating them a frame or two later, after all!)

Personally, I wrap the OpenGL texture ID in a "Texture" class, on which I also store a "reference count"; the number of objects referencing that texture.  As I create objects which use the texture, I increment the reference count, and as I destroy them, I decrement the reference count.  If the reference count is greater than zero, I know that something is still using that texture.  Or if it drops to zero, then I know that nothing's using it any more and it can safely be removed from OpenGL.  And if the reference count drops below zero, then I know I have a bug in my code.  Wink
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see

Valaam0
View Profile
« Reply #6 on: January 29, 2010, 09:21:43 AM »

Another way of dealing with that is writing a simple resource manager that will load every texture you're going to use into video memory, and then your sprites or whatever will just request the instance by the filename.
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic