oculus2

view src/main.c @ 25:0c7d02c58e4e

removed forward declaration of the now removed ovrhmd_EnableHSWDisplaySDKRender function
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Apr 2015 03:52:51 +0300
parents b2afe1196579
children a7a3f89def42
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 static SDL_Window *win;
43 static SDL_GLContext ctx;
44 static int win_width, win_height;
46 static unsigned int fbo, fb_tex, fb_depth;
47 static int fb_width, fb_height;
48 static int fb_tex_width, fb_tex_height;
50 static ovrHmd hmd;
51 static ovrSizei eyeres[2];
52 static ovrEyeRenderDesc eye_rdesc[2];
53 static ovrGLTexture fb_ovr_tex[2];
54 static union ovrGLConfig glcfg;
55 static unsigned int distort_caps;
56 static unsigned int hmd_caps;
58 static unsigned int chess_tex;
61 int main(int argc, char **argv)
62 {
63 if(init() == -1) {
64 return 1;
65 }
67 for(;;) {
68 SDL_Event ev;
69 while(SDL_PollEvent(&ev)) {
70 if(handle_event(&ev) == -1) {
71 goto done;
72 }
73 }
74 display();
75 }
77 done:
78 cleanup();
79 return 0;
80 }
83 int init(void)
84 {
85 int i, x, y;
86 unsigned int flags;
88 /* libovr must be initialized before we create the OpenGL context */
89 ovr_Initialize(0);
91 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
93 x = y = SDL_WINDOWPOS_UNDEFINED;
94 flags = SDL_WINDOW_OPENGL;
95 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1024, 640, flags))) {
96 fprintf(stderr, "failed to create window\n");
97 return -1;
98 }
99 if(!(ctx = SDL_GL_CreateContext(win))) {
100 fprintf(stderr, "failed to create OpenGL context\n");
101 return -1;
102 }
104 glewInit();
106 if(!(hmd = ovrHmd_Create(0))) {
107 fprintf(stderr, "failed to open Oculus HMD, falling back to virtual debug HMD\n");
108 if(!(hmd = ovrHmd_CreateDebug(ovrHmd_DK2))) {
109 fprintf(stderr, "failed to create virtual debug HMD\n");
110 return -1;
111 }
112 }
113 printf("initialized HMD: %s - %s\n", hmd->Manufacturer, hmd->ProductName);
115 /* resize our window to match the HMD resolution */
116 SDL_SetWindowSize(win, hmd->Resolution.w, hmd->Resolution.h);
117 SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
118 win_width = hmd->Resolution.w;
119 win_height = hmd->Resolution.h;
121 /* enable position and rotation tracking */
122 ovrHmd_ConfigureTracking(hmd, ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
123 /* retrieve the optimal render target resolution for each eye */
124 eyeres[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0);
125 eyeres[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0);
127 /* and create a single render target texture to encompass both eyes */
128 fb_width = eyeres[0].w + eyeres[1].w;
129 fb_height = eyeres[0].h > eyeres[1].h ? eyeres[0].h : eyeres[1].h;
130 update_rtarg(fb_width, fb_height);
132 /* fill in the ovrGLTexture structures that describe our render target texture */
133 for(i=0; i<2; i++) {
134 fb_ovr_tex[i].OGL.Header.API = ovrRenderAPI_OpenGL;
135 fb_ovr_tex[i].OGL.Header.TextureSize.w = fb_tex_width;
136 fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
137 /* this next field is the only one that differs between the two eyes */
138 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
139 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = 0;
140 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
141 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
142 fb_ovr_tex[i].OGL.TexId = fb_tex; /* both eyes will use the same texture id */
143 }
145 /* fill in the ovrGLConfig structure needed by the SDK to draw our stereo pair
146 * to the actual HMD display (SDK-distortion mode)
147 */
148 memset(&glcfg, 0, sizeof glcfg);
149 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
150 glcfg.OGL.Header.BackBufferSize.w = win_width;
151 glcfg.OGL.Header.BackBufferSize.h = win_height;
152 glcfg.OGL.Header.Multisample = 1;
154 #ifdef OVR_OS_WIN32
155 glcfg.OGL.Window = GetActiveWindow();
156 glcfg.OGL.DC = wglGetCurrentDC();
157 #elif defined(OVR_OS_LINUX)
158 glcfg.OGL.Disp = glXGetCurrentDisplay();
159 #endif
161 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
162 printf("running in \"extended desktop\" mode\n");
163 } else {
164 /* to sucessfully draw to the HMD display in "direct-hmd" mode, we have to
165 * call ovrHmd_AttachToWindow
166 * XXX: this doesn't work properly yet due to bugs in the oculus 0.4.1 sdk/driver
167 */
168 #ifdef WIN32
169 ovrHmd_AttachToWindow(hmd, glcfg.OGL.Window, 0, 0);
170 #elif defined(OVR_OS_LINUX)
171 ovrHmd_AttachToWindow(hmd, (void*)glXGetCurrentDrawable(), 0, 0);
172 #endif
173 printf("running in \"direct-hmd\" mode\n");
174 }
176 /* enable low-persistence display and dynamic prediction for lattency compensation */
177 hmd_caps = ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction;
178 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
180 /* configure SDK-rendering and enable OLED overdrive and timewrap, which
181 * shifts the image before drawing to counter any lattency between the call
182 * to ovrHmd_GetEyePose and ovrHmd_EndFrame.
183 */
184 distort_caps = ovrDistortionCap_TimeWarp | ovrDistortionCap_Overdrive;
185 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc)) {
186 fprintf(stderr, "failed to configure distortion renderer\n");
187 }
189 /* disable the retarded "health and safety warning" */
190 /*ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);*/
192 glEnable(GL_DEPTH_TEST);
193 glEnable(GL_CULL_FACE);
194 glEnable(GL_LIGHTING);
195 glEnable(GL_LIGHT0);
196 glEnable(GL_LIGHT1);
197 glEnable(GL_NORMALIZE);
199 glClearColor(0.1, 0.1, 0.1, 1);
201 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
202 return 0;
203 }
205 void cleanup(void)
206 {
207 if(hmd) {
208 ovrHmd_Destroy(hmd);
209 }
210 ovr_Shutdown();
212 SDL_Quit();
213 }
215 void toggle_hmd_fullscreen(void)
216 {
217 static int fullscr, prev_x, prev_y;
218 fullscr = !fullscr;
220 if(fullscr) {
221 /* going fullscreen on the rift. save current window position, and move it
222 * to the rift's part of the desktop before going fullscreen
223 */
224 SDL_GetWindowPosition(win, &prev_x, &prev_y);
225 SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
226 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
228 #ifdef OVR_OS_LINUX
229 /* on linux for now we have to deal with screen rotation during rendering. The docs are promoting
230 * not rotating the DK2 screen globally
231 */
232 glcfg.OGL.Header.BackBufferSize.w = hmd->Resolution.h;
233 glcfg.OGL.Header.BackBufferSize.h = hmd->Resolution.w;
235 distort_caps |= ovrDistortionCap_LinuxDevFullscreen;
236 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
237 #endif
238 } else {
239 /* return to windowed mode and move the window back to its original position */
240 SDL_SetWindowFullscreen(win, 0);
241 SDL_SetWindowPosition(win, prev_x, prev_y);
243 #ifdef OVR_OS_LINUX
244 glcfg.OGL.Header.BackBufferSize = hmd->Resolution;
246 distort_caps &= ~ovrDistortionCap_LinuxDevFullscreen;
247 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
248 #endif
249 }
250 }
252 void display(void)
253 {
254 int i;
255 ovrMatrix4f proj;
256 ovrPosef pose[2];
257 float rot_mat[16];
259 /* the drawing starts with a call to ovrHmd_BeginFrame */
260 ovrHmd_BeginFrame(hmd, 0);
262 /* start drawing onto our texture render target */
263 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
264 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
266 /* for each eye ... */
267 for(i=0; i<2; i++) {
268 ovrEyeType eye = hmd->EyeRenderOrder[i];
270 /* -- viewport transformation --
271 * setup the viewport to draw in the left half of the framebuffer when we're
272 * rendering the left eye's view (0, 0, width/2, height), and in the right half
273 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
274 */
275 glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
277 /* -- projection transformation --
278 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
279 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
280 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
281 */
282 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
283 glMatrixMode(GL_PROJECTION);
284 glLoadTransposeMatrixf(proj.M[0]);
286 /* -- view/camera transformation --
287 * we need to construct a view matrix by combining all the information provided by the oculus
288 * SDK, about the position and orientation of the user's head in the world.
289 */
290 /* TODO: use ovrHmd_GetEyePoses out of the loop instead */
291 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);
292 glMatrixMode(GL_MODELVIEW);
293 glLoadIdentity();
294 glTranslatef(eye_rdesc[eye].HmdToEyeViewOffset.x,
295 eye_rdesc[eye].HmdToEyeViewOffset.y,
296 eye_rdesc[eye].HmdToEyeViewOffset.z);
297 /* retrieve the orientation quaternion and convert it to a rotation matrix */
298 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
299 glMultMatrixf(rot_mat);
300 /* translate the view matrix with the positional tracking */
301 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
302 /* move the camera to the eye level of the user */
303 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
305 /* finally draw the scene for this eye */
306 draw_scene();
307 }
309 /* after drawing both eyes into the texture render target, revert to drawing directly to the
310 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
311 * compensated for lens distortion and chromatic abberation onto the HMD screen.
312 */
313 glBindFramebuffer(GL_FRAMEBUFFER, 0);
315 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
317 /* workaround for the oculus sdk distortion renderer bug, which uses a shader
318 * program, and doesn't restore the original binding when it's done.
319 */
320 glUseProgram(0);
322 assert(glGetError() == GL_NO_ERROR);
323 }
325 void reshape(int x, int y)
326 {
327 win_width = x;
328 win_height = y;
329 }
331 void draw_scene(void)
332 {
333 int i;
334 float grey[] = {0.8, 0.8, 0.8, 1};
335 float col[] = {0, 0, 0, 1};
336 float lpos[][4] = {
337 {-8, 2, 10, 1},
338 {0, 15, 0, 1}
339 };
340 float lcol[][4] = {
341 {0.8, 0.8, 0.8, 1},
342 {0.4, 0.3, 0.3, 1}
343 };
345 for(i=0; i<2; i++) {
346 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
347 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
348 }
350 glMatrixMode(GL_MODELVIEW);
352 glPushMatrix();
353 glTranslatef(0, 10, 0);
354 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
355 glBindTexture(GL_TEXTURE_2D, chess_tex);
356 glEnable(GL_TEXTURE_2D);
357 draw_box(30, 20, 30, -1.0);
358 glDisable(GL_TEXTURE_2D);
359 glPopMatrix();
361 for(i=0; i<4; i++) {
362 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
363 glPushMatrix();
364 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
365 draw_box(0.5, 2, 0.5, 1.0);
366 glPopMatrix();
368 col[0] = i & 1 ? 1.0 : 0.3;
369 col[1] = i == 0 ? 1.0 : 0.3;
370 col[2] = i & 2 ? 1.0 : 0.3;
371 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
373 glPushMatrix();
374 if(i & 1) {
375 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
376 } else {
377 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
378 }
379 draw_box(0.5, 0.5, 0.5, 1.0);
380 glPopMatrix();
381 }
383 col[0] = 1;
384 col[1] = 1;
385 col[2] = 0.4;
386 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
387 draw_box(0.05, 1.2, 6, 1.0);
388 draw_box(6, 1.2, 0.05, 1.0);
389 }
391 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
392 {
393 glMatrixMode(GL_MODELVIEW);
394 glPushMatrix();
395 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
397 if(norm_sign < 0.0) {
398 glFrontFace(GL_CW);
399 }
401 glBegin(GL_QUADS);
402 glNormal3f(0, 0, 1 * norm_sign);
403 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
404 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
405 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
406 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
407 glNormal3f(1 * norm_sign, 0, 0);
408 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
409 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
410 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
411 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
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 glEnd();
423 glBegin(GL_TRIANGLE_FAN);
424 glNormal3f(0, 1 * norm_sign, 0);
425 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
426 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
427 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
428 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
429 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
430 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
431 glEnd();
432 glBegin(GL_TRIANGLE_FAN);
433 glNormal3f(0, -1 * norm_sign, 0);
434 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
435 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
436 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
437 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
438 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
439 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
440 glEnd();
442 glFrontFace(GL_CCW);
443 glPopMatrix();
444 }
446 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
447 void update_rtarg(int width, int height)
448 {
449 if(!fbo) {
450 /* if fbo does not exist, then nothing does... create every opengl object */
451 glGenFramebuffers(1, &fbo);
452 glGenTextures(1, &fb_tex);
453 glGenRenderbuffers(1, &fb_depth);
455 glBindTexture(GL_TEXTURE_2D, fb_tex);
456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
458 }
460 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
462 /* calculate the next power of two in both dimensions and use that as a texture size */
463 fb_tex_width = next_pow2(width);
464 fb_tex_height = next_pow2(height);
466 /* create and attach the texture that will be used as a color buffer */
467 glBindTexture(GL_TEXTURE_2D, fb_tex);
468 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
469 GL_RGBA, GL_UNSIGNED_BYTE, 0);
470 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
472 /* create and attach the renderbuffer that will serve as our z-buffer */
473 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
474 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
475 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
477 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
478 fprintf(stderr, "incomplete framebuffer!\n");
479 }
481 glBindFramebuffer(GL_FRAMEBUFFER, 0);
482 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
483 }
485 int handle_event(SDL_Event *ev)
486 {
487 switch(ev->type) {
488 case SDL_QUIT:
489 return -1;
491 case SDL_KEYDOWN:
492 case SDL_KEYUP:
493 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
494 return -1;
495 }
496 break;
498 case SDL_WINDOWEVENT:
499 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
500 reshape(ev->window.data1, ev->window.data2);
501 }
502 break;
504 default:
505 break;
506 }
508 return 0;
509 }
511 int key_event(int key, int state)
512 {
513 if(state) {
514 ovrHSWDisplayState hsw;
515 ovrHmd_GetHSWDisplayState(hmd, &hsw);
516 if(hsw.Displayed) {
517 ovrHmd_DismissHSWDisplay(hmd);
518 }
520 switch(key) {
521 case 27:
522 return -1;
524 case ' ':
525 case 'r':
526 /* allow the user to recenter by pressing space */
527 ovrHmd_RecenterPose(hmd);
528 break;
530 case 'f':
531 /* press f to move the window to the HMD */
532 toggle_hmd_fullscreen();
533 break;
535 case 'v':
536 distort_caps ^= ovrDistortionCap_Vignette;
537 printf("Vignette: %s\n", distort_caps & ovrDistortionCap_Vignette ? "on" : "off");
538 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
539 break;
541 case 't':
542 distort_caps ^= ovrDistortionCap_TimeWarp;
543 printf("Time-warp: %s\n", distort_caps & ovrDistortionCap_TimeWarp ? "on" : "off");
544 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
545 break;
547 case 'o':
548 distort_caps ^= ovrDistortionCap_Overdrive;
549 printf("OLED over-drive: %s\n", distort_caps & ovrDistortionCap_Overdrive ? "on" : "off");
550 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
551 break;
553 case 'l':
554 hmd_caps ^= ovrHmdCap_LowPersistence;
555 printf("Low-persistence display: %s\n", hmd_caps & ovrHmdCap_LowPersistence ? "on" : "off");
556 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
557 break;
559 default:
560 break;
561 }
562 }
563 return 0;
564 }
566 unsigned int next_pow2(unsigned int x)
567 {
568 x -= 1;
569 x |= x >> 1;
570 x |= x >> 2;
571 x |= x >> 4;
572 x |= x >> 8;
573 x |= x >> 16;
574 return x + 1;
575 }
577 /* convert a quaternion to a rotation matrix */
578 void quat_to_matrix(const float *quat, float *mat)
579 {
580 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
581 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
582 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
583 mat[12] = 0.0f;
585 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
586 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
587 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
588 mat[13] = 0.0f;
590 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
591 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
592 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
593 mat[14] = 0.0f;
595 mat[3] = mat[7] = mat[11] = 0.0f;
596 mat[15] = 1.0f;
597 }
599 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
600 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
601 {
602 int i, j;
603 unsigned int tex;
604 unsigned char img[8 * 8 * 3];
605 unsigned char *pix = img;
607 for(i=0; i<8; i++) {
608 for(j=0; j<8; j++) {
609 int black = (i & 1) == (j & 1);
610 pix[0] = (black ? r0 : r1) * 255;
611 pix[1] = (black ? g0 : g1) * 255;
612 pix[2] = (black ? b0 : b1) * 255;
613 pix += 3;
614 }
615 }
617 glGenTextures(1, &tex);
618 glBindTexture(GL_TEXTURE_2D, tex);
619 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
620 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
621 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
623 return tex;
624 }