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

LWJGL顶点索引缓冲区导致加载一半“网格”

  •  0
  • user6273593  · 技术社区  · 7 年前

            // Vertex Index Buffer
            idxVboId = glGenBuffers();
            vertsIdxBuffer = MemoryUtil.memAllocInt(indices.length);
            vertsIdxBuffer.put(indices).flip();
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,idxVboId);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER,vertsIdxBuffer,GL_STATIC_DRAW);
    

        shaderProgram.bind();
    
            //bind to vao
            glBindVertexArray(mesh.getVaoId());
            //enable positions buffer
            glEnableVertexAttribArray(0);
            //enable colour buffer
            glEnableVertexAttribArray(1);
    
            //draw verts
            glDrawElements(GL_TRIANGLES, 
            mesh.getVertexCount(),GL_UNSIGNED_INT,0);
    
            //restore state (?)
            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);
            glBindVertexArray(0);
    
            shaderProgram.unbind();
    

    任何建议都很感谢,因为我已经盯着这几个小时了,真的看不出我犯了什么错误。

    编辑:四边形网格代码

         renderer.init();
    
        float[] positions = new float[]{
                -0.5f,  0.5f, 0.0f,
                -0.5f, -0.5f, 0.0f,
                0.5f,  0.5f, 0.0f,
                0.5f,  0.5f, 0.0f,
                -0.5f, -0.5f, 0.0f,
                0.5f, -0.5f, 0.0f,
        };
    
        float[] colours = new float[]{0.5f,0f,0f,0f,0.5f,0f,0f,0f,0.5f,0f,0.5f,0.5f};
    
        int[] indices = new int[]{
                0, 1, 3, 3, 1, 2,
        };
    
        mesh = new BMesh(positions,colours,indices);
    

    位置阵列更改前后

    Before

    After

    1 回复  |  直到 7 年前
        1
  •  0
  •   meowgoesthedog    7 年前

    3 2 在第二个四边形上是相同的顶点,即两者都在 (0.5, 0.5, 0.0)

    i、 e.也许你是有意的

    float[] positions = new float[]{
            -0.5f,  0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
            0.5f,  0.5f, 0.0f,
            0.5f, -0.5f, 0.0f
    };
    

    { 0, 1, 3, 0, 3, 2 }