conworlds

annotate src/vr/mathutil.c @ 20:782ff06817fb

merged ...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Aug 2014 18:42:53 +0300
parents 778ed91cb7fd
children
rev   line source
nuclear@16 1 #include <stdio.h>
nuclear@16 2 #include <string.h>
nuclear@16 3 #include "mathutil.h"
nuclear@16 4
nuclear@16 5 #define M(i, j) ((i) * 4 + (j))
nuclear@16 6
nuclear@16 7 static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@16 8
nuclear@16 9 void rotation_matrix(const float *quat, float *matrix)
nuclear@16 10 {
nuclear@16 11 matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
nuclear@16 12 matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
nuclear@16 13 matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
nuclear@16 14 matrix[3] = 0.0f;
nuclear@16 15
nuclear@16 16 matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
nuclear@16 17 matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
nuclear@16 18 matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
nuclear@16 19 matrix[7] = 0.0f;
nuclear@16 20
nuclear@16 21 matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
nuclear@16 22 matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
nuclear@16 23 matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
nuclear@16 24 matrix[11] = 0.0f;
nuclear@16 25
nuclear@16 26 matrix[12] = matrix[13] = matrix[14] = 0.0f;
nuclear@16 27 matrix[15] = 1.0f;
nuclear@16 28
nuclear@16 29 transpose_matrix(matrix);
nuclear@16 30 }
nuclear@16 31
nuclear@16 32 void translation_matrix(const float *vec, float *matrix)
nuclear@16 33 {
nuclear@16 34 memcpy(matrix, idmat, sizeof idmat);
nuclear@16 35 matrix[12] = vec[0];
nuclear@16 36 matrix[13] = vec[1];
nuclear@16 37 matrix[14] = vec[2];
nuclear@16 38 }
nuclear@16 39
nuclear@16 40 void mult_matrix(float *dest, const float *m1, const float *m2)
nuclear@16 41 {
nuclear@16 42 int i, j;
nuclear@16 43 float tmp[16];
nuclear@16 44
nuclear@16 45 for(i=0; i<4; i++) {
nuclear@16 46 for(j=0; j<4; j++) {
nuclear@16 47 tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] +
nuclear@16 48 m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)];
nuclear@16 49 }
nuclear@16 50 }
nuclear@16 51 memcpy(dest, tmp, sizeof tmp);
nuclear@16 52 }
nuclear@16 53
nuclear@16 54 void transpose_matrix(float *matrix)
nuclear@16 55 {
nuclear@16 56 int i, j;
nuclear@16 57 float tmp[16];
nuclear@16 58
nuclear@16 59 for(i=0; i<4; i++) {
nuclear@16 60 for(j=0; j<4; j++) {
nuclear@16 61 tmp[M(i, j)] = matrix[M(j, i)];
nuclear@16 62 }
nuclear@16 63 }
nuclear@16 64 memcpy(matrix, tmp, sizeof tmp);
nuclear@16 65 }
nuclear@16 66
nuclear@16 67 void print_matrix(const float *matrix)
nuclear@16 68 {
nuclear@16 69 int i, j;
nuclear@16 70
nuclear@16 71 for(i=0; i<4; i++) {
nuclear@16 72 putchar('[');
nuclear@16 73 for(j=0; j<4; j++) {
nuclear@16 74 printf("%6.3f ", matrix[M(i, j)]);
nuclear@16 75 }
nuclear@16 76 printf("]\n");
nuclear@16 77 }
nuclear@16 78 }