gpt4 book ai didi

java - LWJGL 无法使用 VBO 创建立方体

转载 作者:行者123 更新时间:2023-12-01 12:52:17 25 4
gpt4 key购买 nike

在尝试使用 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
}

最佳答案

按照你当前渲染的方式,你的颜色缓冲区中需要有与位置缓冲区中一样多的 float (如果你的 float 较少,它只使用 0 作为我的其余值,尽管我不是确定这是否会发生在您身上)。

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

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

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

关于java - LWJGL 无法使用 VBO 创建立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com