开发者

iOS OpenGL Shader doen't work on actual device

开发者 https://www.devze.com 2023-02-27 01:56 出处:网络
I\'ve got a working GLSL shader that runs on the PC using a OpenGL ES simulator and in the iOS simulator.When I run it on a real iPad (running iOS v4.2) it doesn\'t render.The shader compiles and link

I've got a working GLSL shader that runs on the PC using a OpenGL ES simulator and in the iOS simulator. When I run it on a real iPad (running iOS v4.2) it doesn't render. The shader compiles and links but I am getting the following error from glValidateProgram:

Fragment program failed to compile with current context state

Vertex program fail开发者_运维知识库ed to compile with current context state

Here is the GLSL code:

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"
    "}   

 


Have you set the current context correctly? I know that I've forgotten to set the context and had my shaders failing to compile as a result.

Make sure that you do a

[EAGLContext setCurrentContext:context];

before you initialize your shaders, after that context has been created using something like

EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];


Removing the - 1.1 from the normal calculation seemed to do the trick, not sure the reason.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号