# HG changeset patch # User John Tsiombikas # Date 1408572483 -10800 # Node ID 3c36bc28c6c283b2841957485daf7185e43ff10f # Parent 8b7da5ab814eb942ceecd1f4b44a4dad983b3945 more stuff in the vr test diff -r 8b7da5ab814e -r 3c36bc28c6c2 Makefile --- a/Makefile Wed Aug 20 16:34:43 2014 +0300 +++ b/Makefile Thu Aug 21 01:08:03 2014 +0300 @@ -4,9 +4,13 @@ dep = $(obj:.o=.d) bin = vrchess -CFLAGS = -pedantic -Wall -g +# comment out to disable LibOVR (oculus sdk) +#ovr_cflags = -DUSE_LIBOVR +#ovr_libs = -lovr + +CFLAGS = -pedantic -Wall -g $(ovr_cflags) CXXFLAGS = -std=c++11 $(CFLAGS) -LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago +LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago $(ovr_libs) libgl_unix = -lGL -lGLU -lglut -lGLEW libgl_mac = -framework OpenGL -framework GLUT -lGLEW diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/game.cc --- a/src/game.cc Wed Aug 20 16:34:43 2014 +0300 +++ b/src/game.cc Thu Aug 21 01:08:03 2014 +0300 @@ -5,6 +5,10 @@ #include "vr/vr.h" static void draw_scene(); +static bool setup_rtarg(int x, int y); + +static Texture *rtarg[2]; +static unsigned int fbo, rtarg_depth; static const float move_speed = 10.0f; @@ -24,6 +28,7 @@ glClearColor(0.1, 0.1, 0.1, 1); + if(!floor_tex.load("data/tiles.png")) { return false; } @@ -36,9 +41,15 @@ { floor_tex.destroy(); vr_shutdown(); + + if(fbo) { + glDeleteFramebuffers(1, &fbo); + glDeleteRenderbuffers(1, &rtarg_depth); + delete rtarg[0]; + delete rtarg[1]; + } } - void game_update(unsigned int msec) { static unsigned int prev_msec; @@ -74,6 +85,8 @@ void game_render(int eye) { + vr_begin(eye <= 0 ? VR_EYE_LEFT : VR_EYE_RIGHT); + float mat[16]; Matrix4x4 view_matrix = cam.get_matrix().inverse(); @@ -96,6 +109,8 @@ glMultTransposeMatrixf(view_matrix[0]); draw_scene(); + + vr_end(); } void game_reshape(int x, int y) @@ -103,6 +118,8 @@ glViewport(0, 0, x, y); fb_width = x; fb_height = y; + + setup_rtarg(x, y); } void game_keyboard(int key, bool pressed, int x, int y) @@ -195,3 +212,39 @@ glPopMatrix(); } + +static bool setup_rtarg(int x, int y) +{ + int tex_width = next_pow2(x); + int tex_height = next_pow2(y); + + /* create render targets for each eye */ + if(!fbo) { + glGenFramebuffers(1, &fbo); + glGenRenderbuffers(1, &rtarg_depth); + + for(int i=0; i<2; i++) { + rtarg[i] = new Texture; + } + } + + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth); + + for(int i=0; i<2; i++) { + rtarg[i] = new Texture; + rtarg[i]->create2d(tex_width, tex_height); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, rtarg[i]->get_texture_id(), 0); + } + + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + fprintf(stderr, "incomplete framebuffer!\n"); + return false; + } + glBindFramebuffer(GL_FRAMEBUFFER, 0); + return true; +} diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/main.cc --- a/src/main.cc Wed Aug 20 16:34:43 2014 +0300 +++ b/src/main.cc Thu Aug 21 01:08:03 2014 +0300 @@ -2,6 +2,7 @@ #include #include "opengl.h" #include "game.h" +#include "vr/vr.h" static bool init(); static void cleanup(); @@ -64,7 +65,9 @@ game_update(msec); game_render(0); - glutSwapBuffers(); + if(!vr_swap_buffers()) { + glutSwapBuffers(); + } } static void idle() diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/texture.cc --- a/src/texture.cc Wed Aug 20 16:34:43 2014 +0300 +++ b/src/texture.cc Thu Aug 21 01:08:03 2014 +0300 @@ -75,3 +75,14 @@ set_image(image); return true; } + +unsigned int next_pow2(unsigned int x) +{ + x -= 1; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return x + 1; +} diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/texture.h --- a/src/texture.h Wed Aug 20 16:34:43 2014 +0300 +++ b/src/texture.h Thu Aug 21 01:08:03 2014 +0300 @@ -26,4 +26,6 @@ bool load(const char *fname); }; +unsigned int next_pow2(unsigned int x); + #endif // TEXTURE_H_ diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/vr/vr.c --- a/src/vr/vr.c Wed Aug 20 16:34:43 2014 +0300 +++ b/src/vr/vr.c Thu Aug 21 01:08:03 2014 +0300 @@ -108,17 +108,25 @@ return 0; } -void vr_present(unsigned int fbtex) +void vr_begin(int eye) { - if(vrm && vrm->draw) { - vrm->draw(fbtex, fbtex_rect[0], fbtex_rect[2], fbtex_rect[1], fbtex_rect[3]); + if(vrm && vrm->begin) { + vrm->begin(eye); } } -void vr_fbrect(float u, float umax, float v, float vmax) +void vr_end(void) { - fbtex_rect[0] = u; - fbtex_rect[1] = v; - fbtex_rect[2] = umax; - fbtex_rect[3] = vmax; + if(vrm && vrm->end) { + vrm->end(); + } } + +int vr_swap_buffers(void) +{ + if(vrm && vrm->present) { + vrm->present(); + return 1; + } + return 0; +} diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/vr/vr.h --- a/src/vr/vr.h Wed Aug 20 16:34:43 2014 +0300 +++ b/src/vr/vr.h Thu Aug 21 01:08:03 2014 +0300 @@ -33,7 +33,7 @@ void vr_begin(int eye); void vr_end(void); -void vr_present(void); +int vr_swap_buffers(void); #ifdef __cplusplus } diff -r 8b7da5ab814e -r 3c36bc28c6c2 src/vr/vr_libovr.c --- a/src/vr/vr_libovr.c Wed Aug 20 16:34:43 2014 +0300 +++ b/src/vr/vr_libovr.c Thu Aug 21 01:08:03 2014 +0300 @@ -4,9 +4,11 @@ #include #include +#include "vr_impl.h" + +#ifdef USE_LIBOVR #include #include -#include "vr_impl.h" static ovrHmd hmd; @@ -105,3 +107,23 @@ } return &m; } + +#else /* no libovr */ + +static int init(void) +{ + return -1; +} + +struct vr_module *vr_module_libovr(void) +{ + static struct vr_module m; + + if(!m.init) { + m.name = "libovr"; + m.init = init; + } + return &m; +} + +#endif /* USE_LIBOVR */