c++ 无法将统一值发送到着色器

hgc7kmma  于 2023-03-14  发布在  其他
关注(0)|答案(1)|浏览(140)

我试图为我的着色器设置一些统一的值,但是我只能得到一个我以前用过的纹理(插槽0),并且所有矩阵都用0填充。
这是我的代码的一部分。

glUseProgram(VSSMTestShaderProgram);
glUniform1i(glGetUniformLocation(VSSMTestShaderProgram, "depthMap"), 10);
glUniform1i(glGetUniformLocation(VSSMTestShaderProgram, "depth2Map"), 11);
transformLoc = glGetUniformLocation(VSSMTestShaderProgram, "transformToScreenSpace");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(view_trans));
glm::mat4 shadowProj = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.01f, 30.0f);
glm::mat4 shadowTransform = shadowProj * glm::lookAt(glm::vec3(5.0,5.0,5.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
transformLoc = glGetUniformLocation(VSSMTestShaderProgram, "transformToLightSpace");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(shadowTransform));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

下面是着色器代码

//shader for VSSM shadows
const char* vertexVSSMTest = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec4 LightSpaceCoord;\n"
"uniform mat4 transformToScreenSpace;\n"
"uniform mat4 transformToLightSpace;\n"
"void main()\n"
"{\n"
"   gl_Position = transformToScreenSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
"   LightSpaceCoord = transformToLightSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
"}\0";
const char* fragVSSMTest = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec4 LightSpaceCoord;\n"
"uniform sampler2D depthMap;\n"
"uniform sampler2D depth2Map;\n"
"void main()\n"
"{\n"//vec2(LightSpaceCoord.x/2+0.5, LightSpaceCoord.y/2+0.5)
"   float lightDepth = texture(depthMap, vec2(0.1, 0.1)).r;\n"
"   if(lightDepth<LightSpaceCoord.z){\n"
"       FragColor = vec4(lightDepth, lightDepth, lightDepth, 1.0);\n"
"   }else{\n"
"       FragColor = gl_FragColor;\n"
"   }\n"
"}\n\0";

该矩阵被设置为:

纹理也设置不正确:

anauzrmj

anauzrmj1#

不能在着色器代码中使用gl_FragColor值。

相关问题