2014-03-28 21:04:20 -04:00
|
|
|
const float DEG2GRAD = 3.14 / 180.;
|
|
|
|
|
|
|
|
|
2014-03-28 20:22:08 -04:00
|
|
|
mat4 getMatrixFromRotation(vec3 rotation)
|
|
|
|
{
|
|
|
|
|
|
|
|
// from irrlicht
|
2014-03-28 21:04:20 -04:00
|
|
|
float cr = cos(DEG2GRAD * rotation.x );
|
|
|
|
float sr = sin(DEG2GRAD * rotation.x );
|
|
|
|
float cp = cos(DEG2GRAD * rotation.y );
|
|
|
|
float sp = sin(DEG2GRAD * rotation.y );
|
|
|
|
float cy = cos(DEG2GRAD * rotation.z );
|
|
|
|
float sy = sin(DEG2GRAD * rotation.z );
|
2014-03-28 20:22:08 -04:00
|
|
|
|
|
|
|
float srsp = sr*sp;
|
|
|
|
float crsp = cr*sp;
|
|
|
|
|
2014-03-28 21:19:08 -04:00
|
|
|
return transpose(mat4(
|
2014-03-28 20:22:08 -04:00
|
|
|
vec4(cp * cy, srsp * cy - cr * sy, crsp * cy + sr * sy, 0.),
|
|
|
|
vec4(cp * sy, srsp * sy + cr * cy, crsp * sy - sr * cy, 0.),
|
|
|
|
vec4(-sp, sr * cp, cr * cp, 0.),
|
2014-03-28 21:19:08 -04:00
|
|
|
vec4(0., 0., 0., 1.)));
|
2014-03-28 20:22:08 -04:00
|
|
|
}
|
|
|
|
|
2014-03-31 12:10:45 -04:00
|
|
|
mat4 getScaleMatrix(vec3 scale)
|
|
|
|
{
|
|
|
|
mat4 result = mat4(
|
|
|
|
vec4(scale.x, 0., 0., 0.),
|
|
|
|
vec4(0., scale.y, 0., 0.),
|
|
|
|
vec4(0., 0., scale.z, 0.),
|
|
|
|
vec4(0., 0., 0., 1.)
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mat4 getWorldMatrix(vec3 translation, vec3 rotation, vec3 scale)
|
2014-03-28 20:22:08 -04:00
|
|
|
{
|
|
|
|
mat4 result = getMatrixFromRotation(rotation);
|
|
|
|
// translation
|
|
|
|
result[3].xyz += translation;
|
2014-03-31 12:10:45 -04:00
|
|
|
return result * getScaleMatrix(scale);
|
2014-03-28 20:22:08 -04:00
|
|
|
}
|
|
|
|
|
2014-03-31 12:10:45 -04:00
|
|
|
mat4 getInverseWorldMatrix(vec3 translation, vec3 rotation, vec3 scale)
|
2014-03-28 20:22:08 -04:00
|
|
|
{
|
|
|
|
mat4 result = transpose(getMatrixFromRotation(rotation));
|
|
|
|
// FIXME: it's wrong but the fourth column is not used
|
|
|
|
result[3].xyz -= translation;
|
2014-03-31 12:10:45 -04:00
|
|
|
return getScaleMatrix(1. / scale) * result;
|
2014-03-28 20:22:08 -04:00
|
|
|
}
|