我是使用VBO渲染数据的新手,当我试图绘制一个立方体时,我得到了一个非常糟糕的错误。
这会导致程序崩溃。我注意到当我在代码中注释掉渲染方法时,错误就消失了,但我似乎无法找到错误的来源,因为我是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.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
public class Render{
private int amountOfVerts = 4;
private int vertexSize = 3;
private int colorSize = 1;
private FloatBuffer vertData, colorData;
private int handle, colorHandle;
public Render() {
vertData = BufferUtils.createFloatBuffer(amountOfVerts * vertexSize);
vertData.put(new float[]{0f,0f,0f, 100f,0f,0f, 100f,100f,0f, 0f,100f,0f});
vertData.flip();
colorData = BufferUtils.createFloatBuffer(amountOfVerts * colorSize);
colorData.put(new float[]{1,0,0});
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 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 static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Vector3f;
public class Screen {
private Render r;
private Camera c;
private int w,h;
public Screen() {
init();
r = new Render();
c = new Camera(new Vector3f(0,0,0), 5f, 0.15f);
gameLoop();
}
public static void main(String[] args) {
Screen s = new Screen();
}
public void init(){
try{
Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
w = Display.getDesktopDisplayMode().getWidth();
h = Display.getDesktopDisplayMode().getHeight();
Display.setFullscreen(true);
Display.create();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(30f, w / h, 0.003f, 1000f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Mouse.setGrabbed(true);
}catch (LWJGLException e){
e.printStackTrace();
}
}
public void gameLoop(){
while (!Display.isCloseRequested()){
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
System.exit(0);
Display.destroy();
}
render();
update();
glLoadIdentity();
c.lookThroughCamera();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void update(){
c.update();
c.mouseX = Mouse.getDX();
c.mouseY = -Mouse.getDY();
c.addYaw(c.mouseX * c.sensetivity);
c.addPitch(c.mouseY * c.sensetivity);
}
public void render(){
r.render();
}
}
答案 0 :(得分:1)
size: Specifies the number of components per color. Must be 3 or 4
将colorSize
更改为3并将所需的浮点数添加到颜色缓冲区应该可以解决您的问题。
colorData.put(new float[]{0f,0f,0f, 1f,0f,0f, 1f,1f,0f, 0f,1f,0f});