getting texture information from openGL
Moderator: Coders of Rage
getting texture information from openGL
Is there any function(s) in openGL that would allow you to the the width and height of a texture that was sent to it?
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: getting texture information from openGL
I don't believe there is one; texture coordinates are provided in a [0,1] range. However, if you really want the original width and height, you can store it before you call glTexImage2D (you need the width and height to call it anyway), and then use it later.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: getting texture information from openGL
I was really hoping that I wouldn't have to do that and I figured since openGL has that information stored anyway that there would be a way to access it when I needed it.Ginto8 wrote:I don't believe there is one; texture coordinates are provided in a [0,1] range. However, if you really want the original width and height, you can store it before you call glTexImage2D (you need the width and height to call it anyway), and then use it later.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: getting texture information from openGL
well... it would probably actually be faster to store that data locally, rather than having to call an OpenGL function, which would then ask the GPU for the data, and then wait for the GPU to send it back. If you want, here's a semi-general template for a Texture class:
Code: Select all
class Texture {
unsigned id;
int w,h;
public:
Texture([texture data]);
~Texture();
void Bind() const;
unsigned ID() const { return this->id; }
int W() const { return this->w; }
int H() const { return this->h; }
};
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: getting texture information from openGL
Thank you for the code but the reason I was wondering if I could get the texture information was because I'm currently working on my own sprite class. I guess I'll go ahead and store the data locally then if that would be more efficient.Ginto8 wrote:well... it would probably actually be faster to store that data locally, rather than having to call an OpenGL function, which would then ask the GPU for the data, and then wait for the GPU to send it back. If you want, here's a semi-general template for a Texture class:Code: Select all
class Texture { unsigned id; int w,h; public: Texture([texture data]); ~Texture(); void Bind() const; unsigned ID() const { return this->id; } int W() const { return this->w; } int H() const { return this->h; } };
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: getting texture information from openGL
You can indeed query the texture's dimensions straight from OpenGL.
Bind the texture.
Call glGetTexLevelParameteriv(target, level, paramName, data);
Where target is likely to be GL_TEXTURE_2D, unless it's a cubemap, 3D texture, or 1D texture.
where level is an integer for the texture level you wish to retrieve. This is 0 for the topmost level (original size). If the texture is mipmapped, then the additional levels represent mip levels.
where paramName is GL_TEXTURE_WIDTH or GL_TEXTURE_HEIGHT
where data is a pointer to an int or an int array. In your case, this will be a pointer to an int since GL_TEXTURE_WIDTH and GL_TEXTURE_HEIGHT are integer fields.
Bind the texture.
Call glGetTexLevelParameteriv(target, level, paramName, data);
Where target is likely to be GL_TEXTURE_2D, unless it's a cubemap, 3D texture, or 1D texture.
where level is an integer for the texture level you wish to retrieve. This is 0 for the topmost level (original size). If the texture is mipmapped, then the additional levels represent mip levels.
where paramName is GL_TEXTURE_WIDTH or GL_TEXTURE_HEIGHT
where data is a pointer to an int or an int array. In your case, this will be a pointer to an int since GL_TEXTURE_WIDTH and GL_TEXTURE_HEIGHT are integer fields.
- MrDeathNote
- ES Beta Backer
- Posts: 594
- Joined: Sun Oct 11, 2009 9:57 am
- Current Project: cocos2d-x project
- Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
- Programming Language of Choice: C/++
- Location: Belfast, Ireland
- Contact:
Re: getting texture information from openGL
Nice qp, i'm with Ginto on writting a container class for the texture though(maybe not identical to his but you get the idea). It's just nice to be able to easily access texture information without lengthy OpenGL calls.qpHalcy0n wrote:You can indeed query the texture's dimensions straight from OpenGL.
Bind the texture.
Call glGetTexLevelParameteriv(target, level, paramName, data);
Where target is likely to be GL_TEXTURE_2D, unless it's a cubemap, 3D texture, or 1D texture.
where level is an integer for the texture level you wish to retrieve. This is 0 for the topmost level (original size). If the texture is mipmapped, then the additional levels represent mip levels.
where paramName is GL_TEXTURE_WIDTH or GL_TEXTURE_HEIGHT
where data is a pointer to an int or an int array. In your case, this will be a pointer to an int since GL_TEXTURE_WIDTH and GL_TEXTURE_HEIGHT are integer fields.
Code: Select all
texture->getWidth()
http://www.youtube.com/user/MrDeathNote1988
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: getting texture information from openGL
I'm being objective in answering the question that was asked :] (I'll bet you two will do just horribly on college tests )
If the question were asking for opinions about how to structure a proper image processing lib...well I can yapp your head off about that one :P
If the question were asking for opinions about how to structure a proper image processing lib...well I can yapp your head off about that one :P
- MrDeathNote
- ES Beta Backer
- Posts: 594
- Joined: Sun Oct 11, 2009 9:57 am
- Current Project: cocos2d-x project
- Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
- Programming Language of Choice: C/++
- Location: Belfast, Ireland
- Contact:
Re: getting texture information from openGL
qpHalcy0n wrote:I'm being objective in answering the question that was asked :] (I'll bet you two will do just horribly on college tests )
If the question were asking for opinions about how to structure a proper image processing lib...well I can yapp your head off about that one :P
fair point, i'm in work and i can't be bothered actually working so i needed a distraction. Sue me!!(And i'm already a college student with a scholarship studying for my masters in Computer Science)
http://www.youtube.com/user/MrDeathNote1988
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup