libgoatvr

view example/src/main.c @ 5:e63cb28fc644

working on the linux side a bit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Sep 2014 10:56:45 +0300
parents
children b71314e80654
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 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
62 x = y = SDL_WINDOWPOS_UNDEFINED;
63 flags = SDL_WINDOW_OPENGL;
64 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1280, 800, flags))) {
65 fprintf(stderr, "failed to create window\n");
66 return -1;
67 }
68 if(!(ctx = SDL_GL_CreateContext(win))) {
69 fprintf(stderr, "failed to create OpenGL context\n");
70 return -1;
71 }
73 glewInit();
75 if(vr_init() == -1) {
76 return -1;
77 }
79 /* resize our window to match the HMD resolution */
80 win_width = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
81 win_height = vr_get_opti(VR_OPT_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_get_opti(VR_OPT_LEYE_XRES) + vr_get_opti(VR_OPT_REYE_XRES);
91 fb_height = vr_get_opti(VR_OPT_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, (float)(fb_tex_height - fb_height) / (float)fb_tex_height,
100 (float)fb_width / (float)fb_tex_width, 1.0);
102 glEnable(GL_DEPTH_TEST);
103 glEnable(GL_CULL_FACE);
104 glEnable(GL_LIGHTING);
105 glEnable(GL_LIGHT0);
106 glEnable(GL_LIGHT1);
107 glEnable(GL_NORMALIZE);
109 glClearColor(0.5, 0.1, 0.1, 1);
111 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
112 return 0;
113 }
115 void cleanup(void)
116 {
117 vr_shutdown();
118 SDL_Quit();
119 }
121 void toggle_hmd_fullscreen(void)
122 {
123 static int fullscr, prev_x, prev_y;
124 fullscr = !fullscr;
126 if(fullscr) {
127 /* going fullscreen on the rift. save current window position, and move it
128 * to the rift's part of the desktop before going fullscreen
129 */
130 SDL_GetWindowPosition(win, &prev_x, &prev_y);
131 SDL_SetWindowPosition(win, vr_get_opti(VR_OPT_WIN_XOFFS), vr_get_opti(VR_OPT_WIN_YOFFS));
132 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
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 }
138 }
140 void display(void)
141 {
142 int i;
143 float proj_mat[16];
144 float rot_mat[16], view_mat[16];
146 /* start drawing onto our texture render target */
147 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
150 /* for each eye ... */
151 for(i=0; i<2; i++) {
152 vr_begin(i);
154 /* -- viewport transformation --
155 * setup the viewport to draw in the left half of the framebuffer when we're
156 * rendering the left eye's view (0, 0, width/2, height), and in the right half
157 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
158 */
159 glViewport(i == 0 ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
161 glMatrixMode(GL_PROJECTION);
162 /* -- projection transformation --
163 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
164 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
165 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
166 */
167 if(vr_proj_matrix(i, 0.5, 500.0, proj_mat)) {
168 glLoadTransposeMatrixf(proj_mat);
169 } else {
170 glLoadIdentity();
171 gluPerspective(50.0, (float)fb_width / 2.0 / (float)fb_height, 0.5, 500.0);
172 }
174 /* -- view/camera transformation --
175 * we need to construct a view matrix by combining all the information provided by the oculus
176 * SDK, about the position and orientation of the user's head in the world.
177 */
178 glMatrixMode(GL_MODELVIEW);
179 vr_view_matrix(i, view_mat);
180 glLoadTransposeMatrixf(view_mat);
181 /* move the camera to the eye level of the user */
182 glTranslatef(0, -vr_get_optf(VR_OPT_EYE_HEIGHT), 0);
184 /* finally draw the scene for this eye */
185 draw_scene();
187 vr_end();
188 }
190 /* after drawing both eyes into the texture render target, revert to drawing directly to the
191 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
192 * compensated for lens distortion and chromatic abberation onto the HMD screen.
193 */
194 glBindFramebuffer(GL_FRAMEBUFFER, 0);
195 glViewport(0, 0, win_width, win_height);
197 vr_swap_buffers();
199 assert(glGetError() == GL_NO_ERROR);
200 }
202 void draw_scene(void)
203 {
204 int i;
205 float grey[] = {0.8, 0.8, 0.8, 1};
206 float col[] = {0, 0, 0, 1};
207 float lpos[][4] = {
208 {-8, 2, 10, 1},
209 {0, 15, 0, 1}
210 };
211 float lcol[][4] = {
212 {0.8, 0.8, 0.8, 1},
213 {0.4, 0.3, 0.3, 1}
214 };
216 for(i=0; i<2; i++) {
217 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
218 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
219 }
221 glMatrixMode(GL_MODELVIEW);
223 glPushMatrix();
224 glTranslatef(0, 10, 0);
225 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
226 glBindTexture(GL_TEXTURE_2D, chess_tex);
227 glEnable(GL_TEXTURE_2D);
228 draw_box(30, 20, 30, -1.0);
229 glDisable(GL_TEXTURE_2D);
230 glPopMatrix();
232 for(i=0; i<4; i++) {
233 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
234 glPushMatrix();
235 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
236 draw_box(0.5, 2, 0.5, 1.0);
237 glPopMatrix();
239 col[0] = i & 1 ? 1.0 : 0.3;
240 col[1] = i == 0 ? 1.0 : 0.3;
241 col[2] = i & 2 ? 1.0 : 0.3;
242 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
244 glPushMatrix();
245 if(i & 1) {
246 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
247 } else {
248 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
249 }
250 draw_box(0.5, 0.5, 0.5, 1.0);
251 glPopMatrix();
252 }
253 }
255 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
256 {
257 glMatrixMode(GL_MODELVIEW);
258 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
260 if(norm_sign < 0.0) {
261 glFrontFace(GL_CW);
262 }
264 glBegin(GL_QUADS);
265 glNormal3f(0, 0, 1 * norm_sign);
266 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
267 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
268 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
269 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
270 glNormal3f(1 * norm_sign, 0, 0);
271 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
272 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
273 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
274 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
275 glNormal3f(0, 0, -1 * norm_sign);
276 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
277 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
278 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
279 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
280 glNormal3f(-1 * norm_sign, 0, 0);
281 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
282 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
283 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
284 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
285 glEnd();
286 glBegin(GL_TRIANGLE_FAN);
287 glNormal3f(0, 1 * norm_sign, 0);
288 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
289 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
290 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
291 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
292 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
293 glTexCoord2f(0, 0); 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();
305 glFrontFace(GL_CCW);
306 }
308 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
309 void update_rtarg(int width, int height)
310 {
311 if(!fbo) {
312 /* if fbo does not exist, then nothing does... create every opengl object */
313 glGenFramebuffers(1, &fbo);
314 glGenTextures(1, &fb_tex);
315 glGenRenderbuffers(1, &fb_depth);
317 glBindTexture(GL_TEXTURE_2D, fb_tex);
318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
320 }
322 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
324 /* calculate the next power of two in both dimensions and use that as a texture size */
325 fb_tex_width = next_pow2(width);
326 fb_tex_height = next_pow2(height);
328 /* create and attach the texture that will be used as a color buffer */
329 glBindTexture(GL_TEXTURE_2D, fb_tex);
330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
331 GL_RGBA, GL_UNSIGNED_BYTE, 0);
332 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
334 /* create and attach the renderbuffer that will serve as our z-buffer */
335 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
336 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
337 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
339 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
340 fprintf(stderr, "incomplete framebuffer!\n");
341 }
343 glBindFramebuffer(GL_FRAMEBUFFER, 0);
344 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
345 }
347 int handle_event(SDL_Event *ev)
348 {
349 switch(ev->type) {
350 case SDL_QUIT:
351 return -1;
353 case SDL_KEYDOWN:
354 case SDL_KEYUP:
355 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
356 return -1;
357 }
358 break;
360 default:
361 break;
362 }
364 return 0;
365 }
367 int key_event(int key, int state)
368 {
369 if(state) {
370 switch(key) {
371 case 27:
372 return -1;
374 case ' ':
375 /* allow the user to recenter by pressing space */
376 vr_recenter();
377 break;
379 case 'f':
380 /* press f to move the window to the HMD */
381 toggle_hmd_fullscreen();
382 break;
384 default:
385 break;
386 }
387 }
388 return 0;
389 }
391 unsigned int next_pow2(unsigned int x)
392 {
393 x -= 1;
394 x |= x >> 1;
395 x |= x >> 2;
396 x |= x >> 4;
397 x |= x >> 8;
398 x |= x >> 16;
399 return x + 1;
400 }
402 /* convert a quaternion to a rotation matrix */
403 void quat_to_matrix(const float *quat, float *mat)
404 {
405 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
406 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
407 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
408 mat[12] = 0.0f;
410 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
411 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
412 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
413 mat[13] = 0.0f;
415 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
416 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
417 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
418 mat[14] = 0.0f;
420 mat[3] = mat[7] = mat[11] = 0.0f;
421 mat[15] = 1.0f;
422 }
424 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
425 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
426 {
427 int i, j;
428 unsigned int tex;
429 unsigned char img[8 * 8 * 3];
430 unsigned char *pix = img;
432 for(i=0; i<8; i++) {
433 for(j=0; j<8; j++) {
434 int black = (i & 1) == (j & 1);
435 pix[0] = (black ? r0 : r1) * 255;
436 pix[1] = (black ? g0 : g1) * 255;
437 pix[2] = (black ? b0 : b1) * 255;
438 pix += 3;
439 }
440 }
442 glGenTextures(1, &tex);
443 glBindTexture(GL_TEXTURE_2D, tex);
444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
445 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
446 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
448 return tex;
449 }