0

我正在为我的 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;
}

我被要求提供我所有的代码,所以就在这里。

4

1 回答 1

1

我不能肯定地说我知道这个问题,但这里是我的建议:

首先我会尝试改变:

GL11.glVertexPointer(3, GL11.GL_FLOAT, 3, 0);

GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);

您的颜色指针也应如此,将 3 替换为零,这可能会起作用。

还有什么可能有帮助:

  • 尝试使用 GL15.glGenBuffers() 创建顶点句柄,而不是使用 int 缓冲区

  • 尽管您的代码显示它是一个接口,但您将 Uni 称为一个类,我相信它应该是一个基于您使用它的方式的类。

  • 从我在这里看到的情况来看,您不断地使用相同的硬编码值创建一个缓冲区,删除它然后再次创建它。我认为在这种情况下,您应该创建一次缓冲区,并且不要在每次渲染后将其删除,除非您更改缓冲区中的值。

这就是我所得到的,所以我希望它有所帮助。

于 2013-05-27T23:36:27.823 回答