Page 1 of 1

getting texture information from openGL

Posted: Thu Oct 21, 2010 6:43 pm
by optLog
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?

Re: getting texture information from openGL

Posted: Thu Oct 21, 2010 9:03 pm
by Ginto8
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.

Re: getting texture information from openGL

Posted: Thu Oct 21, 2010 9:31 pm
by optLog
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.
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.

Re: getting texture information from openGL

Posted: Thu Oct 21, 2010 9:48 pm
by Ginto8
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; }
};

Re: getting texture information from openGL

Posted: Thu Oct 21, 2010 9:58 pm
by optLog
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; }
};
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.

Re: getting texture information from openGL

Posted: Thu Oct 21, 2010 10:17 pm
by qpHalcy0n
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.

Re: getting texture information from openGL

Posted: Thu Oct 21, 2010 11:03 pm
by optLog
thanks!

Re: getting texture information from openGL

Posted: Wed Oct 27, 2010 6:56 am
by MrDeathNote
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.
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.

Code: Select all

texture->getWidth()
I just think it's prettier, especially when you have lots of textures floating around.

Re: getting texture information from openGL

Posted: Wed Oct 27, 2010 9:40 am
by qpHalcy0n
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

Re: getting texture information from openGL

Posted: Wed Oct 27, 2010 9:54 am
by MrDeathNote
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

:lol: 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)