代码之家  ›  专栏  ›  技术社区  ›  TheChubbyPanda

停止libgdx在对象内呈现面

  •  1
  • TheChubbyPanda  · 技术社区  · 5 年前

    因此,默认情况下,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 回复  |  直到 5 年前
        1
  •  2
  •   Rabbid76    5 年前

    什么时候? 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
    };
    

    这意味着第一行、第三行和第五行索引的缠绕顺序错误。必须这样:

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