LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: middy on July 24, 2004, 20:46:05

Title: A little contribution
Post by: middy on July 24, 2004, 20:46:05
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;
}
Title: A little contribution
Post by: princec on July 25, 2004, 16:59:36
That'll crash if f == null with an NPE. You need to instantiate the 4 f[]'s with float[4]'s as well.

Cas :)
Title: A little contribution
Post by: cfmdobbie on July 25, 2004, 19:01:25
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.
Title: heh
Post by: middy on July 25, 2004, 20:01:15
I DID use it before I posted :)
Title: A little contribution
Post by: rgrzywinski on July 26, 2004, 14:23:47
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.
Title: A little contribution
Post by: cfmdobbie on July 26, 2004, 18:52:50
Agreed, it'll only ever automatically-allocate references, primitives or arrays - never objects.