具有可见纹理的透明对象(设置alpha通道)

时间:2014-08-01 04:55:48

标签: ios xcode textures opengl-es-2.0 shader

我必须使用设置为alpha通道的纹理显示透明对象。 使用OpenGL ES 2.0和mtl2opengl.pl我可以在我的iPhone上显示具有纹理的对象,但alpha通道不起作用。

这与mtl2opengl.pl样本几乎相同。我该如何更改代码?

ViewController.m中的

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
    // Clear Buffers
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set View Matrices
    [self updateViewMatrices];
    glUniformMatrix4fv(_uniforms.uProjectionMatrix, 1, 0, _projectionMatrix.m);
    glUniformMatrix4fv(_uniforms.uModelViewMatrix, 1, 0, _modelViewMatrix.m);
    glUniformMatrix3fv(_uniforms.uNormalMatrix, 1, 0, _normalMatrix.m);

    // Attach Texture
    glUniform1i(_uniforms.uTexture, 0);

    // Set View Mode
    glUniform1i(_uniforms.uMode, self.viewMode.selectedSegmentIndex);

    // Enable Attributes
    glEnableVertexAttribArray(_attributes.aVertex);
    glEnableVertexAttribArray(_attributes.aNormal);
    glEnableVertexAttribArray(_attributes.aTexture);

    // Load OBJ Data
    glVertexAttribPointer(_attributes.aVertex, 3, GL_FLOAT, GL_FALSE, 0, buildOBJVerts);
    glVertexAttribPointer(_attributes.aNormal, 3, GL_FLOAT, GL_FALSE, 0, buildOBJNormals);
    glVertexAttribPointer(_attributes.aTexture, 2, GL_FLOAT, GL_FALSE, 0, buildOBJTexCoords);

    // Load MTL Data
    for(int i=0; i<buildMTLNumMaterials; i++)
    {
        glUniform3f(_uniforms.uAmbient, buildMTLAmbient[i][0], buildMTLAmbient[i][1], buildMTLAmbient[i][2]);
        glUniform3f(_uniforms.uDiffuse, buildMTLDiffuse[i][0], buildMTLDiffuse[i][1], buildMTLDiffuse[i][2]);
        glUniform3f(_uniforms.uSpecular, buildMTLSpecular[i][0], buildMTLSpecular[i][1], buildMTLSpecular[i][2]);
        glUniform1f(_uniforms.uExponent, buildMTLExponent[i]);

        // Draw scene by material group
        glDrawArrays(GL_TRIANGLES, buildMTLFirst[i], buildMTLCount[i]);
    }

    // Disable Attributes
    glDisableVertexAttribArray(_attributes.aVertex);
    glDisableVertexAttribArray(_attributes.aNormal);
    glDisableVertexAttribArray(_attributes.aTexture);
}

Shader.fsh:

// FRAGMENT SHADER

static const char* ShaderF = STRINGIFY
(

// Input from Vertex Shader
varying mediump vec3 vNormal;
varying mediump vec2 vTexture;

// MTL Data
uniform lowp vec3 uAmbient;
uniform lowp vec3 uDiffuse;
uniform lowp vec3 uSpecular;
uniform highp float uExponent;

uniform lowp int uMode;
uniform lowp vec3 uColor;
uniform sampler2D uTexture;

lowp vec3 materialDefault(highp float df, highp float sf)
{
    lowp vec3 diffuse = vec3(0.0,1.0,0.0);
    highp float exponent = 1.0;

    sf = pow(sf, exponent);

    return (df * diffuse);
}

lowp vec3 materialMTL(highp float df, highp float sf)
{
    sf = pow(sf, uExponent);

    return (df * uDiffuse);
}

lowp vec3 modelColor(void)
{
    highp vec3 N = normalize(vNormal);
    highp vec3 L = vec3(1.0, 1.0, 0.5);
    highp vec3 E = vec3(0.0, 0.0, 1.0);
    highp vec3 H = normalize(L + E);

    highp float df = max(0.0, dot(N, L));
    highp float sf = max(0.0, dot(N, H));

    // Default
    if(uMode == 0)
        return materialDefault(df, sf);

    // Texture
    else if(uMode == 1)
        return (materialDefault(df, sf) * vec3(texture2D(uTexture, vTexture)));

    // Material
    else if(uMode == 2)
        return materialMTL(df, sf);
}

void main(void)
{
    lowp vec3 color = modelColor();
    gl_FragColor = vec4(color, 1.0);
}

);

2 个答案:

答案 0 :(得分:0)

很明显,从书中学习语言,到处使用vec4(materialDefault,materialMTL,modelColor)并激活alpha混合。

示例:

lowp vec4 materialMTL(highp float df, highp float sf)
{
    sf = pow(sf, uExponent);
    return vec4(df * uDiffuse, 1.0);
}

在lowp vec4 modelColor(void)中:

return (materialDefault(df, sf) * texture2D(uTexture, vTexture));

激活alpha混合:

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

真正的问题是什么?

答案 1 :(得分:-1)

添加

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

在渲染透明对象之前。