iOS OpenGL Shader无法在实际设备上运行

时间:2011-04-20 12:07:00

标签: iphone opengl-es glsl

我有一个工作的GLSL着色器,可以在PC上使用OpenGL ES模拟器和iOS模拟器运行。当我在真正的iPad(运行iOS v4.2)上运行它时,它不会渲染。着色器编译和链接,但我从glValidateProgram得到以下错误:

  

片段程序无法使用当前上下文状态进行编译

     

顶点程序无法使用当前上下文状态进行编译

这是GLSL代码:

GLbyte vShaderStr[] =
    "uniform mat4 ES_MVP;         \n"
    "uniform vec4 ES_LightVector; \n"
    "uniform mat4 ES_InvWorld;    \n"
    "attribute vec4 vPosition;    \n"
    "attribute vec2 vTexture0;    \n"
    "attribute vec4 vNormal;      \n"
    "attribute vec4 vTangent;     \n"
    "varying vec2 iTexture;       \n"
    // "varying vec4 iNormal;       \n"
    "varying vec3 iLightDir;      \n"
    "void main()                  \n"
    "{                            \n"
    "   gl_Position = ES_MVP * vPosition;                    \n"
    "   iTexture = vTexture0;                                \n"
    //"   iNormal = vNormal;                                   \n"
    "   vec3 bitan = cross(vNormal.xyz,vTangent.xyz);        \n"
    "   bitan = bitan * vTangent.w;                          \n"
    "   vec3 fakeLight = (ES_InvWorld * ES_LightVector).xyz; \n"
    "   iLightDir.x = dot(vTangent.xyz,fakeLight);           \n"
    "   iLightDir.y = dot(bitan,fakeLight);                  \n"
    "   iLightDir.z = dot(vNormal.xyz,fakeLight);            \n"
    "}                           \n";

GLbyte fShaderStr[] =
    "precision mediump float;                   \n"
    "varying vec2 iTexture;                     \n"
    // "varying vec4 iNormal;                      \n"
    "varying vec3 iLightDir;                    \n"
    "uniform sampler2D ES_Sampler0;             \n"
    "uniform sampler2D ES_Sampler1;             \n"
    "void main()                                \n"
    "{                                          \n"
    "  vec3 norm = texture2D( ES_Sampler1, iTexture).rgb * 2.0 - 1.1;   \n" 
    "  vec4 diff = texture2D( ES_Sampler0, iTexture ).rgba;             \n"
    "  float nl = clamp(dot(norm,iLightDir),0.2, 1.0);                  \n"
    "  gl_FragColor = vec4(diff.rgb * nl,diff.a);                       \n"
    "}   

2 个答案:

答案 0 :(得分:2)

您是否正确设置了当前上下文?我知道我忘记了设置上下文并让我的着色器无法编译结果。

确保你做了

[EAGLContext setCurrentContext:context];

在初始化着色器之前,使用类似

之类的内容创建上下文之后
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

答案 1 :(得分:2)

从正常计算中删除-1.1似乎可以解决问题,但不确定原因。

" vec3 norm = texture2D( ES_Sampler1, iTexture).rgb * 2.0;   \n"