A little contribution

Started by middy, July 24, 2004, 20:46:05

Previous topic - Next topic

middy

Hey I have been toying a bit with mouse coord to world coord projection and there one has to convert from the famous floatbuffers to 4 X 4 float arrays(because GL uses buffers and GLU uses array)

Anyhow I came upon the new util libary and saw the nice matrix4f class. This one can load from a floatbuffer but not convert into a float[][] array.

So here is the method, hope you can use it

/**
		 * Returns matrix as multidimensional array
		 * @param f if null new is created
		 * @return
		 */
		public float[][] getMatrixAsArray(float[][] f){
			
			if (f==null) f= new float[4][4];
			
			f[0][0]=m00;
			f[1][0]=m10;
			f[2][0]=m20;
			f[3][0]=m30;
			
			f[0][1]=m01;
			f[1][1]=m11;
			f[2][1]=m21;
			f[3][1]=m31;
			
			f[0][2]=m02;
			f[1][2]=m12;
			f[2][2]=m22;
			f[3][2]=m32;
			
			f[0][3]=m03;
			f[1][3]=m13;
			f[2][3]=m23;
			f[3][3]=m33;
			
			return f;
		}
um

princec

That'll crash if f == null with an NPE. You need to instantiate the 4 f[]'s with float[4]'s as well.

Cas :)

cfmdobbie

Naa, whenever you specify a size in a dimension, Java will allocate space for that number of references, primitives or arrays.  If the array dimensions were [4][] you'd get an NPE for sure, but, [4][4] is safe.
ellomynameis Charlie Dobbie.

middy

I DID use it before I posted :)
um

rgrzywinski

To clarify cfmdobbie's statement:  

final List[][] lists = new List[4][4];
lists[0][0].size();


will NPE since the 2d array contains null references whereas:

final float[][] floats = new float[4][4];
floats[0][0] += 0.1f;


is just dandy.

cfmdobbie

Agreed, it'll only ever automatically-allocate references, primitives or arrays - never objects.
ellomynameis Charlie Dobbie.