What is the correct behavior when both a 1D and a 2D texture are bound in OpenGL?

此生再无相见时 提交于 2019-12-05 15:21:41

问题


Say you have something like this:

glBindTexture(GL_TEXTURE_2D, my2dTex);
glBindTexture(GL_TEXTURE_1D, my1dTex);
glBegin...

What is the correct OpenGL behavior? To draw the 1d texture, the 2d or both? For each active texture are there actually multiple textures that can be bound to it at the same time (i.e. a 1d, 2d, 3d cube map, etc.)?


回答1:


The GL state for the bindings is one texture name per target (i.e. 1D/2D/3D/cube). So when calling

glBindTexture(GL_TEXTURE_2D, my2dTex)
glBindTexture(GL_TEXTURE_1D, my1dTex)

the GL will remember both settings.

Now, the answer of which one GL will use depends on whether you have a shader on.

If a shader is on, the GL will use whatever the shader says to use. (based on sampler1d/sampler2d...).

If no shader is on, then it first depends on which glEnable call has been made.

glEnable(GL_TEXTURE_2D)
glEnable(GL_TEXTURE_1D)

If both are enabled, there is a static priority rule in the spec (3.8.15 Texture Application in the GL 1.5 spec).

Cube > 3D > 2D > 1D

So in your case, if both your texture targets are enabled, the 2D one will be used.

As a side note, notice how a shader does not care whether or not the texture target is Enabled...

Edit to add:

And for the people who really want to get to the gritty details, you always have a texture bound for each target * each unit. The name 0 (the default for the binding state) corresponds to a series of texture objects, one per target. glBindTexture(GL_TEXTURE_2D, 0) and glBindTexture(GL_TEXTURE_1D, 0) both bind a texture, but not the same one...

This is historical, specified to match the behavior of GL 1.0, where texture objects did not exist yet. I am not sure what the deprecation in GL3.0 did with this, though.




回答2:


I think that the 1d texture will be drawn. As far as I know, each texture unit can have only one texture bound at a time. The number of dimensions of the texture doesn't really come into it.

To have more than one texture bound you have to activate more than one texture unit (using glActiveTexture), like this:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, my2dTex);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_1D, my1dTex);

This comes from my knowledge of OpenGL 2.1 though, so please correct me if they've introduced some fancy new texture extension in a later version!




回答3:


The applied texture is the last specified with BindTexture routine.

From the specification:

The new texture object bound to target is, and remains a texture of the dimensionality and type specified by target until it is deleted. .... If the bind is successful no change is made to the state of the bound texture object, and any previous binding to target is broken.

Indeed in your example it will be applied the 1D texture, since it "overwrite" the state of the active texture unit.



来源:https://stackoverflow.com/questions/2784937/what-is-the-correct-behavior-when-both-a-1d-and-a-2d-texture-are-bound-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!