Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)?

like image 717
Chris Avatar asked May 06 '10 22:05

Chris


People also ask

How do textures work in OpenGL?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.

How do you show textures in OpenGL?

You need to bind the texture, enable texturing (fixed function pipeline) or use a shader that does texturing (shader pipeline), and draw some shape like a quad. Don't forget the shape you draw needs texture coordinates.

What is OpenGL es texture?

The OpenGL method can be used to generate multiple handles at the same time; here we generate just one. Once we have a texture handle, we use it to load the texture. First, we need to get the texture in a format that OpenGL will understand.

What is Gl_repeat?

GL_REPEAT : The integer part of the coordinate will be ignored and a repeating pattern is formed. GL_MIRRORED_REPEAT : The texture will also be repeated, but it will be mirrored when the integer part of the coordinate is odd. GL_CLAMP_TO_EDGE : The coordinate will simply be clamped between 0 and 1 .


1 Answers

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.

like image 62
Bahbar Avatar answered Nov 14 '22 08:11

Bahbar