-
Notifications
You must be signed in to change notification settings - Fork 119
/
GSmatrix.cpp
441 lines (371 loc) · 14.2 KB
/
GSmatrix.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/** Copyright (C) 2008-2012 Josh Ventura
*** Copyright (C) 2013-2014 Robert Colton, Harijs Grinbergs
*** Copyright (C) 2015 Harijs Grinbergs
*** Copyright (C) 2018 Robert Colton
***
*** This file is a part of the ENIGMA Development Environment.
***
*** ENIGMA 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, version 3 of the license or any later version.
***
*** This application and its source code is distributed AS-IS, 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 this code. If not, see <http://www.gnu.org/licenses/>
**/
#include "GSmatrix_impl.h"
#include "GSmatrix.h"
#include "GSd3d.h"
#include "Widget_Systems/widgets_mandatory.h" // for show_error
#include "Universal_System/scalar.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stack>
namespace {
// used for classic transform stack
std::stack<glm::mat4> trans_stack;
// used for ENIGMA projection stack
// first is view matrix second is projection matrix
std::stack<std::pair<glm::mat4, glm::mat4> > proj_stack;
// used for newer matrix stack functions
std::stack<glm::mat4> matrix_stack;
inline glm::mat4 matrix_rotation(gs_scalar x, gs_scalar y, gs_scalar z) {
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-x), glm::vec3(1.0f, 0.0f, 0.0f));
rotation = glm::rotate(rotation, (float)gs_angle_to_radians(-y), glm::vec3(0.0f, 1.0f, 0.0f));
rotation = glm::rotate(rotation, (float)gs_angle_to_radians(-z), glm::vec3(0.0f, 0.0f, 1.0f));
return rotation;
}
inline var matrix_vararray(const glm::mat4 &mat) {
var pm;
pm(16) = 0;
pm(0) = mat[0][0], pm(4) = mat[0][1], pm(8) = mat[0][2], pm(12) = mat[0][3],
pm(1) = mat[1][0], pm(5) = mat[1][1], pm(9) = mat[1][2], pm(13) = mat[1][3],
pm(2) = mat[2][0], pm(6) = mat[2][1], pm(10) = mat[2][2], pm(14) = mat[2][3],
pm(3) = mat[3][0], pm(7) = mat[3][1], pm(11) = mat[3][2], pm(15) = mat[3][3];
return pm;
}
inline var vec3_vararray(const glm::vec3 &vector) {
var pm;
pm(3) = 0;
pm(0) = vector.x, pm(1) = vector.y, pm(2) = vector.z;
return pm;
}
template<typename T>
inline void stack_clear(std::stack<T> &stack) {
while (!stack.empty()) {
stack.pop();
}
}
} // namespace anonymous
namespace enigma {
bool d3dPerspective = false;
glm::mat4 world = glm::mat4(1.0f),
view = glm::mat4(1.0f),
projection = glm::mat4(1.0f);
} // namespace enigma
namespace enigma_user
{
bool is_matrix(const var& value) {
return (value.array_len() == 16);
}
bool is_vec3(const var& value) {
return (value.array_len() == 3);
}
bool is_vec4(const var& value) {
return (value.array_len() == 4);
}
var matrix_get(int type) {
switch (type) {
case matrix_world:
return matrix_vararray(enigma::world);
case matrix_view:
return matrix_vararray(enigma::view);
case matrix_projection:
return matrix_vararray(enigma::projection);
default:
#ifdef DEBUG_MODE
show_error("Unknown matrix type: " + std::to_string(type), false);
#endif
break;
}
return matrix_vararray(glm::mat4(1.0f));
}
void matrix_set(int type, const var& matrix) {
glm::mat4 glm_matrix = glm::make_mat4((gs_scalar*)matrix.values);
switch (type) {
case matrix_world:
enigma::world = glm_matrix;
break;
case matrix_view:
enigma::view = glm_matrix;
break;
case matrix_projection:
enigma::projection = glm_matrix;
break;
default:
#ifdef DEBUG_MODE
show_error("Unknown matrix type: " + std::to_string(type), false);
#endif
break;
}
enigma::graphics_set_matrix(type);
}
var matrix_multiply(const var& matrix1, const var& matrix2) {
glm::mat4 glm_matrix1 = glm::make_mat4((gs_scalar*)matrix1.values),
glm_matrix2 = glm::make_mat4((gs_scalar*)matrix2.values);
return matrix_vararray(glm_matrix1 * glm_matrix2);
}
var matrix_transform_vertex(const var& matrix, gs_scalar x, gs_scalar y, gs_scalar z) {
glm::mat4 glm_matrix = glm::make_mat4((gs_scalar*)matrix.values);
glm::vec4 vertex(x, y, z, 0);
vertex = glm_matrix * vertex;
return vec3_vararray(glm::vec3(vertex));
}
var matrix_build(gs_scalar x, gs_scalar y, gs_scalar z,
gs_scalar xrotation, gs_scalar yrotation, gs_scalar zrotation,
gs_scalar xscale, gs_scalar yscale, gs_scalar zscale) {
glm::mat4 matrix = matrix_rotation(xrotation, yrotation, zrotation);
matrix = glm::scale(matrix, glm::vec3(xscale, yscale, zscale));
matrix = glm::translate(matrix, glm::vec3(x, y, z));
return matrix_vararray(matrix);
}
var matrix_build_identity() {
return matrix_vararray(glm::mat4(1.0f));
}
var matrix_build_lookat(gs_scalar xfrom, gs_scalar yfrom, gs_scalar zfrom,
gs_scalar xto, gs_scalar yto, gs_scalar zto,
gs_scalar xup, gs_scalar yup, gs_scalar zup) {
return matrix_vararray(glm::lookAt(glm::vec3(xfrom, yfrom, zfrom), glm::vec3(xto, yto, zto), glm::vec3(xup, yup, zup)));
}
var matrix_build_projection_ortho(gs_scalar width, gs_scalar height, gs_scalar znear, gs_scalar zfar) {
return matrix_vararray(glm::ortho(0.0f, width, height, 0.0f, znear, zfar));
}
var matrix_build_projection_perspective(gs_scalar width, gs_scalar height, gs_scalar znear, gs_scalar zfar) {
return matrix_vararray(glm::perspective((float)gs_angle_to_radians(40), width/height, znear, zfar));
}
var matrix_build_projection_perspective_fov(gs_scalar fov_y, gs_scalar aspect, gs_scalar znear, gs_scalar zfar) {
return matrix_vararray(glm::perspective((float)gs_angle_to_radians(fov_y), aspect, znear, zfar));
}
bool matrix_stack_is_empty() {
return matrix_stack.empty();
}
void matrix_stack_clear() {
stack_clear(matrix_stack);
}
void matrix_stack_set(const var& matrix) {
matrix_stack.pop();
glm::mat4 glm_matrix = glm::make_mat4((gs_scalar*)matrix.values);
matrix_stack.emplace(glm_matrix);
}
void matrix_stack_push(const var& matrix) {
glm::mat4 glm_matrix = glm::make_mat4((gs_scalar*)matrix.values);
matrix_stack.emplace(glm_matrix);
}
void matrix_stack_pop() {
matrix_stack.pop();
}
var matrix_stack_top() {
return matrix_vararray(matrix_stack.top());
}
void d3d_set_perspective(bool enable) {
// in GM8.1 and GMS v1.4 this does not take effect
// until the next frame in screen_redraw
enigma::d3dPerspective = enable;
}
bool d3d_get_perspective() {
return enigma::d3dPerspective;
}
void d3d_set_projection(gs_scalar xfrom, gs_scalar yfrom, gs_scalar zfrom,
gs_scalar xto, gs_scalar yto, gs_scalar zto,
gs_scalar xup, gs_scalar yup, gs_scalar zup)
{
enigma::view = glm::lookAt(glm::vec3(xfrom, yfrom, zfrom), glm::vec3(xto, yto, zto), glm::vec3(xup, yup, zup));
enigma::graphics_set_matrix(matrix_view);
}
void d3d_set_projection_ext(gs_scalar xfrom, gs_scalar yfrom, gs_scalar zfrom,
gs_scalar xto, gs_scalar yto, gs_scalar zto,
gs_scalar xup, gs_scalar yup, gs_scalar zup,
gs_scalar angle, gs_scalar aspect, gs_scalar znear, gs_scalar zfar)
{
enigma::view = glm::lookAt(glm::vec3(xfrom, yfrom, zfrom), glm::vec3(xto, yto, zto), glm::vec3(xup, yup, zup));
enigma::projection = glm::perspective((float)gs_angle_to_radians(angle), (float)(aspect), znear, zfar);
enigma::graphics_set_matrix(matrix_view);
enigma::graphics_set_matrix(matrix_projection);
}
void d3d_set_projection_ortho(gs_scalar x, gs_scalar y, gs_scalar width, gs_scalar height, gs_scalar angle)
{
enigma::view = glm::ortho(x, x + width, y + height, y, -32000.0f, 32000.0f);
enigma::projection = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(angle), glm::vec3(0.0f, 0.0f, 1.0f));
enigma::graphics_set_matrix(matrix_view);
enigma::graphics_set_matrix(matrix_projection);
}
void d3d_set_projection_ortho_lookat(gs_scalar x, gs_scalar y, gs_scalar width, gs_scalar height, gs_scalar angle,
gs_scalar xfrom, gs_scalar yfrom, gs_scalar zfrom,
gs_scalar xto, gs_scalar yto, gs_scalar zto,
gs_scalar xup, gs_scalar yup, gs_scalar zup) {
enigma::view = glm::lookAt(glm::vec3(xfrom, yfrom, zfrom), glm::vec3(xto, yto, zto), glm::vec3(xup, yup, zup));
enigma::view = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(angle), glm::vec3(0.0f, 0.0f, 1.0f)) * enigma::view;
enigma::projection = glm::ortho(x, x + width, y + height, y, -32000.0f, 32000.0f);
enigma::graphics_set_matrix(matrix_view);
enigma::graphics_set_matrix(matrix_projection);
}
void d3d_set_projection_perspective(gs_scalar x, gs_scalar y, gs_scalar width, gs_scalar height, gs_scalar angle)
{
enigma::view = glm::translate(glm::mat4(1.0f), glm::vec3(-(x + width/2), -(y + height/2), (width+height)/2));
enigma::view = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(angle), glm::vec3(0.0f, 0.0f, 1.0f)) * enigma::view;
enigma::projection = glm::perspective((float)gs_angle_to_radians(40.0f), width/height, 1.0f, 32000.0f);
enigma::graphics_set_matrix(matrix_view);
enigma::graphics_set_matrix(matrix_projection);
}
var d3d_transform_vertex(gs_scalar x, gs_scalar y, gs_scalar z) {
glm::vec4 vertex(x, y, z, 0);
vertex = enigma::world * vertex;
return vec3_vararray(glm::vec3(vertex));
}
void d3d_transform_set_identity()
{
enigma::world = glm::mat4(1.0f);
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_translation(gs_scalar xt, gs_scalar yt, gs_scalar zt)
{
enigma::world = glm::translate(glm::mat4(1.0f), glm::vec3(xt, yt, zt)) * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_scaling(gs_scalar xs, gs_scalar ys, gs_scalar zs)
{
enigma::world = glm::scale(glm::mat4(1.0f), glm::vec3(xs, ys, zs)) * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_rotation_x(gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(1.0f, 0.0f, 0.0f)) * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_rotation_y(gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(0.0f, 1.0f, 0.0f)) * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_rotation_z(gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(0.0f, 0.0f, 1.0f)) * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_rotation_axis(gs_scalar x, gs_scalar y, gs_scalar z, gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(x, y, z)) * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_add_rotation(gs_scalar x, gs_scalar y, gs_scalar z)
{
glm::mat4 rotation = matrix_rotation(x, y, z);
enigma::world = rotation * enigma::world;
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_translation(gs_scalar xt, gs_scalar yt, gs_scalar zt)
{
enigma::world = glm::translate(glm::mat4(1.0f), glm::vec3(xt, yt, zt));
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_scaling(gs_scalar xs, gs_scalar ys, gs_scalar zs)
{
enigma::world = glm::scale(glm::mat4(1.0f), glm::vec3(xs, ys, zs));
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_rotation_x(gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(1.0f, 0.0f, 0.0f));
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_rotation_y(gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(0.0f, 1.0f, 0.0f));
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_rotation_z(gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(0.0f, 0.0f, 1.0f));
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_rotation_axis(gs_scalar x, gs_scalar y, gs_scalar z, gs_scalar angle)
{
enigma::world = glm::rotate(glm::mat4(1.0f), (float)gs_angle_to_radians(-angle), glm::vec3(x, y, z));
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_set_rotation(gs_scalar x, gs_scalar y, gs_scalar z)
{
enigma::world = matrix_rotation(x, y, z);
enigma::graphics_set_matrix(matrix_world);
}
void d3d_transform_stack_clear()
{
stack_clear(trans_stack);
}
bool d3d_transform_stack_empty()
{
return trans_stack.empty();
}
bool d3d_transform_stack_push()
{
trans_stack.emplace(enigma::world);
return true;
}
bool d3d_transform_stack_top()
{
if (trans_stack.empty()) return false;
enigma::world = trans_stack.top();
enigma::graphics_set_matrix(matrix_world);
return true;
}
bool d3d_transform_stack_pop()
{
if (trans_stack.empty()) return false;
enigma::world = trans_stack.top();
enigma::graphics_set_matrix(matrix_world);
trans_stack.pop();
return true;
}
bool d3d_transform_stack_discard()
{
if (trans_stack.empty()) return false;
trans_stack.pop();
return true;
}
bool d3d_projection_stack_push() {
proj_stack.emplace(enigma::view, enigma::projection);
return true;
}
bool d3d_projection_stack_pop() {
if (proj_stack.empty()) return false;
enigma::view = proj_stack.top().first;
enigma::projection = proj_stack.top().second;
enigma::graphics_set_matrix(matrix_view);
enigma::graphics_set_matrix(matrix_projection);
proj_stack.pop();
return true;
}
void d3d_projection_stack_clear() {
stack_clear(proj_stack);
}
bool d3d_projection_stack_empty() {
return proj_stack.empty();
}
bool d3d_projection_stack_top() {
if (proj_stack.empty()) return false;
enigma::view = proj_stack.top().first;
enigma::projection = proj_stack.top().second;
enigma::graphics_set_matrix(matrix_view);
enigma::graphics_set_matrix(matrix_projection);
return true;
}
bool d3d_projection_stack_discard() {
if (proj_stack.empty()) return false;
proj_stack.pop();
return true;
}
} // namespace enigma_user