rev |
line source |
nuclear@2
|
1 #include <stdio.h>
|
nuclear@2
|
2 #include <assert.h>
|
nuclear@2
|
3 #include <algorithm>
|
nuclear@4
|
4 #include <vmath/vmath.h>
|
nuclear@2
|
5 #include "opengl.h"
|
nuclear@2
|
6 #include "game.h"
|
nuclear@2
|
7 #include "goatvr.h"
|
nuclear@3
|
8 #include "teapot.h"
|
nuclear@5
|
9 #include "console.h"
|
nuclear@5
|
10 #include "drawtext.h"
|
nuclear@2
|
11
|
nuclear@2
|
12 static void draw_scene();
|
nuclear@4
|
13 static void material(float r, float g, float b, float roughness);
|
nuclear@2
|
14 static void toggle_hmd_fullscr();
|
nuclear@2
|
15 static void create_rtarg(int x, int y);
|
nuclear@2
|
16 static int next_pow2(int x);
|
nuclear@2
|
17
|
nuclear@5
|
18 bool opt_separate_walk_look;
|
nuclear@5
|
19
|
nuclear@2
|
20 static int win_width, win_height;
|
nuclear@2
|
21 static unsigned int fb_tex;
|
nuclear@2
|
22 static unsigned int fbo, fb_depth;
|
nuclear@2
|
23 static int fb_xsz, fb_ysz;
|
nuclear@2
|
24 static int fb_tex_xsz, fb_tex_ysz;
|
nuclear@2
|
25
|
nuclear@5
|
26 static unsigned int chess_tex;
|
nuclear@5
|
27
|
nuclear@4
|
28 static float cam_theta, cam_phi;
|
nuclear@4
|
29 static Vector3 cam_pos;
|
nuclear@4
|
30 static bool keystate[256];
|
nuclear@4
|
31
|
nuclear@5
|
32 static Console con;
|
nuclear@5
|
33 static dtx_font *con_font;
|
nuclear@5
|
34
|
nuclear@2
|
35 bool game_init()
|
nuclear@2
|
36 {
|
nuclear@2
|
37 init_opengl();
|
nuclear@2
|
38
|
nuclear@2
|
39 if(vr_init() == -1) {
|
nuclear@2
|
40 return false;
|
nuclear@2
|
41 }
|
nuclear@2
|
42
|
nuclear@2
|
43 glEnable(GL_DEPTH_TEST);
|
nuclear@2
|
44 glEnable(GL_CULL_FACE);
|
nuclear@3
|
45 glEnable(GL_LIGHTING);
|
nuclear@2
|
46
|
nuclear@5
|
47 unsigned char chess_pixels[] = {
|
nuclear@5
|
48 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
|
nuclear@5
|
49 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
|
nuclear@5
|
50 };
|
nuclear@5
|
51 glGenTextures(1, &chess_tex);
|
nuclear@5
|
52 glBindTexture(GL_TEXTURE_2D, chess_tex);
|
nuclear@5
|
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@5
|
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@5
|
55 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
|
nuclear@5
|
56
|
nuclear@5
|
57 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
|
nuclear@5
|
58 fprintf(stderr, "failed to open console font\n");
|
nuclear@5
|
59 return false;
|
nuclear@5
|
60 }
|
nuclear@5
|
61 con.set_size(6, 40);
|
nuclear@5
|
62 con.set_font(con_font, 14);
|
nuclear@5
|
63 con.set_position(0, 0, Console::CENTER);
|
nuclear@5
|
64
|
nuclear@2
|
65 return true;
|
nuclear@2
|
66 }
|
nuclear@2
|
67
|
nuclear@2
|
68 void game_cleanup()
|
nuclear@2
|
69 {
|
nuclear@2
|
70 vr_shutdown();
|
nuclear@2
|
71 }
|
nuclear@2
|
72
|
nuclear@2
|
73 void game_update(long tm)
|
nuclear@2
|
74 {
|
nuclear@4
|
75 static long prev_upd;
|
nuclear@4
|
76 float dt = (tm - prev_upd) / 1000.0;
|
nuclear@4
|
77 prev_upd = tm;
|
nuclear@4
|
78
|
nuclear@5
|
79 if(con.is_visible()) {
|
nuclear@5
|
80 con.update();
|
nuclear@5
|
81 return;
|
nuclear@5
|
82 }
|
nuclear@5
|
83
|
nuclear@5
|
84 float offs = dt * 5.0;
|
nuclear@4
|
85 Vector3 dir;
|
nuclear@4
|
86
|
nuclear@4
|
87 if(keystate['d'] || keystate['D']) {
|
nuclear@4
|
88 dir += Vector3(offs, 0, 0);
|
nuclear@4
|
89 }
|
nuclear@4
|
90 if(keystate['a'] || keystate['A']) {
|
nuclear@4
|
91 dir += Vector3(-offs, 0, 0);
|
nuclear@4
|
92 }
|
nuclear@4
|
93 if(keystate['w'] || keystate['W']) {
|
nuclear@4
|
94 dir += Vector3(0, 0, -offs);
|
nuclear@4
|
95 }
|
nuclear@4
|
96 if(keystate['s'] || keystate['S']) {
|
nuclear@4
|
97 dir += Vector3(0, 0, offs);
|
nuclear@4
|
98 }
|
nuclear@4
|
99
|
nuclear@5
|
100 float cos_theta = cos(DEG_TO_RAD(cam_theta));
|
nuclear@5
|
101 float sin_theta = sin(DEG_TO_RAD(cam_theta));
|
nuclear@5
|
102 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
|
nuclear@5
|
103 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
|
nuclear@5
|
104
|
nuclear@5
|
105 if(!opt_separate_walk_look) {
|
nuclear@5
|
106 }
|
nuclear@2
|
107 }
|
nuclear@2
|
108
|
nuclear@2
|
109 void game_display()
|
nuclear@2
|
110 {
|
nuclear@2
|
111 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@2
|
112 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
113
|
nuclear@2
|
114 for(int i=0; i<2; i++) {
|
nuclear@2
|
115 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
|
nuclear@2
|
116 vr_begin(i);
|
nuclear@2
|
117
|
nuclear@2
|
118 float proj[16];
|
nuclear@2
|
119 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
|
nuclear@2
|
120 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
121 glLoadMatrixf(proj);
|
nuclear@2
|
122 }
|
nuclear@2
|
123
|
nuclear@2
|
124 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
125
|
nuclear@2
|
126 float view[16];
|
nuclear@2
|
127 vr_view_matrix(i, view);
|
nuclear@2
|
128 glLoadMatrixf(view);
|
nuclear@4
|
129 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@4
|
130 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@2
|
131 /* move the camera to the eye level of the user */
|
nuclear@2
|
132 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
|
nuclear@4
|
133 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
|
nuclear@2
|
134
|
nuclear@2
|
135 draw_scene();
|
nuclear@5
|
136 con.draw();
|
nuclear@2
|
137
|
nuclear@2
|
138 vr_end();
|
nuclear@2
|
139 }
|
nuclear@2
|
140
|
nuclear@2
|
141 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@2
|
142 glViewport(0, 0, win_width, win_height);
|
nuclear@2
|
143
|
nuclear@2
|
144 vr_swap_buffers();
|
nuclear@2
|
145 assert(glGetError() == GL_NO_ERROR);
|
nuclear@2
|
146 }
|
nuclear@2
|
147
|
nuclear@2
|
148 void game_reshape(int x, int y)
|
nuclear@2
|
149 {
|
nuclear@2
|
150 win_width = x;
|
nuclear@2
|
151 win_height = y;
|
nuclear@2
|
152
|
nuclear@5
|
153 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
|
nuclear@2
|
154 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
|
nuclear@2
|
155
|
nuclear@2
|
156 /* these might be overriden in VR mode (see game_display) */
|
nuclear@2
|
157 glViewport(0, 0, x, y);
|
nuclear@2
|
158 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
159 glLoadIdentity();
|
nuclear@2
|
160 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
|
nuclear@2
|
161 }
|
nuclear@2
|
162
|
nuclear@2
|
163 void game_keyboard(int key, bool pressed)
|
nuclear@2
|
164 {
|
nuclear@2
|
165 if(pressed) {
|
nuclear@5
|
166 if(con.is_visible()) {
|
nuclear@5
|
167 if(key == 27 || key == '`') {
|
nuclear@5
|
168 con.hide();
|
nuclear@5
|
169 } else {
|
nuclear@5
|
170 con.input_key(key);
|
nuclear@5
|
171 }
|
nuclear@5
|
172 } else {
|
nuclear@5
|
173 switch(key) {
|
nuclear@5
|
174 case 27:
|
nuclear@5
|
175 exit_game();
|
nuclear@5
|
176 break;
|
nuclear@2
|
177
|
nuclear@5
|
178 case 'f':
|
nuclear@5
|
179 toggle_hmd_fullscr();
|
nuclear@5
|
180 break;
|
nuclear@2
|
181
|
nuclear@5
|
182 case 'r':
|
nuclear@5
|
183 vr_recenter();
|
nuclear@5
|
184 break;
|
nuclear@2
|
185
|
nuclear@5
|
186 case '`':
|
nuclear@5
|
187 con.show();
|
nuclear@5
|
188 break;
|
nuclear@5
|
189
|
nuclear@5
|
190 default:
|
nuclear@5
|
191 break;
|
nuclear@5
|
192 }
|
nuclear@2
|
193 }
|
nuclear@2
|
194 }
|
nuclear@4
|
195
|
nuclear@4
|
196 if(key < 256) {
|
nuclear@4
|
197 keystate[key] = pressed;
|
nuclear@4
|
198 }
|
nuclear@2
|
199 }
|
nuclear@2
|
200
|
nuclear@4
|
201
|
nuclear@4
|
202 static int prev_x, prev_y;
|
nuclear@4
|
203 static bool bnstate[32];
|
nuclear@4
|
204
|
nuclear@2
|
205 void game_mouse_button(int bn, bool state, int x, int y)
|
nuclear@2
|
206 {
|
nuclear@4
|
207 bnstate[bn] = state;
|
nuclear@4
|
208 prev_x = x;
|
nuclear@4
|
209 prev_y = y;
|
nuclear@2
|
210 }
|
nuclear@2
|
211
|
nuclear@2
|
212 void game_mouse_motion(int x, int y)
|
nuclear@2
|
213 {
|
nuclear@4
|
214 int dx = x - prev_x;
|
nuclear@4
|
215 int dy = y - prev_y;
|
nuclear@4
|
216 prev_x = x;
|
nuclear@4
|
217 prev_y = y;
|
nuclear@2
|
218
|
nuclear@4
|
219 if(!dx && !dy) {
|
nuclear@4
|
220 return;
|
nuclear@4
|
221 }
|
nuclear@4
|
222 if(bnstate[0]) {
|
nuclear@4
|
223 cam_theta += dx * 0.5;
|
nuclear@4
|
224 cam_phi += dy * 0.5;
|
nuclear@3
|
225
|
nuclear@4
|
226 if(cam_phi < -90) cam_phi = -90;
|
nuclear@4
|
227 if(cam_phi > 90) cam_phi = 90;
|
nuclear@4
|
228 }
|
nuclear@3
|
229 }
|
nuclear@3
|
230
|
nuclear@2
|
231 static void draw_scene()
|
nuclear@2
|
232 {
|
nuclear@3
|
233 float lpos[][4] = {
|
nuclear@3
|
234 {-0.7, 0.7, 1, 0},
|
nuclear@3
|
235 {1, 0, 1, 0}
|
nuclear@3
|
236 };
|
nuclear@3
|
237 float lcol[][4] = {
|
nuclear@3
|
238 {0.9, 0.7, 0.6, 1},
|
nuclear@3
|
239 {0.3, 0.4, 0.75, 1}
|
nuclear@3
|
240 };
|
nuclear@3
|
241 for(int i=0; i<2; i++) {
|
nuclear@3
|
242 glEnable(GL_LIGHT0 + i);
|
nuclear@3
|
243 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
|
nuclear@3
|
244 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
|
nuclear@3
|
245 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
|
nuclear@3
|
246 }
|
nuclear@3
|
247
|
nuclear@3
|
248 glMatrixMode(GL_MODELVIEW);
|
nuclear@3
|
249
|
nuclear@5
|
250 glBindTexture(GL_TEXTURE_2D, chess_tex);
|
nuclear@5
|
251 glEnable(GL_TEXTURE_2D);
|
nuclear@3
|
252 material(1, 1, 1, 1);
|
nuclear@3
|
253 glBegin(GL_QUADS);
|
nuclear@3
|
254 glNormal3f(0, 1, 0);
|
nuclear@5
|
255 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
|
nuclear@5
|
256 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
|
nuclear@5
|
257 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
|
nuclear@5
|
258 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
|
nuclear@3
|
259 glEnd();
|
nuclear@5
|
260 glDisable(GL_TEXTURE_2D);
|
nuclear@3
|
261
|
nuclear@3
|
262 material(1, 1, 1, 0.4);
|
nuclear@3
|
263 glPushMatrix();
|
nuclear@3
|
264 glTranslatef(0, 1.3, -10);
|
nuclear@3
|
265 glFrontFace(GL_CW);
|
nuclear@3
|
266 bezier_teapot(2.0);
|
nuclear@3
|
267 glFrontFace(GL_CCW);
|
nuclear@3
|
268 glPopMatrix();
|
nuclear@2
|
269 }
|
nuclear@2
|
270
|
nuclear@4
|
271 static void material(float r, float g, float b, float roughness)
|
nuclear@4
|
272 {
|
nuclear@4
|
273 float gloss = 1.0 - roughness;
|
nuclear@4
|
274 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
|
nuclear@4
|
275 float specular[] = {gloss, gloss, gloss, 1.0};
|
nuclear@4
|
276 float shin = gloss * 128.0;
|
nuclear@4
|
277
|
nuclear@4
|
278 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
|
nuclear@4
|
279 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
|
nuclear@4
|
280 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
|
nuclear@4
|
281 }
|
nuclear@4
|
282
|
nuclear@4
|
283
|
nuclear@2
|
284 static void toggle_hmd_fullscr()
|
nuclear@2
|
285 {
|
nuclear@2
|
286 static bool fullscr;
|
nuclear@2
|
287 static int prev_x, prev_y;
|
nuclear@2
|
288 //static int prev_xsz, prev_ysz;
|
nuclear@2
|
289
|
nuclear@2
|
290 fullscr = !fullscr;
|
nuclear@2
|
291 if(fullscr) {
|
nuclear@2
|
292 /* entering fullscreen on the HMD */
|
nuclear@2
|
293 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
|
nuclear@2
|
294 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
|
nuclear@2
|
295 if(xoffs != -1) {
|
nuclear@2
|
296 get_window_pos(&prev_x, &prev_y);
|
nuclear@2
|
297 move_window(xoffs, yoffs);
|
nuclear@2
|
298 }
|
nuclear@2
|
299
|
nuclear@2
|
300 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
|
nuclear@2
|
301 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
|
nuclear@2
|
302 if(xsz != -1) {
|
nuclear@2
|
303 //prev_xsz = win_width;
|
nuclear@2
|
304 //prev_ysz = win_height;
|
nuclear@2
|
305 resize_window(xsz, ysz);
|
nuclear@2
|
306 }
|
nuclear@2
|
307 enter_fullscreen();
|
nuclear@2
|
308
|
nuclear@2
|
309 } else {
|
nuclear@2
|
310 /* leaving fullscreen */
|
nuclear@2
|
311 leave_fullscreen();
|
nuclear@2
|
312 /*move_window(prev_x, prev_y);
|
nuclear@2
|
313 resize_window(prev_xsz, prev_ysz);*/
|
nuclear@2
|
314 }
|
nuclear@2
|
315 }
|
nuclear@2
|
316
|
nuclear@2
|
317 static void create_rtarg(int x, int y)
|
nuclear@2
|
318 {
|
nuclear@2
|
319 if(x == fb_xsz && y == fb_ysz) {
|
nuclear@2
|
320 return; // nothing changed
|
nuclear@2
|
321 }
|
nuclear@2
|
322
|
nuclear@2
|
323 fb_xsz = x;
|
nuclear@2
|
324 fb_ysz = y;
|
nuclear@2
|
325 fb_tex_xsz = next_pow2(fb_xsz);
|
nuclear@2
|
326 fb_tex_ysz = next_pow2(fb_ysz);
|
nuclear@2
|
327
|
nuclear@5
|
328 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
|
nuclear@5
|
329
|
nuclear@2
|
330 if(!fbo) {
|
nuclear@2
|
331 glGenFramebuffers(1, &fbo);
|
nuclear@2
|
332
|
nuclear@2
|
333 glGenTextures(1, &fb_tex);
|
nuclear@2
|
334 glBindTexture(GL_TEXTURE_2D, fb_tex);
|
nuclear@2
|
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@2
|
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@2
|
337
|
nuclear@2
|
338 glGenRenderbuffers(1, &fb_depth);
|
nuclear@2
|
339 }
|
nuclear@2
|
340
|
nuclear@2
|
341 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@2
|
342
|
nuclear@2
|
343 glBindTexture(GL_TEXTURE_2D, fb_tex);
|
nuclear@2
|
344 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
nuclear@2
|
345 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
|
nuclear@2
|
346
|
nuclear@2
|
347 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
|
nuclear@2
|
348 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
|
nuclear@2
|
349 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
|
nuclear@2
|
350
|
nuclear@2
|
351 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
nuclear@2
|
352 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@2
|
353 }
|
nuclear@2
|
354
|
nuclear@2
|
355 static int next_pow2(int x)
|
nuclear@2
|
356 {
|
nuclear@2
|
357 x -= 1;
|
nuclear@2
|
358 x |= x >> 1;
|
nuclear@2
|
359 x |= x >> 2;
|
nuclear@2
|
360 x |= x >> 4;
|
nuclear@2
|
361 x |= x >> 8;
|
nuclear@2
|
362 x |= x >> 16;
|
nuclear@2
|
363 return x + 1;
|
nuclear@2
|
364 }
|