Use of multiple samplers in shader under OpenGL

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.