getting texture information from openGL
Posted: Thu Oct 21, 2010 6:43 pm
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?
The Next Generation of 2D Roleplaying Games
http://elysianshadows.com/phpBB3/
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.
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; }
};
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; } };
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()
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