【问题标题】:Transparent objects with visible textures(set alpha channel)具有可见纹理的透明对象(设置 Alpha 通道)
【发布时间】:2014-08-04 18:42:37
【问题描述】:

我必须显示具有设置 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);
}

);

【问题讨论】:

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


【解决方案1】:

很明显,从书中学习语言,到处使用 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);

真正的问题是什么?

【讨论】:

    【解决方案2】:

    添加

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

    在渲染透明对象之前。

    【讨论】:

    • 仅此一项并没有真正的帮助,因为片段着色器输出所有 alpha = 1.0 的片段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2012-11-24
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2010-10-25
    相关资源
    最近更新 更多