2014-06-09 31 views
0

嘗試在java中使用VBO創建多維數據集時,發現只有一個多維數據集的面呈現(並且不正確)。代碼如下,有人可以告訴我爲什麼它不會渲染更多的單邊?LWJGL無法使用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,但我不確定是否這會發生在你身上)。

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

而且你每次翻轉後float[]您添加到您的它位置緩衝,這意味着你將覆蓋以前的數據。一旦完成添加,您只應該翻轉它。

您的amountOfVerts應該是cubes.size() * 24而不是cubes.size() * 72

+0

現在什麼都沒有呈現。我添加了新的代碼(請查看「編輯」部分)。是的cubes.size()* 24現在有意義,是不是形狀的頂點數量*形狀的面數? @Alex – user3140916

+0

@ user3140916您忘記了在編輯中翻轉'colorData'。 – Alex

+1

而你正在渲染你的立方體的方式是每個面4個頂點* 6個面。 – Alex

相關問題