OpenGL ES 2.0中的地球日/夜侧面着色器

时间:2012-05-29 12:57:36

标签: iphone ios opengl-es shader blending

我有一个iPhone应用程序是Planet Earth的模型!我想让它变得现实:有一个球体对象,一个Nightside和Dayside纹理和着色器,但它不起作用!

My Sphere对象的绘制方法:

    -(bool)execute:(GLuint)texture;
 {    
    glBindTexture(GL_TEXTURE_2D, texture);
    glBindVertexArrayOES(m_VertexArrayName);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, m_NumVertices);
    glBindTexture(GL_TEXTURE_2D, 0);
    return true;
 }

我的ViewController调用方法:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
    glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

    glUseProgram(m_NightsideProgram);
    [m_Sphere setBlendMode:0];
    [m_Sphere execute:m_EarthNightTexture.name];

    glUseProgram(m_DaysideProgram);
    [m_Sphere setBlendMode:1];
    [m_Sphere execute:m_EarthDayTexture.name];

    glCullFace(GL_FRONT);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CW);

}

混合模式: 0:glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR); 1:glBlendFunc(GL_ONE,GL_CONSTANT_COLOR); //常量是中蓝色,因为它稍微浅一点

夜间碎片器:

precision mediump float;

varying lowp vec4 colorVarying;
varying vec2 v_texCoord;

uniform sampler2D s_texture;


void main() {
    vec4 newColor;

    newColor=1.0-colorVarying;

    gl_FragColor = texture2D(s_texture, v_texCoord)*newColor;                    
}

DaySide Fragmentshader:

precision mediump float;

varying lowp vec4 colorVarying;
varying lowp vec4 specularColorVarying;
varying vec2 v_texCoord;

uniform sampler2D s_texture;


void main() {
    vec4 finalSpecular=vec4(0,0,0,1);
    vec4 surfaceColor;
    float halfBlue;

    surfaceColor=texture2D(s_texture,v_texCoord);

    halfBlue=0.5*surfaceColor[2];

    if(halfBlue>1.0)
        halfBlue=1.0;

    if((surfaceColor[0]<halfBlue) && (surfaceColor[1]<halfBlue))
        finalSpecular=specularColorVarying;

    gl_FragColor = surfaceColor*colorVarying+colorVarying*finalSpecular;                    
}

如果我使用唯一一个着色器,它看起来很好,但它不会一起工作!

1 个答案:

答案 0 :(得分:1)

要使glUniform...调用生效,必须绑定/使用有效程序,即使这样,它也只更改该特定程序统一的统一值(由统一位置识别)。因此,您必须在相应的glUniform...之后为每个 {<1}}计划调用glUseProgram个函数。

这就是为什么它只适用于一个着色器程序,因为你没有绑定任何其他程序。但它在概念上仍然是错误的,因为在这种情况下,你依赖于已经绑定的特定程序,这总是错误的来源(比如添加第二个程序时),因为 OpenGL是一个状态机

另一方面,即使相应的程序未绑定(glUseProgram(0_or_any_other_program)),统一变量也会保留其值。