-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.c
99 lines (79 loc) · 2.13 KB
/
matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* This proprietary software may be used only as
* authorised by a licensing agreement from ARM Limited
* (C) COPYRIGHT 2009 - 2011 ARM Limited
* ALL RIGHTS RESERVED
* The entire notice above must be reproduced on all authorised
* copies and copies may only be made to the extent permitted
* by a licensing agreement from ARM Limited.
*/
/*
* matrix.c
* Matrix manipulation functions.
*/
#include "matrix.h"
/*
* Simulates desktop's glRotatef. The matrix is returned in column-major
* order.
*/
void rotate_matrix(double angle, double x, double y, double z, float *R) {
double radians, c, s, c1, u[3], length;
int i, j;
radians = (angle * M_PI) / 180.0;
c = cos(radians);
s = sin(radians);
c1 = 1.0 - cos(radians);
length = sqrt(x * x + y * y + z * z);
u[0] = x / length;
u[1] = y / length;
u[2] = z / length;
for (i = 0; i < 16; i++) {
R[i] = 0.0;
}
R[15] = 1.0;
for (i = 0; i < 3; i++) {
R[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
R[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
R[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0);
}
}
}
/*
* Simulates gluPerspectiveMatrix
*/
void perspective_matrix(double fovy, double aspect, double znear, double zfar, float *P) {
int i;
double f;
f = 1.0/tan(fovy * 0.5);
for (i = 0; i < 16; i++) {
P[i] = 0.0;
}
P[0] = f / aspect;
P[5] = f;
P[10] = (znear + zfar) / (znear - zfar);
P[11] = -1.0;
P[14] = (2.0 * znear * zfar) / (znear - zfar);
P[15] = 0.0;
}
/*
* Multiplies A by B and writes out to C. All matrices are 4x4 and column
* major. In-place multiplication is supported.
*/
void multiply_matrix(float *A, float *B, float *C) {
int i, j, k;
float aTmp[16];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
aTmp[j * 4 + i] = 0.0;
for (k = 0; k < 4; k++) {
aTmp[j * 4 + i] += A[k * 4 + i] * B[j * 4 + k];
}
}
}
for (i = 0; i < 16; i++) {
C[i] = aTmp[i];
}
}