/
matrix.c
128 lines (100 loc) · 3.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright (c) 2017-2020 ByteBit
This file is part of BetterSpades.
BetterSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BetterSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "matrix.h"
mat4 matrix_view;
mat4 matrix_model;
mat4 matrix_projection;
#define MATRIX_STACK_DEPTH 8
mat4 matrix_stack[MATRIX_STACK_DEPTH];
int matrix_stack_index = 0;
void matrix_multiply(mat4 m, mat4 n) {
glmc_mat4_mul(n, m, m);
}
void matrix_load(mat4 m, mat4 n) {
glmc_mat4_copy(n, m);
}
void matrix_rotate(mat4 m, float angle, float x, float y, float z) {
glmc_rotate(m, angle / 180.0F * GLM_PI, (vec3) {x, y, z});
}
void matrix_translate(mat4 m, float x, float y, float z) {
glmc_translate(m, (vec3) {x, y, z});
}
void matrix_scale3(mat4 m, float s) {
glmc_scale_uni(m, s);
}
void matrix_scale(mat4 m, float sx, float sy, float sz) {
glmc_scale(m, (vec3) {sx, sy, sz});
}
void matrix_identity(mat4 m) {
glmc_mat4_identity(m);
}
void matrix_push(mat4 m) {
if(matrix_stack_index >= MATRIX_STACK_DEPTH) {
log_fatal("Matrix stack overflow!");
return;
}
glmc_mat4_copy(m, matrix_stack[matrix_stack_index++]);
}
void matrix_pop(mat4 m) {
if(matrix_stack_index < 1) {
log_fatal("Matrix stack underflow!");
return;
}
glmc_mat4_copy(matrix_stack[--matrix_stack_index], m);
}
void matrix_vector(mat4 m, vec4 v) {
glmc_mat4_mulv(m, v, v);
v[0] /= v[3];
v[1] /= v[3];
v[2] /= v[3];
}
void matrix_pointAt(mat4 m, float dx, float dy, float dz) {
float l = sqrt(dx * dx + dy * dy + dz * dz);
if(l) {
dx /= l;
dy /= l;
dz /= l;
}
float rx = -atan2(dz, dx) * GLM_1_PI * 180.0F;
matrix_rotate(m, rx, 0.0F, 1.0F, 0.0F);
if(dy) {
float ry = asin(dy) * GLM_1_PI * 180.0F;
matrix_rotate(m, ry, 0.0F, 0.0F, 1.0F);
}
}
void matrix_ortho(mat4 m, float left, float right, float bottom, float top, float nearv, float farv) {
glmc_ortho(left, right, bottom, top, nearv, farv, m);
}
void matrix_perspective(mat4 m, float fovy, float aspect, float zNear, float zFar) {
glmc_perspective(fovy / 180.0F * GLM_PI, aspect, zNear, zFar, m);
}
void matrix_lookAt(mat4 m, double eyex, double eyey, double eyez, double centerx, double centery, double centerz,
double upx, double upy, double upz) {
glmc_lookat((vec3) {eyex, eyey, eyez}, (vec3) {centerx, centery, centerz}, (vec3) {upx, upy, upz}, m);
// matrix_translate(-eyex, -eyey, -eyez);
}
void matrix_upload() {
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf((float*)matrix_view);
glMultMatrixf((float*)matrix_model);
}
void matrix_upload_p() {
glMatrixMode(GL_PROJECTION);
glLoadMatrixf((float*)matrix_projection);
}