我正在尝试在一个着色器中使用sampler2D
和samplerCube
。我曾经用struct
来做几个sampler2D
。但是现在我决定停止将它们放在struct
中,并注意到一些功能。如果将sampler2D
放入struct
中,则将samplerCube
绑定1次就足够了。但是,如果将sampler2D
放在结构外部,则每次绘制每个模型时都必须绑定samplerCube
。下面,我给出了伪代码的示例。
sampler2D
进入 struct
:
uniform samplerCube shadowMaps;
uniform struct Mapping {
sampler2D ambientMapping;
sampler2D diffuseMapping;
sampler2D specularMapping;
sampler2D normalMapping;
} mapping;
C ++伪代码:
// somewhere in init
// samplerCube
glUniform1i(shadow, 4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_CUBE_MAP, shadowMapId);
// ...
// drawing models
for (size_t i = 0; i < MODELS_COUNT; i++) {
glUniform1i(model[i].ambientMapping, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model[i].texturesIds[0]);
glUniform1i(model[i].diffuseMapping, 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, model[i].texturesIds[1]);
// analogically for specular and normal
// model
}
sampler2D
外部 struct
:
uniform samplerCube shadowMaps;
uniform sampler2D ambientMapping;
uniform sampler2D diffuseMapping;
uniform sampler2D specularMapping;
uniform sampler2D normalMapping;
C ++伪代码:
// drawing models
for (size_t i = 0; i < MODELS_COUNT; i++) {
glUniform1i(model[i].ambientMapping, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model[i].texturesIds[0]);
glUniform1i(model[i].diffuseMapping, 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, model[i].texturesIds[1]);
// analogically for specular and normal
// samplerCube
glUniform1i(shadow, 4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_CUBE_MAP, shadowMapId);
// model
}
在第二种情况下,为什么我每次都应该做glBindTexture(GL_TEXTURE_CUBE_MAP, shadowMapId)
?最好的方法是什么?什么会更有生产力?