为什么我得到FloatBuffer不是直接错误?

时间:2011-07-09 21:11:37

标签: java opengl

我想从顶点数组中绘制一个对象。这种方法需要FloatBuffer而不是数组。我从数组中创建一个浮点缓冲区,但为什么我运行代码我得到一个错误。顺便说一下,所有必需的属性都已启用(openGL)。

代码:

float vertices[] ={
        -1, -1, -1,   -1, -1,  1,   -1,  1,  1,   -1,  1, -1,
        1, -1, -1,    1, -1,  1,    1,  1,  1,    1,  1, -1,
        -1, -1, -1,   -1, -1,  1,    1, -1,  1,    1, -1, -1,
        -1,  1, -1,   -1,  1,  1,    1,  1,  1,    1,  1, -1,
        -1, -1, -1,   -1,  1, -1,    1,  1, -1,    1, -1, -1,
        -1, -1,  1,   -1,  1,  1,    1,  1,  1,    1, -1,  1
};

FloatBuffer temp = FloatBuffer.allocate(vertices.length);
temp.put(vertices);
GL11.glVertexPointer(3, GL11.GL_FLOAT, temp);

错误:

java.lang.IllegalArgumentException: FloatBuffer is not direct
    at org.lwjgl.BufferChecks.checkDirect(BufferChecks.java:139)
    at org.lwjgl.opengl.GL11.glVertexPointer(GL11.java:2622)
    at XLesson01.render(XLesson01.java:95)
    at XLesson01.run(XLesson01.java:51)
    at XLesson01.main(XLesson01.java:42)

新代码:

ByteBuffer temp = ByteBuffer.allocateDirect(vertices.length*8);
    temp.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = temp.asFloatBuffer();
    buffer.put(vertices);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, buffer);

新错误:

 A fatal error has been detected by the Java Runtime Environment:

  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x69aa9410, pid=3524, tid=1032

 JRE version: 6.0_22-b04# Java VM: Java HotSpot(TM) Client VM (17.1-b03 mixed mode, sharing windows-x86 )
 Problematic frame:
 C  [nvoglv32.DLL+0x5a9410]

 An error report file with more information is saved as:
 E:\java\workspace4\opengltest\hs_err_pid3524.log

 If you would like to submit a bug report, please visit:
   http://java.sun.com/webapps/bugreport/crash.jsp
 The crash happened outside the Java Virtual Machine in native code.
 See problematic frame for where to report the bug.

3 个答案:

答案 0 :(得分:10)

您可以尝试使用BufferUtils.createFloatBuffer(int size)BufferUtils是LWJGL的一部分,专为此目的而设计。

答案 1 :(得分:7)

显然是因为它不直接。您可以通过创建带有allocateDirect的ByteBuffer,然后使用asFloatBuffer获取它的FloatBuffer视图来分配直接的FloatBuffer。

答案 2 :(得分:2)

这个问题已经很老了,所以我对它还没有被提及感到印象深刻。在glVertexPointer

中使用缓冲区之前,需要先翻转()缓冲区