oculus2_psprite

view src/main.c @ 23:fe9600396f65

fixed visual studio project
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 22 Jan 2015 14:03:24 +0200
parents dc7af0f549b2
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <SDL2/SDL.h>
5 #include <GL/glew.h>
7 #ifdef WIN32
8 #define OVR_OS_WIN32
9 #elif defined(__APPLE__)
10 #define OVR_OS_MAC
11 #else
12 #define OVR_OS_LINUX
13 #include <X11/Xlib.h>
14 #include <GL/glx.h>
15 #endif
17 #include <OVR_CAPI.h>
18 #include <OVR_CAPI_GL.h>
19 #include "projectile.h"
21 #define GUN_Y 1.5
23 int init(void);
24 int init_rift(void);
25 void cleanup(void);
26 void toggle_hmd_fullscreen(void);
27 void display(void);
28 void display_rift(void);
29 void draw_scene(void);
30 void draw_box(float xsz, float ysz, float zsz, float norm_sign);
31 void update_rtarg(int width, int height);
32 int handle_event(SDL_Event *ev);
33 int key_event(int key, int state);
34 void reshape(int x, int y);
35 unsigned int next_pow2(unsigned int x);
36 void quat_to_matrix(const float *quat, float *mat);
37 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1);
38 int parse_args(int argc, char **argv);
40 /* forward declaration to avoid including non-public headers of libovr */
41 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enable);
43 static SDL_Window *win;
44 static SDL_GLContext ctx;
45 static int win_width, win_height;
47 static unsigned int fbo, fb_tex, fb_depth;
48 static int fb_width, fb_height;
49 static int fb_tex_width, fb_tex_height;
51 static int use_rift = 1;
52 static ovrHmd hmd;
53 static ovrSizei eyeres[2];
54 static ovrEyeRenderDesc eye_rdesc[2];
55 static ovrGLTexture fb_ovr_tex[2];
56 static union ovrGLConfig glcfg;
57 static unsigned int distort_caps;
58 static unsigned int hmd_caps;
60 static unsigned int chess_tex;
62 static float cam_theta, cam_phi;
65 int main(int argc, char **argv)
66 {
67 unsigned int msec, prev_frame_msec = 0;
69 if(parse_args(argc, argv) == -1) {
70 return 1;
71 }
72 if(init() == -1) {
73 return 1;
74 }
76 for(;;) {
77 SDL_Event ev;
78 while(SDL_PollEvent(&ev)) {
79 if(handle_event(&ev) == -1) {
80 goto done;
81 }
82 }
84 msec = SDL_GetTicks();
85 update_shots((msec - prev_frame_msec) / 1000.0);
86 prev_frame_msec = msec;
88 if(use_rift) {
89 display_rift();
90 } else {
91 display();
92 }
93 }
95 done:
96 cleanup();
97 return 0;
98 }
101 int init(void)
102 {
103 int x, y;
104 unsigned int flags;
106 if(use_rift) {
107 /* libovr must be initialized before we create the OpenGL context */
108 ovr_Initialize();
109 }
111 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
113 x = y = SDL_WINDOWPOS_UNDEFINED;
114 flags = SDL_WINDOW_OPENGL;
115 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1024, 640, flags))) {
116 fprintf(stderr, "failed to create window\n");
117 return -1;
118 }
119 if(!(ctx = SDL_GL_CreateContext(win))) {
120 fprintf(stderr, "failed to create OpenGL context\n");
121 return -1;
122 }
124 glewInit();
126 if(use_rift && init_rift() == -1) {
127 return -1;
128 }
130 if(init_shots() == -1) {
131 return -1;
132 }
134 glEnable(GL_DEPTH_TEST);
135 glEnable(GL_CULL_FACE);
136 glEnable(GL_LIGHTING);
137 glEnable(GL_LIGHT0);
138 glEnable(GL_LIGHT1);
139 glEnable(GL_NORMALIZE);
141 glClearColor(0.1, 0.1, 0.1, 1);
143 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
144 return 0;
145 }
147 int init_rift(void)
148 {
149 int i;
151 if(!(hmd = ovrHmd_Create(0))) {
152 fprintf(stderr, "failed to open Oculus HMD, falling back to virtual debug HMD\n");
153 if(!(hmd = ovrHmd_CreateDebug(ovrHmd_DK2))) {
154 fprintf(stderr, "failed to create virtual debug HMD\n");
155 return -1;
156 }
157 }
158 printf("initialized HMD: %s - %s\n", hmd->Manufacturer, hmd->ProductName);
160 /* resize our window to match the HMD resolution */
161 SDL_SetWindowSize(win, hmd->Resolution.w, hmd->Resolution.h);
162 SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
163 win_width = hmd->Resolution.w;
164 win_height = hmd->Resolution.h;
166 /* enable position and rotation tracking */
167 ovrHmd_ConfigureTracking(hmd, ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
168 /* retrieve the optimal render target resolution for each eye */
169 eyeres[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0);
170 eyeres[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0);
172 /* and create a single render target texture to encompass both eyes */
173 fb_width = eyeres[0].w + eyeres[1].w;
174 fb_height = eyeres[0].h > eyeres[1].h ? eyeres[0].h : eyeres[1].h;
175 update_rtarg(fb_width, fb_height);
177 /* fill in the ovrGLTexture structures that describe our render target texture */
178 for(i=0; i<2; i++) {
179 fb_ovr_tex[i].OGL.Header.API = ovrRenderAPI_OpenGL;
180 fb_ovr_tex[i].OGL.Header.TextureSize.w = fb_tex_width;
181 fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
182 /* this next field is the only one that differs between the two eyes */
183 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
184 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = 0;
185 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
186 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
187 fb_ovr_tex[i].OGL.TexId = fb_tex; /* both eyes will use the same texture id */
188 }
190 /* fill in the ovrGLConfig structure needed by the SDK to draw our stereo pair
191 * to the actual HMD display (SDK-distortion mode)
192 */
193 memset(&glcfg, 0, sizeof glcfg);
194 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
195 glcfg.OGL.Header.BackBufferSize.w = win_width;
196 glcfg.OGL.Header.BackBufferSize.h = win_height;
197 glcfg.OGL.Header.Multisample = 1;
199 #ifdef OVR_OS_WIN32
200 glcfg.OGL.Window = GetActiveWindow();
201 glcfg.OGL.DC = wglGetCurrentDC();
202 #elif defined(OVR_OS_LINUX)
203 glcfg.OGL.Disp = glXGetCurrentDisplay();
204 #endif
206 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
207 printf("running in \"extended desktop\" mode\n");
208 } else {
209 /* to sucessfully draw to the HMD display in "direct-hmd" mode, we have to
210 * call ovrHmd_AttachToWindow
211 * XXX: this doesn't work properly yet due to bugs in the oculus 0.4.1 sdk/driver
212 */
213 #ifdef WIN32
214 ovrHmd_AttachToWindow(hmd, glcfg.OGL.Window, 0, 0);
215 #elif defined(OVR_OS_LINUX)
216 ovrHmd_AttachToWindow(hmd, (void*)glXGetCurrentDrawable(), 0, 0);
217 #endif
218 printf("running in \"direct-hmd\" mode\n");
219 }
221 /* enable low-persistence display and dynamic prediction for lattency compensation */
222 hmd_caps = ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction;
223 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
225 /* configure SDK-rendering and enable chromatic abberation correction, vignetting, and
226 * timewrap, which shifts the image before drawing to counter any lattency between the call
227 * to ovrHmd_GetEyePose and ovrHmd_EndFrame.
228 */
229 distort_caps = ovrDistortionCap_Chromatic | ovrDistortionCap_TimeWarp |
230 ovrDistortionCap_Overdrive;
231 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc)) {
232 fprintf(stderr, "failed to configure distortion renderer\n");
233 }
235 /* disable the retarded "health and safety warning" */
236 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
237 return 0;
238 }
240 void cleanup(void)
241 {
242 if(use_rift) {
243 if(hmd) {
244 ovrHmd_Destroy(hmd);
245 }
246 ovr_Shutdown();
247 }
249 SDL_Quit();
250 }
252 void toggle_hmd_fullscreen(void)
253 {
254 static int fullscr, prev_x, prev_y;
255 fullscr = !fullscr;
257 if(fullscr) {
258 /* going fullscreen on the rift. save current window position, and move it
259 * to the rift's part of the desktop before going fullscreen
260 */
261 SDL_GetWindowPosition(win, &prev_x, &prev_y);
262 if(use_rift) {
263 SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
264 }
265 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
267 #ifdef OVR_OS_LINUX
268 if(use_rift) {
269 /* on linux for now we have to deal with screen rotation during rendering. The docs are promoting
270 * not rotating the DK2 screen globally
271 */
272 glcfg.OGL.Header.BackBufferSize.w = hmd->Resolution.h;
273 glcfg.OGL.Header.BackBufferSize.h = hmd->Resolution.w;
275 distort_caps |= ovrDistortionCap_LinuxDevFullscreen;
276 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
277 }
278 #endif
279 } else {
280 /* return to windowed mode and move the window back to its original position */
281 SDL_SetWindowFullscreen(win, 0);
282 SDL_SetWindowPosition(win, prev_x, prev_y);
284 #ifdef OVR_OS_LINUX
285 if(use_rift) {
286 glcfg.OGL.Header.BackBufferSize = hmd->Resolution;
288 distort_caps &= ~ovrDistortionCap_LinuxDevFullscreen;
289 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
290 }
291 #endif
292 }
293 }
295 void display(void)
296 {
297 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
299 glMatrixMode(GL_PROJECTION);
300 glLoadIdentity();
301 gluPerspective(50.0, (float)win_width / (float)win_height, 0.5, 500.0);
303 glMatrixMode(GL_MODELVIEW);
304 glLoadIdentity();
305 glRotatef(cam_phi, 1, 0, 0);
306 glRotatef(cam_theta, 0, 1, 0);
307 glTranslatef(0, -1.6, 0); /* player height */
309 draw_scene();
311 SDL_GL_SwapWindow(win);
312 assert(glGetError() == GL_NO_ERROR);
313 }
315 void display_rift(void)
316 {
317 int i;
318 ovrMatrix4f proj;
319 ovrPosef pose[2];
320 float rot_mat[16];
322 /* the drawing starts with a call to ovrHmd_BeginFrame */
323 ovrHmd_BeginFrame(hmd, 0);
325 /* start drawing onto our texture render target */
326 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
327 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
329 /* for each eye ... */
330 for(i=0; i<2; i++) {
331 ovrEyeType eye = hmd->EyeRenderOrder[i];
333 /* -- viewport transformation --
334 * setup the viewport to draw in the left half of the framebuffer when we're
335 * rendering the left eye's view (0, 0, width/2, height), and in the right half
336 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
337 */
338 glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
340 /* -- projection transformation --
341 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
342 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
343 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
344 */
345 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
346 glMatrixMode(GL_PROJECTION);
347 glLoadTransposeMatrixf(proj.M[0]);
349 /* -- view/camera transformation --
350 * we need to construct a view matrix by combining all the information provided by the oculus
351 * SDK, about the position and orientation of the user's head in the world.
352 */
353 /* TODO: use ovrHmd_GetEyePoses out of the loop instead */
354 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);
355 glMatrixMode(GL_MODELVIEW);
356 glLoadIdentity();
357 glTranslatef(eye_rdesc[eye].HmdToEyeViewOffset.x,
358 eye_rdesc[eye].HmdToEyeViewOffset.y,
359 eye_rdesc[eye].HmdToEyeViewOffset.z);
360 /* retrieve the orientation quaternion and convert it to a rotation matrix */
361 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
362 glMultMatrixf(rot_mat);
363 /* translate the view matrix with the positional tracking */
364 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
365 /* move the camera to the eye level of the user */
366 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
368 /* finally draw the scene for this eye */
369 draw_scene();
370 }
372 /* after drawing both eyes into the texture render target, revert to drawing directly to the
373 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
374 * compensated for lens distortion and chromatic abberation onto the HMD screen.
375 */
376 glBindFramebuffer(GL_FRAMEBUFFER, 0);
378 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
380 /* workaround for the oculus sdk distortion renderer bug, which uses a shader
381 * program, and doesn't restore the original binding when it's done.
382 */
383 glUseProgram(0);
385 assert(glGetError() == GL_NO_ERROR);
386 }
388 void reshape(int x, int y)
389 {
390 win_width = x;
391 win_height = y;
393 glViewport(0, 0, x, y);
394 }
396 void draw_scene(void)
397 {
398 int i;
399 float grey[] = {0.8, 0.8, 0.8, 1};
400 float col[] = {0, 0, 0, 1};
401 float lpos[][4] = {
402 {-8, 2, 10, 1},
403 {0, 15, 0, 1}
404 };
405 float lcol[][4] = {
406 {0.8, 0.8, 0.8, 1},
407 {0.4, 0.3, 0.3, 1}
408 };
410 for(i=0; i<2; i++) {
411 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
412 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
413 }
415 glMatrixMode(GL_MODELVIEW);
417 glPushMatrix();
418 glTranslatef(0, 10, 0);
419 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
420 glBindTexture(GL_TEXTURE_2D, chess_tex);
421 glEnable(GL_TEXTURE_2D);
422 draw_box(30, 20, 30, -1.0);
423 glDisable(GL_TEXTURE_2D);
424 glPopMatrix();
426 for(i=0; i<4; i++) {
427 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
428 glPushMatrix();
429 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
430 draw_box(0.5, 2, 0.5, 1.0);
431 glPopMatrix();
433 col[0] = i & 1 ? 1.0 : 0.3;
434 col[1] = i == 0 ? 1.0 : 0.3;
435 col[2] = i & 2 ? 1.0 : 0.3;
436 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
438 glPushMatrix();
439 if(i & 1) {
440 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
441 } else {
442 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
443 }
444 draw_box(0.5, 0.5, 0.5, 1.0);
445 glPopMatrix();
446 }
448 col[0] = 1;
449 col[1] = 1;
450 col[2] = 0.4;
451 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
452 draw_box(0.05, 1.2, 6, 1.0);
453 draw_box(6, 1.2, 0.05, 1.0);
455 /* draw laser sight */
456 glPushMatrix();
457 glTranslatef(0, GUN_Y, 0);
458 glRotatef(-cam_theta, 0, 1, 0);
459 glRotatef(-cam_phi, 1, 0, 0);
461 glPushAttrib(GL_ENABLE_BIT);
462 glDisable(GL_LIGHTING);
463 glEnable(GL_BLEND);
464 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
466 glDepthMask(0);
467 glLineWidth(3.0);
469 glBegin(GL_LINES);
470 glColor4f(1.0, 0.2, 0.15, 0.4);
471 glVertex3f(0, 0, 0);
472 glVertex3f(0, 0, -100.0);
473 glEnd();
474 glLineWidth(1.0);
476 glPopAttrib();
478 glPopMatrix();
480 draw_shots();
481 }
483 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
484 {
485 glMatrixMode(GL_MODELVIEW);
486 glPushMatrix();
487 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
489 if(norm_sign < 0.0) {
490 glFrontFace(GL_CW);
491 }
493 glBegin(GL_QUADS);
494 glNormal3f(0, 0, 1 * norm_sign);
495 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
496 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
497 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
498 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
499 glNormal3f(1 * norm_sign, 0, 0);
500 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
501 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
502 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
503 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
504 glNormal3f(0, 0, -1 * norm_sign);
505 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
506 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
507 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
508 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
509 glNormal3f(-1 * norm_sign, 0, 0);
510 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
511 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
512 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
513 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
514 glEnd();
515 glBegin(GL_TRIANGLE_FAN);
516 glNormal3f(0, 1 * norm_sign, 0);
517 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
518 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
519 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
520 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
521 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
522 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
523 glEnd();
524 glBegin(GL_TRIANGLE_FAN);
525 glNormal3f(0, -1 * norm_sign, 0);
526 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
527 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
528 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
529 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
530 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
531 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
532 glEnd();
534 glFrontFace(GL_CCW);
535 glPopMatrix();
536 }
538 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
539 void update_rtarg(int width, int height)
540 {
541 if(!fbo) {
542 /* if fbo does not exist, then nothing does... create every opengl object */
543 glGenFramebuffers(1, &fbo);
544 glGenTextures(1, &fb_tex);
545 glGenRenderbuffers(1, &fb_depth);
547 glBindTexture(GL_TEXTURE_2D, fb_tex);
548 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
549 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
550 }
552 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
554 /* calculate the next power of two in both dimensions and use that as a texture size */
555 fb_tex_width = next_pow2(width);
556 fb_tex_height = next_pow2(height);
558 /* create and attach the texture that will be used as a color buffer */
559 glBindTexture(GL_TEXTURE_2D, fb_tex);
560 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
561 GL_RGBA, GL_UNSIGNED_BYTE, 0);
562 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
564 /* create and attach the renderbuffer that will serve as our z-buffer */
565 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
566 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
567 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
569 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
570 fprintf(stderr, "incomplete framebuffer!\n");
571 }
573 glBindFramebuffer(GL_FRAMEBUFFER, 0);
574 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
575 }
577 void fire(void)
578 {
579 vec3_t pos = {0, GUN_Y, 0};
580 vec3_t dir;
581 float theta = DEG_TO_RAD(cam_theta);
582 float phi = DEG_TO_RAD(cam_phi);
584 dir.x = -sin(-theta) * cos(phi);
585 dir.y = -sin(phi);
586 dir.z = -cos(-theta) * cos(phi);
588 shoot(pos, v3_scale(dir, 10.0));
589 }
591 int handle_event(SDL_Event *ev)
592 {
593 switch(ev->type) {
594 case SDL_QUIT:
595 return -1;
597 case SDL_KEYDOWN:
598 case SDL_KEYUP:
599 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
600 return -1;
601 }
602 break;
604 case SDL_WINDOWEVENT:
605 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
606 reshape(ev->window.data1, ev->window.data2);
607 } else if(ev->window.event == SDL_WINDOWEVENT_SHOWN) {
608 int xsz, ysz;
609 SDL_GetWindowSize(win, &xsz, &ysz);
610 reshape(xsz, ysz);
611 }
612 break;
614 case SDL_MOUSEBUTTONDOWN:
615 if(ev->button.button == SDL_BUTTON_LEFT) {
616 fire();
617 }
618 break;
620 case SDL_MOUSEMOTION:
621 {
622 static int prev_x, prev_y;
623 int dx = ev->motion.x - prev_x;
624 int dy = ev->motion.y - prev_y;
625 prev_x = ev->motion.x;
626 prev_y = ev->motion.y;
628 if(ev->motion.state & SDL_BUTTON_RMASK) {
629 cam_theta += dx * 0.5;
630 cam_phi += dy * 0.5;
632 if(cam_phi < -90) cam_phi = -90;
633 if(cam_phi > 90) cam_phi = 90;
634 }
635 }
636 break;
638 default:
639 break;
640 }
642 return 0;
643 }
645 int key_event(int key, int state)
646 {
647 if(state) {
648 /*
649 ovrHSWDisplayState hsw;
650 ovrHmd_GetHSWDisplayState(hmd, &hsw);
651 if(hsw.Displayed) {
652 ovrHmd_DismissHSWDisplay(hmd);
653 }
654 */
656 switch(key) {
657 case 27:
658 return -1;
660 case ' ':
661 case 'r':
662 /* allow the user to recenter by pressing space */
663 ovrHmd_RecenterPose(hmd);
664 break;
666 case 'f':
667 /* press f to move the window to the HMD */
668 toggle_hmd_fullscreen();
669 break;
671 case 'v':
672 distort_caps ^= ovrDistortionCap_Vignette;
673 printf("Vignette: %s\n", distort_caps & ovrDistortionCap_Vignette ? "on" : "off");
674 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
675 break;
677 case 't':
678 distort_caps ^= ovrDistortionCap_TimeWarp;
679 printf("Time-warp: %s\n", distort_caps & ovrDistortionCap_TimeWarp ? "on" : "off");
680 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
681 break;
683 case 'o':
684 distort_caps ^= ovrDistortionCap_Overdrive;
685 printf("OLED over-drive: %s\n", distort_caps & ovrDistortionCap_Overdrive ? "on" : "off");
686 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
687 break;
689 case 'l':
690 hmd_caps ^= ovrHmdCap_LowPersistence;
691 printf("Low-persistence display: %s\n", hmd_caps & ovrHmdCap_LowPersistence ? "on" : "off");
692 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
693 break;
695 default:
696 break;
697 }
698 }
699 return 0;
700 }
702 unsigned int next_pow2(unsigned int x)
703 {
704 x -= 1;
705 x |= x >> 1;
706 x |= x >> 2;
707 x |= x >> 4;
708 x |= x >> 8;
709 x |= x >> 16;
710 return x + 1;
711 }
713 /* convert a quaternion to a rotation matrix */
714 void quat_to_matrix(const float *quat, float *mat)
715 {
716 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
717 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
718 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
719 mat[12] = 0.0f;
721 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
722 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
723 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
724 mat[13] = 0.0f;
726 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
727 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
728 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
729 mat[14] = 0.0f;
731 mat[3] = mat[7] = mat[11] = 0.0f;
732 mat[15] = 1.0f;
733 }
735 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
736 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
737 {
738 int i, j;
739 unsigned int tex;
740 unsigned char img[8 * 8 * 3];
741 unsigned char *pix = img;
743 for(i=0; i<8; i++) {
744 for(j=0; j<8; j++) {
745 int black = (i & 1) == (j & 1);
746 pix[0] = (black ? r0 : r1) * 255;
747 pix[1] = (black ? g0 : g1) * 255;
748 pix[2] = (black ? b0 : b1) * 255;
749 pix += 3;
750 }
751 }
753 glGenTextures(1, &tex);
754 glBindTexture(GL_TEXTURE_2D, tex);
755 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
756 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
757 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
759 return tex;
760 }
762 int parse_args(int argc, char **argv)
763 {
764 int i;
766 for(i=1; i<argc; i++) {
767 if(argv[i][0] == '-') {
768 if(strcmp(argv[i], "-novr") == 0) {
769 use_rift = 0;
770 } else {
771 fprintf(stderr, "invalid option: %s\n", argv[i]);
772 return -1;
773 }
774 } else {
775 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
776 return -1;
777 }
778 }
779 return 0;
780 }