Trying out JBullet: IntBuffer is not direct

May 14th, 2011 | by Sean |
JBullet Basic Demo

JBullet Basic Demo

I’ve been simulating the movement of a line-following robot in plain old Java recently, but wanted to try a few problems that need physics, so I downloaded JBullet – a Java port of the Bullet  Physics Library. It needs LWJGL – the Lightweight Java Game Library – to run its demos, so I installed that in exactly the way they say you don’t need to and ran into this Exception when I tried running the JBullet demos/basic/BasicDemo.java (I setup my PC so that I can right-click on Java files in ROX-Filer and choose ‘File..theJVM’) – here’s the stack trace:

Exception in thread "main" java.lang.IllegalArgumentException: IntBuffer is not direct
at org.lwjgl.BufferChecks.checkDirect(BufferChecks.java:127)
at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1459)
at com.bulletphysics.demos.opengl.FontRender$GLFont.load(FontRender.java:124)
at com.bulletphysics.demos.opengl.FontRender$GLFont.<init>(FontRender.java:68)
at com.bulletphysics.demos.opengl.LwjglGL.init(LwjglGL.java:55)
at com.bulletphysics.demos.opengl.LWJGL.main(LWJGL.java:60)
at com.bulletphysics.demos.basic.BasicDemo.main(BasicDemo.java:221)

I saw a couple of pages that suggested using an old version of JBullet to avoid this error, but a quick read of the API documentation for java.nio.Buffer made me think this wouldn’t be so hard to fix. Here’s the change I made to jbullet-20101010/src/com/bulletphysics/demos/opengl/FontRender.java:

// next line is the one that causes Exception
// glGenTextures(IntBuffer.wrap(id));
ByteBuffer bb = ByteBuffer.allocateDirect(4);
// ENDIANNESS has no effect here?
// bb.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer ib = bb.asIntBuffer();
ib.put(1);
glGenTextures(ib);

recompiling FontRender.java leads to a similar Exception in com.bulletphysics.demos.opengl.LwjglGL.glLight, so a similar change in jbullet-20101010/src/com/bulletphysics/demos/opengl/LwjglGL.java:

/*
public void glLight(int light, int pname, float[] params) {
  GL11.glLight(light, pname, FloatBuffer.wrap(params));
}
*/
public void glLight(int light, int pname, float[] params) {
  ByteBuffer bb = ByteBuffer.allocateDirect(params.length * 4);
  bb.order(ByteOrder.LITTLE_ENDIAN);
  FloatBuffer fb = bb.asFloatBuffer();
  for (float f : params)
    fb.put(f);
  fb.flip();
  GL11.glLight(light, pname, fb);
}

… and we get the demo! I hope this helps someone. I’m sorry there’s no explanation about that ENDIAN constant. Omitting the line in glLight() produces a working demo, but without light. I’m guessing the explanation for why it’s needed is quite straightforward, but it’s 2:30am and I must sleep.

PS – the code looked horrible, and I realised I’ve been posting horribly-formatted code for donkey’s years, so I installed the WordPress SyntaxHighlighter Evolved plugin. I think it looks pretty good – well, comparatively!

Be Sociable, Share!
  1. 10 Responses to “Trying out JBullet: IntBuffer is not direct”

  2. By Anies HP on Aug 18, 2011 | Reply

    Hi Sean,

    It is useful information. Better than have to install the older version of LWJGL. I did change my code and it works. Thank you :)

  3. By Sean on Aug 18, 2011 | Reply

    The artwork on those e-books looks great – lots better than my Satu hingga Sepuluh (I must get round to updating that site!). But why host your book on a filedump site? Did you consider hosting it yourself / charging for it, or even charging for delivering a paper version, but using a site like pc2paper.com for printing?

  4. By Anies HP on Aug 29, 2011 | Reply

    Hahaha.. thanks.. that’s look good too (Satu hinga Sepuluh) :) Whose voice is it? Yours? :) I plan to write Indonesian book for non indonesian children too and inserting voices in the pdf book. Well, it’s free ebooks. Shouldn’t cost everything for everybody….

  5. By m41q on May 28, 2012 | Reply

    Nice tutorial sean, but…
    I get errors somewhere in the .xml file when I try to recompile the jbullet.jar.
    Just in case you have your modified version of jbullet.jar lying around somewhere, could you just upload it, please?
    Thx in advance

  6. By Sean on Jun 4, 2012 | Reply

    I get errors somewhere

    … always copy-paste your error messages! The errors are not “in the .xml file”. build.xml is Ant‘s build configuration.

    The article’s instructions are incomplete. Your error messages (“cannot find symbol”) tell you there’s insufficient clues in the modified sources for the Java compiler to be able to know where to go looking for the new Class references ByteBuffer and ByteOrder. They’re both in the java.nio package, along with FloatBuffer – which is imported at the top of the Java source files. You can add two more lines importing ByteBuffer and ByteOrder, or you can change ‘FloatBuffer’ to ‘*’, so the line looks like:

    import java.nio.*;

    Thanks for the feedback on the article. I hope now that I’ve made this reply there’s absolutely nothing missing from the article.

  7. By m41q on Jun 4, 2012 | Reply

    First of all thx for the reply.
    I have to admit I was not exactly precise with the error message ;-)
    Yeah, the article looks complete, but as far as I can figure it out, my error message is not related so much with your article since it also occurs when I changed nothing in the source code.
    That´s the reason why I was not precise about the error message and asked for the .jar file.

    But still, if you got a clue on what the reason for this error is, I would be happy to understand my failure; here we go:

    I´m using eclipse and have never used Ant files before. So I looked for tutorials on how to do this, created a new project called “JBullet”, imported all the files and tried to compile this using the Ant file.
    This is the console output I get:

    [output from eclipse build]

  8. By Sean on Jun 4, 2012 | Reply

    Ah okay – I’m not an eclipse user but line 78 in the jbullet’s build.xml doesn’t look like it would give you that kind of error. Are you sure Eclipse hasn’t helpfully replaced it for you? If you were planning to use jbullet in your own projects, building it with anything other than its own build.xml using plain old Ant is probably not a good idea. You’ll be able to include the built jar in your own projects, no matter what you build them with.

    Sorry I can’t give you any more help than that. If you’re determined to get it building as an Eclipse project, perhaps you should try posting the question on an Eclipse forum. Good luck!

  9. By m41q on Jun 4, 2012 | Reply

    Ok, I´m sure eclipse didn´t replace it for me.
    I will give it a try at the eclipse forums or try to use netbeans.

    Is it possible for you to upload YOUR .jar file? Since understanding is not my primary goal, i would be fine with the built file as well.

    But still many thanks for your try to help : )

  10. By p4checo on Jul 16, 2012 | Reply

    Thanks for sharing this solution. Worked like a charm! ;)

  11. By JorgeM on Oct 15, 2012 | Reply

    Thanks, you made my day.
    I actually went a little further and linked JBullet source against LWJGL-2.8.4 (latest version at the moment), fixed exactly what you said and got the demos running.
    Thanks again for sharing the tips.

Post a Comment