我正在为我的 phisics 引擎制作一个 api,并且我正在使用 VBO 进行绘制。
我的问题是我得到了一个Cannot use ofsets when array buffer object is disabled
这是完整的错误:
Exception in thread "main" org.lwjgl.opengl.OpenGLException: Cannot use offsets when Array Buffer Object is disabled
at org.lwjgl.opengl.GLChecks.ensureArrayVBOenabled(GLChecks.java:93)
at org.lwjgl.opengl.GL11.glVertexPointer(GL11.java:2657)
at com.FuZi0nHD.MyFirstGame.main.Render.draw(Render.java:38)
at com.FuZi0nHD.MyFirstGame.main.Game.<init>(Game.java:12)
at com.FuZi0nHD.MyFirstGame.client.Start.main(Start.java:14)
这是我的代码:
课堂游戏
public class Game {
public Game() throws LWJGLException {
new Init().initalize();
Render r = new Render();
r.dod();
r.draw();
Display.update();
}
}
类初始化
public class Init {
private static int initListSize = 8;
protected static final Init[] initList;
public static String[] initListNames;
private int id = 0;
static {
initList = new Init[initListSize];
for (int i = 0; i < initList.length; i++) {
initList[i] = null;
}
initListNames = new String[initListSize];
for (int i = 0; i < initListNames.length; i++) {
initListNames[i] = "NULLOBJECTNAME";
}
}
public void initalize() {
addDefault();
Initdo();
RunList();
}
private void RunList() {
int no = initList.length;
int i = 0;
while (i < no) {
if (initList[i] != null) {
System.out.println("run id: " + i);
initList[i].RunInit();
}
i++;
}
}
private void RunList(String name) {
}
private void addDefault() {
addToArray(new InitDisplay(), "Display");
}
private void addToArray( Init clazz, String name) {
initList[id] = (Init) clazz;
initListNames[id] = name;
id = id + 1;
}
}
类初始化显示
public class InitDisplay extends Init{
@Override
public void RunInit() {
try {
JDisplay.setDisplay(640, 420, false);
Display.setResizable(true);
Display.setTitle("My Game");
ARBBufferObject.glGenBuffersARB(Uni.ib);
displayRender();
} catch (LWJGLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void displayRender() {
GL11.glViewport(0, 100, Display.getX(), Display.getY());
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glEnable(GL15.GL_ARRAY_BUFFER_BINDING);
}
}
JDisplay类
public class JDisplay {
public static void setDisplay(int X, int Y, boolean fullscreen) throws LWJGLException {
Display.setDisplayMode(new DisplayMode(X, Y));
Display.setFullscreen(fullscreen);
Display.create();
}
public static void setDefault() throws LWJGLException {
Display.setDisplayMode(Display.getDesktopDisplayMode());
Display.create();
}
public static void setFullscreen() throws LWJGLException {
Display.setFullscreen(true);
Display.create();
}
}
类渲染
错误在这里
线:GL11.glVertexPointer(3, GL11.GL_FLOAT, 3, 0);
public class Render {
private FloatBuffer cBuffer = Uni.cBuffer;
private FloatBuffer vBuffer = Uni.vBuffer;
private IntBuffer ib = Uni.ib;
private int cHandle = Uni.cHandle;
private int vHandle = Uni.vHandle;
public void createNewColourBuf(int size) {
cBuffer = BufferUtils.createFloatBuffer(size);
}
public void createNewVertexBuf(int size) {
vBuffer = BufferUtils.createFloatBuffer(size);
}
public void draw() {
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, Uni.vHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, Uni.vBuffer, GL15.GL_STATIC_DRAW);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 3, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, Uni.cHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, Uni.cBuffer, GL15.GL_STATIC_DRAW);
//If a have Alfa colour change to 4
GL11.glColorPointer(3, GL11.GL_FLOAT, 3, 0);
GL11.glDrawArrays(GL11.GL_QUADS, 0, 4 /* elements */);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
// cleanup VBO handles
ib.put(0, Uni.vHandle);
ib.put(1, Uni.cHandle);
ARBBufferObject.glDeleteBuffersARB(Uni.ib);
}
public void dod() {
vBuffer.put(0.0f).put(0.0f).put(0.0f);
vBuffer.put(1.0f).put(0.0f).put(0.0f);
vBuffer.put(1.0f).put(1.0f).put(0.0f);
vBuffer.put(0.0f).put(1.0f).put(0.0f);
vBuffer.flip();
cBuffer.put(0.5f).put(0.5f).put(0.5f);
cBuffer.put(0.5f).put(0.5f).put(0.5f);
cBuffer.put(0.5f).put(0.5f).put(0.5f);
cBuffer.put(0.5f).put(0.5f).put(0.5f);
cBuffer.flip();
}
}
单班
public interface Uni {
PropotiesFinal pf = new PropotiesFinal();
public FloatBuffer vBuffer = BufferUtils.createFloatBuffer(pf.VC_BUFFER_SIZE);
public FloatBuffer cBuffer = BufferUtils.createFloatBuffer(pf.VC_BUFFER_SIZE);
public IntBuffer ib = BufferUtils.createIntBuffer(pf.BUFFFERS);
public int vHandle = ib.get(0);
public int cHandle = ib.get(1);
}
类 PropotiesFinal
public class PropotiesFinal {
/*
* The size of the Vertex/Color Buffers
*/
public final int VC_BUFFER_SIZE = 1024;
public final int BUFFFERS = 2;
}
我被要求提供我所有的代码,所以就在这里。