如何使用采样器立方体作为数组
How to use samplerCube as array
我正在使用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);
靠着阴影的力量
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '