我正在为我的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;
}
}
类InitDisplay
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);
}
}
Class 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();
}
}
Class Uni
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);
}
CLass PropotiesFinal
public class PropotiesFinal {
/*
* The size of the Vertex/Color Buffers
*/
public final int VC_BUFFER_SIZE = 1024;
public final int BUFFFERS = 2;
}
我被要求提供我的所有代码,所以在这里。
答案 0 :(得分:1)
我无法明确表示我知道这个问题,但这是我的建议:
首先我会尝试改变:
GL11.glVertexPointer(3, GL11.GL_FLOAT, 3, 0);
到
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
同样应该使用你的颜色指针,将零替换为零,这可能有效。
还有什么可以帮助:
尝试使用GL15.glGenBuffers()创建顶点句柄而不是使用int缓冲区
虽然您的代码显示它是一个界面,但您认为Uni是一个类,我相信它应该是基于您使用它的方式的类。
从我在这里看到的你不断创建一个使用相同硬编码值的缓冲区,删除它然后再创建它。我认为在这种情况下你应该创建一次缓冲区,除非你改变缓冲区中的值,否则不要在每次渲染后删除它。
这就是我所得到的所以我希望它有所帮助。