libgoatvr

view example/src/main.c @ 6:b71314e80654

fixed the null plugin
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 19 Sep 2014 05:30:20 +0300
parents e63cb28fc644
children 3d9ec6fe97d7
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, 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_get_opti(VR_OPT_WIN_XOFFS), vr_get_opti(VR_OPT_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];
143 float rot_mat[16], view_mat[16];
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 == 0 ? 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 glLoadTransposeMatrixf(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 glLoadTransposeMatrixf(view_mat);
180 /* move the camera to the eye level of the user */
181 glTranslatef(0, -vr_get_optf(VR_OPT_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 }
252 }
254 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
255 {
256 glMatrixMode(GL_MODELVIEW);
257 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
259 if(norm_sign < 0.0) {
260 glFrontFace(GL_CW);
261 }
263 glBegin(GL_QUADS);
264 glNormal3f(0, 0, 1 * norm_sign);
265 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
266 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
267 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
268 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
269 glNormal3f(1 * norm_sign, 0, 0);
270 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
271 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
272 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
273 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
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 glEnd();
285 glBegin(GL_TRIANGLE_FAN);
286 glNormal3f(0, 1 * norm_sign, 0);
287 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 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 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
293 glEnd();
294 glBegin(GL_TRIANGLE_FAN);
295 glNormal3f(0, -1 * norm_sign, 0);
296 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
297 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
298 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
299 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
300 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
301 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
302 glEnd();
304 glFrontFace(GL_CCW);
305 }
307 /* update_rtarg creates (and/or resizes) the render target used to draw the two stero views */
308 void update_rtarg(int width, int height)
309 {
310 if(!fbo) {
311 /* if fbo does not exist, then nothing does... create every opengl object */
312 glGenFramebuffers(1, &fbo);
313 glGenTextures(1, &fb_tex);
314 glGenRenderbuffers(1, &fb_depth);
316 glBindTexture(GL_TEXTURE_2D, fb_tex);
317 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
319 }
321 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
323 /* calculate the next power of two in both dimensions and use that as a texture size */
324 fb_tex_width = next_pow2(width);
325 fb_tex_height = next_pow2(height);
327 /* create and attach the texture that will be used as a color buffer */
328 glBindTexture(GL_TEXTURE_2D, fb_tex);
329 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
330 GL_RGBA, GL_UNSIGNED_BYTE, 0);
331 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
333 /* create and attach the renderbuffer that will serve as our z-buffer */
334 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
335 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
336 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
338 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
339 fprintf(stderr, "incomplete framebuffer!\n");
340 }
342 glBindFramebuffer(GL_FRAMEBUFFER, 0);
343 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
344 }
346 int handle_event(SDL_Event *ev)
347 {
348 switch(ev->type) {
349 case SDL_QUIT:
350 return -1;
352 case SDL_KEYDOWN:
353 case SDL_KEYUP:
354 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
355 return -1;
356 }
357 break;
359 default:
360 break;
361 }
363 return 0;
364 }
366 int key_event(int key, int state)
367 {
368 if(state) {
369 switch(key) {
370 case 27:
371 return -1;
373 case ' ':
374 /* allow the user to recenter by pressing space */
375 vr_recenter();
376 break;
378 case 'f':
379 /* press f to move the window to the HMD */
380 toggle_hmd_fullscreen();
381 break;
383 default:
384 break;
385 }
386 }
387 return 0;
388 }
390 unsigned int next_pow2(unsigned int x)
391 {
392 x -= 1;
393 x |= x >> 1;
394 x |= x >> 2;
395 x |= x >> 4;
396 x |= x >> 8;
397 x |= x >> 16;
398 return x + 1;
399 }
401 /* convert a quaternion to a rotation matrix */
402 void quat_to_matrix(const float *quat, float *mat)
403 {
404 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
405 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
406 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
407 mat[12] = 0.0f;
409 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
410 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
411 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
412 mat[13] = 0.0f;
414 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
415 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
416 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
417 mat[14] = 0.0f;
419 mat[3] = mat[7] = mat[11] = 0.0f;
420 mat[15] = 1.0f;
421 }
423 /* generate a chessboard texture with tiles colored (r0, g0, b0) and (r1, g1, b1) */
424 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
425 {
426 int i, j;
427 unsigned int tex;
428 unsigned char img[8 * 8 * 3];
429 unsigned char *pix = img;
431 for(i=0; i<8; i++) {
432 for(j=0; j<8; j++) {
433 int black = (i & 1) == (j & 1);
434 pix[0] = (black ? r0 : r1) * 255;
435 pix[1] = (black ? g0 : g1) * 255;
436 pix[2] = (black ? b0 : b1) * 255;
437 pix += 3;
438 }
439 }
441 glGenTextures(1, &tex);
442 glBindTexture(GL_TEXTURE_2D, tex);
443 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
445 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
447 return tex;
448 }