I'm having heavly troubles with setting up a View Matrix and a Projection Matrix. It simply doesnt work. So I think my problem is related to my rotationMatrix function. I'm using this tutorial to learn some basics about Matrix maths.
This method works fine to rotate a object in 3 angles, but I think it have a bug or something like that. So I need your help just to know if this method is doing hes job right, if so, I'll try to rewrite again all my Camera code to see if I made something wrong (I've do this 2 times already).
public static Matrix4 rotationMatrix(float x, float y, float z){
//Set as Identity Matrix;
float[] result = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
/*
Row-Major Matrix
----------------------------
| 1 0 0 0 |
Rx = | 0 cos(X) -sin(X) 0 |
| 0 sin(X) cos(X) 0 |
| 0 0 0 1 |
----------------------------
| cos(Y) 0 sin(Y) 0 |
Ry = | 0 1 0 0 |
| -sin(Y) 0 cos(Y) 0 |
| 0 0 0 1 |
----------------------------
| cos(Z) sin(Z) 0 0 |
Rz = | -sin(Z) cos(Z) 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
----------------------------
*/
float cos, sin;
if (x != 0.0f){ //If it has a X value, lets compute the Matrix.
cos = (float) Math.cos(x);
sin = (float) Math.sin(x);
result[0] += 1.0f;
result[5] += cos;
result[6] -= sin;
result[9] += sin;
result[10] += cos;
result[15] += 1.0f;
}
if (y != 0.0f){//If it has a Y value, lets compute the Matrix.
cos = (float) Math.cos(y);
sin = (float) Math.sin(y);
result[0] += cos;
result[2] += sin;
result[5] += 1.0f;
result[8] -= sin;
result[10] += cos;
result[15] += 1.0f;
}
if (z != 0.0f){//If it has a Z value, lets compute the Matrix.
cos = (float) Math.cos(z);
sin = (float) Math.sin(z);
result[0] += cos;
result[1] -= sin;
result[4] += sin;
result[5] += cos;
result[10] += 1.0f;
result[15] += 1.0f;
}
return new Matrix4(result);
}
Also I think this rotation Matrix is in row-major (I dont have sure). I send this Matrix to a Vertex Shader and it multiplies by the Vec3 position, so I think if I got any problem, is with this method.
Thanks for reading it!