我有一个可用的 GLSL 着色器,它可以使用 OpenGL ES 模拟器在 PC 上运行,也可以在 iOS 模拟器中运行。当我在真正的 iPad(运行 iOS v4.2)上运行它时,它不会呈现。着色器编译并链接,但我从 glValidateProgram 收到以下错误:
Fragment program failed to compile with current context state
Vertex program failed to compile with current context state
这是 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"
"}
最佳答案
您是否正确设置了当前上下文?我知道我忘记设置上下文,导致我的着色器无法编译。
确保您执行了
[EAGLContext setCurrentContext:context];
在初始化着色器之前,在使用类似内容创建上下文之后
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
关于iphone - iOS OpenGL Shader 在实际设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5729952/