-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc.bak
354 lines (293 loc) · 9.17 KB
/
main.cc.bak
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
#include <vector>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <random>
#include <cassert>
#include <iostream>
#include <functional>
#include <stdexcept>
#include <cstdint>
#include <atomic>
#include <unistd.h>
#include <execinfo.h>
#define GLFW_INCLUDE_GLCOREARB
#define GL_SILENCE_DEPRECATION
#include <GLFW/glfw3.h>
#include "auto.h"
#include "rendering.h"
#include "shape.h"
#include "integration.h"
#include "timestamp.h"
#include "callstack.h"
#include "solid_bsp_tree.h"
// Dynamics and simulation
// =======================
struct Body {
// const
Shape shape;
double mass;
//dmat3 inertial_tensor;
// mutable
double4 position;
double4 orientation;
double4 velocity;
double4 angular_velocity;
};
class Joint {
// ball joint (common point) 3DF
// hinge / axel joint (common edge) 1DF
// cylindrical joint (common line) 2DF
// prismatic joint 1DF (two common parallel lines)
};
class BallJoint {
double4 pa, pb;
};
class HingeJoint {
double4 pa, da, pb, db;
double4 ra, rb; // just for computing relative angle (must be unit and normal to da / db)
};
// Open chain articulated only
// TODO describe children with relative coordinates, but allow computation of absolute ones
class ArticulatedBody {
Body m_body;
vector<pair<ArticulatedBody, Joint>> m_children;
};
// TODO update to use custom integrator
void update(Body& body, double dt) {
double4 bias_velocity, bias_angular_velocity;
body.position += dt * (body.velocity + bias_velocity);
// dq/dt = w*q/2 => q' = q + (w*q)*(dt/2)
//body.angular_velocity * body.orientation;
//body.orientation = glm::normalize(body.orientation + (body.angular_velocity + bias_angular_velocity) * body.orientation * (dt / 2));
double4 torque, force;
body.velocity += force * (dt / body.mass);
//auto invI = glm::inverse(body.inertial_tensor);
//auto I = body.inertial_tensor;
// body.angular_velocity += invI * (torque - glm::cross(body.angular_velocity, I * body.angular_velocity)) * dt;
}
// =====================
Text* text = nullptr;
struct FpvCamera {
double4 position;
double4 orientation;
};
FpvCamera camera;
struct mat4 {
double4 a, b, c, d;
mat4() { }
mat4(double v) {
a = b = c = d = double4{v, v, v, v};
}
};
mat4 Translate(mat4 a, double4 v) {
mat4 m = a;
m.a.w += v.x;
m.b.w += v.y;
m.c.w += v.z;
return m;
}
double4 g_position{0, 0, 0, 1};
float g_yaw = 0, g_pitch = 0;
mat4 g_orientation; // compute from g_yaw and g_roll
void on_key(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE && mods == GLFW_MOD_SHIFT) {
glfwSetWindowShouldClose(window, GL_TRUE);
return;
}
double4 forward, right;
if (action == GLFW_PRESS && key == GLFW_KEY_W && mods == 0) {
// g_position += forward;
}
if (action == GLFW_PRESS && key == GLFW_KEY_S && mods == 0) {
// TODO backward
}
if (action == GLFW_PRESS && key == GLFW_KEY_A && mods == 0) {
// TODO shuffle left
}
if (action == GLFW_PRESS && key == GLFW_KEY_D && mods == 0) {
// TODO shuffle right
}
// TODO:
// Q and E, roll
// update g_orientation of mouse move
}
void on_mouse_button(GLFWwindow* window, int button, int action, int mods) {
if (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) {
}
}
void on_scroll(GLFWwindow* window, double x, double y) {
}
// =====================
// OpenGL
int render_width = 0, render_height = 0;
void model_init(GLFWwindow* window) {
glfwSetKeyCallback(window, on_key);
glfwSetMouseButtonCallback(window, on_mouse_button);
glfwSetScrollCallback(window, on_scroll);
}
mat4 perspective, perspective_rotation;
mat4 Perspective(double fovy, double aspect, double zNear, double zFar) {
const double tanHalfFovy = tan(fovy / 2);
mat4 result(0);
result.a.x = 1 / (aspect * tanHalfFovy);
result.b.y = 1 / (tanHalfFovy);
result.c.z = - (zFar + zNear) / (zFar - zNear);
result.c.w = -1;
result.d.z = - (2 * zFar * zNear) / (zFar - zNear);
return result;
}
// angle is in radians
mat4 Rotate(const mat4& m, double angle, const double4& v) {
const double c = cos(angle);
const double s = sin(angle);
double4 axis = normalize(v);
double4 t = (1 - c) * axis;
mat4 rotate;
rotate.a.x = c + t.x * axis.x;
rotate.a.y = t.x * axis.y + s * axis.z;
rotate.a.z = t.x * axis.z - s * axis.y;
rotate.b.x = t.y * axis.x - s * axis.z;
rotate.b.y = c + t.y * axis.y;
rotate.b.z = t.y * axis.z + s * axis.x;
rotate.c.x = t.z * axis.x + s * axis.y;
rotate.c.y = t.z * axis.y - s * axis.x;
rotate.c.z = c + t.z * axis.z;
mat4 result;
result.a = m.a * rotate.a.x + m.b * rotate.a.y + m.c * rotate.a.z;
result.b = m.a * rotate.b.x + m.b * rotate.b.y + m.c * rotate.b.z;
result.c = m.a * rotate.c.x + m.b * rotate.c.y + m.c * rotate.c.z;
result.d = m.d;
return result;
}
void render_init() {
fprintf(stderr, "OpenGL version: [%s]\n", glGetString(GL_VERSION));
glEnable(GL_CULL_FACE);
glClearColor(0.0, 0.5, 0.0, 1.0);
glViewport(0, 0, render_width, render_height);
perspective = Perspective(M_PI / 180 * 90, render_width / (double)render_height, 0.03, 1000);
perspective_rotation = Rotate(perspective, 0, double4{1, 0, 0, 0});
perspective_rotation = Rotate(perspective_rotation, 0, double4{0, 1, 0, 0});
perspective_rotation = Rotate(perspective_rotation, double(M_PI / 2), double4{-1, 0, 0, 0});
text = new Text;
text->fg_color = double4{1, 1, 1, 1};
text->bg_color = double4{0, 0, 0, 1};
}
struct Triangle {
double4 vertex[3];
double4 color;
};
struct Line {
double4 vectex[2];
double4 color;
};
void render_line(double4 vertex_a, double4 vertex_b, double4 color) {
}
void render_world(const mat4& matrix) {
glClear(GL_COLOR_BUFFER_BIT /*| GL_DEPTH_BUFFER_BIT*/);
glEnable(GL_DEPTH_TEST);
// TODO floor checkerbox
// TODO two boxes in space
render_line(double4{0,0,0}, double4{3,0,0}, double4{1,1,1});
render_line(double4{0,0,0}, double4{0,2,0}, double4{1,1,1});
render_line(double4{0,0,0}, double4{0,0,1}, double4{1,1,1});
glDisable(GL_DEPTH_TEST);
}
void render_gui() {
//glMatrixMode(GL_PROJECTION);
//glOrtho(0, 1, 0, 1, -1, 1);
//glMatrixMode(GL_MODELVIEW);
glColor3d(1, 0, 0);
glBegin(GL_LINES);
glVertex2d(0, 0);
glVertex2d(1, 1);
glVertex2d(0, 1);
glVertex2d(1, 0);
glEnd();
/*glm::mat4 matrix = glm::ortho<float>(0, render_width, 0, render_height, -1, 1);
text->Reset(render_width, render_height, matrix, true);
text->Print("Hello world!");*/
}
bool last_cursor_init = false;
double last_cursor_x, last_cursor_y;
void turn(double dx, double dy) {
g_yaw += dx;
g_pitch += dy;
if (g_pitch > M_PI / 2 * 0.999)
g_pitch = M_PI / 2 * 0.999;
if (g_pitch < -M_PI / 2 * 0.999)
g_pitch = -M_PI / 2 * 0.999;
g_orientation = Rotate(mat4(), -g_yaw, double4{0, 0, 1, 0});
g_orientation = Rotate(g_orientation, -g_pitch, double4{1, 0, 0, 0});
}
void model_orientation(GLFWwindow* window) {
double cursor_x, cursor_y;
glfwGetCursorPos(window, &cursor_x, &cursor_y);
if (!last_cursor_init) {
last_cursor_init = true;
last_cursor_x = cursor_x;
last_cursor_y = cursor_y;
}
if (cursor_x != last_cursor_x || cursor_y != last_cursor_y) {
turn((cursor_x - last_cursor_x) / 150, (cursor_y - last_cursor_y) / 150);
last_cursor_x = cursor_x;
last_cursor_y = cursor_y;
perspective_rotation = Rotate(perspective, g_pitch, double4{1, 0, 0, 0});
perspective_rotation = Rotate(perspective_rotation, g_yaw, double4{0, 1, 0, 0});
perspective_rotation = Rotate(perspective_rotation, M_PI / 2, double4{-1, 0, 0});
}
}
void OnError(int error, const char* message) {
fprintf(stderr, "GLFW error %d: %s\n", error, message);
}
GLFWwindow* create_window() {
glfwSetErrorCallback(OnError);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
GLFWwindow* window = glfwCreateWindow(1000, 1000, "Sima", NULL, NULL);
//GLFWwindow* window = glfwCreateWindow(mode->width * 2, mode->height * 2, "Sima", glfwGetPrimaryMonitor(), NULL);
if (!window)
return nullptr;
glfwMakeContextCurrent(window);
glfwSwapInterval(1/*VSYNC*/);
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetFramebufferSize(window, &render_width, &render_height);
return window;
}
void sigsegv_handler(int sig) {
fprintf(stderr, "Error: signal %d:\n", sig);
Callstack stack;
string s;
stack.write(s, {"sigsegv_handler(int)", "_sigtramp"});
fputs(s.c_str(), stderr);
exit(1);
}
int main(int argc, char** argv) {
void sigsegv_handler(int sig);
signal(SIGSEGV, sigsegv_handler);
Timestamp::init();
if (!glfwInit())
return -1;
GLFWwindow* window = create_window();
if (!window)
return -1;
model_init(window);
render_init();
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
model_orientation(window);
mat4 matrix = Translate(perspective_rotation, g_position);
//Frustum frustum(matrix);
//g_player.cpos = glm::idouble4(glm::floor(g_player.position)) >> ChunkSizeBits;*/
render_world(matrix);
render_gui();
glfwSwapBuffers(window);
}
glfwTerminate();
_exit(0); // exit(0) is not enough
return 0;
}