Matrix4f and glLoadMatrix

Hi,

do i need to store the Matrix in a FloatBuffer before i can upload it to OpenGL? It seems a bit cumbersome to me.
It would be nice if someone could post an example, i’m new to Java…

Thanks in advance

I’ve never had to set the matrix as such, generally you manipulate it rather than set it using glMatrixMode(), glLoadIdentity(), glTranslate(), glRotate(), glPushMatrix() and glPopMatrix().

If you need to get the matrices (for ray projection for example), then you do need to use a nasty NIO buffer.

This is the code I use:


     private final FloatBuffer buffer = EngineUtil.createBuffer( 16 );

      /**
	 * @return Current modelview matrix (column-major)
	 */
	public Matrix getModelviewMatrix() {
		return getMatrix( GL11.GL_MODELVIEW_MATRIX );
	}
	
	/**
	 * @return Current projection matrix (column-major)
	 */
	public Matrix getProjectionMatrix() {
		return getMatrix( GL11.GL_PROJECTION_MATRIX );
	}
	
	/**
	 * Retrieves the specified matrix.
	 * @param name Matrix name
	 */
	private Matrix getMatrix( int name ) {
		// Retrieve specified matrix buffer
		buffer.rewind();
		GL11.glGetFloat( name, buffer );
		
		// Convert to array
		final float[] array = new float[ 16 ];
		buffer.get( array );
		
		// Convert to matrix
		return new Matrix( array );
	}

Hope this helps.

Thanks this gives me some clues!

I still wonder why the Matrix class isnt integrated into the LWJGL OpenGL API. For example, theres no glLoadMatrix(Matrix) method. Im used to manipulate matrices directly and then upload the result to OpenGL with glMultMatrix() or glLoadMatrix().

glLoadMatrix is generally more performant then multiple JNI native calls that you oncurr with glRotate, glScale and glTransform. It also gives you the opportunity to manipulate the matrices how you see fit using methods other than what GL has integrated in it.

The reason it doesn’t have a Matrix class is because it is not a game engine. LWJGL provides an interface to OpenGL and stays true to that. OpenGL takes a pointer to the array of floats representing a matrix, teh equivilant to that in Java is a FloatBuffer (there are reasons for using Buffers and not arrays).

I would suggest you grab vecmath from vecmath.dev.java.net and start experimenting with that.

DP :slight_smile:

Thanks for your explanation. I’ll give vecmath a try.

vecmath :s Take care of your performance >:(

How so? It works well for me and I don’t see reason for performance problems in it’s design. Only matrix invert() is producing some memory garbage, but that’s not usually called often.