C++ OpenGL 着色器错误 1282

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15722803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:41:58  来源:igfitidea点击:

OpenGL Shader error 1282

c++opengl

提问by user2227713

I am trying to add lighting to my current scene of a simple cube. After setting up my uniforms I get a 1282 error from glGetError() for this piece of code

我正在尝试为我当前的简单立方体场景添加照明。设置完我的制服后,我从 glGetError() 收到了一段代码的 1282 错误

GLuint ambientHandle = glGetUniformLocation(program->getHandle(), "ambientProduct");
glUniform4fv( ambientHandle, 1, ambientProduct );
GLuint diffuseHandle = glGetUniformLocation(program->getHandle(), "diffuseProduct");
glUniform4fv( diffuseHandle, 1, diffuseProduct );
GLuint specularHandle = glGetUniformLocation(program->getHandle(), "specularProduct");
glUniform4fv( specularHandle, 1, specularProduct );
GLuint lightPosHandle = glGetUniformLocation(program->getHandle(), "lightPosition");
glUniform4fv( lightPosHandle, 1, light.position );
GLuint shinyHandle = glGetUniformLocation(program->getHandle(), "shininess");
glUniform1f( shinyHandle, materialShininess );

Here are my shaders: vertex.glsl

这是我的着色器:vertex.glsl

#version 120

attribute vec4 coord3d;
attribute vec3 normal3d;

// output values that will be interpretated per-fragment
varying  vec3 fN;
varying  vec3 fE;
varying  vec3 fL;

uniform vec4 lightPosition;
uniform mat4 mTransform;

void main()
{
    fN = normal3d;
    fE = coord3d.xyz;
    fL = lightPosition.xyz;

    if( lightPosition.w != 0.0 ) {
    fL = lightPosition.xyz - coord3d.xyz;
    }

    gl_Position = mTransform*coord3d;
}

fragment.glsl

片段.glsl

// per-fragment interpolated values from the vertex shader
varying  vec3 fN;
varying  vec3 fL;
varying  vec3 fE;

uniform vec4 ambientProduct, diffuseProduct, specularProduct;
uniform mat4 mTransform;
uniform vec4 lightPosition;
uniform float shininess;

void main() 
{ 
    // Normalize the input lighting vectors
    vec3 N = normalize(fN);
    vec3 E = normalize(fE);
    vec3 L = normalize(fL);

    vec3 H = normalize( L + E );

    vec4 ambient = ambientProduct;

    float Kd = max(dot(L, N), 0.0);
    vec4 diffuse = Kd*diffuseProduct;

    float Ks = pow(max(dot(N, H), 0.0), shininess);
    vec4 specular = Ks*specularProduct;

    // discard the specular highlight if the light's behind the vertex
    if( dot(L, N) < 0.0 ) {
    specular = vec4(0.0, 0.0, 0.0, 1.0);
    }

    gl_FragColor = ambient + diffuse + specular;
    gl_FragColor.a = 1.0;
} 

The products and position are each a struct of three GLfloats and shininess is a float. I have checked all of the values of the handles and the values I am passing and they all seem valid. Ideas?

product 和 position 都是三个 GLfloats 的结构体,而 shininess 是一个浮点数。我已经检查了句柄的所有值和我传递的值,它们似乎都有效。想法?

--EDIT: I have narrowed it to the glUniform4fv calls. It happens after each one. Also I have double checked that the program->getHandle() is pointing to something that looks valid.

--编辑:我已将其范围缩小到 glUniform4fv 调用。它发生在每一个之后。我还仔细检查了 program->getHandle() 是否指向看起来有效的东西。

I have checked program->getHandle is a valid program Here are the values of all handles: Program handle 3 ambientHandle 0 diffuseHandle 1 specularHandle 5 lightPosHandle 2 shinyHandle 4

我检查了 program->getHandle 是一个有效的程序 这里是所有句柄的值: Program handle 3ambientHandle 0diffuseHandle 1 specularHandle 5 lightPosHandle 2 ShinyHandle 4

So they all look good. For testing I am commenting out the lines below the ones for ambientProduct. For clarity I am explicitly using this line instead

所以他们都看起来不错。为了测试,我注释掉了环境产品下面的行。为清楚起见,我明确使用这一行

glUniform4f( ambientHandle, ambientProd.x, ambientProd.y, ambientProd.z, ambientProd.w );

These are the values for ambientProd at the time that line is executed. x = 0.200000003, y = 0.0, z = 0.200000003, w = 1.0

这些是执行该行时ambientProd 的值。x = 0.200000003,y = 0.0,z = 0.200000003,w = 1.0

A collaborator on this project moved the call for glUseProgram. Thanks for the help folks.

该项目的一位合作者提出了对 glUseProgram 的呼吁。谢谢大家的帮助。

回答by mikyra

Error number ′1282` is not very descriptive.

错误号 '1282' 不是很具有描述性。

Possible error codes for glGetUniformLocationare:

可能的错误代码glGetUniformLocation是:

GL_INVALID_VALUE
GL_INVALID_OPERATION

Which don't have a fixed value. Try to get the error string with gluErrorString()or take a look in the header to which of those 1282maps.

哪个没有固定值。尝试获取错误字符串gluErrorString()或查看这些1282映射的标题。

Just a shot in the dark: but did you ...

只是在黑暗中射击:但是你...

  • check your shader got compiled without error?

  • check your shader got linked without error?

  • 检查你的着色器是否编译没有错误?

  • 检查您的着色器是否正确链接?

BTW: return type is GLint not GLuint

顺便说一句:返回类型是 GLint 而不是 GLuint

"Shaders compiled and linked without error" Hmm, this looks odd.

“着色器编译和链接没有错误”嗯,这看起来很奇怪。

According to spec (see: http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml) GL_INVALID_OPERATIONshould only be generated if:

根据规范(请参阅:http: //www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xmlGL_INVALID_OPERATION仅应在以下情况下生成:

  • program is not a program objec

  • program has not been successfully linked

  • 程序不是程序对象

  • 程序尚未成功链接

Other question:

其他问题:

  • are you sure the getHandle()method of the class your programObject belongs to returns the right id. I mean the one that was used in the sucessfully linking.

    you should be able to verify with checking if glIsProgram(program-getHandle())returns GL_TRUE

  • 您确定getHandle()您的program对象所属的类的方法返回正确的 id。我的意思是在成功链接中使用的那个。

    您应该能够通过检查是否glIsProgram(program-getHandle())返回来验证GL_TRUE

EDIT:Ah - I missed those calls to glUniform4fvin your example.

编辑:啊 - 我错过glUniform4fv了你的例子中的那些电话。

Correct return type for glGetUniformLocationis still GLint, but I don't think thats the problem.

正确的返回类型glGetUniformLocation仍然是GLint,但我认为这不是问题所在。

According to spec (see: http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml) GLUniformXXgenerates GL_INVALID_OPERATIONfor a whole bunch of reasons. To me the only one that possibly seems to apply is:

根据规范(参见:http: //www.opengl.org/sdk/docs/man/xhtml/glUniform.xml)出于各种原因GLUniformXX生成GL_INVALID_OPERATION。对我来说,唯一可能适用的是:

  • there is no current program object

    Did you call glUseProgram (program->getHandle())prior to trying to calling glUniform()?

  • 没有当前的程序对象

    glUseProgram (program->getHandle())在尝试打电话之前打电话了glUniform()吗?

回答by suraj kumar

Generally this error number occurs when you are using a different programID from the programID generated by openGL at the time of creating the shader. It means that you are using a different programID at the time of binding vertexShaderor fragmentShaderor whatever other shader you are using.

通常,当您使用与创建着色器时由 openGL 生成的 programID 不同的 programID 时,会出现此错误编号。这意味着您在绑定vertexShaderfragmentShader正在使用的任何其他着色器时使用了不同的程序 ID 。