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
}
이제 아무 것도 렌더링되지 않습니다. 새 코드를 추가했습니다 ("편집"섹션 확인). 예 큐브. 크기() * 24는 이제 모양의 정점 수 * 모양의면 수와 같지 않습니까? @Alex – user3140916
@ user3140916 'colorData'를 편집하는 것을 잊었습니다. – Alex
그리고 큐브를 렌더링하는 방법은 얼굴 당 4 개의 정점 * 6면입니다. – Alex