Search code examples
iosxcodetexturesopengl-es-2.0shader

Transparent objects with visible textures(set alpha channel)


I have to show transparent objects with texture that is set alpha channel. Using OpenGL ES 2.0 and mtl2opengl.pl I could show object with texture on my iPhone but alpha channel didn't work.

This is almost same as mtl2opengl.pl sample. How should I change code?

in 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);
}

);

Solution

  • It's obvious, learn the language from a book, use vec4 everywhere (materialDefault, materialMTL, modelColor) and activate alpha blending.

    example:

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

    In lowp vec4 modelColor(void):

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

    activate alpha blending:

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

    What's the real problem ?