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