1
\$\begingroup\$

The title might be a bit misleading but I'm having a hard time to explain the problem so I'll try with pictures:

enter image description here

Same tree from opposite site: enter image description here

I'm trying to create a voxel game and at the moment I try to get textures with transparency working. The mesh is generated for each chunk individually so that every chunk has its own ModelInstance that is rendered.

And the result should be like in the second picture (almost) so that every block is rendered behind the transparent textures but thats not the case for every side.

The relevant code for the transparent textures:

        Mesh mesh = new Mesh(true, meshDataBlended.sizeEstimate, (int)Math.round(meshDataBlended.sizeEstimate * 1.5),
            new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
            new VertexAttribute(VertexAttributes.Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE),
            new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));

        BlendingAttribute blendingAttribute = new BlendingAttribute();
        blendingAttribute.blended = false;
        blendingAttribute.sourceFunction = GL20.GL_SRC_ALPHA;
        blendingAttribute.destFunction = GL20.GL_ONE_MINUS_SRC_ALPHA;

        mesh.setVertices(meshDataBlended.vertices);
        mesh.setIndices(meshDataBlended.indices);

        Node terrainNode = modelBuilder.node();
        terrainNode.id = "terrain_blended";

        modelBuilder.part(
                "terrain_blended", mesh, GL20.GL_TRIANGLES,
                new Material(TextureAttribute.createDiffuse(World.TEXTURE), blendingAttribute)
        );

I'm pretty new to OpenGL, so I'm totally lost with this problem. No idea why it behaves like this and I even don't know where I could find the needed informations. So far i just know that it depends on what is rendered first (that's why it works from one side but not the opposite side) Sorry for using Minecraft textures it's just for testing purposes.

Let me know if some additional information is needed I'll add them then.

Thanks Blakk

EDIT: setting blended to true will make everything visible behind the leaves except other leaves (still only from specific sides).

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Solved the problem by writing my own shader which now renders the mesh with the transparent textures.

Fragment shader:

#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoords;
uniform sampler2D u_texture;

void main() {

    vec4 texel = texture(u_texture, v_texCoords);
    if (texel.a < 0.5) {
        discard;
    }

    gl_FragColor = texel;
}

Render function:

public void render(ShaderProgram solidShader, ShaderProgram transparentShader) {
    Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
    Gdx.gl.glEnable(GL20.GL_CULL_FACE);
    World.TEXTURE.bind();

    //Render transparent mesh
    renderMesh(transparentShader, transparentMesh);

    //Render solid mesh
    renderMesh(solidShader, solidMesh);

    Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
    Gdx.gl.glDisable(GL20.GL_CULL_FACE);
}

private void renderMesh(ShaderProgram shader, Mesh mesh) {
    if (mesh != null) {
        shader.begin();
        shader.setUniformMatrix("u_projTrans", Game.getPlayer().getCamera().combined);
        shader.setUniformi("u_texture", 0);
        mesh.render(shader, GL20.GL_TRIANGLES);
        shader.end();
    }
}

I hope this will help someone in the future (:

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .