关于 ios:iPad Opengl ES 程序在模拟器上运行良好,但在设备上运行良好

iPad Opengl ES program works fine on simulator but not device

对于设备,我所有的着色器都可以正常加载,除了一个。对于这个着色器程序,当我调用 glGetProgramInfoLog(...);

时,我收到"片段程序无法使用当前上下文状态编译"错误,然后是顶点着色器的类似错误

顶点着色器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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);
}

片段着色器:

1
2
3
4
5
6
7
8
9
10
#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,


换行

1
gl_FragColor = vec4((color + specularCoefficient*MaterialSpecular).rgb, 1.0);

在片段着色器中到

1
gl_FragColor = vec4((1.0*color + specularCoefficient*MaterialSpecular).rgb, 1.0);

解决了这个问题。我怀疑它与可变变量颜色相关的精度有关,用于将行重新排序为

1
gl_FragColor = vec4((MaterialSpecular + specularCoefficient*color).rgb, 1.0);

也可以。