-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
351 lines (286 loc) · 8 KB
/
main.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
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
#include "mio.h"
#define TIME_STEP (1000 / 30)
#define MAX_TIME_STEP (250)
float dpi_scale = 1;
extern struct scene *scene;
static int screenw = 800, screenh = 600;
static int mousex, mousey, mouseleft = 0, mousemiddle = 0, mouseright = 0;
static int lasttime = 0;
static int totaltime = 0;
static int accumtime = 0;
static int showconsole = 0;
static struct font *font_sans;
static struct font *font_mono;
static float cam_dist = 5;
static float cam_yaw = 0;
static float cam_pitch = -20;
void togglefullscreen(void)
{
static int oldw = 100, oldh = 100, oldx = 0, oldy = 0;
static int isfullscreen = 0;
if (!isfullscreen) {
oldw = glutGet(GLUT_WINDOW_WIDTH);
oldh = glutGet(GLUT_WINDOW_HEIGHT);
oldx = glutGet(GLUT_WINDOW_X);
oldy = glutGet(GLUT_WINDOW_Y);
glutFullScreen();
} else {
glutPositionWindow(oldx, oldy);
glutReshapeWindow(oldw, oldh);
}
isfullscreen = !isfullscreen;
}
static void pushmod(int mod)
{
if ((mod & GLUT_ACTIVE_ALT) && (mod & GLUT_ACTIVE_CTRL) && (mod & GLUT_ACTIVE_SHIFT))
lua_pushliteral(L, "ACS");
else if ((mod & GLUT_ACTIVE_ALT) && (mod & GLUT_ACTIVE_CTRL))
lua_pushliteral(L, "AC");
else if ((mod & GLUT_ACTIVE_ALT) && (mod & GLUT_ACTIVE_SHIFT))
lua_pushliteral(L, "AS");
else if ((mod & GLUT_ACTIVE_CTRL) && (mod & GLUT_ACTIVE_SHIFT))
lua_pushliteral(L, "CS");
else if ((mod & GLUT_ACTIVE_ALT))
lua_pushliteral(L, "A");
else if ((mod & GLUT_ACTIVE_CTRL))
lua_pushliteral(L, "C");
else if ((mod & GLUT_ACTIVE_SHIFT))
lua_pushliteral(L, "S");
else
lua_pushliteral(L, "");
}
static void mouse(int button, int state, int x, int y)
{
state = state == GLUT_DOWN;
if (button == GLUT_LEFT_BUTTON) mouseleft = state;
if (button == GLUT_MIDDLE_BUTTON) mousemiddle = state;
if (button == GLUT_RIGHT_BUTTON) mouseright = state;
mousex = x;
mousey = y;
glutPostRedisplay();
lua_getglobal(L, "on_mouse");
lua_pushnumber(L, button);
lua_pushnumber(L, state);
pushmod(glutGetModifiers());
lua_pushnumber(L, x);
lua_pushnumber(L, y);
docall(L, 5, 0);
}
static void motion(int x, int y)
{
int dx = x - mousex;
int dy = y - mousey;
if (mouseleft) {
cam_yaw -= dx * 0.3;
cam_pitch -= dy * 0.2;
if (cam_pitch < -85) cam_pitch = -85;
if (cam_pitch > 85) cam_pitch = 85;
if (cam_yaw < 0) cam_yaw += 360;
if (cam_yaw > 360) cam_yaw -= 360;
}
if (mousemiddle || mouseright) {
cam_dist += dy * 0.01 * cam_dist;
if (cam_dist < 0.1) cam_dist = 0.1;
if (cam_dist > 100) cam_dist = 100;
}
mousex = x;
mousey = y;
glutPostRedisplay();
lua_getglobal(L, "on_motion");
lua_pushnumber(L, x);
lua_pushnumber(L, y);
docall(L, 2, 0);
}
static void keyboard(unsigned char key, int x, int y)
{
unsigned char buf[2];
int mod = glutGetModifiers();
glutPostRedisplay();
if ((mod & GLUT_ACTIVE_ALT) && key == '\r') {
togglefullscreen();
} else if (showconsole) {
if (key == '`' || key == 27)
showconsole = !showconsole;
else
console_keyboard(key, mod);
} else {
if (key == 27)
exit(0);
if (key == '`' || key == '\r') {
showconsole = !showconsole;
return;
}
if (mod & GLUT_ACTIVE_CTRL)
key = key + 64;
buf[0] = key;
buf[1] = 0;
lua_getglobal(L, "on_keyboard");
lua_pushstring(L, (char*)buf);
pushmod(mod);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
docall(L, 4, 0);
}
}
static void special(int key, int x, int y)
{
int mod = glutGetModifiers();
if (key == GLUT_KEY_F4 && mod == GLUT_ACTIVE_ALT)
exit(0);
if (showconsole) {
console_special(key, mod);
} else {
const char *s = NULL;
switch (key) {
case GLUT_KEY_F1: s = "F1"; break;
case GLUT_KEY_F2: s = "F2"; break;
case GLUT_KEY_F3: s = "F3"; break;
case GLUT_KEY_F4: s = "F4"; break;
case GLUT_KEY_F5: s = "F5"; break;
case GLUT_KEY_F6: s = "F6"; break;
case GLUT_KEY_F7: s = "F7"; break;
case GLUT_KEY_F8: s = "F8"; break;
case GLUT_KEY_F9: s = "F9"; break;
case GLUT_KEY_F10: s = "F10"; break;
case GLUT_KEY_F11: s = "F11"; break;
case GLUT_KEY_F12: s = "F12"; break;
case GLUT_KEY_LEFT: s = "LEFT"; break;
case GLUT_KEY_UP: s = "UP"; break;
case GLUT_KEY_RIGHT: s = "RIGHT"; break;
case GLUT_KEY_DOWN: s = "DOWN"; break;
case GLUT_KEY_PAGE_UP: s = "PAGEUP"; break;
case GLUT_KEY_PAGE_DOWN: s = "PAGEDOWN"; break;
case GLUT_KEY_HOME: s = "HOME"; break;
case GLUT_KEY_END: s = "END"; break;
case GLUT_KEY_INSERT: s = "INSERT"; break;
default: return;
}
lua_getglobal(L, "on_keyboard");
lua_pushstring(L, s);
pushmod(mod);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
docall(L, 4, 0);
}
glutPostRedisplay();
}
static void spacemotion(int dx, int dy, int dz)
{
//printf("spacemotion %d %d %d\n", dx, dy, dz);
cam_dist += dy * 0.0001 * cam_dist;
if (cam_dist < 0.1) cam_dist = 0.1;
if (cam_dist > 100) cam_dist = 100;
}
static void spacerotate(int dx, int dy, int dz)
{
//printf("spacerotate %d %d %d\n", dx, dy, dz);
cam_yaw += dy / 45;
cam_pitch += dx / 60;
}
static void reshape(int w, int h)
{
screenw = w;
screenh = h;
glViewport(0, 0, w, h);
render_reshape(w, h);
}
static void display(void)
{
float projection[16];
float view[16];
int thistime, frametime;
thistime = glutGet(GLUT_ELAPSED_TIME);
frametime = thistime - lasttime;
lasttime = thistime;
// update world at fixed time steps
if (frametime > MAX_TIME_STEP)
frametime = MAX_TIME_STEP; // avoid death spiral
accumtime += frametime;
while (accumtime >= TIME_STEP) {
// prevstate = curstate;
// update(curstate)
run_function("update");
totaltime += TIME_STEP;
accumtime -= TIME_STEP;
}
float alpha = (float) accumtime / TIME_STEP;
// drawstate = lerp(prevstate, curstate, alpha)
// draw world
mat_perspective(projection, 75, (float)screenw / screenh, 0.1, 1000);
mat_identity(view);
mat_rotate_x(view, -90);
mat_translate(view, 0, cam_dist, -cam_dist / 5);
mat_rotate_x(view, -cam_pitch);
mat_rotate_z(view, -cam_yaw);
render_camera(projection, view);
render_geometry_pass();
run_function("draw_geometry");
render_light_pass();
run_function("draw_light");
render_forward_pass();
render_sky();
render_finish();
// draw ui
glViewport(0, 0, screenw, screenh);
mat_ortho(projection, 0, screenw, screenh, 0, -1, 1);
mat_identity(view);
// copy forward buffer to screen
render_blit(projection, view, screenw, screenh);
//render_debug_buffers(projection, view);
if (showconsole)
console_draw(projection, view, font_mono, 12 * dpi_scale);
glutSwapBuffers();
glutPostRedisplay();
gl_assert("swap buffers");
}
int main(int argc, char **argv)
{
int i;
glutInit(&argc, argv);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
int wmm = glutGet(GLUT_SCREEN_WIDTH_MM);
int hmm = glutGet(GLUT_SCREEN_HEIGHT_MM);
int wpx = glutGet(GLUT_SCREEN_WIDTH);
int hpx = glutGet(GLUT_SCREEN_HEIGHT);
int dpi = ((wpx * 254 / wmm) + (hpx * 254 / hmm)) / 20;
dpi_scale = (float) dpi / 72;
glutInitWindowPosition(50, 50+24);
glutInitWindowSize(640 * dpi_scale, 480 * dpi_scale);
#ifdef HAVE_SRGB_FRAMEBUFFER
glutInitDisplayMode(GLUT_SRGB | GLUT_DOUBLE | GLUT_3_2_CORE_PROFILE);
#else
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_3_2_CORE_PROFILE);
#endif
glutCreateWindow("Mio");
gl3wInit();
warn("OpenGL %s; GLSL %s", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
warn("Screen %dx%d at %ddpi (scale %g)", wpx, hpx, dpi, dpi_scale);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutSpaceballMotionFunc(spacemotion);
glutSpaceballRotateFunc(spacerotate);
glEnable(GL_FRAMEBUFFER_SRGB);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
register_directory("data/");
register_directory("data/textures/");
font_sans = load_font("fonts/SourceSansPro-Regular.ttf");
if (!font_sans)
exit(1);
font_mono = load_font("fonts/SourceCodePro-Regular.ttf");
if (!font_mono)
exit(1);
init_lua();
run_string("require 'strict'");
run_file("proxy.lua");
for (i = 1; i < argc; i++)
run_file(argv[i]);
glutMainLoop();
return 0;
}