英文:
Can't send uniform values to shader
问题
我尝试为我的着色器设置一些统一值,但我只能获取之前已使用的纹理(槽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);
这是着色器代码:
// VSSM阴影着色器
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"
"}// VSSM阴影着色器
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"
" 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";
";
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"
" 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// VSSM阴影着色器
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"
" 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";
";
矩阵设置如下:
纹理也被错误地设置为:
英文:
I'm trying to set some uniform values for my shader, but I can only get a texture I already used before(slot 0), and all matrix were filled with 0.
Here is a part of my code.
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);
here is the shader code
//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"
"}//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";
";
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//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";
";
the matrix is set as:
and texture is also set incorrectly:
答案1
得分: 1
根据您发布的代码,似乎您没有做错任何事情。这意味着可能是在其他地方出了问题。一个明显需要检查的地方是确保在绑定纹理和调用绘制函数之前,调用了glActiveTexture(GL_TEXTUREn),其中n是槽位。但在您的代码中,可能还有很多其他问题,因为OpenGL对于调用的顺序等非常敏感。
如果您还没有这样做,我强烈建议查看这个教程:https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping。特别是要查看并复制示例代码。
我还强烈建议将一些OpenGL函数抽象为单独的方法,并逐步测试每个方法。如果您能让这些方法按照您的要求执行,那么以后就不必担心这些方法会引起问题。目前,您只是直接调用一系列OpenGL函数,这使得很难确定问题出在哪里。
英文:
Based on the code you have posted it doesn't seem that you have done anything wrong. That means something is probably wrong somewhere else. An obvious place to check would be to make sure you are calling glActiveTexture(GL_TEXTUREn) where n is the slot before binding your texture and calling your draw function. But there could be any of many things going wrong elsewhere in your code, as openGL can be very sensitive to wrong order calls and such.
I would strongly recommend checking out this tutorial if you haven't already: https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping. Especially to look at and replicate the sample code.
I would also strongly recommend abstracting away some of those openGL functions to separate methods and testing each method incrementally. If you can get the methods to do what you want them to, then you don't have to worry about those methods causing the problems in the future. Right now you are just directly calling a series of openGL functions which makes it difficult to determine where exactly the problem is occuring.
答案2
得分: 0
你不能在着色器代码中使用gl_FragColor的值。
英文:
You can't use gl_FragColor values in shader code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论