对于设备,我的所有着色器都加载正常,除了一个。对于这个着色器程序,我得到“片段程序无法使用当前上下文状态编译”错误,当我调用glGetProgramInfoLog(...)时,顶点着色器后面出现类似的错误;
顶点着色器:
#version 100
uniform mat4 Projection;
uniform mat4 Modelview;
uniform mat4 Rotation;
uniform vec3 Translation;
uniform vec4 LightDirection;
uniform vec4 MaterialDiffuse;
uniform float MaterialShininess;
attribute vec3 position;
attribute vec3 normal;
varying vec4 color;
varying float specularCoefficient;
void main() {
vec3 _normal = normalize(mat3(Modelview[0].xyz, Modelview[1].xyz, Modelview[2].xyz)*normal);
// There is an easier way to do the above using typecast, but is apparently broken
float NdotL = dot(-_normal, normalize(vec3(LightDirection)));
if(NdotL < 0.0){
NdotL = 0.0;
}
color = NdotL * MaterialDiffuse;
float NdotO = dot(-_normal, vec3(0.0, 0.0, -1.0));
if(NdotO < 0.0){
NdotO = 0.0;
}
specularCoefficient = pow(NdotO, MaterialShininess);
vec3 p = position + Translation;
gl_Position = Projection*Modelview*vec4(p, 1.0);
}
片段着色器:
#version 100
precision mediump float;
varying vec4 color;
varying float specularCoefficient;
uniform vec4 MaterialSpecular;
void main(){
gl_FragColor = vec4((color + specularCoefficient*MaterialSpecular).rgb, 1.0);
}
我不确定发生了什么,特别是因为我有一个与添加纹理坐标完全相同的程序。此外,当我使用glGetShaderiv(theShader,GL_COMPILE_STATUS和&amp; result)链接程序时,我检查了每个着色器的编译状态,并且它们都检查完好。有任何想法吗?
答案 0 :(得分:1)
更改行
gl_FragColor = vec4((color + specularCoefficient*MaterialSpecular).rgb, 1.0);
片段着色器中的
gl_FragColor = vec4((1.0*color + specularCoefficient*MaterialSpecular).rgb, 1.0);
解决了这个问题。我怀疑它与变化的变量颜色相关的精度有关,用于重新排序行
gl_FragColor = vec4((MaterialSpecular + specularCoefficient*color).rgb, 1.0);
也可以。