LWJGL无法使用VBO创建多维数据集

时间:2014-06-09 22:53:37

标签: java arraylist lwjgl vbo

在尝试使用java中的VBO创建一个多维数据集时,我发现只渲染了多维数据集的一个面(并且也是错误的)。代码如下,有人可以告诉我为什么它不会呈现多个方面吗?

渲染类

package engine;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glGenBuffers;

import java.nio.FloatBuffer;
import java.util.ArrayList;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;

public class Render{

    private int amountOfVerts;
    private int vertexSize = 3;
    private int colorSize = 3;
    private FloatBuffer vertData, colorData;
    private int handle, colorHandle;
    private ArrayList<Cube> cubes = new ArrayList<Cube>();

    public Render() {
        addCube(new Cube(new Vector3f(0,0,0)));
        amountOfVerts = cubes.size() * 72;
        vertData = BufferUtils.createFloatBuffer(amountOfVerts * vertexSize);
        createCubeArray();

        colorData = BufferUtils.createFloatBuffer(amountOfVerts * colorSize);
        colorData.put(new float[]{1f,1f,1f, 1f,1f,1f, 1f,1f,1f, 1f,1f,1f});
        colorData.flip();

        handle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, handle); //sets the current buffer to this
        glBufferData(GL_ARRAY_BUFFER, vertData, GL_STATIC_DRAW); // fills the new buffer / stores data
        glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds

        colorHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, colorHandle); //sets the current buffer to this
        glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW); // fills the new buffer / stores data
        glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds
    }

    public void createCubeArray(){
        for (int i = 0; i < cubes.size(); i++){
            Cube c = cubes.get(i);
            storeVertexData(c.getData());
        }
    }

    public void storeVertexData(float[] data){
        vertData.put(data);
        vertData.flip();
    }

    public void addCube(Cube c){
        this.cubes.add(c);
    }

    public void removeCube(Cube c){
        this.cubes.remove(c);
    }

    public void render(){
        glEnable(GL_DEPTH_TEST);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindBuffer(GL_ARRAY_BUFFER, handle);
        GL11.glVertexPointer(vertexSize, GL11.GL_FLOAT, 0, 0L);

        glBindBuffer(GL_ARRAY_BUFFER, colorHandle);
        GL11.glColorPointer(colorSize, GL11.GL_FLOAT, 0, 0L);

        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
        GL11.glDrawArrays(GL11.GL_QUADS, 0, amountOfVerts);
        GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);

    }


}

多维数据集类

package engine;

import org.lwjgl.util.vector.Vector3f;

public class Cube {

    private Vector3f pos = null;
    private float cubeSize = 100f;

    public Cube(Vector3f pos) {
        this.pos = pos;
    }

    public float[] getData(){
        return new float[] {    pos.x,pos.y,pos.z,
                    pos.x + cubeSize,pos.y,pos.z,
                    pos.x + cubeSize,pos.y + cubeSize,pos.z,
                    pos.x,pos.y + cubeSize,pos.z,

                    pos.x,pos.y,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y + cubeSize,pos.z + cubeSize,
                    pos.x,pos.y + cubeSize,pos.z + cubeSize,

                    pos.x,pos.y,pos.z,
                    pos.x,pos.y,pos.z + cubeSize,
                    pos.x,pos.y + cubeSize,pos.z + cubeSize,
                    pos.x,pos.y + cubeSize,pos.z,

                    pos.x + cubeSize,pos.y,pos.z,
                    pos.x + cubeSize,pos.y,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y + cubeSize,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y + cubeSize,pos.z,

                    pos.x,pos.y,pos.z,
                    pos.x,pos.y,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y,pos.z,

                    pos.x,pos.y + cubeSize,pos.z,
                    pos.x,pos.y + cubeSize,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y + cubeSize,pos.z + cubeSize,
                    pos.x + cubeSize,pos.y + cubeSize,pos.z,
                };
    }

}

编辑

public Render() {
    addCube(new Cube(new Vector3f(0,0,0)));
    amountOfVerts = cubes.size() * 24;
    vertData = BufferUtils.createFloatBuffer(amountOfVerts * vertexSize);
    createCubeArray();
    vertData.flip();
    colorData = BufferUtils.createFloatBuffer(amountOfVerts * colorSize);
    float[] color = new float[amountOfVerts * colorSize];
    Arrays.fill(color, 1f);
    colorData.put(color);
    handle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, handle); //sets the current buffer to this
    glBufferData(GL_ARRAY_BUFFER, vertData, GL_STATIC_DRAW); // fills the new buffer / stores data
    glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds

    colorHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, colorHandle); //sets the current buffer to this
    glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW); // fills the new buffer / stores data
    glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds
}

1 个答案:

答案 0 :(得分:1)

你当前渲染的方式你需要在你的颜色缓冲区中拥有与你的位置缓冲区一样多的浮点数(如果你有更少的浮点数,它只是为我的其余值使用0,虽然我不是确定你是否会这样做。)

float[] color = new float[amountOfVerts * colorSize];
Arrays.fill(color, 1f);
colorData.put(color);

此外,您在添加每个float[]后翻转位置缓冲区,这意味着您将覆盖以前的数据。只有在完成添加后才能翻转它。

您的amountOfVerts也应该是cubes.size() * 24而不是cubes.size() * 72