libgoatvr

view example/src/main.c @ 30:1a8343ea54ce

fixed on windows
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Apr 2015 04:01:47 +0300
parents 5136dfcea7b1
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <SDL2/SDL.h>
5 #include <GL/glew.h>
6 #include "vr.h"
8 int init(void);
9 void cleanup(void);
10 void toggle_hmd_fullscreen(void);
11 void display(void);
12 void draw_scene(void);
13 void draw_box(float xsz, float ysz, float zsz, float norm_sign);
14 void update_rtarg(int width, int height);
15 int handle_event(SDL_Event *ev);
16 int key_event(int key, int state);
17 void reshape(int x, int y);
18 unsigned int next_pow2(unsigned int x);
19 void quat_to_matrix(const float *quat, float *mat);
20 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1);
22 static SDL_Window *win;
23 static SDL_GLContext ctx;
24 static int win_width, win_height;
26 static unsigned int fbo, fb_tex, fb_depth;
27 static int fb_width, fb_height;
28 static int fb_tex_width, fb_tex_height;
30 static unsigned int chess_tex;
33 int main(int argc, char **argv)
34 {
35 if(init() == -1) {
36 return 1;
37 }
39 for(;;) {
40 SDL_Event ev;
41 while(SDL_PollEvent(&ev)) {
42 if(handle_event(&ev) == -1) {
43 goto done;
44 }
45 }
46 display();
47 }
49 done:
50 cleanup();
51 return 0;
52 }
55 int init(void)
56 {
57 int x, y;
58 unsigned int flags;
60 if(vr_init() == -1) {
61 return -1;
62 }
64 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
66 x = y = SDL_WINDOWPOS_UNDEFINED;
67 flags = SDL_WINDOW_OPENGL;
68 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1280, 800, flags))) {
69 fprintf(stderr, "failed to create window\n");
70 return -1;
71 }
72 if(!(ctx = SDL_GL_CreateContext(win))) {
73 fprintf(stderr, "failed to create OpenGL context\n");
74 return -1;
75 }
77 glewInit();
79 /* resize our window to match the HMD resolution */
80 win_width = vr_geti(VR_DISPLAY_WIDTH);
81 win_height = vr_geti(VR_DISPLAY_HEIGHT);
82 if(!win_width || !win_height) {
83 SDL_GetWindowSize(win, &win_width, &win_height);
84 } else {
85 SDL_SetWindowSize(win, win_width, win_height);
86 SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
87 }
89 /* and create a single render target texture to encompass both eyes */
90 fb_width = vr_geti(VR_LEYE_XRES) + vr_geti(VR_REYE_XRES);
91 fb_height = vr_geti(VR_LEYE_YRES); /* assuming both are the same */
92 if(!fb_width || !fb_height) {
93 fb_width = win_width;
94 fb_height = win_height;
95 }
96 update_rtarg(fb_width, fb_height);
98 /* set our render texture and its active area */
99 vr_output_texture(fb_tex, 0, 0, (float)fb_width / (float)fb_tex_width, (float)fb_height / (float)fb_tex_height);
101 glEnable(GL_DEPTH_TEST);
102 glEnable(GL_CULL_FACE);
103 glEnable(GL_LIGHTING);
104 glEnable(GL_LIGHT0);
105 glEnable(GL_LIGHT1);
106 glEnable(GL_NORMALIZE);
108 glClearColor(0.5, 0.1, 0.1, 1);
110 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
111 return 0;
112 }
114 void cleanup(void)
115 {
116 vr_shutdown();
117 SDL_Quit();
118 }
120 void toggle_hmd_fullscreen(void)
121 {
122 static int fullscr, prev_x, prev_y;
123 fullscr = !fullscr;
125 if(fullscr) {
126 /* going fullscreen on the rift. save current window position, and move it
127 * to the rift's part of the desktop before going fullscreen
128 */
129 SDL_GetWindowPosition(win, &prev_x, &prev_y);
130 SDL_SetWindowPosition(win, vr_geti(VR_WIN_XOFFS), vr_geti(VR_WIN_YOFFS));
131 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
132 SDL_SetRelativeMouseMode(1);
133 } else {
134 /* return to windowed mode and move the window back to its original position */
135 SDL_SetWindowFullscreen(win, 0);
136 SDL_SetWindowPosition(win, prev_x, prev_y);
137 SDL_SetRelativeMouseMode(0);
138 }
139 }
141 void display(void)
142 {
143 int i;
144 float proj_mat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
145 float view_mat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
147 /* start drawing onto our texture render target */
148 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
149 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
151 /* for each eye ... */
152 for(i=0; i<2; i++) {
153 vr_begin(i);
155 /* -- viewport transformation --
156 * setup the viewport to draw in the left half of the framebuffer when we're
157 * rendering the left eye's view (0, 0, width/2, height), and in the right half
158 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
159 */
160 glViewport(i == VR_EYE_LEFT ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
162 glMatrixMode(GL_PROJECTION);
163 /* -- projection transformation --
164 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
165 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
166 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
167 */
168 if(vr_proj_matrix(i, 0.5, 500.0, proj_mat)) {
169 glLoadMatrixf(proj_mat);
170 } else {
171 glLoadIdentity();
172 gluPerspective(50.0, (float)fb_width / 2.0 / (float)fb_height, 0.5, 500.0);
173 }
175 /* -- view/camera transformation --
176 * we need to construct a view matrix by combining all the information provided by the oculus
177 * SDK, about the position and orientation of the user's head in the world.
178 */
179 glMatrixMode(GL_MODELVIEW);
180 vr_view_matrix(i, view_mat);
181 glLoadMatrixf(view_mat);
182 /* move the camera to the eye level of the user */
183 glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
185 /* finally draw the scene for this eye */
186 draw_scene();
188 vr_end();
189 }
191 /* after drawing both eyes into the texture render target, revert to drawing directly to the
192 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
193 * compensated for lens distortion and chromatic abberation onto the HMD screen.
194 */
195 glBindFramebuffer(GL_FRAMEBUFFER, 0);
196 glViewport(0, 0, win_width, win_height);
198 vr_swap_buffers();
200 assert(glGetError() == GL_NO_ERROR);
201 }
203 void draw_scene(void)
204 {
205 int i;
206 float grey[] = {0.8, 0.8, 0.8, 1};
207 float col[] = {0, 0, 0, 1};
208 float lpos[][4] = {
209 {-8, 2, 10, 1},
210 {0, 15, 0, 1}
211 };
212 float lcol[][4] = {
213 {0.8, 0.8, 0.8, 1},
214 {0.4, 0.3, 0.3, 1}
215 };
217 for(i=0; i<2; i++) {
218 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
219 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
220 }
222 glMatrixMode(GL_MODELVIEW);
224 glPushMatrix();
225 glTranslatef(0, 10, 0);
226 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
227 glBindTexture(GL_TEXTURE_2D, chess_tex);
228 glEnable(GL_TEXTURE_2D);
229 draw_box(30, 20, 30, -1.0);
230 glDisable(GL_TEXTURE_2D);
231 glPopMatrix();
233 for(i=0; i<4; i++) {
234 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
235 glPushMatrix();
236 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
237 draw_box(0.5, 2, 0.5, 1.0);
238 glPopMatrix();
240 col[0] = i & 1 ? 1.0 : 0.3;
241 col[1] = i == 0 ? 1.0 : 0.3;
242 col[2] = i & 2 ? 1.0 : 0.3;
243 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
245 glPushMatrix();
246 if(i & 1) {
247 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
248 } else {
249 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
250 }
251 draw_box(0.5, 0.5, 0.5, 1.0);
252 glPopMatrix();
253 }
255 col[0] = 1;
256 col[1] = 1;
257 col[2] = 0.4;
258 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
259 draw_box(0.05, 1.2, 6, 1.0);
260 draw_box(6, 1.2, 0.05, 1.0);
261 }
263 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
264 {
265 glMatrixMode(GL_MODELVIEW);
266 glPushMatrix();
267 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
269 if(norm_sign < 0.0) {
270 glFrontFace(GL_CW);
271 }
273 glBegin(GL_QUADS);
274 glNormal3f(0, 0, 1 * norm_sign);
275 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
276 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
277 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
278 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
279 glNormal3f(1 * norm_sign, 0, 0);
280 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
281 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
282 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
283 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
284 glNormal3f(0, 0, -1 * norm_sign);
285 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
286 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
287 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
288 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
289 glNormal3f(-1 * norm_sign, 0, 0);
290 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
291 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
292 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
293 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
294 glEnd();
295 glBegin(GL_TRIANGLE_FAN);
296 glNormal3f(0, 1 * norm_sign, 0);
297 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
298 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
299 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
300 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
301 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
302 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
303 glEnd();
304 glBegin(GL_TRIANGLE_FAN);
305 glNormal3f(0, -1 * norm_sign, 0);
306 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
307 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
308 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
309 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
310 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
311 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
312 glEnd();
314 glFrontFace(GL_CCW);
315 glPopMatrix();
316 }
318 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
319 void update_rtarg(int width, int height)
320 {
321 if(!fbo) {
322 /* if fbo does not exist, then nothing does... create every opengl object */
323 glGenFramebuffers(1, &fbo);
324 glGenTextures(1, &fb_tex);
325 glGenRenderbuffers(1, &fb_depth);
327 glBindTexture(GL_TEXTURE_2D, fb_tex);
328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
330 }
332 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
334 /* calculate the next power of two in both dimensions and use that as a texture size */
335 fb_tex_width = next_pow2(width);
336 fb_tex_height = next_pow2(height);
338 /* create and attach the texture that will be used as a color buffer */
339 glBindTexture(GL_TEXTURE_2D, fb_tex);
340 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
341 GL_RGBA, GL_UNSIGNED_BYTE, 0);
342 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
344 /* create and attach the renderbuffer that will serve as our z-buffer */
345 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
346 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
347 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
349 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
350 fprintf(stderr, "incomplete framebuffer!\n");
351 }
353 glBindFramebuffer(GL_FRAMEBUFFER, 0);
354 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
355 }
357 int handle_event(SDL_Event *ev)
358 {
359 switch(ev->type) {
360 case SDL_QUIT:
361 return -1;
363 case SDL_KEYDOWN:
364 case SDL_KEYUP:
365 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
366 return -1;
367 }
368 break;
370 default:
371 break;
372 }
374 return 0;
375 }
377 int key_event(int key, int state)
378 {
379 if(state) {
380 switch(key) {
381 case 27:
382 return -1;
384 case ' ':
385 /* allow the user to recenter by pressing space */
386 vr_recenter();
387 break;
389 case 'f':
390 /* press f to move the window to the HMD */
391 toggle_hmd_fullscreen();
392 break;
394 default:
395 break;
396 }
397 }
398 return 0;
399 }
401 unsigned int next_pow2(unsigned int x)
402 {
403 x -= 1;
404 x |= x >> 1;
405 x |= x >> 2;
406 x |= x >> 4;
407 x |= x >> 8;
408 x |= x >> 16;
409 return x + 1;
410 }
412 /* convert a quaternion to a rotation matrix */
413 void quat_to_matrix(const float *quat, float *mat)
414 {
415 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
416 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
417 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
418 mat[12] = 0.0f;
420 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
421 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
422 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
423 mat[13] = 0.0f;
425 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
426 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
427 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
428 mat[14] = 0.0f;
430 mat[3] = mat[7] = mat[11] = 0.0f;
431 mat[15] = 1.0f;
432 }
434 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
435 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
436 {
437 int i, j;
438 unsigned int tex;
439 unsigned char img[8 * 8 * 3];
440 unsigned char *pix = img;
442 for(i=0; i<8; i++) {
443 for(j=0; j<8; j++) {
444 int black = (i & 1) == (j & 1);
445 pix[0] = (black ? r0 : r1) * 255;
446 pix[1] = (black ? g0 : g1) * 255;
447 pix[2] = (black ? b0 : b1) * 255;
448 pix += 3;
449 }
450 }
452 glGenTextures(1, &tex);
453 glBindTexture(GL_TEXTURE_2D, tex);
454 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
455 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
456 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
458 return tex;
459 }