libgoatvr
view example/src/main.c @ 24:d659cbedde1d
works on linux with 0.4.4
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 14 Jan 2015 08:03:27 +0200 |
parents | 34d4643d61f9 |
children | 5136dfcea7b1 |
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_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 } else {
133 /* return to windowed mode and move the window back to its original position */
134 SDL_SetWindowFullscreen(win, 0);
135 SDL_SetWindowPosition(win, prev_x, prev_y);
136 }
137 }
139 void display(void)
140 {
141 int i;
142 float proj_mat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
143 float view_mat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
145 /* start drawing onto our texture render target */
146 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
147 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
149 /* for each eye ... */
150 for(i=0; i<2; i++) {
151 vr_begin(i);
153 /* -- viewport transformation --
154 * setup the viewport to draw in the left half of the framebuffer when we're
155 * rendering the left eye's view (0, 0, width/2, height), and in the right half
156 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
157 */
158 glViewport(i == VR_EYE_LEFT ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
160 glMatrixMode(GL_PROJECTION);
161 /* -- projection transformation --
162 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
163 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
164 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
165 */
166 if(vr_proj_matrix(i, 0.5, 500.0, proj_mat)) {
167 glLoadMatrixf(proj_mat);
168 } else {
169 glLoadIdentity();
170 gluPerspective(50.0, (float)fb_width / 2.0 / (float)fb_height, 0.5, 500.0);
171 }
173 /* -- view/camera transformation --
174 * we need to construct a view matrix by combining all the information provided by the oculus
175 * SDK, about the position and orientation of the user's head in the world.
176 */
177 glMatrixMode(GL_MODELVIEW);
178 vr_view_matrix(i, view_mat);
179 glLoadMatrixf(view_mat);
180 /* move the camera to the eye level of the user */
181 glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
183 /* finally draw the scene for this eye */
184 draw_scene();
186 vr_end();
187 }
189 /* after drawing both eyes into the texture render target, revert to drawing directly to the
190 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
191 * compensated for lens distortion and chromatic abberation onto the HMD screen.
192 */
193 glBindFramebuffer(GL_FRAMEBUFFER, 0);
194 glViewport(0, 0, win_width, win_height);
196 vr_swap_buffers();
198 assert(glGetError() == GL_NO_ERROR);
199 }
201 void draw_scene(void)
202 {
203 int i;
204 float grey[] = {0.8, 0.8, 0.8, 1};
205 float col[] = {0, 0, 0, 1};
206 float lpos[][4] = {
207 {-8, 2, 10, 1},
208 {0, 15, 0, 1}
209 };
210 float lcol[][4] = {
211 {0.8, 0.8, 0.8, 1},
212 {0.4, 0.3, 0.3, 1}
213 };
215 for(i=0; i<2; i++) {
216 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
217 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
218 }
220 glMatrixMode(GL_MODELVIEW);
222 glPushMatrix();
223 glTranslatef(0, 10, 0);
224 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
225 glBindTexture(GL_TEXTURE_2D, chess_tex);
226 glEnable(GL_TEXTURE_2D);
227 draw_box(30, 20, 30, -1.0);
228 glDisable(GL_TEXTURE_2D);
229 glPopMatrix();
231 for(i=0; i<4; i++) {
232 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
233 glPushMatrix();
234 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
235 draw_box(0.5, 2, 0.5, 1.0);
236 glPopMatrix();
238 col[0] = i & 1 ? 1.0 : 0.3;
239 col[1] = i == 0 ? 1.0 : 0.3;
240 col[2] = i & 2 ? 1.0 : 0.3;
241 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
243 glPushMatrix();
244 if(i & 1) {
245 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
246 } else {
247 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
248 }
249 draw_box(0.5, 0.5, 0.5, 1.0);
250 glPopMatrix();
251 }
253 col[0] = 1;
254 col[1] = 1;
255 col[2] = 0.4;
256 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
257 draw_box(0.05, 1.2, 6, 1.0);
258 draw_box(6, 1.2, 0.05, 1.0);
259 }
261 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
262 {
263 glMatrixMode(GL_MODELVIEW);
264 glPushMatrix();
265 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
267 if(norm_sign < 0.0) {
268 glFrontFace(GL_CW);
269 }
271 glBegin(GL_QUADS);
272 glNormal3f(0, 0, 1 * norm_sign);
273 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
274 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
275 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
276 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
277 glNormal3f(1 * norm_sign, 0, 0);
278 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
279 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
280 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
281 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
282 glNormal3f(0, 0, -1 * norm_sign);
283 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
284 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
285 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
286 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
287 glNormal3f(-1 * norm_sign, 0, 0);
288 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
289 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
290 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
291 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
292 glEnd();
293 glBegin(GL_TRIANGLE_FAN);
294 glNormal3f(0, 1 * norm_sign, 0);
295 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
296 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
297 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
298 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
299 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
300 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
301 glEnd();
302 glBegin(GL_TRIANGLE_FAN);
303 glNormal3f(0, -1 * norm_sign, 0);
304 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
305 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
306 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
307 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
308 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
309 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
310 glEnd();
312 glFrontFace(GL_CCW);
313 glPopMatrix();
314 }
316 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
317 void update_rtarg(int width, int height)
318 {
319 if(!fbo) {
320 /* if fbo does not exist, then nothing does... create every opengl object */
321 glGenFramebuffers(1, &fbo);
322 glGenTextures(1, &fb_tex);
323 glGenRenderbuffers(1, &fb_depth);
325 glBindTexture(GL_TEXTURE_2D, fb_tex);
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
328 }
330 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
332 /* calculate the next power of two in both dimensions and use that as a texture size */
333 fb_tex_width = next_pow2(width);
334 fb_tex_height = next_pow2(height);
336 /* create and attach the texture that will be used as a color buffer */
337 glBindTexture(GL_TEXTURE_2D, fb_tex);
338 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
339 GL_RGBA, GL_UNSIGNED_BYTE, 0);
340 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
342 /* create and attach the renderbuffer that will serve as our z-buffer */
343 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
344 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
345 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
347 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
348 fprintf(stderr, "incomplete framebuffer!\n");
349 }
351 glBindFramebuffer(GL_FRAMEBUFFER, 0);
352 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
353 }
355 int handle_event(SDL_Event *ev)
356 {
357 switch(ev->type) {
358 case SDL_QUIT:
359 return -1;
361 case SDL_KEYDOWN:
362 case SDL_KEYUP:
363 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
364 return -1;
365 }
366 break;
368 default:
369 break;
370 }
372 return 0;
373 }
375 int key_event(int key, int state)
376 {
377 if(state) {
378 switch(key) {
379 case 27:
380 return -1;
382 case ' ':
383 /* allow the user to recenter by pressing space */
384 vr_recenter();
385 break;
387 case 'f':
388 /* press f to move the window to the HMD */
389 toggle_hmd_fullscreen();
390 break;
392 default:
393 break;
394 }
395 }
396 return 0;
397 }
399 unsigned int next_pow2(unsigned int x)
400 {
401 x -= 1;
402 x |= x >> 1;
403 x |= x >> 2;
404 x |= x >> 4;
405 x |= x >> 8;
406 x |= x >> 16;
407 return x + 1;
408 }
410 /* convert a quaternion to a rotation matrix */
411 void quat_to_matrix(const float *quat, float *mat)
412 {
413 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
414 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
415 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
416 mat[12] = 0.0f;
418 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
419 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
420 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
421 mat[13] = 0.0f;
423 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
424 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
425 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
426 mat[14] = 0.0f;
428 mat[3] = mat[7] = mat[11] = 0.0f;
429 mat[15] = 1.0f;
430 }
432 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
433 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
434 {
435 int i, j;
436 unsigned int tex;
437 unsigned char img[8 * 8 * 3];
438 unsigned char *pix = img;
440 for(i=0; i<8; i++) {
441 for(j=0; j<8; j++) {
442 int black = (i & 1) == (j & 1);
443 pix[0] = (black ? r0 : r1) * 255;
444 pix[1] = (black ? g0 : g1) * 255;
445 pix[2] = (black ? b0 : b1) * 255;
446 pix += 3;
447 }
448 }
450 glGenTextures(1, &tex);
451 glBindTexture(GL_TEXTURE_2D, tex);
452 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
453 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
454 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
456 return tex;
457 }