vrchess

annotate src/vr/mathutil.c @ 12:778ed91cb7fd

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