rev |
line source |
nuclear@2
|
1 #include <stdio.h>
|
nuclear@2
|
2 #include <assert.h>
|
nuclear@6
|
3 #include <vector>
|
nuclear@2
|
4 #include <algorithm>
|
nuclear@4
|
5 #include <vmath/vmath.h>
|
nuclear@2
|
6 #include "opengl.h"
|
nuclear@2
|
7 #include "game.h"
|
nuclear@2
|
8 #include "goatvr.h"
|
nuclear@3
|
9 #include "teapot.h"
|
nuclear@5
|
10 #include "console.h"
|
nuclear@5
|
11 #include "drawtext.h"
|
nuclear@7
|
12 #include "game_var.h"
|
nuclear@8
|
13 #include "scenefile.h"
|
nuclear@2
|
14
|
nuclear@2
|
15 static void draw_scene();
|
nuclear@4
|
16 static void material(float r, float g, float b, float roughness);
|
nuclear@2
|
17 static void toggle_hmd_fullscr();
|
nuclear@2
|
18 static void create_rtarg(int x, int y);
|
nuclear@2
|
19 static int next_pow2(int x);
|
nuclear@6
|
20 static void con_handle(const char *cmd);
|
nuclear@2
|
21
|
nuclear@2
|
22 static int win_width, win_height;
|
nuclear@2
|
23 static unsigned int fb_tex;
|
nuclear@2
|
24 static unsigned int fbo, fb_depth;
|
nuclear@2
|
25 static int fb_xsz, fb_ysz;
|
nuclear@2
|
26 static int fb_tex_xsz, fb_tex_ysz;
|
nuclear@2
|
27
|
nuclear@5
|
28 static unsigned int chess_tex;
|
nuclear@5
|
29
|
nuclear@8
|
30 static float cam_theta = -90, cam_phi;
|
nuclear@8
|
31 static Vector3 cam_pos = {15.4, 0, 0};
|
nuclear@4
|
32 static bool keystate[256];
|
nuclear@4
|
33
|
nuclear@5
|
34 static Console con;
|
nuclear@5
|
35 static dtx_font *con_font;
|
nuclear@5
|
36
|
nuclear@8
|
37 static SceneFile scn;
|
nuclear@8
|
38
|
nuclear@8
|
39 #define GVAR_LOOK_WALK "look-walk"
|
nuclear@8
|
40 #define GVAR_MAX_STEP "max-step"
|
nuclear@7
|
41
|
nuclear@2
|
42 bool game_init()
|
nuclear@2
|
43 {
|
nuclear@2
|
44 init_opengl();
|
nuclear@2
|
45
|
nuclear@8
|
46 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
|
nuclear@8
|
47 fprintf(stderr, "failed to open console font\n");
|
nuclear@8
|
48 return false;
|
nuclear@8
|
49 }
|
nuclear@8
|
50 con.set_font(con_font, 14);
|
nuclear@8
|
51 con.set_size(7, 52);
|
nuclear@8
|
52 con.set_position(0, 0, Console::CENTER);
|
nuclear@8
|
53 con.set_command_func(con_handle);
|
nuclear@8
|
54 con.set_echo(false);
|
nuclear@8
|
55
|
nuclear@8
|
56
|
nuclear@2
|
57 if(vr_init() == -1) {
|
nuclear@2
|
58 return false;
|
nuclear@2
|
59 }
|
nuclear@2
|
60
|
nuclear@2
|
61 glEnable(GL_DEPTH_TEST);
|
nuclear@2
|
62 glEnable(GL_CULL_FACE);
|
nuclear@3
|
63 glEnable(GL_LIGHTING);
|
nuclear@2
|
64
|
nuclear@5
|
65 unsigned char chess_pixels[] = {
|
nuclear@5
|
66 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
|
nuclear@5
|
67 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
|
nuclear@5
|
68 };
|
nuclear@5
|
69 glGenTextures(1, &chess_tex);
|
nuclear@5
|
70 glBindTexture(GL_TEXTURE_2D, chess_tex);
|
nuclear@5
|
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@5
|
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@5
|
73 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
|
nuclear@5
|
74
|
nuclear@8
|
75 if(!scn.load("data/sibenik.goatsce")) {
|
nuclear@8
|
76 fprintf(stderr, "failed to load scene file\n");
|
nuclear@5
|
77 return false;
|
nuclear@5
|
78 }
|
nuclear@7
|
79
|
nuclear@7
|
80 /* initialize all option variables */
|
nuclear@8
|
81 set_gvar_bool(GVAR_LOOK_WALK, true);
|
nuclear@8
|
82 set_gvar_num(GVAR_MAX_STEP, 0.5);
|
nuclear@5
|
83
|
nuclear@2
|
84 return true;
|
nuclear@2
|
85 }
|
nuclear@2
|
86
|
nuclear@2
|
87 void game_cleanup()
|
nuclear@2
|
88 {
|
nuclear@2
|
89 vr_shutdown();
|
nuclear@2
|
90 }
|
nuclear@2
|
91
|
nuclear@2
|
92 void game_update(long tm)
|
nuclear@2
|
93 {
|
nuclear@4
|
94 static long prev_upd;
|
nuclear@4
|
95 float dt = (tm - prev_upd) / 1000.0;
|
nuclear@4
|
96 prev_upd = tm;
|
nuclear@4
|
97
|
nuclear@5
|
98 if(con.is_visible()) {
|
nuclear@5
|
99 con.update();
|
nuclear@5
|
100 return;
|
nuclear@5
|
101 }
|
nuclear@5
|
102
|
nuclear@5
|
103 float offs = dt * 5.0;
|
nuclear@4
|
104 Vector3 dir;
|
nuclear@4
|
105
|
nuclear@4
|
106 if(keystate['d'] || keystate['D']) {
|
nuclear@4
|
107 dir += Vector3(offs, 0, 0);
|
nuclear@4
|
108 }
|
nuclear@4
|
109 if(keystate['a'] || keystate['A']) {
|
nuclear@4
|
110 dir += Vector3(-offs, 0, 0);
|
nuclear@4
|
111 }
|
nuclear@4
|
112 if(keystate['w'] || keystate['W']) {
|
nuclear@4
|
113 dir += Vector3(0, 0, -offs);
|
nuclear@4
|
114 }
|
nuclear@4
|
115 if(keystate['s'] || keystate['S']) {
|
nuclear@4
|
116 dir += Vector3(0, 0, offs);
|
nuclear@4
|
117 }
|
nuclear@4
|
118
|
nuclear@8
|
119 if(get_gvar_bool(GVAR_LOOK_WALK)) {
|
nuclear@7
|
120 float rot[4];
|
nuclear@7
|
121 vr_view_rotation(0, rot);
|
nuclear@7
|
122 Quaternion q(rot[3], rot[0], rot[1], rot[2]);
|
nuclear@7
|
123
|
nuclear@7
|
124 dir.transform(q);
|
nuclear@7
|
125 }
|
nuclear@7
|
126
|
nuclear@5
|
127 float cos_theta = cos(DEG_TO_RAD(cam_theta));
|
nuclear@5
|
128 float sin_theta = sin(DEG_TO_RAD(cam_theta));
|
nuclear@5
|
129 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
|
nuclear@5
|
130 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
|
nuclear@5
|
131
|
nuclear@8
|
132 float max_step = get_gvar_num(GVAR_MAX_STEP);
|
nuclear@8
|
133 cam_pos = scn.find_walk_pos(cam_pos + Vector3(0, max_step, 0));
|
nuclear@2
|
134 }
|
nuclear@2
|
135
|
nuclear@2
|
136 void game_display()
|
nuclear@2
|
137 {
|
nuclear@2
|
138 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@2
|
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
140
|
nuclear@2
|
141 for(int i=0; i<2; i++) {
|
nuclear@2
|
142 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
|
nuclear@2
|
143 vr_begin(i);
|
nuclear@2
|
144
|
nuclear@2
|
145 float proj[16];
|
nuclear@2
|
146 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
|
nuclear@2
|
147 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
148 glLoadMatrixf(proj);
|
nuclear@2
|
149 }
|
nuclear@2
|
150
|
nuclear@2
|
151 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
152
|
nuclear@2
|
153 float view[16];
|
nuclear@2
|
154 vr_view_matrix(i, view);
|
nuclear@2
|
155 glLoadMatrixf(view);
|
nuclear@4
|
156 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@4
|
157 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@2
|
158 /* move the camera to the eye level of the user */
|
nuclear@2
|
159 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
|
nuclear@4
|
160 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
|
nuclear@2
|
161
|
nuclear@2
|
162 draw_scene();
|
nuclear@5
|
163 con.draw();
|
nuclear@2
|
164
|
nuclear@2
|
165 vr_end();
|
nuclear@2
|
166 }
|
nuclear@2
|
167
|
nuclear@2
|
168 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@2
|
169 glViewport(0, 0, win_width, win_height);
|
nuclear@2
|
170
|
nuclear@2
|
171 vr_swap_buffers();
|
nuclear@2
|
172 assert(glGetError() == GL_NO_ERROR);
|
nuclear@2
|
173 }
|
nuclear@2
|
174
|
nuclear@2
|
175 void game_reshape(int x, int y)
|
nuclear@2
|
176 {
|
nuclear@2
|
177 win_width = x;
|
nuclear@2
|
178 win_height = y;
|
nuclear@2
|
179
|
nuclear@5
|
180 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
|
nuclear@2
|
181 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
|
nuclear@2
|
182
|
nuclear@2
|
183 /* these might be overriden in VR mode (see game_display) */
|
nuclear@2
|
184 glViewport(0, 0, x, y);
|
nuclear@2
|
185 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
186 glLoadIdentity();
|
nuclear@2
|
187 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
|
nuclear@2
|
188 }
|
nuclear@2
|
189
|
nuclear@2
|
190 void game_keyboard(int key, bool pressed)
|
nuclear@2
|
191 {
|
nuclear@2
|
192 if(pressed) {
|
nuclear@5
|
193 if(con.is_visible()) {
|
nuclear@5
|
194 if(key == 27 || key == '`') {
|
nuclear@5
|
195 con.hide();
|
nuclear@5
|
196 } else {
|
nuclear@5
|
197 con.input_key(key);
|
nuclear@5
|
198 }
|
nuclear@5
|
199 } else {
|
nuclear@5
|
200 switch(key) {
|
nuclear@5
|
201 case 27:
|
nuclear@5
|
202 exit_game();
|
nuclear@5
|
203 break;
|
nuclear@2
|
204
|
nuclear@5
|
205 case 'f':
|
nuclear@5
|
206 toggle_hmd_fullscr();
|
nuclear@5
|
207 break;
|
nuclear@2
|
208
|
nuclear@5
|
209 case 'r':
|
nuclear@5
|
210 vr_recenter();
|
nuclear@5
|
211 break;
|
nuclear@2
|
212
|
nuclear@5
|
213 case '`':
|
nuclear@5
|
214 con.show();
|
nuclear@5
|
215 break;
|
nuclear@5
|
216
|
nuclear@5
|
217 default:
|
nuclear@5
|
218 break;
|
nuclear@5
|
219 }
|
nuclear@2
|
220 }
|
nuclear@2
|
221 }
|
nuclear@4
|
222
|
nuclear@4
|
223 if(key < 256) {
|
nuclear@4
|
224 keystate[key] = pressed;
|
nuclear@4
|
225 }
|
nuclear@2
|
226 }
|
nuclear@2
|
227
|
nuclear@4
|
228
|
nuclear@4
|
229 static int prev_x, prev_y;
|
nuclear@4
|
230 static bool bnstate[32];
|
nuclear@4
|
231
|
nuclear@2
|
232 void game_mouse_button(int bn, bool state, int x, int y)
|
nuclear@2
|
233 {
|
nuclear@4
|
234 bnstate[bn] = state;
|
nuclear@4
|
235 prev_x = x;
|
nuclear@4
|
236 prev_y = y;
|
nuclear@2
|
237 }
|
nuclear@2
|
238
|
nuclear@2
|
239 void game_mouse_motion(int x, int y)
|
nuclear@2
|
240 {
|
nuclear@4
|
241 int dx = x - prev_x;
|
nuclear@4
|
242 int dy = y - prev_y;
|
nuclear@4
|
243 prev_x = x;
|
nuclear@4
|
244 prev_y = y;
|
nuclear@2
|
245
|
nuclear@4
|
246 if(!dx && !dy) {
|
nuclear@4
|
247 return;
|
nuclear@4
|
248 }
|
nuclear@4
|
249 if(bnstate[0]) {
|
nuclear@4
|
250 cam_theta += dx * 0.5;
|
nuclear@4
|
251 cam_phi += dy * 0.5;
|
nuclear@3
|
252
|
nuclear@4
|
253 if(cam_phi < -90) cam_phi = -90;
|
nuclear@4
|
254 if(cam_phi > 90) cam_phi = 90;
|
nuclear@4
|
255 }
|
nuclear@3
|
256 }
|
nuclear@3
|
257
|
nuclear@8
|
258 MeshFace dbg_walk_face;
|
nuclear@8
|
259
|
nuclear@2
|
260 static void draw_scene()
|
nuclear@2
|
261 {
|
nuclear@3
|
262 float lpos[][4] = {
|
nuclear@3
|
263 {-0.7, 0.7, 1, 0},
|
nuclear@3
|
264 {1, 0, 1, 0}
|
nuclear@3
|
265 };
|
nuclear@3
|
266 float lcol[][4] = {
|
nuclear@3
|
267 {0.9, 0.7, 0.6, 1},
|
nuclear@3
|
268 {0.3, 0.4, 0.75, 1}
|
nuclear@3
|
269 };
|
nuclear@3
|
270 for(int i=0; i<2; i++) {
|
nuclear@3
|
271 glEnable(GL_LIGHT0 + i);
|
nuclear@3
|
272 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
|
nuclear@3
|
273 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
|
nuclear@3
|
274 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
|
nuclear@3
|
275 }
|
nuclear@3
|
276
|
nuclear@3
|
277 glMatrixMode(GL_MODELVIEW);
|
nuclear@3
|
278
|
nuclear@8
|
279 /*
|
nuclear@5
|
280 glBindTexture(GL_TEXTURE_2D, chess_tex);
|
nuclear@5
|
281 glEnable(GL_TEXTURE_2D);
|
nuclear@3
|
282 material(1, 1, 1, 1);
|
nuclear@3
|
283 glBegin(GL_QUADS);
|
nuclear@3
|
284 glNormal3f(0, 1, 0);
|
nuclear@5
|
285 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
|
nuclear@5
|
286 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
|
nuclear@5
|
287 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
|
nuclear@5
|
288 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
|
nuclear@3
|
289 glEnd();
|
nuclear@5
|
290 glDisable(GL_TEXTURE_2D);
|
nuclear@8
|
291 */
|
nuclear@3
|
292
|
nuclear@8
|
293 /*material(1, 1, 1, 0.4);
|
nuclear@3
|
294 glPushMatrix();
|
nuclear@3
|
295 glTranslatef(0, 1.3, -10);
|
nuclear@3
|
296 glFrontFace(GL_CW);
|
nuclear@3
|
297 bezier_teapot(2.0);
|
nuclear@3
|
298 glFrontFace(GL_CCW);
|
nuclear@8
|
299 glPopMatrix();*/
|
nuclear@8
|
300
|
nuclear@8
|
301 for(size_t i=0; i<scn.obj.size(); i++) {
|
nuclear@8
|
302 scn.obj[i]->draw();
|
nuclear@8
|
303 }
|
nuclear@8
|
304
|
nuclear@8
|
305 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@8
|
306 glDisable(GL_DEPTH_TEST);
|
nuclear@8
|
307 glDisable(GL_LIGHTING);
|
nuclear@8
|
308
|
nuclear@8
|
309 glBegin(GL_TRIANGLES);
|
nuclear@8
|
310 glColor3f(0, 1, 0);
|
nuclear@8
|
311 glVertex3fv(&dbg_walk_face.v[0].x);
|
nuclear@8
|
312 glVertex3fv(&dbg_walk_face.v[1].x);
|
nuclear@8
|
313 glVertex3fv(&dbg_walk_face.v[2].x);
|
nuclear@8
|
314 glColor3f(1, 1, 1);
|
nuclear@8
|
315 glEnd();
|
nuclear@8
|
316
|
nuclear@8
|
317 glPopAttrib();
|
nuclear@2
|
318 }
|
nuclear@2
|
319
|
nuclear@4
|
320 static void material(float r, float g, float b, float roughness)
|
nuclear@4
|
321 {
|
nuclear@4
|
322 float gloss = 1.0 - roughness;
|
nuclear@4
|
323 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
|
nuclear@4
|
324 float specular[] = {gloss, gloss, gloss, 1.0};
|
nuclear@4
|
325 float shin = gloss * 128.0;
|
nuclear@4
|
326
|
nuclear@4
|
327 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
|
nuclear@4
|
328 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
|
nuclear@4
|
329 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
|
nuclear@4
|
330 }
|
nuclear@4
|
331
|
nuclear@4
|
332
|
nuclear@2
|
333 static void toggle_hmd_fullscr()
|
nuclear@2
|
334 {
|
nuclear@2
|
335 static bool fullscr;
|
nuclear@2
|
336 static int prev_x, prev_y;
|
nuclear@2
|
337 //static int prev_xsz, prev_ysz;
|
nuclear@2
|
338
|
nuclear@2
|
339 fullscr = !fullscr;
|
nuclear@2
|
340 if(fullscr) {
|
nuclear@2
|
341 /* entering fullscreen on the HMD */
|
nuclear@2
|
342 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
|
nuclear@2
|
343 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
|
nuclear@2
|
344 if(xoffs != -1) {
|
nuclear@2
|
345 get_window_pos(&prev_x, &prev_y);
|
nuclear@2
|
346 move_window(xoffs, yoffs);
|
nuclear@2
|
347 }
|
nuclear@2
|
348
|
nuclear@2
|
349 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
|
nuclear@2
|
350 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
|
nuclear@2
|
351 if(xsz != -1) {
|
nuclear@2
|
352 //prev_xsz = win_width;
|
nuclear@2
|
353 //prev_ysz = win_height;
|
nuclear@2
|
354 resize_window(xsz, ysz);
|
nuclear@2
|
355 }
|
nuclear@2
|
356 enter_fullscreen();
|
nuclear@2
|
357
|
nuclear@2
|
358 } else {
|
nuclear@2
|
359 /* leaving fullscreen */
|
nuclear@2
|
360 leave_fullscreen();
|
nuclear@2
|
361 /*move_window(prev_x, prev_y);
|
nuclear@2
|
362 resize_window(prev_xsz, prev_ysz);*/
|
nuclear@2
|
363 }
|
nuclear@2
|
364 }
|
nuclear@2
|
365
|
nuclear@2
|
366 static void create_rtarg(int x, int y)
|
nuclear@2
|
367 {
|
nuclear@2
|
368 if(x == fb_xsz && y == fb_ysz) {
|
nuclear@2
|
369 return; // nothing changed
|
nuclear@2
|
370 }
|
nuclear@2
|
371
|
nuclear@2
|
372 fb_xsz = x;
|
nuclear@2
|
373 fb_ysz = y;
|
nuclear@2
|
374 fb_tex_xsz = next_pow2(fb_xsz);
|
nuclear@2
|
375 fb_tex_ysz = next_pow2(fb_ysz);
|
nuclear@2
|
376
|
nuclear@5
|
377 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
|
nuclear@5
|
378
|
nuclear@2
|
379 if(!fbo) {
|
nuclear@2
|
380 glGenFramebuffers(1, &fbo);
|
nuclear@2
|
381
|
nuclear@2
|
382 glGenTextures(1, &fb_tex);
|
nuclear@2
|
383 glBindTexture(GL_TEXTURE_2D, fb_tex);
|
nuclear@2
|
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@2
|
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@2
|
386
|
nuclear@2
|
387 glGenRenderbuffers(1, &fb_depth);
|
nuclear@2
|
388 }
|
nuclear@2
|
389
|
nuclear@2
|
390 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@2
|
391
|
nuclear@2
|
392 glBindTexture(GL_TEXTURE_2D, fb_tex);
|
nuclear@2
|
393 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
nuclear@2
|
394 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
|
nuclear@2
|
395
|
nuclear@2
|
396 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
|
nuclear@2
|
397 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
|
nuclear@2
|
398 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
|
nuclear@2
|
399
|
nuclear@2
|
400 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
nuclear@2
|
401 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@2
|
402 }
|
nuclear@2
|
403
|
nuclear@2
|
404 static int next_pow2(int x)
|
nuclear@2
|
405 {
|
nuclear@2
|
406 x -= 1;
|
nuclear@2
|
407 x |= x >> 1;
|
nuclear@2
|
408 x |= x >> 2;
|
nuclear@2
|
409 x |= x >> 4;
|
nuclear@2
|
410 x |= x >> 8;
|
nuclear@2
|
411 x |= x >> 16;
|
nuclear@2
|
412 return x + 1;
|
nuclear@2
|
413 }
|
nuclear@6
|
414
|
nuclear@6
|
415 static const char *strip_spaces(const char *str)
|
nuclear@6
|
416 {
|
nuclear@6
|
417 while(*str && isspace(*str)) str++;
|
nuclear@6
|
418 return str;
|
nuclear@6
|
419 }
|
nuclear@6
|
420
|
nuclear@7
|
421 struct Variable {
|
nuclear@7
|
422 const char *var;
|
nuclear@7
|
423 enum { BOOL, NUM } type;
|
nuclear@7
|
424 bool bool_val;
|
nuclear@7
|
425 float num_val;
|
nuclear@7
|
426 };
|
nuclear@7
|
427
|
nuclear@6
|
428 static void con_handle(const char *cmd)
|
nuclear@6
|
429 {
|
nuclear@8
|
430 /* set debug values */
|
nuclear@8
|
431 set_gvar_num("cam-pos-x", cam_pos[0]);
|
nuclear@8
|
432 set_gvar_num("cam-pos-y", cam_pos[1]);
|
nuclear@8
|
433 set_gvar_num("cam-pos-z", cam_pos[2]);
|
nuclear@8
|
434
|
nuclear@6
|
435 std::vector<char*> argv;
|
nuclear@6
|
436 cmd = strip_spaces(cmd);
|
nuclear@6
|
437
|
nuclear@6
|
438 if(!*cmd || *cmd == '#') {
|
nuclear@6
|
439 return;
|
nuclear@6
|
440 }
|
nuclear@6
|
441
|
nuclear@6
|
442 char *line = (char*)alloca(strlen(cmd) + 1);
|
nuclear@6
|
443 strcpy(line, cmd);
|
nuclear@6
|
444
|
nuclear@6
|
445 char *tok = 0;
|
nuclear@6
|
446 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
|
nuclear@6
|
447 argv.push_back(tok);
|
nuclear@6
|
448 }
|
nuclear@6
|
449
|
nuclear@6
|
450 if(strcmp(argv[0], "set") == 0) {
|
nuclear@6
|
451 if(argv.size() < 3) {
|
nuclear@7
|
452 con.printf("set must be followed by 2 arguments\n");
|
nuclear@6
|
453 return;
|
nuclear@6
|
454 }
|
nuclear@7
|
455 if(have_gvar(argv[1])) {
|
nuclear@7
|
456 GameVariable &gv = get_gvar(argv[1]);
|
nuclear@7
|
457 con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
|
nuclear@6
|
458 } else {
|
nuclear@7
|
459 con.printf("%s->%s\n", argv[1], argv[2]);
|
nuclear@6
|
460 }
|
nuclear@7
|
461 set_gvar_parse(argv[1], argv[2]);
|
nuclear@7
|
462
|
nuclear@7
|
463 } else if(strcmp(argv[0], "get") == 0) {
|
nuclear@7
|
464 if(argv.size() < 2) {
|
nuclear@7
|
465 con.printf("get must be followed by the variable name\n");
|
nuclear@7
|
466 return;
|
nuclear@7
|
467 }
|
nuclear@7
|
468 if(have_gvar(argv[1])) {
|
nuclear@7
|
469 GameVariable &gv = get_gvar(argv[1]);
|
nuclear@7
|
470 con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
|
nuclear@7
|
471 }
|
nuclear@7
|
472
|
nuclear@7
|
473 } else if(strcmp(argv[0], "vars") == 0) {
|
nuclear@7
|
474 std::list<std::string> vars = get_gvar_list();
|
nuclear@7
|
475 std::list<std::string>::const_iterator it = vars.begin();
|
nuclear@7
|
476 while(it != vars.end()) {
|
nuclear@7
|
477 con.printf(" %s\n", it++->c_str());
|
nuclear@7
|
478 }
|
nuclear@7
|
479
|
nuclear@6
|
480 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
|
nuclear@6
|
481 exit_game();
|
nuclear@7
|
482
|
nuclear@7
|
483 } else if(strcmp(argv[0], "help") == 0) {
|
nuclear@7
|
484 con.printf("available commands:\n");
|
nuclear@7
|
485 con.printf(" set <var> <val> set value to a variable\n");
|
nuclear@7
|
486 con.printf(" get <var> print the value of a variable\n");
|
nuclear@7
|
487 con.printf(" vars print a list of all variables\n");
|
nuclear@7
|
488 con.printf(" help print this help screen\n");
|
nuclear@7
|
489 con.printf(" exit or quit quit the program\n");
|
nuclear@7
|
490
|
nuclear@6
|
491 } else {
|
nuclear@6
|
492 con.printf("invalid command: %s\n", argv[0]);
|
nuclear@6
|
493 }
|
nuclear@6
|
494 }
|