conworlds

diff 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
line diff
     1.1 --- a/src/vr/mathutil.c	Tue Aug 26 18:40:23 2014 +0300
     1.2 +++ b/src/vr/mathutil.c	Tue Aug 26 18:42:53 2014 +0300
     1.3 @@ -1,78 +1,78 @@
     1.4 -#include <stdio.h>
     1.5 -#include <string.h>
     1.6 -#include "mathutil.h"
     1.7 -
     1.8 -#define M(i, j)	((i) * 4 + (j))
     1.9 -
    1.10 -static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.11 -
    1.12 -void rotation_matrix(const float *quat, float *matrix)
    1.13 -{
    1.14 -	matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
    1.15 -	matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
    1.16 -	matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
    1.17 -	matrix[3] = 0.0f;
    1.18 -
    1.19 -	matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
    1.20 -	matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
    1.21 -	matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
    1.22 -	matrix[7] = 0.0f;
    1.23 -
    1.24 -	matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
    1.25 -	matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
    1.26 -	matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
    1.27 -	matrix[11] = 0.0f;
    1.28 -
    1.29 -	matrix[12] = matrix[13] = matrix[14] = 0.0f;
    1.30 -	matrix[15] = 1.0f;
    1.31 -
    1.32 -	transpose_matrix(matrix);
    1.33 -}
    1.34 -
    1.35 -void translation_matrix(const float *vec, float *matrix)
    1.36 -{
    1.37 -	memcpy(matrix, idmat, sizeof idmat);
    1.38 -	matrix[12] = vec[0];
    1.39 -	matrix[13] = vec[1];
    1.40 -	matrix[14] = vec[2];
    1.41 -}
    1.42 -
    1.43 -void mult_matrix(float *dest, const float *m1, const float *m2)
    1.44 -{
    1.45 -	int i, j;
    1.46 -	float tmp[16];
    1.47 -
    1.48 -	for(i=0; i<4; i++) {
    1.49 -		for(j=0; j<4; j++) {
    1.50 -			tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] +
    1.51 -				m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)];
    1.52 -		}
    1.53 -	}
    1.54 -	memcpy(dest, tmp, sizeof tmp);
    1.55 -}
    1.56 -
    1.57 -void transpose_matrix(float *matrix)
    1.58 -{
    1.59 -	int i, j;
    1.60 -	float tmp[16];
    1.61 -
    1.62 -	for(i=0; i<4; i++) {
    1.63 -		for(j=0; j<4; j++) {
    1.64 -			tmp[M(i, j)] = matrix[M(j, i)];
    1.65 -		}
    1.66 -	}
    1.67 -	memcpy(matrix, tmp, sizeof tmp);
    1.68 -}
    1.69 -
    1.70 -void print_matrix(const float *matrix)
    1.71 -{
    1.72 -	int i, j;
    1.73 -
    1.74 -	for(i=0; i<4; i++) {
    1.75 -		putchar('[');
    1.76 -		for(j=0; j<4; j++) {
    1.77 -			printf("%6.3f ", matrix[M(i, j)]);
    1.78 -		}
    1.79 -		printf("]\n");
    1.80 -	}
    1.81 -}
    1.82 \ No newline at end of file
    1.83 +#include <stdio.h>
    1.84 +#include <string.h>
    1.85 +#include "mathutil.h"
    1.86 +
    1.87 +#define M(i, j)	((i) * 4 + (j))
    1.88 +
    1.89 +static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.90 +
    1.91 +void rotation_matrix(const float *quat, float *matrix)
    1.92 +{
    1.93 +	matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
    1.94 +	matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
    1.95 +	matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
    1.96 +	matrix[3] = 0.0f;
    1.97 +
    1.98 +	matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
    1.99 +	matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
   1.100 +	matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
   1.101 +	matrix[7] = 0.0f;
   1.102 +
   1.103 +	matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
   1.104 +	matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
   1.105 +	matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
   1.106 +	matrix[11] = 0.0f;
   1.107 +
   1.108 +	matrix[12] = matrix[13] = matrix[14] = 0.0f;
   1.109 +	matrix[15] = 1.0f;
   1.110 +
   1.111 +	transpose_matrix(matrix);
   1.112 +}
   1.113 +
   1.114 +void translation_matrix(const float *vec, float *matrix)
   1.115 +{
   1.116 +	memcpy(matrix, idmat, sizeof idmat);
   1.117 +	matrix[12] = vec[0];
   1.118 +	matrix[13] = vec[1];
   1.119 +	matrix[14] = vec[2];
   1.120 +}
   1.121 +
   1.122 +void mult_matrix(float *dest, const float *m1, const float *m2)
   1.123 +{
   1.124 +	int i, j;
   1.125 +	float tmp[16];
   1.126 +
   1.127 +	for(i=0; i<4; i++) {
   1.128 +		for(j=0; j<4; j++) {
   1.129 +			tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] +
   1.130 +				m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)];
   1.131 +		}
   1.132 +	}
   1.133 +	memcpy(dest, tmp, sizeof tmp);
   1.134 +}
   1.135 +
   1.136 +void transpose_matrix(float *matrix)
   1.137 +{
   1.138 +	int i, j;
   1.139 +	float tmp[16];
   1.140 +
   1.141 +	for(i=0; i<4; i++) {
   1.142 +		for(j=0; j<4; j++) {
   1.143 +			tmp[M(i, j)] = matrix[M(j, i)];
   1.144 +		}
   1.145 +	}
   1.146 +	memcpy(matrix, tmp, sizeof tmp);
   1.147 +}
   1.148 +
   1.149 +void print_matrix(const float *matrix)
   1.150 +{
   1.151 +	int i, j;
   1.152 +
   1.153 +	for(i=0; i<4; i++) {
   1.154 +		putchar('[');
   1.155 +		for(j=0; j<4; j++) {
   1.156 +			printf("%6.3f ", matrix[M(i, j)]);
   1.157 +		}
   1.158 +		printf("]\n");
   1.159 +	}
   1.160 +}