{"id":1269,"date":"2011-05-14T02:58:23","date_gmt":"2011-05-13T18:58:23","guid":{"rendered":"http:\/\/blog.lolyco.com\/sean\/?p=1269"},"modified":"2011-05-14T03:13:49","modified_gmt":"2011-05-13T19:13:49","slug":"trying-out-jbullet-intbuffer-is-not-direct","status":"publish","type":"post","link":"https:\/\/blog.lolyco.com\/sean\/2011\/05\/14\/trying-out-jbullet-intbuffer-is-not-direct\/","title":{"rendered":"Trying out JBullet: IntBuffer is not direct"},"content":{"rendered":"<div id=\"attachment_1271\" style=\"width: 160px\" class=\"wp-caption alignright\"><a href=\"http:\/\/blog.lolyco.com\/sean\/wp-content\/uploads\/2011\/05\/jbulletbasicdemo.jpeg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1271\" class=\"size-thumbnail wp-image-1271\" title=\"JBullet Basic Demo\" src=\"http:\/\/blog.lolyco.com\/sean\/wp-content\/uploads\/2011\/05\/jbulletbasicdemo-150x150.jpg\" alt=\"JBullet Basic Demo\" width=\"150\" height=\"150\" \/><\/a><p id=\"caption-attachment-1271\" class=\"wp-caption-text\">JBullet Basic Demo<\/p><\/div>\n<p>I&#8217;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 <a href=\"http:\/\/jbullet.advel.cz\/\">JBullet<\/a> &#8211; a Java port of the <a href=\"http:\/\/bulletphysics.org\/\">Bullet\u00a0 Physics Library<\/a>. It needs LWJGL &#8211; the <a href=\"http:\/\/lwjgl.org\/\">Lightweight Java Game Library<\/a> &#8211; to run its demos, so I installed that in exactly the way they say you don&#8217;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 <a href=\"http:\/\/roscidus.com\/desktop\/about_rox\">ROX-Filer<\/a> and choose &#8216;File..theJVM&#8217;) &#8211; here&#8217;s the stack trace:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: IntBuffer is not direct\r\nat org.lwjgl.BufferChecks.checkDirect(BufferChecks.java:127)\r\nat org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1459)\r\nat com.bulletphysics.demos.opengl.FontRender$GLFont.load(FontRender.java:124)\r\nat com.bulletphysics.demos.opengl.FontRender$GLFont.&amp;lt;init&amp;gt;(FontRender.java:68)\r\nat com.bulletphysics.demos.opengl.LwjglGL.init(LwjglGL.java:55)\r\nat com.bulletphysics.demos.opengl.LWJGL.main(LWJGL.java:60)\r\nat com.bulletphysics.demos.basic.BasicDemo.main(BasicDemo.java:221)<\/pre>\n<p>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&#8217;t be so hard to fix. Here&#8217;s the change I made to jbullet-20101010\/src\/com\/bulletphysics\/demos\/opengl\/FontRender.java:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\/\/ next line is the one that causes Exception\r\n\/\/ glGenTextures(IntBuffer.wrap(id));\r\nByteBuffer bb = ByteBuffer.allocateDirect(4);\r\n\/\/ ENDIANNESS has no effect here?\r\n\/\/ bb.order(ByteOrder.LITTLE_ENDIAN);\r\nIntBuffer ib = bb.asIntBuffer();\r\nib.put(1);\r\nglGenTextures(ib);<\/pre>\n<p>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:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\/*\r\npublic void glLight(int light, int pname, float&#x5B;] params) {\r\n  GL11.glLight(light, pname, FloatBuffer.wrap(params));\r\n}\r\n*\/\r\npublic void glLight(int light, int pname, float&#x5B;] params) {\r\n  ByteBuffer bb = ByteBuffer.allocateDirect(params.length * 4);\r\n  bb.order(ByteOrder.LITTLE_ENDIAN);\r\n  FloatBuffer fb = bb.asFloatBuffer();\r\n  for (float f : params)\r\n    fb.put(f);\r\n  fb.flip();\r\n  GL11.glLight(light, pname, fb);\r\n}<\/pre>\n<p>&#8230; and we get the demo! I hope this helps someone. I&#8217;m sorry there&#8217;s no explanation about that ENDIAN constant. Omitting the line in glLight() produces a working demo, but without light. I&#8217;m guessing the explanation for why it&#8217;s needed is quite straightforward, but it&#8217;s 2:30am and I must sleep.<\/p>\n<p>PS &#8211; the code looked horrible, and I realised I&#8217;ve been posting horribly-formatted code for donkey&#8217;s years, so I installed the WordPress <a href=\"http:\/\/wordpress.org\/extend\/plugins\/syntaxhighlighter\/\">SyntaxHighlighter Evolved plugin<\/a>. I think it looks pretty good &#8211; well, comparatively!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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 &#8211; a Java port of the Bullet\u00a0 Physics Library. It needs LWJGL &#8211; the Lightweight Java Game Library &#8211; to run its demos, so I installed that [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,20,50,3],"tags":[80,33,81],"class_list":["post-1269","post","type-post","status-publish","format-standard","hentry","category-broken","category-fixed","category-foss","category-software","tag-3d","tag-java","tag-physics"],"_links":{"self":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts\/1269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/comments?post=1269"}],"version-history":[{"count":14,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts\/1269\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts\/1269\/revisions\/1280"}],"wp:attachment":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/media?parent=1269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/categories?post=1269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/tags?post=1269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}