OpenGl Texture unit - opengl

’m observing a strange behavior from my application which I hope you can explain to me.
You see, I have these two 3D textures that are being sent to the fragment shader and are rendered perfectly OK. But there is a problem, As soon as I even create another texture (it’s a 1D texture), a black screen is being rendered instead of the correct former result.
About this 1D texture, I’m not even sending it to the fragment shader. The minute I call glTexImage1D(…) the black screen shows up. I comment this line and it goes away!!And the two textures are rendered.
I figured there has to be some kind of problem with texture units. Because when I run the application with gDebugger, the texture unit of one of 3D textures is THE SAME as the one assigned to the 1D texture.
I did not assign any texture unit to the 1D texture, I just created it. Apparently texture unit GL_TEXTURE0 is being automatically assigned to the 1D texture. The weird part is that although I use GL_TEXTURE2 and GL_TEXTURE3 for the 3D texture, one of them is being bound to GL_TEXTURE0 as a result of calling glTexImage1D!
Here is a snapshot from the textures list window of gDebugger:
Texture 1 (unit 2 ,bound3d)-enabled
Texture 2 (unit 0 ,bound1d)
Texture 3 (unit 0 ,bound3d)-Enabled (unit 3,bound3D)-enabled
Why is this happening?
the problem is not why texture 1D is being bound to GL_TEXTURE0, it is why it can affect the state of another already bound texture. the code is like this :
.
...
// generating the first texture_3d
glTexImage(GL_TEXTURE_3D,....);
glBindTexture(GL_TEXTURE_3D,id1);
//render loop for the first texture_3d
GLuint glEnum = GL_TEXTURE2;
vtkgl::ActiveTexture(glEnum);
glBindTexture(vtkgl::TEXTURE_3D,id1);
program->setUniform("TEX1",2);
// generating the second texture_3d
glTexImage(GL_TEXTURE_3D,....);
glBindTexture(GL_TEXTURE_3D,id2);
//render loop for the first texture_3d
GLuint glEnum = GL_TEXTURE3;
vtkgl::ActiveTexture(glEnum);
glBindTexture(vtkgl::TEXTURE_3D,id2);
program->setUniform("TEX2",3);
// generating texture 1D
glTexImage(GL_TEXTURE_1D,....);
glBindTexture(GL_TEXTURE_1D,id3);
we expect GL_TEXTURE2 and GL_TEXTURE3 to be active but gDebugger indicates that GL_TEXTURE0 and GL_TEXTURE2 are active.

Call glActiveTexture BEFORE glBindTexture
glBindsTexture binds to CURRENT active texture, so in your current code you first you bind id1 to texture 0, then bind it again to texture2, then bind id2 to texture2 (replacing id1), etc.

You should not have both 1D and 3D textures bound to the same texture unit at the same time. To avoid this, either unbind the 3D texture: glBindTexture() or switch to a new texture unit: vtkgl::ActiveTexture() before binding the 1D texture.
As the above poster has stated, the call to vtkgl::ActiveTexture() must come before the corresponding call to glBindTexture().

Related

Confused about the texture unit value of texture sampler

I'm learning openGL texture from leanrnOpenGL : Textures
I can distinguish between glGetUniformLocation and "texture unit.
glGetUniformLocation : an integer that represents the location of a specific uniform variable within a program object.
texture unit : the location of a texture is known as a texture unit.
The pages has the following sentences:
We also have to tell OpenGL to which texture unit each shader sampler belongs to
by setting each sampler using `glUniform1i`.
The corresponding code is as follows :
ourShader.use();
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0); // set it manually
ourShader.setInt("texture2", 1); // or with shader class
I made some wrong changes as follows :
assigned the texture unit value of texture1 to 3, and texture2 to 4.
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 3);
ourShader.setInt("texture2", 4);
The program can be executed, but show a COMPLETELY PURE black texture(or nothing?)
Does the value of texture unit have to start from 0 ? and why?
Why assigning 3 or 4 cause a wrong effect ?
Under the guidance of #Rabbid76, I got the answer.
In the original text of learnOpenGL : Texture, there is a sentence that caused my confusion:
This location of a texture is more commonly known as a texture unit.
#Rabbid76 correct me:
The binding point between the texture object and the sampler uniform is the `texture unit`.
Does the value of texture unit have to start from 0 ?
A: No.
We can specify it by 0~15 at least.
OpenGL should have a at least a minimum of 16 texture units for you to use
which you can activate using GL_TEXTURE0 to GL_TEXTURE15.
Corresponding, when drawing,
invoke glActiveTexture by specify GL_TEXTURE0 to GL_TEXTURE15.
The following is the correct code aftet my correction:
// set `3` `4` as texture unit value
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 3);
ourShader.setInt("texture2", 4);
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE3); // replace GL_TEXTURE0 with GL_TEXTURE3
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE4); // replace GL_TEXTURE1 with GL_TEXTURE4
glBindTexture(GL_TEXTURE_2D, texture2);
The code run normally and show the correct overlay effect(no longer a black view).

Render to a layer of a texture array in OpenGL

I use OpenGL 3.2 to render shadow maps. For this, I construct a framebuffer that renders to a depth texture.
To attach the texture to the framebuffer, I use:
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shdw_texture, 0 );
This works great. After rendering the light view, my GLSL shader can sample the depth texture to solve visibility of light.
The problem I am trying to solve now, is to have many more shadow maps, let's say 50 of them. In my main render pass I don't want to be sampling from 50 different textures. I could use an atlas, but I wondered: could I pass all these shadow maps as slices from a 2D texture array?
So, somehow create a GL_TEXTURE_2D_ARRAY with a DEPTH format, and bind one layer of the array to the framebuffer?
Can framebuffers be backed for DEPTH by a texture array layer, instead of just a depth texture?
In general, you need to distinguish whether you want to create a layered framebuffer (see Layered Images) or whether you want to attach a single layer of a multilayered texture to a framebuffer.
Use glFramebufferTexture3D to attach a layer of a 3D texture (TEXTURE_3D) or array texture to a framebuffer or use glFramebufferTextureLayer to attach a layer of a three-dimensional or array texture to the framebuffer. In either case the last argument specifies the layer of the texture.
Layered attachments can be attached with glFramebufferTexture. See Layered rendering.

OpenGL confused about multiple textures

I'm developing a 3d program with OpenGL 3.3. So far I managed to render many cubes and some spheres. I need to texture all the faces of all the cubes with a common texture except one face which should have a different texture. I tried with a single texture and everything worked fine but when I try to add another one the program seems to behave randomly.
My questions:
is there a suitable way of passing multiple textures to the shaders?
how am I supposed to keep track of faces in order to render the right texture?
Googling I found out that it could be useful to define vertices twice, but I don't really get why.
Is there a suitable way of passing multiple textures to the shaders?
You'd use glUniform1i() along with glActiveTexture(). Thus given your fragment shader has multiple uniform sampler2D:
uniform sampler2D tex1;
uniform sampler2D tex2;
Then as you're setting up your shader, you set the sampler uniforms to the texture units you want them associated with:
glUniform1i(glGetUniformLocation(program, "tex1"), 0)
glUniform1i(glGetUniformLocation(program, "tex2"), 1)
You then set the active texture to either GL_TEXTURE0 or GL_TEXTURE1 and bind a texture.
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture1)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, texture2)
How am I supposed to keep track of faces in order to render the right texture?
It depends on what you want.
You could decide which texture to use based the normal (usually done with tri-planar texture mapping).
You could also have another attribute that decides how much to crossfade between the two textures.
color = mix(texture(tex1, texCoord), texture(tex2, texCoord), 0.2)
Like before, you can equally have a uniform float transition. This would allow you to fade between textures, making it possible to fade between slides like in PowerPoint, so to speak.
Try reading LearnOpenGL's Textures tutorial.
Lastly there's a minimum of 80 texture units. You can check specifically how many you have available using GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS.
You can use index buffers. Define the vertices once, and then use one index buffer to draw the portion of the mesh with the first texture, then use the second index buffer to draw the portion that needs the second texture.
Here's the general formula:
Setup the vertex buffer
Setup the shader
Setup the first texture
Setup and draw the index buffer for the part of the mesh that should use the first texture
Setup the second texture
Setup and draw the index buffer for the part of the mesh that should use the second texture.

Can I use one Texture unit for 5 different 2D textures

I know there can be GL_MAX_TEXTURE_IMAGE_UNITS texture units in an OpenGL Program. But lets assume I am using only one.
Suppose I have 5 2D textures. Can I "map" them to a single texture unit (TEXTURE0) to render all of them.
I mean I will do this:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
//specify tex data and tex params for tex1
glBindTexture(GL_TEXTURE_2D, tex2);
//specify tex data and tex params for tex1
.. and so on for all 5 textures
Render Loop:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
//draw
glBindTexture(GL_TEXTURE_2D, tex2);
//draw
..and so on for all 5 textures
In the fragment shader I am using only 1 sampler2d variable which is initialized to zero (for texture unit 0)
If this will not work correctly, then should I use 5 different texture units(TEXTURE0, TEXTURE0+1, ....TEXTURE0+5) and "map" each texture to a different texture unit.
Most important question: If my first approach is correct, how many 2D textures(lets just only talk about 2D textures for now) can I "map" to a Texture unit?
As long as you don't want to use several textures on a single piece of geometry simultanously, this is perfectly possible. Actually it's the usual thing to do, because the purpose of multitexturing and texture units is not to hold different textures for different objects, but different layers of textures used on the same object.
Think of texture units as something like layers in Photoshop.
Well, as I understand OpenGL texture units:
Text units are a way for shaders to point texture for sampling. If your shader must access more than one texture, you must separate them into differents texture units.
In your case, as you seem to need only 1 texture at the same time, glActiveTexture is useless, just let's Opengl use the default GL_TEXTURE0 for all.
So, yes, your render loop is correct.
How many is the limit of texture units? 48+, more than enough!
Note:
Remember that OpenGL is a state machine, so to configure more than 1 texture to work at the same time, you need a way to select them, that is exactly what glActiveTexture do.
I hope it will help you.

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