getting texture information from openGL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

getting texture information from openGL

Post 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?
User avatar
Ginto8
ES Beta Backer
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

Post 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.
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.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: getting texture information from openGL

Post 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.
User avatar
Ginto8
ES Beta Backer
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

Post 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; }
};
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.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: getting texture information from openGL

Post 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.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: getting texture information from openGL

Post 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.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: getting texture information from openGL

Post by optLog »

thanks!
User avatar
MrDeathNote
ES Beta Backer
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

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"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
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: getting texture information from openGL

Post 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
User avatar
MrDeathNote
ES Beta Backer
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

Post 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)
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"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
Post Reply