conworlds

changeset 6:3c36bc28c6c2

more stuff in the vr test
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 Aug 2014 01:08:03 +0300
parents 8b7da5ab814e
children bd8202d6d28d
files Makefile src/game.cc src/main.cc src/texture.cc src/texture.h src/vr/vr.c src/vr/vr.h src/vr/vr_libovr.c
diffstat 8 files changed, 117 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Wed Aug 20 16:34:43 2014 +0300
     1.2 +++ b/Makefile	Thu Aug 21 01:08:03 2014 +0300
     1.3 @@ -4,9 +4,13 @@
     1.4  dep = $(obj:.o=.d)
     1.5  bin = vrchess
     1.6  
     1.7 -CFLAGS = -pedantic -Wall -g
     1.8 +# comment out to disable LibOVR (oculus sdk)
     1.9 +#ovr_cflags = -DUSE_LIBOVR
    1.10 +#ovr_libs = -lovr
    1.11 +
    1.12 +CFLAGS = -pedantic -Wall -g $(ovr_cflags)
    1.13  CXXFLAGS = -std=c++11 $(CFLAGS)
    1.14 -LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago
    1.15 +LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago $(ovr_libs)
    1.16  
    1.17  libgl_unix = -lGL -lGLU -lglut -lGLEW
    1.18  libgl_mac = -framework OpenGL -framework GLUT -lGLEW
     2.1 --- a/src/game.cc	Wed Aug 20 16:34:43 2014 +0300
     2.2 +++ b/src/game.cc	Thu Aug 21 01:08:03 2014 +0300
     2.3 @@ -5,6 +5,10 @@
     2.4  #include "vr/vr.h"
     2.5  
     2.6  static void draw_scene();
     2.7 +static bool setup_rtarg(int x, int y);
     2.8 +
     2.9 +static Texture *rtarg[2];
    2.10 +static unsigned int fbo, rtarg_depth;
    2.11  
    2.12  static const float move_speed = 10.0f;
    2.13  
    2.14 @@ -24,6 +28,7 @@
    2.15  
    2.16  	glClearColor(0.1, 0.1, 0.1, 1);
    2.17  
    2.18 +
    2.19  	if(!floor_tex.load("data/tiles.png")) {
    2.20  		return false;
    2.21  	}
    2.22 @@ -36,9 +41,15 @@
    2.23  {
    2.24  	floor_tex.destroy();
    2.25  	vr_shutdown();
    2.26 +
    2.27 +	if(fbo) {
    2.28 +		glDeleteFramebuffers(1, &fbo);
    2.29 +		glDeleteRenderbuffers(1, &rtarg_depth);
    2.30 +		delete rtarg[0];
    2.31 +		delete rtarg[1];
    2.32 +	}
    2.33  }
    2.34  
    2.35 -
    2.36  void game_update(unsigned int msec)
    2.37  {
    2.38  	static unsigned int prev_msec;
    2.39 @@ -74,6 +85,8 @@
    2.40  
    2.41  void game_render(int eye)
    2.42  {
    2.43 +	vr_begin(eye <= 0 ? VR_EYE_LEFT : VR_EYE_RIGHT);
    2.44 +
    2.45  	float mat[16];
    2.46  	Matrix4x4 view_matrix = cam.get_matrix().inverse();
    2.47  
    2.48 @@ -96,6 +109,8 @@
    2.49  	glMultTransposeMatrixf(view_matrix[0]);
    2.50  
    2.51  	draw_scene();
    2.52 +
    2.53 +	vr_end();
    2.54  }
    2.55  
    2.56  void game_reshape(int x, int y)
    2.57 @@ -103,6 +118,8 @@
    2.58  	glViewport(0, 0, x, y);
    2.59  	fb_width = x;
    2.60  	fb_height = y;
    2.61 +
    2.62 +	setup_rtarg(x, y);
    2.63  }
    2.64  
    2.65  void game_keyboard(int key, bool pressed, int x, int y)
    2.66 @@ -195,3 +212,39 @@
    2.67  
    2.68  	glPopMatrix();
    2.69  }
    2.70 +
    2.71 +static bool setup_rtarg(int x, int y)
    2.72 +{
    2.73 +	int tex_width = next_pow2(x);
    2.74 +	int tex_height = next_pow2(y);
    2.75 +
    2.76 +	/* create render targets for each eye */
    2.77 +	if(!fbo) {
    2.78 +		glGenFramebuffers(1, &fbo);
    2.79 +		glGenRenderbuffers(1, &rtarg_depth);
    2.80 +
    2.81 +		for(int i=0; i<2; i++) {
    2.82 +			rtarg[i] = new Texture;
    2.83 +		}
    2.84 +	}
    2.85 +
    2.86 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    2.87 +
    2.88 +	glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
    2.89 +	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
    2.90 +	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
    2.91 +
    2.92 +	for(int i=0; i<2; i++) {
    2.93 +		rtarg[i] = new Texture;
    2.94 +		rtarg[i]->create2d(tex_width, tex_height);
    2.95 +		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
    2.96 +				GL_TEXTURE_2D, rtarg[i]->get_texture_id(), 0);
    2.97 +	}
    2.98 +
    2.99 +	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
   2.100 +		fprintf(stderr, "incomplete framebuffer!\n");
   2.101 +		return false;
   2.102 +	}
   2.103 +	glBindFramebuffer(GL_FRAMEBUFFER, 0);
   2.104 +	return true;
   2.105 +}
     3.1 --- a/src/main.cc	Wed Aug 20 16:34:43 2014 +0300
     3.2 +++ b/src/main.cc	Thu Aug 21 01:08:03 2014 +0300
     3.3 @@ -2,6 +2,7 @@
     3.4  #include <stdlib.h>
     3.5  #include "opengl.h"
     3.6  #include "game.h"
     3.7 +#include "vr/vr.h"
     3.8  
     3.9  static bool init();
    3.10  static void cleanup();
    3.11 @@ -64,7 +65,9 @@
    3.12  	game_update(msec);
    3.13  	game_render(0);
    3.14  
    3.15 -	glutSwapBuffers();
    3.16 +	if(!vr_swap_buffers()) {
    3.17 +		glutSwapBuffers();
    3.18 +	}
    3.19  }
    3.20  
    3.21  static void idle()
     4.1 --- a/src/texture.cc	Wed Aug 20 16:34:43 2014 +0300
     4.2 +++ b/src/texture.cc	Thu Aug 21 01:08:03 2014 +0300
     4.3 @@ -75,3 +75,14 @@
     4.4  	set_image(image);
     4.5  	return true;
     4.6  }
     4.7 +
     4.8 +unsigned int next_pow2(unsigned int x)
     4.9 +{
    4.10 +	x -= 1;
    4.11 +	x |= x >> 1;
    4.12 +	x |= x >> 2;
    4.13 +	x |= x >> 4;
    4.14 +	x |= x >> 8;
    4.15 +	x |= x >> 16;
    4.16 +	return x + 1;
    4.17 +}
     5.1 --- a/src/texture.h	Wed Aug 20 16:34:43 2014 +0300
     5.2 +++ b/src/texture.h	Thu Aug 21 01:08:03 2014 +0300
     5.3 @@ -26,4 +26,6 @@
     5.4  	bool load(const char *fname);
     5.5  };
     5.6  
     5.7 +unsigned int next_pow2(unsigned int x);
     5.8 +
     5.9  #endif	// TEXTURE_H_
     6.1 --- a/src/vr/vr.c	Wed Aug 20 16:34:43 2014 +0300
     6.2 +++ b/src/vr/vr.c	Thu Aug 21 01:08:03 2014 +0300
     6.3 @@ -108,17 +108,25 @@
     6.4  	return 0;
     6.5  }
     6.6  
     6.7 -void vr_present(unsigned int fbtex)
     6.8 +void vr_begin(int eye)
     6.9  {
    6.10 -	if(vrm && vrm->draw) {
    6.11 -		vrm->draw(fbtex, fbtex_rect[0], fbtex_rect[2], fbtex_rect[1], fbtex_rect[3]);
    6.12 +	if(vrm && vrm->begin) {
    6.13 +		vrm->begin(eye);
    6.14  	}
    6.15  }
    6.16  
    6.17 -void vr_fbrect(float u, float umax, float v, float vmax)
    6.18 +void vr_end(void)
    6.19  {
    6.20 -	fbtex_rect[0] = u;
    6.21 -	fbtex_rect[1] = v;
    6.22 -	fbtex_rect[2] = umax;
    6.23 -	fbtex_rect[3] = vmax;
    6.24 +	if(vrm && vrm->end) {
    6.25 +		vrm->end();
    6.26 +	}
    6.27  }
    6.28 +
    6.29 +int vr_swap_buffers(void)
    6.30 +{
    6.31 +	if(vrm && vrm->present) {
    6.32 +		vrm->present();
    6.33 +		return 1;
    6.34 +	}
    6.35 +	return 0;
    6.36 +}
     7.1 --- a/src/vr/vr.h	Wed Aug 20 16:34:43 2014 +0300
     7.2 +++ b/src/vr/vr.h	Thu Aug 21 01:08:03 2014 +0300
     7.3 @@ -33,7 +33,7 @@
     7.4  
     7.5  void vr_begin(int eye);
     7.6  void vr_end(void);
     7.7 -void vr_present(void);
     7.8 +int vr_swap_buffers(void);
     7.9  
    7.10  #ifdef __cplusplus
    7.11  }
     8.1 --- a/src/vr/vr_libovr.c	Wed Aug 20 16:34:43 2014 +0300
     8.2 +++ b/src/vr/vr_libovr.c	Thu Aug 21 01:08:03 2014 +0300
     8.3 @@ -4,9 +4,11 @@
     8.4  
     8.5  #include <stdio.h>
     8.6  #include <stdlib.h>
     8.7 +#include "vr_impl.h"
     8.8 +
     8.9 +#ifdef USE_LIBOVR
    8.10  #include <OVR_CAPI.h>
    8.11  #include <OVR_CAPI_GL.h>
    8.12 -#include "vr_impl.h"
    8.13  
    8.14  static ovrHmd hmd;
    8.15  
    8.16 @@ -105,3 +107,23 @@
    8.17  	}
    8.18  	return &m;
    8.19  }
    8.20 +
    8.21 +#else	/* no libovr */
    8.22 +
    8.23 +static int init(void)
    8.24 +{
    8.25 +	return -1;
    8.26 +}
    8.27 +
    8.28 +struct vr_module *vr_module_libovr(void)
    8.29 +{
    8.30 +	static struct vr_module m;
    8.31 +
    8.32 +	if(!m.init) {
    8.33 +		m.name = "libovr";
    8.34 +		m.init = init;
    8.35 +	}
    8.36 +	return &m;
    8.37 +}
    8.38 +
    8.39 +#endif	/* USE_LIBOVR */