无法使用 glUniform1i 函数加载纹理统一

Can't load texture uniform with glUniform1i function

我尝试在 'The Cherno' 频道上关注 YT 上有关 OpenGL 的教程(您可以找到我的代码 here)。我有两个制服 u_Coloru_Texture,我使用从 Shader.cpp 文件调用的 glUniform1i 函数加载它们。 u_Color 一切正常,但是当我尝试加载 u_Texture 时,我收到错误代码

0x502 (GL_INVALID_OPERATION; Qt debugging stops printing out "Illegal operation" error).

我试图在着色器和 cpp 代码中删除对 "u_Color" uniform 的未使用调用,我试图在 GLCall 宏和其他一些东西之外使用该函数,但它根本不想工作。我确定纹理的位置没问题(unsigned int),我认为我的代码看起来与实际有效的教程中的代码完全一样!

我在 Linux 系统 (18.04) 上工作,使用 Intel 显卡,我正在使用带有 g++ 编译器 (7.5.0) 的 Qt Creator(基于 Qt 5.14.2 的 Qt Creator 4.11.2) ).

如果有人能检查一下,我将不胜感激。

那是来自 "Shader.cpp"

的代码中有问题的部分
GLuint uniformlocation = static_cast<GLuint>(glGetUniformLocation(m_rendererID, "u_Texture"));
glUniform1f(uniformLocation, value);

这是使用 u_Texture

的片段着色器
#version 330 core

layout(location = 0) out vec4 color;

in vec2 v_TexCoord;

uniform vec4 u_Color;
uniform sampler2D u_Texture;

void main()
{
    vec4 texColor = texture2D(u_Texture, v_TexCoord);
    color = texColor;
}

glUniform1f sets a value of a uniform of the currently installed program, thus the program has to be installed by glUseProgram,之前:

GLuint uniformlocation = static_cast<GLuint>(glGetUniformLocation(m_rendererID, "u_Texture"));
glUseProgram(m_rendererID);
glUniform1f(uniformLocation, value);

分别是:

shader.bind();
shader.setUniform1i("u_Texture", 0);

GL_INVALID_OPERATION是因为没有当前程序对象造成的


或者您可以使用glProgramUniform为指定的程序对象指定统一变量的值