java - 为什么我得到一个 FloatBuffer is not direct 错误?

标签 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.

最佳答案

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

关于java - 为什么我得到一个 FloatBuffer is not direct 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6637491/

相关文章:

java - java.io.writer 的 append 和 write 方法有什么区别?

Javafx 表格行边框导致内部元素移动

java - 让 Java 和 TWAIN 很好地协同工作

c++ - 在不使用 GLSL 的情况下迁移到最新的 OGL

java - JTable 列中的复选框渲染器不正确

java - 比较两个表中的值并将从第一个表中获取的值插入到第二个表中

c++ - SDL OpenGL 渲染问题

c++ - OpenGL 更改可分离着色器程序中的不同位置

c++ - GlGenTextures 不断返回 0

c++ - OpenCL/GL 互操作 : write_imagef to shared gltexture is all white (1, 1,1,1)