18void mat4_tostr(
const mat4 *m,
char *buf,
size_t len )
21 for (
int i = 0; i < 4; i++ ) {
22 for (
int j = 0; j < 4; j++ )
23 l +=
scnprintf( &buf[l], len - l,
"%7.g ", m->m[j][i] );
28void mat4_print(
const mat4 *m )
30 for (
int i = 0; i < 4; i++ ) {
31 for (
int j = 0; j < 4; j++ )
32 printf(
"%7.g ", m->m[j][i] );
48 for (
int i = 0; i < 4; i++ ) {
49 for (
int j = 0; j < 4; j++ ) {
51 for (
int k = 0; k < 4; k++ )
52 v += m1->m[i][k] * m2->m[k][j];
69 for (
int i = 0; i < 3; i++ ) {
70 GLfloat a = m->m[3][i];
71 for (
int j = 0; j < 3; j++ )
72 a += m->m[j][i] * v->v[j];
87 for (
int i = 0; i < 4; i++ ) {
88 float l0 = lhs->m[i][0];
89 float l1 = lhs->m[i][1];
90 float l2 = lhs->m[i][2];
92 float r0 = l0 * rhs->m[0][0] + l1 * rhs->m[1][0] + l2 * rhs->m[2][0];
93 float r1 = l0 * rhs->m[0][1] + l1 * rhs->m[1][1] + l2 * rhs->m[2][1];
94 float r2 = l0 * rhs->m[0][2] + l1 * rhs->m[1][2] + l2 * rhs->m[2][2];
100 lhs->m[3][0] += rhs->m[3][0];
101 lhs->m[3][1] += rhs->m[3][1];
102 lhs->m[3][2] += rhs->m[3][2];
115 for (
int i = 0; i < 4; i++ ) {
121void mat4_scale_xy(
mat4 *m,
double x,
double y )
123 for (
int i = 0; i < 4; i++ ) {
139 for (
int i = 0; i < 4; i++ )
140 m->m[3][i] += m->m[0][i] * x + m->m[1][i] * y + m->m[2][i] * z;
142void mat4_translate_x(
mat4 *m,
double x )
144 for (
int i = 0; i < 4; i++ )
145 m->m[3][i] += m->m[0][i] * x;
147void mat4_translate_xy(
mat4 *m,
double x,
double y )
149 for (
int i = 0; i < 4; i++ )
150 m->m[3][i] += m->m[0][i] * x + m->m[1][i] * y;
152void mat4_translate_scale_xy(
mat4 *m,
double x,
double y,
double w,
double h )
154 for (
int i = 0; i < 4; i++ ) {
155 m->m[3][i] += m->m[0][i] * x + m->m[1][i] * y;
175 m->m[0][0] =
c * x + s * y;
176 m->m[1][0] = -s * x +
c * y;
180 m->m[0][1] =
c * x + s * y;
181 m->m[1][1] = -s * x +
c * y;
197 m->m[0][0] =
c * x + s * y;
198 m->m[1][0] = -s * x +
c * y;
202 m->m[0][1] =
c * x + s * y;
203 m->m[1][1] = -s * x +
c * y;
227 R.m[0][0] = x * x * ( 1. -
c ) +
c;
228 R.m[0][1] = y * x * ( 1. -
c ) + z * s;
229 R.m[0][2] = x * z * ( 1. -
c ) - y * s;
231 R.m[1][0] = x * y * ( 1. -
c ) - z * s;
232 R.m[1][1] = y * y * ( 1. -
c ) +
c;
233 R.m[1][2] = y * z * ( 1. -
c ) + x * s;
235 R.m[2][0] = x * z * ( 1. -
c ) + y * s;
236 R.m[2][1] = y * z * ( 1. -
c ) - x * s;
237 R.m[2][2] = z * z * ( 1. -
c ) +
c;
257 GLfloat qx, qy, qz, qw;
263 R.ptr[0] = ( 1. - 2. * qy * qy - 2. * qz * qz );
264 R.ptr[1] = ( 2. * qx * qy + 2. * qz * qw );
265 R.ptr[2] = ( 2. * qx * qz - 2. * qy * qw );
268 R.ptr[4] = ( 2. * qx * qy - 2. * qz * qw );
269 R.ptr[5] = ( 1. - 2. * qx * qx - 2. * qz * qz );
270 R.ptr[6] = ( 2. * qy * qz + 2. * qx * qw );
273 R.ptr[8] = ( 2. * qx * qz + 2. * qy * qw );
274 R.ptr[9] = ( 2. * qy * qz - 2. * qx * qw );
275 R.ptr[10] = ( 1. - 2. * qx * qx - 2. * qy * qy );
293 GLfloat qx, qy, qz, qw;
309 m->ptr[0] = ( 1 - 2 * qy * qy - 2 * qz * qz ) * sx;
310 m->ptr[1] = ( 2 * qx * qy + 2 * qz * qw ) * sx;
311 m->ptr[2] = ( 2 * qx * qz - 2 * qy * qw ) * sx;
314 m->ptr[4] = ( 2 * qx * qy - 2 * qz * qw ) * sy;
315 m->ptr[5] = ( 1 - 2 * qx * qx - 2 * qz * qz ) * sy;
316 m->ptr[6] = ( 2 * qy * qz + 2 * qx * qw ) * sy;
319 m->ptr[8] = ( 2 * qx * qz + 2 * qy * qw ) * sz;
320 m->ptr[9] = ( 2 * qy * qz - 2 * qx * qw ) * sz;
321 m->ptr[10] = ( 1 - 2 * qx * qx - 2 * qy * qy ) * sz;
337 const mat4 m = { .m = { { 1., 0., 0., 0. },
340 { 0., 0., 0., 1. } } };
348 double nearVal,
double farVal )
350 mat4 mat = { { { { 0 } } } };
355 tx = -( right + left ) / ( right - left );
356 ty = -( top + bottom ) / ( top - bottom );
357 tz = -( farVal + nearVal ) / ( farVal - nearVal );
359 mat.m[0][0] = 2. / ( right - left );
360 mat.m[1][1] = 2. / ( top - bottom );
361 mat.m[2][2] = -2. / ( farVal - nearVal );
384 vec3 forward, side, upc;
387 vec3_sub( &forward, center, eye );
388 vec3_normalize( &forward );
391 vec3_cross( &side, &forward, up );
392 vec3_normalize( &side );
395 vec3_cross( &upc, &side, &forward );
399 H.m[0][0] = side.v[0];
400 H.m[1][0] = side.v[1];
401 H.m[2][0] = side.v[2];
404 H.m[0][1] = upc.v[0];
405 H.m[1][1] = upc.v[1];
406 H.m[2][1] = upc.v[2];
409 H.m[0][2] = -forward.v[0];
410 H.m[1][2] = -forward.v[1];
411 H.m[2][2] = -forward.v[2];
436 double c = 1. / tan( fov * 0.5 );
437 double d = far - near;
440 H.m[0][0] =
c / aspect;
452 H.m[2][2] = -( far + near ) /
d;
457 H.m[3][2] = -2. * far * near /
d;
463void quat_normalize(
quat *q )
465 GLfloat
d = sqrtf(
pow2( q->q[0] ) +
pow2( q->q[1] ) +
pow2( q->q[2] ) +
467 for (
int i = 0; i < 4; i++ )
471void quat_slerp( GLfloat qm[4],
const GLfloat qa[4],
const GLfloat qb[4],
475 double cosHalfTheta =
476 qa[3] * qb[3] + qa[0] * qb[0] + qa[1] * qb[1] + qa[2] * qb[2];
478 if ( fabs( cosHalfTheta ) >= 1.0 ) {
486 double halfTheta = acos( cosHalfTheta );
487 double sinHalfTheta = sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
490 if ( fabs( sinHalfTheta ) < 0.001 ) {
491 qm[3] = ( qa[3] * 0.5 + qb[3] * 0.5 );
492 qm[0] = ( qa[0] * 0.5 + qb[0] * 0.5 );
493 qm[1] = ( qa[1] * 0.5 + qb[1] * 0.5 );
494 qm[2] = ( qa[2] * 0.5 + qb[2] * 0.5 );
497 double ratioA = sin( ( 1 - t ) * halfTheta ) / sinHalfTheta;
498 double ratioB = sin( t * halfTheta ) / sinHalfTheta;
500 qm[3] = ( qa[3] * ratioA + qb[3] * ratioB );
501 qm[0] = ( qa[0] * ratioA + qb[0] * ratioB );
502 qm[1] = ( qa[1] * ratioA + qb[1] * ratioB );
503 qm[2] = ( qa[2] * ratioA + qb[2] * ratioB );
void mat4_translate(mat4 *m, double x, double y, double z)
Translates a homogenous transformation matrix.
void mat4_apply(mat4 *lhs, const mat4 *rhs)
Applies a transformation to another, storing the result in the left hand side.
mat4 mat4_lookat(const vec3 *eye, const vec3 *center, const vec3 *up)
Creates a matrix with a transformation to look at a center point from an eye with an up vector.
mat4 mat4_identity(void)
Creates an identity matrix.
mat4 mat4_perspective(double fov, double aspect, double near, double far)
Creates a matrix with a perspective transformation.
void mat4_rotate(mat4 *m, double angle, double x, double y, double z)
Multiplies the given matrix by a rotation. (Follows the right-hand rule.)
void mat4_mul_vec(vec3 *out, const mat4 *m, const vec3 *v)
Multiplies a matrix with a vector (out = m * v);.
void mat4_scale(mat4 *m, double x, double y, double z)
Scales a homogeneous transformation matrix.
void mat4_mul(mat4 *out, const mat4 *m1, const mat4 *m2)
Multiplies two matrices (out = m1 * m2).
void mat4_rotate_quaternion(mat4 *m, const quat *q)
Applies a quaternion transformation.
void mat4_trs(mat4 *m, const vec3 *t, const quat *r, const vec3 *s)
Creates a homogeneous transform matrix from a translation, rotation, and scaling. Uses T*R*S order.
mat4 mat4_ortho(double left, double right, double bottom, double top, double nearVal, double farVal)
Creates an orthographic projection matrix.
void mat4_rotate2d(mat4 *m, double angle)
Rotates an angle, in radians, around the z axis.
void mat4_rotate2dv(mat4 *m, double c, double s)
Rotates the +x axis to the given vector.
Header file with generic functions and naev-specifics.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....