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