oculus2
view src/main.c @ 17:5558b43eb653
moved the glUseProgram(0) state cleanup to its proper place
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 29 Oct 2014 01:56:42 +0200 |
parents | 0888d4e24e21 |
children | 64089dd45d50 |
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.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 /* for each eye ... */
271 for(i=0; i<2; i++) {
272 ovrEyeType eye = hmd->EyeRenderOrder[i];
274 /* -- viewport transformation --
275 * setup the viewport to draw in the left half of the framebuffer when we're
276 * rendering the left eye's view (0, 0, width/2, height), and in the right half
277 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
278 */
279 glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
281 /* -- projection transformation --
282 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
283 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
284 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
285 */
286 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
287 glMatrixMode(GL_PROJECTION);
288 glLoadTransposeMatrixf(proj.M[0]);
290 /* -- view/camera transformation --
291 * we need to construct a view matrix by combining all the information provided by the oculus
292 * SDK, about the position and orientation of the user's head in the world.
293 */
294 /* TODO: use ovrHmd_GetEyePoses out of the loop instead */
295 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);
296 glMatrixMode(GL_MODELVIEW);
297 glLoadIdentity();
298 glTranslatef(eye_rdesc[eye].HmdToEyeViewOffset.x,
299 eye_rdesc[eye].HmdToEyeViewOffset.y,
300 eye_rdesc[eye].HmdToEyeViewOffset.z);
301 /* retrieve the orientation quaternion and convert it to a rotation matrix */
302 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
303 glMultMatrixf(rot_mat);
304 /* translate the view matrix with the positional tracking */
305 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
306 /* move the camera to the eye level of the user */
307 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
309 /* finally draw the scene for this eye */
310 draw_scene();
311 }
313 /* after drawing both eyes into the texture render target, revert to drawing directly to the
314 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
315 * compensated for lens distortion and chromatic abberation onto the HMD screen.
316 */
317 glBindFramebuffer(GL_FRAMEBUFFER, 0);
319 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
321 /* workaround for the oculus sdk distortion renderer bug, which uses a shader
322 * program, and doesn't restore the original binding when it's done.
323 */
324 glUseProgram(0);
326 assert(glGetError() == GL_NO_ERROR);
327 }
329 void reshape(int x, int y)
330 {
331 win_width = x;
332 win_height = y;
333 }
335 void draw_scene(void)
336 {
337 int i;
338 float grey[] = {0.8, 0.8, 0.8, 1};
339 float col[] = {0, 0, 0, 1};
340 float lpos[][4] = {
341 {-8, 2, 10, 1},
342 {0, 15, 0, 1}
343 };
344 float lcol[][4] = {
345 {0.8, 0.8, 0.8, 1},
346 {0.4, 0.3, 0.3, 1}
347 };
349 for(i=0; i<2; i++) {
350 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
351 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
352 }
354 glMatrixMode(GL_MODELVIEW);
356 glPushMatrix();
357 glTranslatef(0, 10, 0);
358 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
359 glBindTexture(GL_TEXTURE_2D, chess_tex);
360 glEnable(GL_TEXTURE_2D);
361 draw_box(30, 20, 30, -1.0);
362 glDisable(GL_TEXTURE_2D);
363 glPopMatrix();
365 for(i=0; i<4; i++) {
366 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
367 glPushMatrix();
368 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
369 draw_box(0.5, 2, 0.5, 1.0);
370 glPopMatrix();
372 col[0] = i & 1 ? 1.0 : 0.3;
373 col[1] = i == 0 ? 1.0 : 0.3;
374 col[2] = i & 2 ? 1.0 : 0.3;
375 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
377 glPushMatrix();
378 if(i & 1) {
379 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
380 } else {
381 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
382 }
383 draw_box(0.5, 0.5, 0.5, 1.0);
384 glPopMatrix();
385 }
387 col[0] = 1;
388 col[1] = 1;
389 col[2] = 0.4;
390 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
391 draw_box(0.05, 1.2, 6, 1.0);
392 draw_box(6, 1.2, 0.05, 1.0);
393 }
395 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
396 {
397 glMatrixMode(GL_MODELVIEW);
398 glPushMatrix();
399 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
401 if(norm_sign < 0.0) {
402 glFrontFace(GL_CW);
403 }
405 glBegin(GL_QUADS);
406 glNormal3f(0, 0, 1 * norm_sign);
407 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
408 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
409 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
410 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
411 glNormal3f(1 * norm_sign, 0, 0);
412 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
413 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
414 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
415 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
416 glNormal3f(0, 0, -1 * norm_sign);
417 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
418 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
419 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
420 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
421 glNormal3f(-1 * norm_sign, 0, 0);
422 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
423 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
424 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
425 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
426 glEnd();
427 glBegin(GL_TRIANGLE_FAN);
428 glNormal3f(0, 1 * norm_sign, 0);
429 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
430 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
431 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
432 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
433 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
434 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
435 glEnd();
436 glBegin(GL_TRIANGLE_FAN);
437 glNormal3f(0, -1 * norm_sign, 0);
438 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
439 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
440 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
441 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
442 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
443 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
444 glEnd();
446 glFrontFace(GL_CCW);
447 glPopMatrix();
448 }
450 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
451 void update_rtarg(int width, int height)
452 {
453 if(!fbo) {
454 /* if fbo does not exist, then nothing does... create every opengl object */
455 glGenFramebuffers(1, &fbo);
456 glGenTextures(1, &fb_tex);
457 glGenRenderbuffers(1, &fb_depth);
459 glBindTexture(GL_TEXTURE_2D, fb_tex);
460 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
461 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
462 }
464 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
466 /* calculate the next power of two in both dimensions and use that as a texture size */
467 fb_tex_width = next_pow2(width);
468 fb_tex_height = next_pow2(height);
470 /* create and attach the texture that will be used as a color buffer */
471 glBindTexture(GL_TEXTURE_2D, fb_tex);
472 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
473 GL_RGBA, GL_UNSIGNED_BYTE, 0);
474 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
476 /* create and attach the renderbuffer that will serve as our z-buffer */
477 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
478 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
479 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
481 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
482 fprintf(stderr, "incomplete framebuffer!\n");
483 }
485 glBindFramebuffer(GL_FRAMEBUFFER, 0);
486 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
487 }
489 int handle_event(SDL_Event *ev)
490 {
491 switch(ev->type) {
492 case SDL_QUIT:
493 return -1;
495 case SDL_KEYDOWN:
496 case SDL_KEYUP:
497 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
498 return -1;
499 }
500 break;
502 case SDL_WINDOWEVENT:
503 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
504 reshape(ev->window.data1, ev->window.data2);
505 }
506 break;
508 default:
509 break;
510 }
512 return 0;
513 }
515 int key_event(int key, int state)
516 {
517 if(state) {
518 /*
519 ovrHSWDisplayState hsw;
520 ovrHmd_GetHSWDisplayState(hmd, &hsw);
521 if(hsw.Displayed) {
522 ovrHmd_DismissHSWDisplay(hmd);
523 }
524 */
526 switch(key) {
527 case 27:
528 return -1;
530 case ' ':
531 case 'r':
532 /* allow the user to recenter by pressing space */
533 ovrHmd_RecenterPose(hmd);
534 break;
536 case 'f':
537 /* press f to move the window to the HMD */
538 toggle_hmd_fullscreen();
539 break;
541 case 'v':
542 distort_caps ^= ovrDistortionCap_Vignette;
543 printf("Vignette: %s\n", distort_caps & ovrDistortionCap_Vignette ? "on" : "off");
544 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
545 break;
547 case 't':
548 distort_caps ^= ovrDistortionCap_TimeWarp;
549 printf("Time-warp: %s\n", distort_caps & ovrDistortionCap_TimeWarp ? "on" : "off");
550 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
551 break;
553 case 'o':
554 distort_caps ^= ovrDistortionCap_Overdrive;
555 printf("OLED over-drive: %s\n", distort_caps & ovrDistortionCap_Overdrive ? "on" : "off");
556 ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
557 break;
559 case 'l':
560 hmd_caps ^= ovrHmdCap_LowPersistence;
561 printf("Low-persistence display: %s\n", hmd_caps & ovrHmdCap_LowPersistence ? "on" : "off");
562 ovrHmd_SetEnabledCaps(hmd, hmd_caps);
563 break;
565 default:
566 break;
567 }
568 }
569 return 0;
570 }
572 unsigned int next_pow2(unsigned int x)
573 {
574 x -= 1;
575 x |= x >> 1;
576 x |= x >> 2;
577 x |= x >> 4;
578 x |= x >> 8;
579 x |= x >> 16;
580 return x + 1;
581 }
583 /* convert a quaternion to a rotation matrix */
584 void quat_to_matrix(const float *quat, float *mat)
585 {
586 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
587 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
588 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
589 mat[12] = 0.0f;
591 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
592 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
593 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
594 mat[13] = 0.0f;
596 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
597 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
598 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
599 mat[14] = 0.0f;
601 mat[3] = mat[7] = mat[11] = 0.0f;
602 mat[15] = 1.0f;
603 }
605 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
606 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
607 {
608 int i, j;
609 unsigned int tex;
610 unsigned char img[8 * 8 * 3];
611 unsigned char *pix = img;
613 for(i=0; i<8; i++) {
614 for(j=0; j<8; j++) {
615 int black = (i & 1) == (j & 1);
616 pix[0] = (black ? r0 : r1) * 255;
617 pix[1] = (black ? g0 : g1) * 255;
618 pix[2] = (black ? b0 : b1) * 255;
619 pix += 3;
620 }
621 }
623 glGenTextures(1, &tex);
624 glBindTexture(GL_TEXTURE_2D, tex);
625 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
626 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
627 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
629 return tex;
630 }