【问题标题】:iPad Opengl ES program works fine on simulator but not deviceiPad Opengl ES 程序在模拟器上运行良好,但在设备上运行良好
【发布时间】:2023-10-08 16:24:01
【问题描述】:

对于设备,我所有的着色器都可以正常加载,除了一个。对于这个着色器程序,当我调用 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, &result) 链接程序时,我检查了每个着色器的编译状态,它们都检查得很好。有任何想法吗?

【问题讨论】:

  • 通常信息日志会为您提供发生错误的确切行。它是否给出了具体的行号?
  • 不,它没有。确切的日志是:“验证失败:片段程序无法在当前上下文状态下编译。验证失败:顶点程序无法在当前上下文状态下编译。”
  • 另外,当我使用 glGetShaderiv(theShader, GL_COMPILE_STATUS, &result) 链接程序时,我检查了每个着色器的编译状态,它们都检查得很好。 (编辑问题以反映这一事实)。

标签: ios ipad opengl-es-2.0 glsles


【解决方案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);

同样有效。

【讨论】: