Quake II RTX doxygen  1.0 dev
matrix.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2018 Christoph Schied
3 Copyright (C) 2003-2006 Andrey Nazarov
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include "vkpt.h"
21 
22 void
23 create_entity_matrix(float matrix[16], entity_t *e, qboolean enable_left_hand)
24 {
25  vec3_t axis[3];
26  vec3_t origin;
27  origin[0] = (1.f-e->backlerp) * e->origin[0] + e->backlerp * e->oldorigin[0];
28  origin[1] = (1.f-e->backlerp) * e->origin[1] + e->backlerp * e->oldorigin[1];
29  origin[2] = (1.f-e->backlerp) * e->origin[2] + e->backlerp * e->oldorigin[2];
30 
31  AnglesToAxis(e->angles, axis);
32 
33  float scale = (e->scale > 0.f) ? e->scale : 1.f;
34 
35  vec3_t scales = { scale, scale, scale };
36  if ((e->flags & RF_LEFTHAND) && enable_left_hand)
37  {
38  scales[1] *= -1.f;
39  }
40 
41  matrix[0] = axis[0][0] * scales[0];
42  matrix[4] = axis[1][0] * scales[1];
43  matrix[8] = axis[2][0] * scales[2];
44  matrix[12] = origin[0];
45 
46  matrix[1] = axis[0][1] * scales[0];
47  matrix[5] = axis[1][1] * scales[1];
48  matrix[9] = axis[2][1] * scales[2];
49  matrix[13] = origin[1];
50 
51  matrix[2] = axis[0][2] * scales[0];
52  matrix[6] = axis[1][2] * scales[1];
53  matrix[10] = axis[2][2] * scales[2];
54  matrix[14] = origin[2];
55 
56  matrix[3] = 0.0f;
57  matrix[7] = 0.0f;
58  matrix[11] = 0.0f;
59  matrix[15] = 1.0f;
60 }
61 
62 void
63 create_projection_matrix(float matrix[16], float znear, float zfar, float fov_x, float fov_y)
64 {
65  float xmin, xmax, ymin, ymax;
66  float width, height, depth;
67 
68  ymax = znear * tan(fov_y * M_PI / 360.0);
69  ymin = -ymax;
70 
71  xmax = znear * tan(fov_x * M_PI / 360.0);
72  xmin = -xmax;
73 
74  width = xmax - xmin;
75  height = ymax - ymin;
76  depth = zfar - znear;
77 
78  matrix[0] = 2 * znear / width;
79  matrix[4] = 0;
80  matrix[8] = (xmax + xmin) / width;
81  matrix[12] = 0;
82 
83  matrix[1] = 0;
84  matrix[5] = -2 * znear / height;
85  matrix[9] = (ymax + ymin) / height;
86  matrix[13] = 0;
87 
88  matrix[2] = 0;
89  matrix[6] = 0;
90  matrix[10] = (zfar + znear) / depth;
91  matrix[14] = 2 * zfar * znear / depth;
92 
93  matrix[3] = 0;
94  matrix[7] = 0;
95  matrix[11] = 1;
96  matrix[15] = 0;
97 }
98 
99 void
100 create_orthographic_matrix(float matrix[16], float xmin, float xmax,
101  float ymin, float ymax, float znear, float zfar)
102 {
103  float width, height, depth;
104 
105  width = xmax - xmin;
106  height = ymax - ymin;
107  depth = zfar - znear;
108 
109  matrix[0] = 2 / width;
110  matrix[4] = 0;
111  matrix[8] = 0;
112  matrix[12] = -(xmax + xmin) / width;
113 
114  matrix[1] = 0;
115  matrix[5] = 2 / height;
116  matrix[9] = 0;
117  matrix[13] = -(ymax + ymin) / height;
118 
119  matrix[2] = 0;
120  matrix[6] = 0;
121  matrix[10] = 1 / depth;
122  matrix[14] = -znear / depth;
123 
124  matrix[3] = 0;
125  matrix[7] = 0;
126  matrix[11] = 0;
127  matrix[15] = 1;
128 }
129 
130 void
131 create_view_matrix(float matrix[16], refdef_t *fd)
132 {
133  vec3_t viewaxis[3];
134  AnglesToAxis(fd->viewangles, viewaxis);
135 
136  matrix[0] = -viewaxis[1][0];
137  matrix[4] = -viewaxis[1][1];
138  matrix[8] = -viewaxis[1][2];
139  matrix[12] = DotProduct(viewaxis[1], fd->vieworg);
140 
141  matrix[1] = viewaxis[2][0];
142  matrix[5] = viewaxis[2][1];
143  matrix[9] = viewaxis[2][2];
144  matrix[13] = -DotProduct(viewaxis[2], fd->vieworg);
145 
146  matrix[2] = viewaxis[0][0];
147  matrix[6] = viewaxis[0][1];
148  matrix[10] = viewaxis[0][2];
149  matrix[14] = -DotProduct(viewaxis[0], fd->vieworg);
150 
151  matrix[3] = 0;
152  matrix[7] = 0;
153  matrix[11] = 0;
154  matrix[15] = 1;
155 }
156 
157 void
158 inverse(const float *m, float *inv)
159 {
160  inv[0] = m[5] * m[10] * m[15] -
161  m[5] * m[11] * m[14] -
162  m[9] * m[6] * m[15] +
163  m[9] * m[7] * m[14] +
164  m[13] * m[6] * m[11] -
165  m[13] * m[7] * m[10];
166 
167  inv[1] = -m[1] * m[10] * m[15] +
168  m[1] * m[11] * m[14] +
169  m[9] * m[2] * m[15] -
170  m[9] * m[3] * m[14] -
171  m[13] * m[2] * m[11] +
172  m[13] * m[3] * m[10];
173 
174  inv[2] = m[1] * m[6] * m[15] -
175  m[1] * m[7] * m[14] -
176  m[5] * m[2] * m[15] +
177  m[5] * m[3] * m[14] +
178  m[13] * m[2] * m[7] -
179  m[13] * m[3] * m[6];
180 
181  inv[3] = -m[1] * m[6] * m[11] +
182  m[1] * m[7] * m[10] +
183  m[5] * m[2] * m[11] -
184  m[5] * m[3] * m[10] -
185  m[9] * m[2] * m[7] +
186  m[9] * m[3] * m[6];
187 
188  inv[4] = -m[4] * m[10] * m[15] +
189  m[4] * m[11] * m[14] +
190  m[8] * m[6] * m[15] -
191  m[8] * m[7] * m[14] -
192  m[12] * m[6] * m[11] +
193  m[12] * m[7] * m[10];
194 
195  inv[5] = m[0] * m[10] * m[15] -
196  m[0] * m[11] * m[14] -
197  m[8] * m[2] * m[15] +
198  m[8] * m[3] * m[14] +
199  m[12] * m[2] * m[11] -
200  m[12] * m[3] * m[10];
201 
202  inv[6] = -m[0] * m[6] * m[15] +
203  m[0] * m[7] * m[14] +
204  m[4] * m[2] * m[15] -
205  m[4] * m[3] * m[14] -
206  m[12] * m[2] * m[7] +
207  m[12] * m[3] * m[6];
208 
209  inv[7] = m[0] * m[6] * m[11] -
210  m[0] * m[7] * m[10] -
211  m[4] * m[2] * m[11] +
212  m[4] * m[3] * m[10] +
213  m[8] * m[2] * m[7] -
214  m[8] * m[3] * m[6];
215 
216  inv[8] = m[4] * m[9] * m[15] -
217  m[4] * m[11] * m[13] -
218  m[8] * m[5] * m[15] +
219  m[8] * m[7] * m[13] +
220  m[12] * m[5] * m[11] -
221  m[12] * m[7] * m[9];
222 
223  inv[9] = -m[0] * m[9] * m[15] +
224  m[0] * m[11] * m[13] +
225  m[8] * m[1] * m[15] -
226  m[8] * m[3] * m[13] -
227  m[12] * m[1] * m[11] +
228  m[12] * m[3] * m[9];
229 
230  inv[10] = m[0] * m[5] * m[15] -
231  m[0] * m[7] * m[13] -
232  m[4] * m[1] * m[15] +
233  m[4] * m[3] * m[13] +
234  m[12] * m[1] * m[7] -
235  m[12] * m[3] * m[5];
236 
237  inv[11] = -m[0] * m[5] * m[11] +
238  m[0] * m[7] * m[9] +
239  m[4] * m[1] * m[11] -
240  m[4] * m[3] * m[9] -
241  m[8] * m[1] * m[7] +
242  m[8] * m[3] * m[5];
243 
244  inv[12] = -m[4] * m[9] * m[14] +
245  m[4] * m[10] * m[13] +
246  m[8] * m[5] * m[14] -
247  m[8] * m[6] * m[13] -
248  m[12] * m[5] * m[10] +
249  m[12] * m[6] * m[9];
250 
251  inv[13] = m[0] * m[9] * m[14] -
252  m[0] * m[10] * m[13] -
253  m[8] * m[1] * m[14] +
254  m[8] * m[2] * m[13] +
255  m[12] * m[1] * m[10] -
256  m[12] * m[2] * m[9];
257 
258  inv[14] = -m[0] * m[5] * m[14] +
259  m[0] * m[6] * m[13] +
260  m[4] * m[1] * m[14] -
261  m[4] * m[2] * m[13] -
262  m[12] * m[1] * m[6] +
263  m[12] * m[2] * m[5];
264 
265  inv[15] = m[0] * m[5] * m[10] -
266  m[0] * m[6] * m[9] -
267  m[4] * m[1] * m[10] +
268  m[4] * m[2] * m[9] +
269  m[8] * m[1] * m[6] -
270  m[8] * m[2] * m[5];
271 
272  float det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
273 
274  det = 1.0f / det;
275 
276  for(int i = 0; i < 16; i++)
277  inv[i] = inv[i] * det;
278 }
279 
280 void
281 mult_matrix_matrix(float *p, const float *a, const float *b)
282 {
283  for(int i = 0; i < 4; i++) {
284  for(int j = 0; j < 4; j++) {
285  p[i * 4 + j] =
286  a[0 * 4 + j] * b[i * 4 + 0] +
287  a[1 * 4 + j] * b[i * 4 + 1] +
288  a[2 * 4 + j] * b[i * 4 + 2] +
289  a[3 * 4 + j] * b[i * 4 + 3];
290  }
291  }
292 }
293 
294 void
295 mult_matrix_vector(float *p, const float *a, const float *b)
296 {
297  int j;
298  for (j = 0; j < 4; j++) {
299  p[j] =
300  a[0 * 4 + j] * b[0] +
301  a[1 * 4 + j] * b[1] +
302  a[2 * 4 + j] * b[2] +
303  a[3 * 4 + j] * b[3];
304  }
305 }
306 
height
static int height
Definition: physical_sky.c:39
vkpt.h
create_orthographic_matrix
void create_orthographic_matrix(float matrix[16], float xmin, float xmax, float ymin, float ymax, float znear, float zfar)
Definition: matrix.c:100
width
static int width
Definition: physical_sky.c:38
inverse
void inverse(const float *m, float *inv)
Definition: matrix.c:158
create_entity_matrix
void create_entity_matrix(float matrix[16], entity_t *e, qboolean enable_left_hand)
Definition: matrix.c:23
m
static struct mdfour * m
Definition: mdfour.c:32
origin
static vec3_t origin
Definition: mesh.c:27
mult_matrix_vector
void mult_matrix_vector(float *p, const float *a, const float *b)
Definition: matrix.c:295
mult_matrix_matrix
void mult_matrix_matrix(float *p, const float *a, const float *b)
Definition: matrix.c:281
create_projection_matrix
void create_projection_matrix(float matrix[16], float znear, float zfar, float fov_x, float fov_y)
Definition: matrix.c:63
create_view_matrix
void create_view_matrix(float matrix[16], refdef_t *fd)
Definition: matrix.c:131