In OpenGL, sampler2d/cube can be used as uniform in fragment shader. Combine glactivetexture and gluniform1i to bind texture to sampler.
Under normal circumstances, you can refer to the examples in various glsl tutorials. In this case, multiple samplers are used in the shader normally.
For example, multiple samplers are declared in the fragment shader:
uniform samplerCube environmentMap;uniform sampler2D baseTexture;uniform sampler2D bumpTexture;uniform bool enableEnvronmentMap;uniform bool useBaseTexture;uniform bool enableBump;
We can see from the declaration that three variables are used to control whether the corresponding sampler is used in the shader program. For example, if you set enablebump to false, you do not need to call sampler2d bumptexture In the shader. Therefore, glactivetexture, glbindtexture, and gl_texture_2d can be rarely called in the program.
Wait. Can I call glactivetexture less?
Previously, I thought so. After practice, we found that this is indeed the case. Of course, the uniform corresponding to sampler must be set.
Another problem is: Do sampler uniform values need to be adjusted after enablebump is set to false?
If basetextue corresponds to 0, bumptexture corresponds to 1, and environmentmap corresponds to 2, enablebump is set to false without bumptexture or glactivetexture for bump, what values should the other two unirom be set? What value is set for bumptexture? Answer: Change bumptexture to environmentmap. It can be learned that if only environmentmap is used, it should be: environmentmap: 0, basetexture: 1, bumptexture: 2, or the Base should be exchanged with bump.
Now, the truth is clear.
Use of multiple samplers in shader under OpenGL