opengl 3.2 drawElements,只有一个四边形可见

时间:2015-04-21 10:15:54

标签: java opengl game-engine

编辑:我应该澄清一下......

这就是我计划开展工作的方式:

  1. 每次我的应用程序渲染(60hz)时,我都希望将所有要渲染的顶点放入一个巨大的缓冲区中。然后,该缓冲区将上传到GPU。 (glBufferdata)。

  2. 然后我将使用glDrawElements在一次调用中呈现整个事物。

  3. 这就是我尝试实施它的方式:

    设定: 1.创建一个巨大的FloatBuffer(java) 2.初始化我的VOB(这对我来说仍然有些虚幻,但我认为我已经做对了。)我使用EBO来减少顶点。

    渲染: 1.在我的FloatBuffer中放入大量的顶点 2.将我的浮动缓冲区上传到GPU 3.使用glDrawElements渲染它。

    结果: 第一个四边形渲染很好。所有其他人根本不会渲染。

    问题 为什么不渲染所有四边形?

    这是我使用下面的Renderer2类的方法:

    r = new Renderer(); 环: Renderer.bind(); 对于很多很多物体...... Renderer.render(x1,x2,y1,y2,Color top,Color bottom);     ... Renderer.flush(); 打破循环;

    public class Renderer2
    {
        private util.ShaderProgram shaderProgram;
    
    private int vaoID;
    private int vboVertID;
    private int eboID;
    
    FloatBuffer vboBuff;
    
    private final int floatsPerQuad = 6;
    private int nrOfVert = 0;
    
    
    public Renderer2(){
        String VERTEX = "#version 330 core" + "\n"
                + "layout(location = 0) in vec2 position;" + "\n"
                + "layout(location = 1) in vec4 color;" + "\n"
                + "out vec4 vColor;" + "\n"
                + "void main(){" + "\n"
                + "vColor = color;" + "\n"
                + "gl_Position = vec4(position, 0.0, 1.0);" + "\n"
                + "}";
    
        String FRAGMENT = "#version 330 core" + "\n"
                + "in vec4 vColor;" + "\n"
                + "out vec4 fragColor;" + "\n"
                + "void main(){" + "\n"
                + "fragColor = vColor;" + "\n"
                + "}";
    
        shaderProgram = new ShaderProgram();
        shaderProgram.attachVertexShader(VERTEX);
        shaderProgram.attachFragmentShader(FRAGMENT);
        shaderProgram.link();
    
        vboBuff = BufferUtils.createFloatBuffer(25000);
    
        // Generate and bind a Vertex Array
        vaoID = glGenVertexArrays();
        glBindVertexArray(vaoID);
    
        // The indices that form the rectangle
        short[] indices = new short[]
        {
            0, 1, 2,  // The indices for the left triangle
            1, 2, 3   // The indices for the right triangle
        };
    
        // Create a Buffer Object and upload the vertices buffer
        vboVertID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
    
        // Point the buffer at location 0, the location we set
        // inside the vertex shader. You can use any location
        // but the locations should match
        glVertexAttribPointer(0, 2, GL_FLOAT, false, 24, 0);
        glVertexAttribPointer(1, 4, GL_FLOAT, false, 24, 8);
        // Create a Buffer Object and upload the colors buffer
    
        // Create a ShortBuffer of indices
        ShortBuffer indicesBuffer = BufferUtils.createShortBuffer(indices.length);
        indicesBuffer.put(indices).flip();
    
        // Create the Element Buffer object
        eboID = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
    
        // Enable the vertex attribute locations
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
    
        glBindVertexArray(0);
    }
    
    public void bind(){
        vboBuff.clear();
        glBindVertexArray(vaoID);
        shaderProgram.bind();
        nrOfVert = 0;
    }
    
    public void render(float x1, float x2, float y1, float y2, Color top, Color bottom){
    
        vboBuff.put(x1).put(y1);
        vboBuff.put(top.r).put(top.g).put(top.b).put(top.a);
        vboBuff.put(x2).put(y1);
        vboBuff.put(top.r).put(top.g).put(top.b).put(top.a);
        vboBuff.put(x1).put(y2);
        vboBuff.put(bottom.r).put(bottom.g).put(bottom.b).put(bottom.a);
        vboBuff.put(x2).put(y2);
        vboBuff.put(bottom.r).put(bottom.g).put(bottom.b).put(bottom.a);
    
        nrOfVert += floatsPerQuad;
    }
    
    public void flush(){
    
        vboBuff.flip();
    
        glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
    
        glBufferData(GL_ARRAY_BUFFER, vboBuff, GL_DYNAMIC_DRAW);
    
        glDrawElements(GL_TRIANGLES, nrOfVert, GL_UNSIGNED_SHORT, 0);
    
        glBindVertexArray(0);
        ShaderProgram.unbind();
    }
    
    public void dispose()
    {
        // Dispose the program
        shaderProgram.dispose();
    
        // Dispose the vertex array
        glBindVertexArray(0);
        glDeleteVertexArrays(vaoID);
    
        // Dispose the buffer object
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glDeleteBuffers(vboVertID);
    
        // Dispose the element buffer object
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glDeleteBuffers(eboID);
    }
    

    }

1 个答案:

答案 0 :(得分:0)

因为你已经在评论部分找到了你的答案,现在正在询问是否制作一个巨大的索引缓冲区并保持静态是有效的:

如果要更改数据,则应使用GL_STATIC_DRAW声明数组缓冲区。 GL_DYNAMIC_DRAW向GPU暗示,您将不断更改缓冲区数据,并使驱动程序以不同方式处理您的数据。

如果您真的担心性能问题,我建议您查看不同的渲染方法,例如实例化,如果您渲染的四边形相同或仅根据颜色或其他因素而变化。看一下这个OpenGL Best Practices并尝试一些方法。