如何使用采样器立方体作为数组

How to use samplerCube as array

本文关键字:数组 立方体 何使用 采样器      更新时间:2023-10-16

我正在使用samplerCube作为点光源阴影贴图。对于多个光源,我在下面将samplerCube实现为数组。

uniform samplerCube pointShadowMapTexture[MAX_LIGHT];

但不知何故,我无法索引这个samplerCube.着色器编译,没有问题。这适用于sampler2D数组。

我尝试在着色器中使用 [0]、[1] .. 索引它,但始终是相同的图像。我正在为每个光源发送不同的立方体纹理,但不知何故着色器没有索引它或不接受它。

我正在对定向光做同样的事情,就像采样器2D阵列一样。但是当涉及到采样器立方体时,它不起作用。

将采样器多维数据集发送到着色器的代码

void ShaderProgram::bindTexture(GLenum target , const char * name , int id){
GLuint TextureID  = glGetUniformLocation(programID, name);
glActiveTexture(GL_TEXTURE0 + textureOrder);
glBindTexture(target , id);
glUniform1i(TextureID, textureOrder);
textureOrder++;
App::checkErrors(__FILE__,__LINE__,name);
}
//depthMapTexture is member of Light class
std::string PointShadowMapTexture = "pointShadowMapTexture[" + std::to_string(LightNumber) + "]";
ShaderProgram::shaders["deferred"]->bindTexture(GL_TEXTURE_CUBE_MAP, PointShadowMapTexture.data(), depthMapTexture );
float SoftPointShadowCalculation(int index , vec3 fragPos ,vec3 lightPos){
vec3 fragToLight = fragPos - lightPos;
float currentDepth = length(fragToLight);
float shadow = 0.0;
float bias = 0.0;
int samples = 20;
float viewDistance = length(viewPos - fragPos);
float diskRadius = (1.0 + (viewDistance / farPlane)) / 25.0;
for(int i = 0; i < samples; ++i){
float closestDepth = texture(pointShadowMapTexture[index], fragToLight + gridSamplingDisk[i] * diskRadius).r;
closestDepth *= farPlane;//farplane
if(currentDepth - bias > closestDepth){
shadow += 0.5;
}
}
shadow /= float(samples);
return shadow;
}

这对samplerCube类型有效吗?如果不是,我应该怎么做才能拥有采样器多维数据集数组?

我意识到所有的灯都显示了最后的灯光。因此,当我渲染模型时,我为每个光源使用了最后一个光源投影*视图矩阵。:)花了几个小时才意识到。

现在,每个光源都使用自己的矩阵进行渲染。

如果遇到相同的问题,这可能会对某人有所帮助

glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
for(Light * light : Light::shadowCasterLights){
glBindFramebuffer(GL_FRAMEBUFFER, light->depthMapFrameBuffer);App::checkFrameBufferError(__FILE__,__LINE__);
glViewport(0,0,light->depthMapTextureSize,light->depthMapTextureSize);
ShaderProgram::shaders[light->depthMapShader]->use("ShadowCaster Model");
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("ModelMatrix", &scene->ModelMatrix[0][0]);
ShaderProgram::shaders[light->depthMapShader]->attributeBuffer("ModelSpaceVertexPosition", mesh->vertexBufferID, 3);
switch (light->lightType) {
case LightType::DIRECTIONAL:
ShaderProgram::shaders[light->depthMapShader]->use("Light");
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix",&light->lightSpaceMatrix[0][0]);
break;
case LightType::POINT:
ShaderProgram::shaders[light->depthMapShader]->use("Light");
ShaderProgram::shaders[light->depthMapShader]->uniform3f("LightPosition" , &positionVector[0]);
ShaderProgram::shaders[light->depthMapShader]->uniform1f("FarPlane" , light->farPlane);
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[0]",&light->lightSpaceMatrixCube[0][0][0]);
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[1]",&light->lightSpaceMatrixCube[1][0][0]);
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[2]",&light->lightSpaceMatrixCube[2][0][0]);
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[3]",&light->lightSpaceMatrixCube[3][0][0]);
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[4]",&light->lightSpaceMatrixCube[4][0][0]);
ShaderProgram::shaders[light->depthMapShader]->uniformMatrix4("LightSpaceMatrix[5]",&light->lightSpaceMatrixCube[5][0][0]);
break;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->elementBufferID);
glDrawElements(
GL_TRIANGLES,      // mode
mesh->indices.size(),    // count
GL_UNSIGNED_SHORT,   // type
(void *) 0           // element array buffer offset
);
ShaderProgram::shaders[light->depthMapShader]->reset();
}
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,1920,1080);

靠着阴影的力量