oculus2_psprite
view src/main.c @ 15:62a37bd5bc74
ok oculus2 test now works on linux with SDK 0.4.3 experimental.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 29 Oct 2014 01:08:38 +0200 |
parents | 256d8fcf02f1 |
children | 0888d4e24e21 |
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>
14 #include <X11/Xlib.h>
15 #include <GL/glx.h>
17 #ifdef WIN32
18 #define OVR_OS_WIN32
19 #elif defined(__APPLE__)
20 #define OVR_OS_MAC
21 #else
22 #define OVR_OS_LINUX
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.RTSize = 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 glcfg.OGL.Win = glXGetCurrentDrawable();
162 #endif
164 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
165 printf("running in \"extended desktop\" mode\n");
166 } else {
167 /* to sucessfully draw to the HMD display in "direct-hmd" mode, we have to
168 * call ovrHmd_AttachToWindow
169 * XXX: this doesn't work properly yet due to bugs in the oculus 0.4.1 sdk/driver
170 */
171 #ifdef WIN32
172 ovrHmd_AttachToWindow(hmd, glcfg.OGL.Window, 0, 0);
173 #else
174 ovrHmd_AttachToWindow(hmd, (void*)glcfg.OGL.Win, 0, 0);
175 #endif
176 printf("running in \"direct-hmd\" mode\n");
177 }
179 /* enable low-persistence display and dynamic prediction for lattency compensation */
180 hmd_caps = ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction;
181 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
183 /* configure SDK-rendering and enable chromatic abberation correction, vignetting, and
184 * timewrap, which shifts the image before drawing to counter any lattency between the call
185 * to ovrHmd_GetEyePose and ovrHmd_EndFrame.
186 */
187 distort_caps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
188 ovrDistortionCap_Overdrive;
189 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc)) {
190 fprintf(stderr, "failed to configure distortion renderer\n");
191 }
193 /* disable the retarded "health and safety warning" */
194 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
196 glEnable(GL_DEPTH_TEST);
197 glEnable(GL_CULL_FACE);
198 glEnable(GL_LIGHTING);
199 glEnable(GL_LIGHT0);
200 glEnable(GL_LIGHT1);
201 glEnable(GL_NORMALIZE);
203 glClearColor(0.1, 0.1, 0.1, 1);
205 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
206 return 0;
207 }
209 void cleanup(void)
210 {
211 if(hmd) {
212 ovrHmd_Destroy(hmd);
213 }
214 ovr_Shutdown();
216 SDL_Quit();
217 }
219 void toggle_hmd_fullscreen(void)
220 {
221 static int fullscr, prev_x, prev_y;
222 fullscr = !fullscr;
224 if(fullscr) {
225 /* going fullscreen on the rift. save current window position, and move it
226 * to the rift's part of the desktop before going fullscreen
227 */
228 SDL_GetWindowPosition(win, &prev_x, &prev_y);
229 SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
230 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
232 #ifdef OVR_OS_LINUX
233 /* on linux for now we have to deal with screen rotation during rendering. The docs are promoting
234 * not rotating the DK2 screen globally
235 */
236 glcfg.OGL.Header.RTSize.w = hmd->Resolution.h;
237 glcfg.OGL.Header.RTSize.h = hmd->Resolution.w;
239 distort_caps |= ovrDistortionCap_LinuxDevFullscreen;
240 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
241 #endif
242 } else {
243 /* return to windowed mode and move the window back to its original position */
244 SDL_SetWindowFullscreen(win, 0);
245 SDL_SetWindowPosition(win, prev_x, prev_y);
247 #ifdef OVR_OS_LINUX
248 glcfg.OGL.Header.RTSize = hmd->Resolution;
250 distort_caps &= ~ovrDistortionCap_LinuxDevFullscreen;
251 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
252 #endif
253 }
254 }
256 void display(void)
257 {
258 int i;
259 ovrMatrix4f proj;
260 ovrPosef pose[2];
261 float rot_mat[16];
263 /* the drawing starts with a call to ovrHmd_BeginFrame */
264 ovrHmd_BeginFrame(hmd, 0);
266 /* start drawing onto our texture render target */
267 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
268 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
270 glUseProgram(0);
272 /* for each eye ... */
273 for(i=0; i<2; i++) {
274 ovrEyeType eye = hmd->EyeRenderOrder[i];
276 /* -- viewport transformation --
277 * setup the viewport to draw in the left half of the framebuffer when we're
278 * rendering the left eye's view (0, 0, width/2, height), and in the right half
279 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
280 */
281 glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
283 /* -- projection transformation --
284 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
285 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
286 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
287 */
288 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
289 glMatrixMode(GL_PROJECTION);
290 glLoadTransposeMatrixf(proj.M[0]);
292 /* -- view/camera transformation --
293 * we need to construct a view matrix by combining all the information provided by the oculus
294 * SDK, about the position and orientation of the user's head in the world.
295 */
296 /* TODO: use ovrHmd_GetEyePoses out of the loop instead */
297 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);
298 glMatrixMode(GL_MODELVIEW);
299 glLoadIdentity();
300 glTranslatef(eye_rdesc[eye].HmdToEyeViewOffset.x,
301 eye_rdesc[eye].HmdToEyeViewOffset.y,
302 eye_rdesc[eye].HmdToEyeViewOffset.z);
303 /* retrieve the orientation quaternion and convert it to a rotation matrix */
304 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
305 glMultMatrixf(rot_mat);
306 /* translate the view matrix with the positional tracking */
307 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
308 /* move the camera to the eye level of the user */
309 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
311 /* finally draw the scene for this eye */
312 draw_scene();
313 }
315 /* after drawing both eyes into the texture render target, revert to drawing directly to the
316 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
317 * compensated for lens distortion and chromatic abberation onto the HMD screen.
318 */
319 glBindFramebuffer(GL_FRAMEBUFFER, 0);
321 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
323 assert(glGetError() == GL_NO_ERROR);
324 }
326 void reshape(int x, int y)
327 {
328 win_width = x;
329 win_height = y;
330 }
332 void draw_scene(void)
333 {
334 int i;
335 float grey[] = {0.8, 0.8, 0.8, 1};
336 float col[] = {0, 0, 0, 1};
337 float lpos[][4] = {
338 {-8, 2, 10, 1},
339 {0, 15, 0, 1}
340 };
341 float lcol[][4] = {
342 {0.8, 0.8, 0.8, 1},
343 {0.4, 0.3, 0.3, 1}
344 };
346 for(i=0; i<2; i++) {
347 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
348 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
349 }
351 glMatrixMode(GL_MODELVIEW);
353 glPushMatrix();
354 glTranslatef(0, 10, 0);
355 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
356 glBindTexture(GL_TEXTURE_2D, chess_tex);
357 glEnable(GL_TEXTURE_2D);
358 draw_box(30, 20, 30, -1.0);
359 glDisable(GL_TEXTURE_2D);
360 glPopMatrix();
362 for(i=0; i<4; i++) {
363 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
364 glPushMatrix();
365 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
366 draw_box(0.5, 2, 0.5, 1.0);
367 glPopMatrix();
369 col[0] = i & 1 ? 1.0 : 0.3;
370 col[1] = i == 0 ? 1.0 : 0.3;
371 col[2] = i & 2 ? 1.0 : 0.3;
372 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
374 glPushMatrix();
375 if(i & 1) {
376 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
377 } else {
378 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
379 }
380 draw_box(0.5, 0.5, 0.5, 1.0);
381 glPopMatrix();
382 }
383 }
385 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
386 {
387 glMatrixMode(GL_MODELVIEW);
388 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
390 if(norm_sign < 0.0) {
391 glFrontFace(GL_CW);
392 }
394 glBegin(GL_QUADS);
395 glNormal3f(0, 0, 1 * norm_sign);
396 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
397 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
398 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
399 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
400 glNormal3f(1 * norm_sign, 0, 0);
401 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
402 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
403 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
404 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
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 glEnd();
416 glBegin(GL_TRIANGLE_FAN);
417 glNormal3f(0, 1 * norm_sign, 0);
418 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
419 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
420 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
421 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
422 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
423 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
424 glEnd();
425 glBegin(GL_TRIANGLE_FAN);
426 glNormal3f(0, -1 * norm_sign, 0);
427 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 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 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
433 glEnd();
435 glFrontFace(GL_CCW);
436 }
438 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
439 void update_rtarg(int width, int height)
440 {
441 if(!fbo) {
442 /* if fbo does not exist, then nothing does... create every opengl object */
443 glGenFramebuffers(1, &fbo);
444 glGenTextures(1, &fb_tex);
445 glGenRenderbuffers(1, &fb_depth);
447 glBindTexture(GL_TEXTURE_2D, fb_tex);
448 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
449 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
450 }
452 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
454 /* calculate the next power of two in both dimensions and use that as a texture size */
455 fb_tex_width = next_pow2(width);
456 fb_tex_height = next_pow2(height);
458 /* create and attach the texture that will be used as a color buffer */
459 glBindTexture(GL_TEXTURE_2D, fb_tex);
460 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
461 GL_RGBA, GL_UNSIGNED_BYTE, 0);
462 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
464 /* create and attach the renderbuffer that will serve as our z-buffer */
465 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
466 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
467 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
469 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
470 fprintf(stderr, "incomplete framebuffer!\n");
471 }
473 glBindFramebuffer(GL_FRAMEBUFFER, 0);
474 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
475 }
477 int handle_event(SDL_Event *ev)
478 {
479 switch(ev->type) {
480 case SDL_QUIT:
481 return -1;
483 case SDL_KEYDOWN:
484 case SDL_KEYUP:
485 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
486 return -1;
487 }
488 break;
490 case SDL_WINDOWEVENT:
491 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
492 reshape(ev->window.data1, ev->window.data2);
493 }
494 break;
496 default:
497 break;
498 }
500 return 0;
501 }
503 int key_event(int key, int state)
504 {
505 if(state) {
506 /*
507 ovrHSWDisplayState hsw;
508 ovrHmd_GetHSWDisplayState(hmd, &hsw);
509 if(hsw.Displayed) {
510 ovrHmd_DismissHSWDisplay(hmd);
511 }
512 */
514 switch(key) {
515 case 27:
516 return -1;
518 case ' ':
519 case 'r':
520 /* allow the user to recenter by pressing space */
521 ovrHmd_RecenterPose(hmd);
522 break;
524 case 'f':
525 /* press f to move the window to the HMD */
526 toggle_hmd_fullscreen();
527 break;
529 case 'v':
530 distort_caps ^= ovrDistortionCap_Vignette;
531 printf("Vignette: %s\n", distort_caps & ovrDistortionCap_Vignette ? "on" : "off");
532 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
533 break;
535 case 't':
536 distort_caps ^= ovrDistortionCap_TimeWarp;
537 printf("Time-warp: %s\n", distort_caps & ovrDistortionCap_TimeWarp ? "on" : "off");
538 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
539 break;
541 case 'o':
542 distort_caps ^= ovrDistortionCap_Overdrive;
543 printf("OLED over-drive: %s\n", distort_caps & ovrDistortionCap_Overdrive ? "on" : "off");
544 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
545 break;
547 case 'l':
548 hmd_caps ^= ovrHmdCap_LowPersistence;
549 printf("Low-persistence display: %s\n", hmd_caps & ovrHmdCap_LowPersistence ? "on" : "off");
550 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
551 break;
553 default:
554 break;
555 }
556 }
557 return 0;
558 }
560 unsigned int next_pow2(unsigned int x)
561 {
562 x -= 1;
563 x |= x >> 1;
564 x |= x >> 2;
565 x |= x >> 4;
566 x |= x >> 8;
567 x |= x >> 16;
568 return x + 1;
569 }
571 /* convert a quaternion to a rotation matrix */
572 void quat_to_matrix(const float *quat, float *mat)
573 {
574 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
575 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
576 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
577 mat[12] = 0.0f;
579 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
580 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
581 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
582 mat[13] = 0.0f;
584 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
585 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
586 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
587 mat[14] = 0.0f;
589 mat[3] = mat[7] = mat[11] = 0.0f;
590 mat[15] = 1.0f;
591 }
593 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
594 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
595 {
596 int i, j;
597 unsigned int tex;
598 unsigned char img[8 * 8 * 3];
599 unsigned char *pix = img;
601 for(i=0; i<8; i++) {
602 for(j=0; j<8; j++) {
603 int black = (i & 1) == (j & 1);
604 pix[0] = (black ? r0 : r1) * 255;
605 pix[1] = (black ? g0 : g1) * 255;
606 pix[2] = (black ? b0 : b1) * 255;
607 pix += 3;
608 }
609 }
611 glGenTextures(1, &tex);
612 glBindTexture(GL_TEXTURE_2D, tex);
613 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
614 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
615 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
617 return tex;
618 }