停止对象中的LibGDX渲染面

时间:2019-02-09 20:25:37

标签: opengl 3d libgdx mesh face

因此,默认情况下,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();
}

1 个答案:

答案 0 :(得分:2)

启用Face Culling时,然后根据顶点坐标的缠绕顺序(如图所示)丢弃图元。 为此,用glFrontFace定义了正面的缠绕顺序。默认情况下,这是逆时针方向。 如果启用了背面清理:

with
  test_data as (
    select 1 id, 'blah bahwl NUM 40003.26 in 4 pieces. etc' str from dual union all
    select 2   , 'bwh balshbh NUM 6006.16 in 9 pieces. etc'     from dual union all
    select 3   , 'badh sh alshbh NUM 200 in 30 pieces. etc'     from dual union all
    select 4   , 'bfda bdafl hxksw NUM 33 in 4 pieces. etc'     from dual union all
    select 5   , 'bl lshbh NUM 54545.01 in 700 pieces. etc'     from dual union all
    select 6   , 'blah blah blah'                               from dual union all
    select 7   , 'blah blah NUM 23 in pieces etc.'              from dual union all
    select 8   , 'blah NUM 23 in 0 pieces'                      from dual union all
    select 9   , 'blah NUM 12 in three pieces etc.'             from dual
  )
    ............  How should the exceptions for id between 6 and 9 be handled?

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

根据您的顶点坐标

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
};
相关问题