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