Jump to content
  • Advertisement
Sign in to follow this  
andrew111

OpenGL Question about glBindTexture and using different targets with the same ID

This topic is 3030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been poking around with glBindTexture, and I noticed that you can store at least two different textures under the same texture ID by using different targets. For example:
void init() {
  glActiveTexture(GL_TEXTURE_0);

  glBindTexture(GL_TEXTURE_2D, id); //2d target
  loadSome2DTexture(); //store 2d texture here

  glBindTexture(GL_TEXTURE_CUBE, id); //cube target
  loadSkyTextures(); //store cube map for sky texture here
}

void render() {
  ..
  useSkyShader(); //with samplerCube type in the glsl code, for cube map texture
  drawSky(); 
  useQuadShader(); //with sampler2d type in the glsl code, for the 2d texture
  drawQuad();
}



And this will actually work, both the sky and the quad will render with their different textures. So I was wondering if there is a point of allowing different textures to be stored under the same ID? From what I've quickly looked at direct3d, it doesn't do odd things like this. I ask because I am writing my own effects system and am wrapping up certain opengl functions, and I am trying to decide on how to handle this, like maybe I should hide this functionality, since it seems pointless. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
The name spaces are separate for different texture dimensions. It means that two textures of different dimensions may have the same name returned by glGenTextures, not that one name returned by glGenTextures can be used for multiple textures of different dimension.

Your code is, therefore, logically incorrect. One ID should reference one texture, but you use one ID for multiple textures. In OpenGL 3.0 and up, you cannot allocate your own names anymore, and that code is, even in practice, invalid. Every object must be allocated by the corresponding glGen-function, and you cannot use object names that wasn't allocated.

Share this post


Link to post
Share on other sites
Sorry, I didn't include this in the code above:

glGenTextures(1, &id);


But it was used when I tested it.

Quote:
Original post by Brother Bob
The name spaces are separate for different texture dimensions. It means that two textures of different dimensions may have the same name returned by glGenTextures, not that one name returned by glGenTextures can be used for multiple textures of different dimension.


What do you mean the glGenTextures may return the same name? Normally I thought it's just a different ID/name (I assume you are using name, ID interchangeably) is used per texture of any dimension.

Quote:
Original post by Brother Bob
In OpenGL 3.0 and up, you cannot allocate your own names anymore, and that code is, even in practice, invalid. Every object must be allocated by the corresponding glGen-function, and you cannot use object names that wasn't allocated.


I didn't understand what you mean that you cannot allocate own names anymore. Is there somewhere I can read about how opengl 3.0 allocation of names is different from the previous versions?

Thanks.

[Edited by - andrew111 on April 30, 2010 11:58:29 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by andrew111
Quote:
Original post by Brother Bob
The name spaces are separate for different texture dimensions. It means that two textures of different dimensions may have the same name returned by glGenTextures, not that one name returned by glGenTextures can be used for multiple textures of different dimension.


What do you mean the glGenTextures may return the same name? Normally I thought it's just a different ID/name (I assume you are using name, ID interchangeably) is used per texture of any dimension.

You can ignore that. I thought for a moment you had to specify the dimension when allocating names, since the name spaces are different, but that is not the case. It will never return the same ID twice, unless it's deleted.
Quote:
Original post by andrew111
Quote:
Original post by Brother Bob
In OpenGL 3.0 and up, you cannot allocate your own names anymore, and that code is, even in practice, invalid. Every object must be allocated by the corresponding glGen-function, and you cannot use object names that wasn't allocated.


I didn't understand what you mean that you cannot allocate own names anymore. Is there somewhere I can read about how opengl 3.0 allocation of names is different from the previous versions?

It is, or was, perfectly OK to allocate your own names and bypass glGenTextures.

GLuint ID = 1;
glBindTexture(GL_TEXTURE_2D, ID);

When allocating the names manually, it is (or, again, was) OK to allocated ID's separate for different texture dimensions. Thus, two textures could have the same ID, but since they belong to different dimensions, they reference different texture objects. Having two variables with the same ID is logically correct, but having the same variable for two textures is not.

You can real all about it in the specification.

Share this post


Link to post
Share on other sites
Quote:
Original post by andrew111
So I was wondering if there is a point of allowing different textures to be stored under the same ID? From what I've quickly looked at direct3d, it doesn't do odd things like this.


The ID returned by glGenTextures is different everytime you call it.

This is wrong
glActiveTexture(GL_TEXTURE_0);

it should be
glActiveTexture(GL_TEXTURE0);

I also suggest different variable names like (id1 and id2 instead of having id for both)
glBindTexture(GL_TEXTURE_2D, id1);
glBindTexture(GL_TEXTURE_CUBE, id2); //cube target

also, there is no such thing as GL_TEXTURE_CUBE. There is something wrong with your glext.h file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!