iphone - iOS OpenGL Shader 在实际设备上不起作用

标签 iphone opengl-es glsl

我有一个可用的 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/

相关文章:

opengl-es - 如何在 GLSL 中编写片段着色器来对 9 个 float 的数组进行排序

ios - 如何同时关闭并弹出到 View Controller

ios - UICollectionViewController 如何调整单元格的大小?

android - CCRenderTexture、GL11ExtensionPack、Libgdx 如何操作

c - 为什么 FreeImage 加载 BGR 格式的图像?

opengl - 有什么更好的方法来同步计算着色器的所有调用?

iphone - 在 iOS 上与 facebook、twitter 分享

iphone - Google Analytics(分析)中关于我的 iPhone 应用程序的绝对独立访问者是多少?

opengl-es - 如何读取 WebGL 中的深度缓冲区?

c++ - Vulkan 计算着色器不会在无限循环上停止