我正在尝试为我当前的简单立方体场景添加照明。设置我的制服后,我从 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 );
这是我的着色器: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;
}
片段.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;
}
产品和位置都是三个 GLfloat 的结构,而 shininess 是一个浮点数。我检查了句柄的所有值和我传递的值,它们似乎都是有效的。想法?
--编辑:我已将其范围缩小到 glUniform4fv 调用。它发生在每一个之后。我还仔细检查了 program->getHandle() 是否指向看起来有效的东西。
我检查了 program->getHandle 是一个有效的程序 以下是所有句柄的值: 程序句柄 3ambientHandle 0diffuseHandle 1 specularHandle 5 lightPosHandle 2 shinyHandle 4
所以他们看起来都很好。为了测试,我将注释掉环境产品下面的行。为了清楚起见,我明确地使用了这一行
glUniform4f( ambientHandle, ambientProd.x, ambientProd.y, ambientProd.z, ambientProd.w );
这些是执行该行时ambientProd 的值。x = 0.200000003,y = 0.0,z = 0.200000003,w = 1.0
该项目的一位合作者提出了 glUseProgram 的呼吁。感谢各位的帮助。