无法向着色器发送统一值

huangapple go评论82阅读模式
英文:

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, &quot;depthMap&quot;), 10);
glUniform1i(glGetUniformLocation(VSSMTestShaderProgram, &quot;depth2Map&quot;), 11);
transformLoc = glGetUniformLocation(VSSMTestShaderProgram, &quot;transformToScreenSpace&quot;);
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, &quot;transformToLightSpace&quot;);
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 = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;out vec4 LightSpaceCoord;\n&quot;
&quot;uniform mat4 transformToScreenSpace;\n&quot;
&quot;uniform mat4 transformToLightSpace;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transformToScreenSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;	LightSpaceCoord = transformToLightSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;}
//shader for VSSM shadows
const char* vertexVSSMTest = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;out vec4 LightSpaceCoord;\n&quot;
&quot;uniform mat4 transformToScreenSpace;\n&quot;
&quot;uniform mat4 transformToLightSpace;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transformToScreenSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;	LightSpaceCoord = transformToLightSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;}\0&quot;;
const char* fragVSSMTest = &quot;#version 330 core\n&quot;
&quot;out vec4 FragColor;\n&quot;
&quot;in vec4 LightSpaceCoord;\n&quot;
&quot;uniform sampler2D depthMap;\n&quot;
&quot;uniform sampler2D depth2Map;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;//vec2(LightSpaceCoord.x/2+0.5, LightSpaceCoord.y/2+0.5)
&quot;	float lightDepth = texture(depthMap, vec2(0.1, 0.1)).r;\n&quot;
&quot;	if(lightDepth&lt;LightSpaceCoord.z){\n&quot;
&quot;		FragColor = vec4(lightDepth, lightDepth, lightDepth, 1.0);\n&quot;
&quot;	}else{\n&quot;
&quot;		FragColor = gl_FragColor;\n&quot;
&quot;	}\n&quot;
&quot;}\n\0&quot;;
&quot;; const char* fragVSSMTest = &quot;#version 330 core\n&quot; &quot;out vec4 FragColor;\n&quot; &quot;in vec4 LightSpaceCoord;\n&quot; &quot;uniform sampler2D depthMap;\n&quot; &quot;uniform sampler2D depth2Map;\n&quot; &quot;void main()\n&quot; &quot;{\n&quot;//vec2(LightSpaceCoord.x/2+0.5, LightSpaceCoord.y/2+0.5) &quot; float lightDepth = texture(depthMap, vec2(0.1, 0.1)).r;\n&quot; &quot; if(lightDepth&lt;LightSpaceCoord.z){\n&quot; &quot; FragColor = vec4(lightDepth, lightDepth, lightDepth, 1.0);\n&quot; &quot; }else{\n&quot; &quot; FragColor = gl_FragColor;\n&quot; &quot; }\n&quot; &quot;}\n
//shader for VSSM shadows
const char* vertexVSSMTest = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;out vec4 LightSpaceCoord;\n&quot;
&quot;uniform mat4 transformToScreenSpace;\n&quot;
&quot;uniform mat4 transformToLightSpace;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transformToScreenSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;	LightSpaceCoord = transformToLightSpace * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;}\0&quot;;
const char* fragVSSMTest = &quot;#version 330 core\n&quot;
&quot;out vec4 FragColor;\n&quot;
&quot;in vec4 LightSpaceCoord;\n&quot;
&quot;uniform sampler2D depthMap;\n&quot;
&quot;uniform sampler2D depth2Map;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;//vec2(LightSpaceCoord.x/2+0.5, LightSpaceCoord.y/2+0.5)
&quot;	float lightDepth = texture(depthMap, vec2(0.1, 0.1)).r;\n&quot;
&quot;	if(lightDepth&lt;LightSpaceCoord.z){\n&quot;
&quot;		FragColor = vec4(lightDepth, lightDepth, lightDepth, 1.0);\n&quot;
&quot;	}else{\n&quot;
&quot;		FragColor = gl_FragColor;\n&quot;
&quot;	}\n&quot;
&quot;}\n\0&quot;;
&quot;;

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.

huangapple
  • 本文由 发表于 2023年3月9日 17:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定