libgoatvr

annotate src/mathutil.c @ 8:3d9ec6fe97d7

- added distortion mesh generation for the OpenHMD module (unfinished) - changed internal implementation function naming to use the vrimp_ prefix - added an opengl helper function to load extension entry points
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 20 Sep 2014 13:22:53 +0300
parents ded3d0a74e19
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <string.h>
nuclear@0 3 #include "mathutil.h"
nuclear@0 4
nuclear@0 5 #define M(i, j) ((i) * 4 + (j))
nuclear@0 6
nuclear@0 7 static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@0 8
nuclear@8 9 void vrimp_rotation_matrix(const float *quat, float *matrix)
nuclear@0 10 {
nuclear@0 11 matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
nuclear@0 12 matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
nuclear@0 13 matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
nuclear@0 14 matrix[3] = 0.0f;
nuclear@0 15
nuclear@0 16 matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
nuclear@0 17 matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
nuclear@0 18 matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
nuclear@0 19 matrix[7] = 0.0f;
nuclear@0 20
nuclear@0 21 matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
nuclear@0 22 matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
nuclear@0 23 matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
nuclear@0 24 matrix[11] = 0.0f;
nuclear@0 25
nuclear@0 26 matrix[12] = matrix[13] = matrix[14] = 0.0f;
nuclear@0 27 matrix[15] = 1.0f;
nuclear@0 28
nuclear@8 29 vrimp_transpose_matrix(matrix);
nuclear@0 30 }
nuclear@0 31
nuclear@8 32 void vrimp_translation_matrix(const float *vec, float *matrix)
nuclear@0 33 {
nuclear@0 34 memcpy(matrix, idmat, sizeof idmat);
nuclear@0 35 matrix[12] = vec[0];
nuclear@0 36 matrix[13] = vec[1];
nuclear@0 37 matrix[14] = vec[2];
nuclear@0 38 }
nuclear@0 39
nuclear@8 40 void vrimp_mult_matrix(float *dest, const float *m1, const float *m2)
nuclear@0 41 {
nuclear@0 42 int i, j;
nuclear@0 43 float tmp[16];
nuclear@0 44
nuclear@0 45 for(i=0; i<4; i++) {
nuclear@0 46 for(j=0; j<4; j++) {
nuclear@0 47 tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] +
nuclear@0 48 m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)];
nuclear@0 49 }
nuclear@0 50 }
nuclear@0 51 memcpy(dest, tmp, sizeof tmp);
nuclear@0 52 }
nuclear@0 53
nuclear@8 54 void vrimp_transpose_matrix(float *matrix)
nuclear@0 55 {
nuclear@0 56 int i, j;
nuclear@0 57 float tmp[16];
nuclear@0 58
nuclear@0 59 for(i=0; i<4; i++) {
nuclear@0 60 for(j=0; j<4; j++) {
nuclear@0 61 tmp[M(i, j)] = matrix[M(j, i)];
nuclear@0 62 }
nuclear@0 63 }
nuclear@0 64 memcpy(matrix, tmp, sizeof tmp);
nuclear@0 65 }
nuclear@0 66
nuclear@8 67 void vrimp_print_matrix(const float *matrix)
nuclear@0 68 {
nuclear@0 69 int i, j;
nuclear@0 70
nuclear@0 71 for(i=0; i<4; i++) {
nuclear@0 72 putchar('[');
nuclear@0 73 for(j=0; j<4; j++) {
nuclear@0 74 printf("%6.3f ", matrix[M(i, j)]);
nuclear@0 75 }
nuclear@0 76 printf("]\n");
nuclear@0 77 }
nuclear@0 78 }