PDA

View Full Version : Changing texture targets



awhig
07-16-2009, 05:03 PM
I know that a texture object can be changed a number of times, by just replacing a new texture name viz:


//initially
glBindTexture(GL_TEXTURE_2D , tex_name_1);

//next line
glBindTexture(GL_TEXTURE_2D , tex_name_2);


Also, if the texture unit is to be unbound we do


glActiveTexture(<tex unit num>)
glBindTexture(GL_TEXTURE_2D , 0); //unbinds


But now I want to confirm if we can really change the target type of a valid texture id


//initially..let suppose it is bounded to GL_TEXTURE0
glBindTexture(GL_TEXTURE_2D , tex_name);

//unbind
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D , 0);

//disable GL_TEXTURE_2D
glDisable(GL_TEXTURE_2D);

//let say now I want to define a new target
glEnable(GL_TEXTURE_3D);

//assume GL_TEXTURE0 is still active
//then do
glBindTexture(GL_TEXTURE_3D , tex_name);

Is this possible???

Currently I am doing above but when I bind my texture to 3D
it throws error GL_INVALID_OPERATION.


Then I did following just before binding tex_name to GL_TEXTURE_3D



glDeleteTextures(..)
glGenTextures(.. , &amp;tex_name)

followed by binding tex_name to 3D and it worked ....

Why so?

Does this mean that once a target is specified for a texture id , it retains forever even after disable that target type ???

Note: The above lines are called in a loop and I do not want to delete and generate texture names everytime. As debugging shows, if I do above in a loop , the texture id values change and since it is of GLuint type, it has some limits.


Inputs welcome....

scratt
07-16-2009, 09:20 PM
My understanding is that glEnable(GL_TEXTURE_XX) is becoming less relevant with the advent of shaders and it basically enables all texture formats up to and including it's level. Certainly when using shaders you can ignore the texture setting. AFAIK you don't even need to enable them *when using shaders*. I always do, but have noticed (and been told) that it really doesn't matter as far as shaders are concerned.

But if you are asking if that command can alter a texture's dimensions. No. Not that I have ever seen.

You are not supposed to access a texture with texture coords that are not valid. i.e. You can't use a 2D texture as a 3D one, or a 1D texture as a 2D one.
And you should not bind a texture with a texture format other than it's own.

What you can do though is access individual layers of a 3D one, so use it like an array of 2D textures, although depending on how you access it / set it up you may or may not get a clear definition between layers. I believe texture_arrays are the proper way to do that if you have the extension, but I have been using bog standard 3D textures as 2D arrays for some time on HW that does not formally support texture_arrays.

Likewise you could use the first line of a 2D texture as a 1D one.

Although why you'd do that is beyond me. But then again, It's just a form of texture atlas isn't it..

awhig
07-16-2009, 09:42 PM
You are not supposed to access a texture with texture coords that are not valid. i.e. You can't use a 2D texture as a 3D one, or a 1D texture as a 2D one.
And you should not bind a texture with a texture format other than it's own.


Agree.



When we do :


glBindTexture(GL_TEXTURE_2D , tex) ; => tex is now of 2D type

next if I do

glBindTexture(GL_TEXTURE_2D , 0) => unbinds the current texture.

So, along with currently active texture unit does the type of tex also goes off ???




Although why you'd do that is beyond me

Its a requirement for the problem that I am doing.

scratt
07-16-2009, 11:09 PM
When we do :


glBindTexture(GL_TEXTURE_2D , tex) ; => tex is now of 2D type

next if I do

glBindTexture(GL_TEXTURE_2D , 0) => unbinds the current texture.

So, along with currently active texture unit does the type of tex also goes off ???

No. "tex" is always a 2D texture, bound or unbound.

The setting made with glEnable(GL_TEXTURE_xx) remains. All unbinding does is "unbind" that texture object so it's not current anymore for drawing etc.


Its a requirement for the problem that I am doing.



How so?