0

I try render translucent 3D cube with hole in libgdx? For render back face I set cull face to none and depth test to false.

public void init() {
    batch = new ModelBatch();

    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.2f, -0.8f, -1f));

    modelInstance = new ModelInstance(assets.get("cube.g3dj", Model.class));

    Material material = modelInstance.getMaterial("Main");
    material.clear();
    material.set(ColorAttribute.createDiffuse(new Color(0x0075f4ff)));
    material.set(new BlendingAttribute(0.6f));
    material.set(IntAttribute.createCullFace(GL20.GL_NONE));
    material.set(new DepthTestAttribute(false));
}

public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    batch.begin(camera);
    batch.render(modelInstance, environment);
    batch.end();
}

cube.g3dj:

{
    "version": [  0,   1], 
    "id": "", 
    "meshes": [
        {
            "attributes": ["POSITION", "NORMAL"], 
            "vertices": [
                -1.000000, -1.000000,  1.000000, -1.000000,  0.000000,  0.000000, 
                -1.000000,  1.000000,  1.000000, -1.000000,  0.000000,  0.000000, 
                -1.000000,  1.000000, -1.000000, -1.000000,  0.000000,  0.000000, 
                -1.000000, -1.000000, -1.000000, -1.000000,  0.000000,  0.000000, 
                 1.000000,  1.000000,  1.000000,  0.000000, -0.000000,  1.000000, 
                -1.000000,  1.000000,  1.000000,  0.000000, -0.000000,  1.000000, 
                 1.000000, -1.000000,  1.000000,  0.000000, -0.000000,  1.000000, 
                -1.000000, -1.000000,  1.000000,  0.000000, -0.000000,  1.000000, 
                 1.000000, -1.000000, -1.000000,  1.000000, -0.000000,  0.000000, 
                 1.000000,  1.000000,  1.000000,  1.000000, -0.000000,  0.000000, 
                 1.000000, -1.000000,  1.000000,  1.000000, -0.000000,  0.000000, 
                 1.000000,  1.000000, -1.000000,  1.000000, -0.000000,  0.000000, 
                -1.000000, -1.000000, -1.000000,  0.000000,  0.000000, -1.000000, 
                 1.000000,  1.000000, -1.000000,  0.000000,  0.000000, -1.000000, 
                 1.000000, -1.000000, -1.000000,  0.000000,  0.000000, -1.000000, 
                -1.000000,  1.000000, -1.000000,  0.000000,  0.000000, -1.000000, 
                 1.000000, -1.000000, -1.000000,  0.000000, -1.000000, -0.000000, 
                -0.500000, -1.000000, -0.500000,  0.000000, -1.000000, -0.000000, 
                -1.000000, -1.000000, -1.000000,  0.000000, -1.000000, -0.000000, 
                 0.500000, -1.000000, -0.500000,  0.000000, -1.000000, -0.000000, 
                -1.000000,  1.000000, -1.000000, -0.000000,  1.000000,  0.000000, 
                -0.500000,  1.000000,  0.500000, -0.000000,  1.000000,  0.000000, 
                -0.500000,  1.000000, -0.500000, -0.000000,  1.000000,  0.000000, 
                -1.000000,  1.000000,  1.000000, -0.000000,  1.000000,  0.000000, 
                -1.000000,  1.000000,  1.000000,  0.000000,  1.000000,  0.000000, 
                 1.000000,  1.000000,  1.000000,  0.000000,  1.000000,  0.000000, 
                -0.500000,  1.000000,  0.500000,  0.000000,  1.000000,  0.000000, 
                 0.500000,  1.000000,  0.500000,  0.000000,  1.000000,  0.000000, 
                 1.000000,  1.000000,  1.000000,  0.000000,  1.000000,  0.000000, 
                 1.000000,  1.000000, -1.000000,  0.000000,  1.000000,  0.000000, 
                 0.500000,  1.000000,  0.500000,  0.000000,  1.000000,  0.000000, 
                 0.500000,  1.000000, -0.500000,  0.000000,  1.000000,  0.000000, 
                -1.000000,  1.000000, -1.000000,  0.000000,  1.000000, -0.000000, 
                 0.500000,  1.000000, -0.500000,  0.000000,  1.000000, -0.000000, 
                 1.000000,  1.000000, -1.000000,  0.000000,  1.000000, -0.000000, 
                -0.500000,  1.000000, -0.500000,  0.000000,  1.000000, -0.000000, 
                -1.000000, -1.000000,  1.000000, -0.000000, -1.000000,  0.000000, 
                -0.500000, -1.000000, -0.500000, -0.000000, -1.000000,  0.000000, 
                -0.500000, -1.000000,  0.500000, -0.000000, -1.000000,  0.000000, 
                -1.000000, -1.000000, -1.000000, -0.000000, -1.000000,  0.000000, 
                 1.000000, -1.000000,  1.000000,  0.000000, -1.000000, -0.000000, 
                -1.000000, -1.000000,  1.000000,  0.000000, -1.000000, -0.000000, 
                 0.500000, -1.000000,  0.500000,  0.000000, -1.000000, -0.000000, 
                -0.500000, -1.000000,  0.500000,  0.000000, -1.000000, -0.000000, 
                 1.000000, -1.000000,  1.000000,  0.000000, -1.000000,  0.000000, 
                 0.500000, -1.000000, -0.500000,  0.000000, -1.000000,  0.000000, 
                 1.000000, -1.000000, -1.000000,  0.000000, -1.000000,  0.000000, 
                 0.500000, -1.000000,  0.500000,  0.000000, -1.000000,  0.000000, 
                -0.500000, -1.000000,  0.500000,  1.000000,  0.000000,  0.000000, 
                -0.500000,  1.000000, -0.500000,  1.000000,  0.000000,  0.000000, 
                -0.500000,  1.000000,  0.500000,  1.000000,  0.000000,  0.000000, 
                -0.500000, -1.000000, -0.500000,  1.000000,  0.000000,  0.000000, 
                 0.500000, -1.000000,  0.500000, -1.000000,  0.000000,  0.000000, 
                 0.500000,  1.000000,  0.500000, -1.000000,  0.000000,  0.000000, 
                 0.500000,  1.000000, -0.500000, -1.000000,  0.000000,  0.000000, 
                 0.500000, -1.000000, -0.500000, -1.000000,  0.000000,  0.000000, 
                -0.500000, -1.000000,  0.500000,  0.000000,  0.000000, -1.000000, 
                 0.500000,  1.000000,  0.500000,  0.000000,  0.000000, -1.000000, 
                 0.500000, -1.000000,  0.500000,  0.000000,  0.000000, -1.000000, 
                -0.500000,  1.000000,  0.500000,  0.000000,  0.000000, -1.000000, 
                -0.500000,  1.000000, -0.500000,  0.000000,  0.000000,  1.000000, 
                -0.500000, -1.000000, -0.500000,  0.000000,  0.000000,  1.000000, 
                 0.500000,  1.000000, -0.500000,  0.000000,  0.000000,  1.000000, 
                 0.500000, -1.000000, -0.500000,  0.000000,  0.000000,  1.000000
            ], 
            "parts": [
                {
                    "id": "Cube_part1", 
                    "type": "TRIANGLES", 
                    "indices": [
                          0,   1,   2,   0,   2,   3,   4,   5,   6,   5,   7,   6, 
                          8,   9,  10,   9,   8,  11,  12,  13,  14,  13,  12,  15, 
                         16,  17,  18,  17,  16,  19,  20,  21,  22,  21,  20,  23, 
                         24,  25,  26,  26,  25,  27,  28,  29,  30,  30,  29,  31, 
                         32,  33,  34,  33,  32,  35,  36,  37,  38,  37,  36,  39, 
                         40,  41,  42,  42,  41,  43,  44,  45,  46,  45,  44,  47, 
                         48,  49,  50,  49,  48,  51,  52,  53,  54,  52,  54,  55, 
                         56,  57,  58,  57,  56,  59,  60,  61,  62,  62,  61,  63
                    ]
                }
            ]
        }
    ], 
    "materials": [
        {
            "id": "Main", 
            "ambient": [ 0.000000,  0.000000,  0.000000], 
            "diffuse": [ 0.422876,  0.619359,  0.800000], 
            "emissive": [ 0.422876,  0.619359,  0.800000], 
            "opacity":  0.614078, 
            "specular": [ 1.000000,  1.000000,  1.000000], 
            "shininess":  9.607843
        }
    ], 
    "nodes": [
        {
            "id": "Cube", 
            "rotation": [-0.707107,  0.000000,  0.000000,  0.707107], 
            "scale": [ 0.500000,  0.500000,  0.500000], 
            "parts": [
                {
                    "meshpartid": "Cube_part1", 
                    "materialid": "Main"
                }
            ]
        }
    ], 
    "animations": []
}

But I have two problem:

  1. Only two faces of the hole are displayed instead of four.
  2. Different displaying of the cube when rotating.

The cube image rotated by 0, 90, 180 and 270 degrees

Bogdan
  • 56
  • 1
  • 3
  • see [OpenGL - How to create Order Independent transparency?](https://stackoverflow.com/a/37783085/2521214) – Spektre Mar 29 '18 at 06:09
  • @Spektre thank you very much, it helped. But there was a problem with the light on the back faces. After added direct light in the opposite direction for back face it's looks pretty. Is this the right decision? – Bogdan Mar 31 '18 at 12:04
  • better is to turn double sided lighting off `glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);` that means the same normal will be used for both front and back faces ignoring the sign of lighting dot product... – Spektre Mar 31 '18 at 12:25
  • @Spektre unfortunately in OpenGL ES 2.0 API this function is missing – Bogdan Mar 31 '18 at 14:50
  • than you can use `glFrontFace(GL_CW);` and `GL_CCW` and use direct normals for one call and negated for the other ... but yes having 2 light is easier for this .... – Spektre Mar 31 '18 at 16:30

0 Answers0