BufferUtils has no newDoubleBuffer ???

Hey all,

One problem I’ve found (and not really sure it’s a problem) is with net.java.games.jogl.util.BufferUtils FloatBuffer. It’s truncating my double values into floats. I could use java.nio.DoubleBuffer, but I don’t see that BufferUtils has a newDoubleBuffer() which means I must use newFloatBuffer() so my values will get truncated.

original double values … x: 0.0, y: -0.01999999955, z: -0.09652002156
float values truncated … x: 0.0, y: -0.02, z: -0.09652002

Any help much appreciated.

— code snip —

FloatBuffer vertexBuffer = BufferUtils.newFloatBuffer(3 * 3);


// loop 3 nodes and get x, y, z points
vertexBuffer.put(idx++, (float) node.getX());
vertexBuffer.put(idx++, (float) node.getY());
vertexBuffer.put(idx++, (float) node.getZ());

gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL.GL_TRIANGLES, 0, 8);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);

I think you can use the java.nio buffers, but be sure to set the order correctly…

DoubleBuffer buffer = ByteBuffer.allocateDirect(334).order( ByteOrder.nativeOrder() ).asDoubleBuffer();

Your video card will do all calculations with floats so you might want to consider using floats rather than doubles.

[quote]DoubleBuffer buffer = ByteBuffer.allocateDirect(334).order( ByteOrder.nativeOrder() ).asDoubleBuffer();

Your video card will do all calculations with floats so you might want to consider using floats rather than doubles.
[/quote]
Note that a double is actually 8 bytes, so you probably want

ByteBuffer.allocateDirect(3*3*8).order( ByteOrder.nativeOrder() ).asDoubleBuffer();

Just create a newDoubleBuffer(int size) method for yourself.

Yes you want eight bytes not four. DoubleBuffer( int ) will be much slower than the code I posted. There is a penalty for copying non-direct buffers out to OpenGL. Do not use DoubleBuffer( int ) or face the wrath of the JVM.

I think Matei meant create a function/class


import java.nio.*;
import net.java.games.jogl.util.BufferUtils;


/** Extra utility routine for dealing with direct buffers. */

public class BetaBufferUtils extends BufferUtils {

  public static final int SIZEOF_DOUBLE = 4;

  public static FloatBuffer newFloatBuffer(int numElements) 
  {
    ByteBuffer bb = newByteBuffer(numElements * SIZEOF_DOUBLE);
    return bb.asDoubleBuffer();
  }
}

rather than invoking new DoubleBuffer(*). Of course I may be wrong :-/

spot the deliberate mistake :-[

That should be :




import java.nio.*;
import net.java.games.jogl.util.BufferUtils;
 
 
/** Extra utility routine for dealing with direct buffers. */
 
public class BetaBufferUtils extends BufferUtils {
 
  public static final int SIZEOF_DOUBLE = 4;
 
  public static DoubleBuffer newDoubleBuffer(int numElements)  
  {
    ByteBuffer bb = newByteBuffer(numElements * SIZEOF_DOUBLE);
    return bb.asDoubleBuffer();
  }
} 


damn it I’ve drunk too much coffee SIZEOF_DOUBLE = 8! not 4 as I typed… Appologies all,