无法在OpenGL着色器中设置统一值 [英] Can't set uniform value in OpenGL shader
问题描述
我目前正在学习OpenGL编程,我已经取得了一些进展,但我现在已经陷入困境了。
I am currently learning OpenGL programming, and I have made some good progress, but I seem to be stuck now.
目前,我想获得简单的着色器上班。更具体地说,我想设置着色器的统一值。着色器编译成功,如果我消除了统一值,它可以正常工作。这里是(顶点)着色器代码:
Currently, I am trying to get simple shaders to work. To be more specific, I am trying to set a uniform value of the shader. The shaders compile successfully, and work correctly if I eliminate the uniform value. Here is the (vertex) shader code:
#version 440 core
layout(location = 0) in vec3 vertex;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(vertex, 1);
}
我用来设置值的代码( glGetUniformLocation
按预期返回0):
And the code I use to set the value (glGetUniformLocation
returns 0 as expected):
mvp = glm::mat4(
glm::vec4(3.0f, 0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 1.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 0.0f, 2.0f)
);
GLuint matrix = glGetUniformLocation(program, "mvp");
glUniformMatrix4fv(matrix, 1, GL_FALSE, glm::value_ptr(mvp));
问题是它似乎根本没有设置值。如果我使用 glGetUniformfv
查询统一的值,它返回默认值(而不是我刚才设置的值),几何不会显示出来只是一个后果,这不是这里的问题。)
The problem is that it doesn't seem to set the value at all. If I query the uniform's value using glGetUniformfv
, it returns the default value (not the one I just set), and the geometry doesn't show up at all (but that is just a consequence, that's not the problem here).
如果我在着色器中硬编码统一的值,应用程序正确显示几何,当要求的值再次使用 glGetUniformfv
),它返回正确的值。 vec3顶点中的
If I hardcode the uniform's value in the shader, the application displays the geometry correctly, and when asked for the value (again, using glGetUniformfv
), it returns the correct value.
#version 440 core
layout(location = 0) in vec3 vertex;
uniform mat4 mvp = mat4(
vec4(1.0f, 0.0f, 0.0f, 0.0f),
vec4(0.0f, 1.0f, 0.0f, 0.0f),
vec4(0.0f, 0.0f, 1.0f, 0.0f),
vec4(0.0f, 0.0f, 0.0f, 2.0f)
);
void main()
{
gl_Position = mvp * vec4(vertex, 1);
}
任何想法为什么会发生?我使用的是OpenGL 4.4,但我尝试了很多不同的版本,结果相同。
Any idea why does this happen? I am using OpenGL 4.4, but I have tried many different versions with the same result.
感谢您的时间和精力,
Levente
推荐答案
在设置制服之前调用glUseProgram?完整的工作列表可以在很多在线示例中找到,包括我在某个时候写的内容( https:// github.com/prabindh/sgxperf/blob/master/sgxperf_gles20_vg.cpp ),显示序列为:
Are you calling glUseProgram before setting the uniforms ? A full working listing can be found in many examples online, including what I had written sometime back (https://github.com/prabindh/sgxperf/blob/master/sgxperf_gles20_vg.cpp) that shows the sequence to be:
glCreateProgram();
glCreateProgram();
glAttachShader()
glAttachShader()
glLinkProgram
glLinkProgram
glUseProgram()
glUseProgram()
glGetUniformLocation()
glGetUniformLocation()
glUniformMatrix4fv
glUniformMatrix4fv
这篇关于无法在OpenGL着色器中设置统一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!