oculus2

view src/main.c @ 21:46291bf81d0a

delay switching to fullscreen until after the window has been resized
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 30 Mar 2015 07:27:01 +0300
parents 6a3a9840c303
children 9ee3ab70ca6b
line source
1 /* Very simple OculusSDK OpenGL usage example.
2 *
3 * Uses SDL2 (www.libsdl.org) for event handling and OpenGL context management.
4 * Uses GLEW (glew.sourceforge.net) for OpenGL extension wrangling.
5 *
6 * Author: John Tsiombikas <nuclear@member.fsf.org>
7 * This code is in the public domain. Do whatever you like with it.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <assert.h>
12 #include <SDL2/SDL.h>
13 #include <GL/glew.h>
15 #ifdef WIN32
16 #define OVR_OS_WIN32
17 #elif defined(__APPLE__)
18 #define OVR_OS_MAC
19 #else
20 #define OVR_OS_LINUX
21 #include <X11/Xlib.h>
22 #include <GL/glx.h>
23 #endif
25 #include <OVR_CAPI.h>
26 #include <OVR_CAPI_GL.h>
28 int init(void);
29 void cleanup(void);
30 void toggle_hmd_fullscreen(void);
31 void display(void);
32 void draw_scene(void);
33 void draw_box(float xsz, float ysz, float zsz, float norm_sign);
34 void update_rtarg(int width, int height);
35 int handle_event(SDL_Event *ev);
36 int key_event(int key, int state);
37 void reshape(int x, int y);
38 unsigned int next_pow2(unsigned int x);
39 void quat_to_matrix(const float *quat, float *mat);
40 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1);
42 /* forward declaration to avoid including non-public headers of libovr */
43 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enable);
45 static SDL_Window *win;
46 static SDL_GLContext ctx;
47 static int win_width, win_height;
49 static int fullscr_pending;
51 static unsigned int fbo, fb_tex, fb_depth;
52 static int fb_width, fb_height;
53 static int fb_tex_width, fb_tex_height;
55 static ovrHmd hmd;
56 static ovrSizei eyeres[2];
57 static ovrEyeRenderDesc eye_rdesc[2];
58 static ovrGLTexture fb_ovr_tex[2];
59 static union ovrGLConfig glcfg;
60 static unsigned int distort_caps;
61 static unsigned int hmd_caps;
63 static unsigned int chess_tex;
66 int main(int argc, char **argv)
67 {
68 if(init() == -1) {
69 return 1;
70 }
72 for(;;) {
73 SDL_Event ev;
74 while(SDL_PollEvent(&ev)) {
75 if(handle_event(&ev) == -1) {
76 goto done;
77 }
78 }
79 display();
80 }
82 done:
83 cleanup();
84 return 0;
85 }
88 int init(void)
89 {
90 int i, x, y;
91 unsigned int flags;
93 /* libovr must be initialized before we create the OpenGL context */
94 ovr_Initialize();
96 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
98 x = y = SDL_WINDOWPOS_UNDEFINED;
99 flags = SDL_WINDOW_OPENGL;
100 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1024, 640, flags))) {
101 fprintf(stderr, "failed to create window\n");
102 return -1;
103 }
104 if(!(ctx = SDL_GL_CreateContext(win))) {
105 fprintf(stderr, "failed to create OpenGL context\n");
106 return -1;
107 }
109 glewInit();
111 if(!(hmd = ovrHmd_Create(0))) {
112 fprintf(stderr, "failed to open Oculus HMD, falling back to virtual debug HMD\n");
113 if(!(hmd = ovrHmd_CreateDebug(ovrHmd_DK2))) {
114 fprintf(stderr, "failed to create virtual debug HMD\n");
115 return -1;
116 }
117 }
118 printf("initialized HMD: %s - %s\n", hmd->Manufacturer, hmd->ProductName);
120 /* resize our window to match the HMD resolution */
121 SDL_SetWindowSize(win, hmd->Resolution.w, hmd->Resolution.h);
122 SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
123 win_width = hmd->Resolution.w;
124 win_height = hmd->Resolution.h;
126 /* enable position and rotation tracking */
127 ovrHmd_ConfigureTracking(hmd, ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
128 /* retrieve the optimal render target resolution for each eye */
129 eyeres[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0);
130 eyeres[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0);
132 /* and create a single render target texture to encompass both eyes */
133 fb_width = eyeres[0].w + eyeres[1].w;
134 fb_height = eyeres[0].h > eyeres[1].h ? eyeres[0].h : eyeres[1].h;
135 update_rtarg(fb_width, fb_height);
137 /* fill in the ovrGLTexture structures that describe our render target texture */
138 for(i=0; i<2; i++) {
139 fb_ovr_tex[i].OGL.Header.API = ovrRenderAPI_OpenGL;
140 fb_ovr_tex[i].OGL.Header.TextureSize.w = fb_tex_width;
141 fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
142 /* this next field is the only one that differs between the two eyes */
143 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
144 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = 0;
145 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
146 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
147 fb_ovr_tex[i].OGL.TexId = fb_tex; /* both eyes will use the same texture id */
148 }
150 /* fill in the ovrGLConfig structure needed by the SDK to draw our stereo pair
151 * to the actual HMD display (SDK-distortion mode)
152 */
153 memset(&glcfg, 0, sizeof glcfg);
154 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
155 glcfg.OGL.Header.BackBufferSize.w = win_width;
156 glcfg.OGL.Header.BackBufferSize.h = win_height;
157 glcfg.OGL.Header.Multisample = 1;
159 #ifdef OVR_OS_WIN32
160 glcfg.OGL.Window = GetActiveWindow();
161 glcfg.OGL.DC = wglGetCurrentDC();
162 #elif defined(OVR_OS_LINUX)
163 glcfg.OGL.Disp = glXGetCurrentDisplay();
164 #endif
166 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
167 printf("running in \"extended desktop\" mode\n");
168 } else {
169 /* to sucessfully draw to the HMD display in "direct-hmd" mode, we have to
170 * call ovrHmd_AttachToWindow
171 * XXX: this doesn't work properly yet due to bugs in the oculus 0.4.1 sdk/driver
172 */
173 #ifdef WIN32
174 ovrHmd_AttachToWindow(hmd, glcfg.OGL.Window, 0, 0);
175 #elif defined(OVR_OS_LINUX)
176 ovrHmd_AttachToWindow(hmd, (void*)glXGetCurrentDrawable(), 0, 0);
177 #endif
178 printf("running in \"direct-hmd\" mode\n");
179 }
181 /* enable low-persistence display and dynamic prediction for lattency compensation */
182 hmd_caps = ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction;
183 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
185 /* configure SDK-rendering and enable chromatic abberation correction, vignetting, and
186 * timewrap, which shifts the image before drawing to counter any lattency between the call
187 * to ovrHmd_GetEyePose and ovrHmd_EndFrame.
188 */
189 distort_caps = ovrDistortionCap_Chromatic | ovrDistortionCap_TimeWarp |
190 ovrDistortionCap_Overdrive;
191 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc)) {
192 fprintf(stderr, "failed to configure distortion renderer\n");
193 }
195 /* disable the retarded "health and safety warning" */
196 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
198 glEnable(GL_DEPTH_TEST);
199 glEnable(GL_CULL_FACE);
200 glEnable(GL_LIGHTING);
201 glEnable(GL_LIGHT0);
202 glEnable(GL_LIGHT1);
203 glEnable(GL_NORMALIZE);
205 glClearColor(0.1, 0.1, 0.1, 1);
207 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
208 return 0;
209 }
211 void cleanup(void)
212 {
213 if(hmd) {
214 ovrHmd_Destroy(hmd);
215 }
216 ovr_Shutdown();
218 SDL_Quit();
219 }
221 void toggle_hmd_fullscreen(void)
222 {
223 static int fullscr, prev_x, prev_y, prev_xsz, prev_ysz;
224 fullscr = !fullscr;
226 if(fullscr) {
227 /* going fullscreen on the rift. save current window position, and move it
228 * to the rift's part of the desktop before going fullscreen
229 */
230 SDL_GetWindowPosition(win, &prev_x, &prev_y);
231 SDL_GetWindowSize(win, &prev_xsz, &prev_ysz);
232 SDL_SetWindowSize(win, hmd->Resolution.h, hmd->Resolution.w);
233 SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
234 fullscr_pending = 1;
235 /*SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);*/
237 #ifdef OVR_OS_LINUX
238 /* on linux for now we have to deal with screen rotation during rendering. The docs are promoting
239 * not rotating the DK2 screen globally
240 */
241 glcfg.OGL.Header.BackBufferSize.w = hmd->Resolution.h;
242 glcfg.OGL.Header.BackBufferSize.h = hmd->Resolution.w;
244 distort_caps |= ovrDistortionCap_LinuxDevFullscreen;
245 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
246 #endif
247 } else {
248 /* return to windowed mode and move the window back to its original position */
249 SDL_SetWindowFullscreen(win, 0);
250 SDL_SetWindowPosition(win, prev_x, prev_y);
251 SDL_SetWindowSize(win, prev_xsz, prev_ysz);
253 #ifdef OVR_OS_LINUX
254 glcfg.OGL.Header.BackBufferSize = hmd->Resolution;
256 distort_caps &= ~ovrDistortionCap_LinuxDevFullscreen;
257 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
258 #endif
259 }
260 }
262 void display(void)
263 {
264 int i;
265 ovrMatrix4f proj;
266 ovrPosef pose[2];
267 float rot_mat[16];
269 /* the drawing starts with a call to ovrHmd_BeginFrame */
270 ovrHmd_BeginFrame(hmd, 0);
272 /* start drawing onto our texture render target */
273 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
274 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
276 /* for each eye ... */
277 for(i=0; i<2; i++) {
278 ovrEyeType eye = hmd->EyeRenderOrder[i];
280 /* -- viewport transformation --
281 * setup the viewport to draw in the left half of the framebuffer when we're
282 * rendering the left eye's view (0, 0, width/2, height), and in the right half
283 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
284 */
285 glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
287 /* -- projection transformation --
288 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
289 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
290 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
291 */
292 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
293 glMatrixMode(GL_PROJECTION);
294 glLoadTransposeMatrixf(proj.M[0]);
296 /* -- view/camera transformation --
297 * we need to construct a view matrix by combining all the information provided by the oculus
298 * SDK, about the position and orientation of the user's head in the world.
299 */
300 /* TODO: use ovrHmd_GetEyePoses out of the loop instead */
301 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);
302 glMatrixMode(GL_MODELVIEW);
303 glLoadIdentity();
304 glTranslatef(eye_rdesc[eye].HmdToEyeViewOffset.x,
305 eye_rdesc[eye].HmdToEyeViewOffset.y,
306 eye_rdesc[eye].HmdToEyeViewOffset.z);
307 /* retrieve the orientation quaternion and convert it to a rotation matrix */
308 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
309 glMultMatrixf(rot_mat);
310 /* translate the view matrix with the positional tracking */
311 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
312 /* move the camera to the eye level of the user */
313 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
315 /* finally draw the scene for this eye */
316 draw_scene();
317 }
319 /* after drawing both eyes into the texture render target, revert to drawing directly to the
320 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
321 * compensated for lens distortion and chromatic abberation onto the HMD screen.
322 */
323 glBindFramebuffer(GL_FRAMEBUFFER, 0);
325 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
327 /* workaround for the oculus sdk distortion renderer bug, which uses a shader
328 * program, and doesn't restore the original binding when it's done.
329 */
330 glUseProgram(0);
332 assert(glGetError() == GL_NO_ERROR);
333 }
335 void reshape(int x, int y)
336 {
337 win_width = x;
338 win_height = y;
339 }
341 void draw_scene(void)
342 {
343 int i;
344 float grey[] = {0.8, 0.8, 0.8, 1};
345 float col[] = {0, 0, 0, 1};
346 float lpos[][4] = {
347 {-8, 2, 10, 1},
348 {0, 15, 0, 1}
349 };
350 float lcol[][4] = {
351 {0.8, 0.8, 0.8, 1},
352 {0.4, 0.3, 0.3, 1}
353 };
355 for(i=0; i<2; i++) {
356 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
357 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
358 }
360 glMatrixMode(GL_MODELVIEW);
362 glPushMatrix();
363 glTranslatef(0, 10, 0);
364 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
365 glBindTexture(GL_TEXTURE_2D, chess_tex);
366 glEnable(GL_TEXTURE_2D);
367 draw_box(30, 20, 30, -1.0);
368 glDisable(GL_TEXTURE_2D);
369 glPopMatrix();
371 for(i=0; i<4; i++) {
372 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
373 glPushMatrix();
374 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
375 draw_box(0.5, 2, 0.5, 1.0);
376 glPopMatrix();
378 col[0] = i & 1 ? 1.0 : 0.3;
379 col[1] = i == 0 ? 1.0 : 0.3;
380 col[2] = i & 2 ? 1.0 : 0.3;
381 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
383 glPushMatrix();
384 if(i & 1) {
385 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
386 } else {
387 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
388 }
389 draw_box(0.5, 0.5, 0.5, 1.0);
390 glPopMatrix();
391 }
393 col[0] = 1;
394 col[1] = 1;
395 col[2] = 0.4;
396 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
397 draw_box(0.05, 1.2, 6, 1.0);
398 draw_box(6, 1.2, 0.05, 1.0);
399 }
401 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
402 {
403 glMatrixMode(GL_MODELVIEW);
404 glPushMatrix();
405 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
407 if(norm_sign < 0.0) {
408 glFrontFace(GL_CW);
409 }
411 glBegin(GL_QUADS);
412 glNormal3f(0, 0, 1 * norm_sign);
413 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
414 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
415 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
416 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
417 glNormal3f(1 * norm_sign, 0, 0);
418 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
419 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
420 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
421 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
422 glNormal3f(0, 0, -1 * norm_sign);
423 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
424 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
425 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
426 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
427 glNormal3f(-1 * norm_sign, 0, 0);
428 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
429 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
430 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
431 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
432 glEnd();
433 glBegin(GL_TRIANGLE_FAN);
434 glNormal3f(0, 1 * norm_sign, 0);
435 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
436 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
437 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
438 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
439 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
440 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
441 glEnd();
442 glBegin(GL_TRIANGLE_FAN);
443 glNormal3f(0, -1 * norm_sign, 0);
444 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
445 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
446 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
447 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
448 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
449 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
450 glEnd();
452 glFrontFace(GL_CCW);
453 glPopMatrix();
454 }
456 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
457 void update_rtarg(int width, int height)
458 {
459 if(!fbo) {
460 /* if fbo does not exist, then nothing does... create every opengl object */
461 glGenFramebuffers(1, &fbo);
462 glGenTextures(1, &fb_tex);
463 glGenRenderbuffers(1, &fb_depth);
465 glBindTexture(GL_TEXTURE_2D, fb_tex);
466 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
467 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
468 }
470 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
472 /* calculate the next power of two in both dimensions and use that as a texture size */
473 fb_tex_width = next_pow2(width);
474 fb_tex_height = next_pow2(height);
476 /* create and attach the texture that will be used as a color buffer */
477 glBindTexture(GL_TEXTURE_2D, fb_tex);
478 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
479 GL_RGBA, GL_UNSIGNED_BYTE, 0);
480 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
482 /* create and attach the renderbuffer that will serve as our z-buffer */
483 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
484 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
485 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
487 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
488 fprintf(stderr, "incomplete framebuffer!\n");
489 }
491 glBindFramebuffer(GL_FRAMEBUFFER, 0);
492 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
493 }
495 int handle_event(SDL_Event *ev)
496 {
497 switch(ev->type) {
498 case SDL_QUIT:
499 return -1;
501 case SDL_KEYDOWN:
502 case SDL_KEYUP:
503 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
504 return -1;
505 }
506 break;
508 case SDL_WINDOWEVENT:
509 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
510 reshape(ev->window.data1, ev->window.data2);
511 if(fullscr_pending) {
512 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);
513 fullscr_pending = 0;
514 }
515 }
516 break;
518 default:
519 break;
520 }
522 return 0;
523 }
525 int key_event(int key, int state)
526 {
527 if(state) {
528 /*
529 ovrHSWDisplayState hsw;
530 ovrHmd_GetHSWDisplayState(hmd, &hsw);
531 if(hsw.Displayed) {
532 ovrHmd_DismissHSWDisplay(hmd);
533 }
534 */
536 switch(key) {
537 case 27:
538 return -1;
540 case ' ':
541 case 'r':
542 /* allow the user to recenter by pressing space */
543 ovrHmd_RecenterPose(hmd);
544 break;
546 case 'f':
547 /* press f to move the window to the HMD */
548 toggle_hmd_fullscreen();
549 break;
551 case 'v':
552 distort_caps ^= ovrDistortionCap_Vignette;
553 printf("Vignette: %s\n", distort_caps & ovrDistortionCap_Vignette ? "on" : "off");
554 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
555 break;
557 case 't':
558 distort_caps ^= ovrDistortionCap_TimeWarp;
559 printf("Time-warp: %s\n", distort_caps & ovrDistortionCap_TimeWarp ? "on" : "off");
560 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
561 break;
563 case 'o':
564 distort_caps ^= ovrDistortionCap_Overdrive;
565 printf("OLED over-drive: %s\n", distort_caps & ovrDistortionCap_Overdrive ? "on" : "off");
566 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
567 break;
569 case 'l':
570 hmd_caps ^= ovrHmdCap_LowPersistence;
571 printf("Low-persistence display: %s\n", hmd_caps & ovrHmdCap_LowPersistence ? "on" : "off");
572 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
573 break;
575 default:
576 break;
577 }
578 }
579 return 0;
580 }
582 unsigned int next_pow2(unsigned int x)
583 {
584 x -= 1;
585 x |= x >> 1;
586 x |= x >> 2;
587 x |= x >> 4;
588 x |= x >> 8;
589 x |= x >> 16;
590 return x + 1;
591 }
593 /* convert a quaternion to a rotation matrix */
594 void quat_to_matrix(const float *quat, float *mat)
595 {
596 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
597 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
598 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
599 mat[12] = 0.0f;
601 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
602 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
603 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
604 mat[13] = 0.0f;
606 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
607 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
608 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
609 mat[14] = 0.0f;
611 mat[3] = mat[7] = mat[11] = 0.0f;
612 mat[15] = 1.0f;
613 }
615 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
616 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
617 {
618 int i, j;
619 unsigned int tex;
620 unsigned char img[8 * 8 * 3];
621 unsigned char *pix = img;
623 for(i=0; i<8; i++) {
624 for(j=0; j<8; j++) {
625 int black = (i & 1) == (j & 1);
626 pix[0] = (black ? r0 : r1) * 255;
627 pix[1] = (black ? g0 : g1) * 255;
628 pix[2] = (black ? b0 : b1) * 255;
629 pix += 3;
630 }
631 }
633 glGenTextures(1, &tex);
634 glBindTexture(GL_TEXTURE_2D, tex);
635 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
636 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
637 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
639 return tex;
640 }