首页 技术 正文
技术 2022年11月19日
0 收藏 792 点赞 2,393 浏览 955 个字

在OpenGL中,sampler2D/Cube等做为uniform可以在fragment shader中使用。结合glActiveTexture和glUniform1i,实现texture与sampler的绑定。
正常情况下的使用,可以参照各种GLSL教程里的例子。这里说一下非正常情况下在shader中使用多个sampler。
比如在fragment shader中声明了多个sampler:


uniform samplerCube environmentMap;
uniform sampler2D baseTexture;
uniform sampler2D bumpTexture;
uniform bool enableEnvronmentMap;
uniform bool useBaseTexture;
uniform bool enableBump;

从声明上可以看出,这里用了三个变量分别控制是否在shader program中使用对应的sampler。比如,把enableBump设置为false,shader中就不用调用bumpTexture这个sampler2D。从而,在程序中就可以少调用glActiveTexture、glBindTexture、glEnable(GL_TEXTURE_2D)等。

等等,真的可以少调用glActiveTexture了吗?

之前,我是这样认为的。经过实践发现,确实是这样的。当然,与sampler对应的uniform必须设置。

另外一个问题,把enableBump设置为false后,sampler uniform的值需要调整吗?

假如baseTextue对应0, bumpTexture对应1,environmentMap对应2,那么如果把enableBump设置为false,不使用bumpTexture,也不为bump设置glActiveTexture,那其他两个unifrom应该设置何值?给bumpTexture设置何值?答案:0,1,2,即bumpTexture与environmentMap换位置了。从而可以得知,如果只使用environmentMap,应该为:environmentMap:0, baseTexture:1, bumpTexture:2,或者base与bump互换。

至此,真相大白。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,946
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,472
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,285
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,101
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,733
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,768