停止 LibGDX 渲染对象内部的面孔

Stop LibGDX rendering faces inside objects

所以默认情况下,libgdx 渲染面部的两面。当您使用网格(提供顶点和索引)制作立方体时,它会渲染立方体内部的面(浪费渲染时间)。

我想停止这个,因为当我在屏幕上有 x 数量的体素时它会浪费渲染时间。

我试过面部剔除,但它完全坏了。它去除了侧面和所有东西。

这是我的代码:

public void setup() {
String vertexShader = "attribute vec4 a_position;    \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoord0;\n" + "uniform mat4 u_projTrans;\n" + "varying vec4 v_color;" + "varying vec2 v_texCoords;" + "void main()                  \n" + "{                            \n" + "   v_color = vec4(1, 1, 1, 1); \n" + "   v_texCoords = a_texCoord0; \n" + "   gl_Position =  u_projTrans * a_position;  \n" + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n" + "varying vec2 v_texCoords;\n" + "uniform sampler2D u_texture;\n" + "void main()                                  \n" + "{                                            \n" + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + "}";

    shader = new ShaderProgram(vertexShader, fragmentShader);

    float[] vertices = { -0.5f, 0.5f, -0.5f, 0, 0, -0.5f, -0.5f, -0.5f, 0, 1, 0.5f, -0.5f, -0.5f, 1, 1, 0.5f, 0.5f, -0.5f, 1, 0, -0.5f, 0.5f, 0.5f, 0, 0, -0.5f, -0.5f, 0.5f, 0, 1, 0.5f, -0.5f, 0.5f, 1, 1, 0.5f, 0.5f, 0.5f, 1, 0, 0.5f, 0.5f, -0.5f, 0, 0, 0.5f, -0.5f, -0.5f, 0, 1, 0.5f, -0.5f, 0.5f, 1, 1, 0.5f, 0.5f, 0.5f, 1, 0, -0.5f, 0.5f, -0.5f, 0, 0, -0.5f, -0.5f, -0.5f, 0, 1, -0.5f, -0.5f, 0.5f, 1, 1, -0.5f, 0.5f, 0.5f, 1, 0, -0.5f, 0.5f, 0.5f, 0, 0, -0.5f, 0.5f, -0.5f, 0, 1, 0.5f, 0.5f, -0.5f, 1, 1, 0.5f, 0.5f, 0.5f, 1, 0, -0.5f, -0.5f, 0.5f, 0, 0, -0.5f, -0.5f, -0.5f, 0, 1, 0.5f, -0.5f, -0.5f, 1, 1, 0.5f, -0.5f, 0.5f, 1, 0

    };

    short[] indices = { 0, 1, 3, 3, 1, 2, 4, 5, 7, 7, 5, 6, 8, 9, 11, 11, 9, 10, 12, 13, 15, 15, 13, 14, 16, 17, 19, 19, 17, 18, 20, 21, 23, 23, 21, 22

    };

    texture = new Texture("texture.png");

    Gdx.input.setCursorCatched(false);

    mesh = new Mesh(true, vertices.length / 3, indices.length, VertexAttribute.Position(), VertexAttribute.TexCoords(0));
    mesh.setVertices(vertices);
    mesh.setIndices(indices);

    GL40.glEnable(GL40.GL_DEPTH_TEST);
    //GL40.glEnable(GL40.GL_CULL_FACE);
    //GL40.glCullFace(GL40.GL_BACK);
}

public void render() {
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    cameraController.update();

    texture.bind();
    shader.begin();
    shader.setUniformMatrix("u_projTrans", camera.combined);
    shader.setUniformi("u_texture", 0);
    mesh.render(shader, GL46.GL_TRIANGLES);
    shader.end();
}

启用 Face Culling 后,基元将被丢弃,具体取决于从视角看的顶点坐标的缠绕顺序。 为此,正面的缠绕顺序由 glFrontFace 定义。默认情况下这是逆时针方向。 如果启用背面划线:

GL40.glEnable(GL40.GL_CULL_FACE);
GL40.glCullFace(GL40.GL_BACK);

然后丢弃具有相反缠绕顺序(顺时针)的多边形。

根据你的顶点坐标

float[] vertices = { 
    -0.5f,  0.5f, -0.5f, 0, 0, 
    -0.5f, -0.5f, -0.5f, 0, 1,
     0.5f, -0.5f, -0.5f, 1, 1,
     0.5f,  0.5f, -0.5f, 1, 0,

    -0.5f,  0.5f,  0.5f, 0, 0,
    -0.5f, -0.5f,  0.5f, 0, 1,
     0.5f, -0.5f,  0.5f, 1, 1,
     0.5f,  0.5f,  0.5f, 1, 0,

     0.5f,  0.5f, -0.5f, 0, 0,
     0.5f, -0.5f, -0.5f, 0, 1,
     0.5f, -0.5f,  0.5f, 1, 1,
     0.5f,  0.5f,  0.5f, 1, 0,

    -0.5f,  0.5f, -0.5f, 0, 0,
    -0.5f, -0.5f, -0.5f, 0, 1,
    -0.5f, -0.5f,  0.5f, 1, 1,
    -0.5f,  0.5f,  0.5f, 1, 0,

    -0.5f,  0.5f,  0.5f, 0, 0,
    -0.5f,  0.5f, -0.5f, 0, 1,
     0.5f,  0.5f, -0.5f, 1, 1,
     0.5f,  0.5f,  0.5f, 1, 0,

    -0.5f, -0.5f,  0.5f, 0, 0,
    -0.5f, -0.5f, -0.5f, 0, 1,
     0.5f, -0.5f, -0.5f, 1, 1,
     0.5f, -0.5f,  0.5f, 1, 0
};

和指数

short[] indices = {
     0,  1,  3,  3,  1,  2,
     4,  5,  7,  7,  5,  6,
     8,  9, 11, 11,  9, 10,
    12, 13, 15, 15, 13, 14,
    16, 17, 19, 19, 17, 18,
    20, 21, 23, 23, 21, 22
};

这意味着第 1、3、5 行索引的缠绕顺序错误。它必须是:

short[] indices = {
     0,  3,  1,  3,  2,  1,
     4,  5,  7,  7,  5,  6,
     8, 11,  9, 11,  10, 9,
    12, 13, 15, 15, 13, 14,
    16, 19, 17, 19, 18, 17,
    20, 21, 23, 23, 21, 22
};