Stas Jaro
Stas Jaro

Reputation: 4885

why am i getting a FloatBuffer is not direct error?

I am tring to draw an object from an array of vertices. The method for this requires a FloatBuffer instead of an array. I make a float buffer from the array but why i run the code i get an error. Btw all of the required attributes are enabled(openGL).

Code:

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);

Error:

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)

New Code:

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);

New Error:

 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.

Upvotes: 2

Views: 7354

Answers (3)

mortusnegati
mortusnegati

Reputation: 21

This question is pretty old so I'm impressed it hasn't been mentioned yet. You need to flip() the buffer before using it in glVertexPointer

Upvotes: 2

lopsided98
lopsided98

Reputation: 579

You could try using BufferUtils.createFloatBuffer(int size). BufferUtils is part of LWJGL and is designed for that purpose.

Upvotes: 10

President James K. Polk
President James K. Polk

Reputation: 41974

Obviously because it is not direct. You can allocate a direct FloatBuffer by creating a ByteBuffer with allocateDirect and then getting a FloatBuffer view of it with asFloatBuffer.

Upvotes: 7

Related Questions