vrchess

changeset 4:e6948e131526

adding a vr wrapper
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 20 Aug 2014 06:33:43 +0300
parents a797e426e309
children 8b7da5ab814e
files .clang_complete Makefile src/game.cc src/vr/vr.c src/vr/vr.h src/vr/vr_impl.h src/vr/vr_libovr.c src/vr/vr_modules.c src/vr/vr_null.c
diffstat 9 files changed, 394 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.clang_complete	Wed Aug 20 06:33:43 2014 +0300
     1.3 @@ -0,0 +1,2 @@
     1.4 +-Isrc
     1.5 +-Isrc/vr
     2.1 --- a/Makefile	Tue Aug 19 11:01:04 2014 +0300
     2.2 +++ b/Makefile	Wed Aug 20 06:33:43 2014 +0300
     2.3 @@ -1,4 +1,4 @@
     2.4 -csrc = $(wildcard src/*.c)
     2.5 +csrc = $(wildcard src/*.c) $(wildcard src/vr/*.c)
     2.6  ccsrc = $(wildcard src/*.cc)
     2.7  obj = $(csrc:.c=.o) $(ccsrc:.cc=.o)
     2.8  dep = $(obj:.o=.d)
     3.1 --- a/src/game.cc	Tue Aug 19 11:01:04 2014 +0300
     3.2 +++ b/src/game.cc	Wed Aug 20 06:33:43 2014 +0300
     3.3 @@ -2,7 +2,7 @@
     3.4  #include "opengl.h"
     3.5  #include "camera.h"
     3.6  #include "texture.h"
     3.7 -//#include "OVR_CAPI_GL.h"
     3.8 +#include "vr/vr.h"
     3.9  
    3.10  static void draw_scene();
    3.11  
    3.12 @@ -15,6 +15,8 @@
    3.13  
    3.14  bool game_init()
    3.15  {
    3.16 +	vr_init();
    3.17 +
    3.18  	glEnable(GL_DEPTH_TEST);
    3.19  	glEnable(GL_CULL_FACE);
    3.20  	glEnable(GL_LIGHTING);
    3.21 @@ -33,6 +35,7 @@
    3.22  void game_cleanup()
    3.23  {
    3.24  	floor_tex.destroy();
    3.25 +	vr_shutdown();
    3.26  }
    3.27  
    3.28  
    3.29 @@ -71,17 +74,26 @@
    3.30  
    3.31  void game_render(int eye)
    3.32  {
    3.33 +	float mat[16];
    3.34  	Matrix4x4 view_matrix = cam.get_matrix().inverse();
    3.35  
    3.36  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3.37  
    3.38  	glMatrixMode(GL_PROJECTION);
    3.39  	glLoadIdentity();
    3.40 -	gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
    3.41 +	if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, mat)) {
    3.42 +		gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
    3.43 +	} else {
    3.44 +		glLoadTransposeMatrixf(mat);
    3.45 +	}
    3.46  
    3.47  	glMatrixMode(GL_MODELVIEW);
    3.48 -	glLoadIdentity();
    3.49 -	glLoadTransposeMatrixf(view_matrix[0]);
    3.50 +	if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
    3.51 +		glLoadIdentity();
    3.52 +	} else {
    3.53 +		glLoadTransposeMatrixf(mat);
    3.54 +	}
    3.55 +	glMultTransposeMatrixf(view_matrix[0]);
    3.56  
    3.57  	draw_scene();
    3.58  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/vr/vr.c	Wed Aug 20 06:33:43 2014 +0300
     4.3 @@ -0,0 +1,124 @@
     4.4 +#include <stdio.h>
     4.5 +#include <string.h>
     4.6 +#include "vr.h"
     4.7 +#include "vr_impl.h"
     4.8 +
     4.9 +
    4.10 +static struct vr_module *vrm;
    4.11 +static float idmat[] = {
    4.12 +	1, 0, 0, 0,
    4.13 +	0, 1, 0, 0,
    4.14 +	0, 0, 1, 0,
    4.15 +	0, 0, 0, 1
    4.16 +};
    4.17 +static float fbtex_rect[] = {
    4.18 +	0, 0, 1, 1
    4.19 +};
    4.20 +
    4.21 +int vr_init(void)
    4.22 +{
    4.23 +	int i, nmodules;
    4.24 +
    4.25 +	if(vrm) {
    4.26 +		vr_shutdown();
    4.27 +	}
    4.28 +
    4.29 +	vr_init_modules();
    4.30 +
    4.31 +	nmodules = vr_get_num_modules();
    4.32 +	for(i=0; i<nmodules; i++) {
    4.33 +		struct vr_module *m = vr_get_module(i);
    4.34 +		if(m->init() != -1) {
    4.35 +			/* add to the active modules array */
    4.36 +			vr_activate_module(i);
    4.37 +			if(!vrm) {
    4.38 +				vr_use_module(0);
    4.39 +			}
    4.40 +		}
    4.41 +	}
    4.42 +
    4.43 +	if(!vrm) {
    4.44 +		return -1;
    4.45 +	}
    4.46 +	return 0;
    4.47 +}
    4.48 +
    4.49 +void vr_shutdown(void)
    4.50 +{
    4.51 +	vr_clear_modules();
    4.52 +	vrm = 0;
    4.53 +	fbtex_rect[0] = fbtex_rect[1] = 0;
    4.54 +	fbtex_rect[2] = fbtex_rect[3] = 1;
    4.55 +}
    4.56 +
    4.57 +int vr_module_count(void)
    4.58 +{
    4.59 +	return vr_get_num_active_modules();
    4.60 +}
    4.61 +
    4.62 +const char *vr_module_name(int idx)
    4.63 +{
    4.64 +	struct vr_module *m = vr_get_active_module(idx);
    4.65 +	if(!m) {
    4.66 +		return 0;
    4.67 +	}
    4.68 +	return m->name;
    4.69 +}
    4.70 +
    4.71 +int vr_use_module(int idx)
    4.72 +{
    4.73 +	if(idx >= 0 && idx < vr_get_num_active_modules()) {
    4.74 +		vrm = vr_get_active_module(idx);
    4.75 +		printf("using vr module: %s\n", vrm->name);
    4.76 +		return 0;
    4.77 +	}
    4.78 +	return -1;
    4.79 +}
    4.80 +
    4.81 +int vr_use_module_named(const char *name)
    4.82 +{
    4.83 +	int i, count = vr_get_num_active_modules();
    4.84 +
    4.85 +	for(i=0; i<count; i++) {
    4.86 +		struct vr_module *m = vr_get_active_module(i);
    4.87 +		if(strcmp(m->name, name) == 0) {
    4.88 +			return vr_use_module(i);
    4.89 +		}
    4.90 +	}
    4.91 +	return -1;
    4.92 +}
    4.93 +
    4.94 +int vr_view_matrix(int eye, float *mat)
    4.95 +{
    4.96 +	if(vrm && vrm->view_matrix) {
    4.97 +		vrm->view_matrix(eye, mat);
    4.98 +		return 1;
    4.99 +	}
   4.100 +	memcpy(mat, idmat, sizeof idmat);
   4.101 +	return 0;
   4.102 +}
   4.103 +
   4.104 +int vr_proj_matrix(int eye, float *mat)
   4.105 +{
   4.106 +	if(vrm && vrm->proj_matrix) {
   4.107 +		vrm->proj_matrix(eye, mat);
   4.108 +		return 1;
   4.109 +	}
   4.110 +	memcpy(mat, idmat, sizeof idmat);
   4.111 +	return 0;
   4.112 +}
   4.113 +
   4.114 +void vr_present(unsigned int fbtex)
   4.115 +{
   4.116 +	if(vrm && vrm->draw) {
   4.117 +		vrm->draw(fbtex, fbtex_rect[0], fbtex_rect[2], fbtex_rect[1], fbtex_rect[3]);
   4.118 +	}
   4.119 +}
   4.120 +
   4.121 +void vr_fbrect(float u, float umax, float v, float vmax)
   4.122 +{
   4.123 +	fbtex_rect[0] = u;
   4.124 +	fbtex_rect[1] = v;
   4.125 +	fbtex_rect[2] = umax;
   4.126 +	fbtex_rect[3] = vmax;
   4.127 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/vr/vr.h	Wed Aug 20 06:33:43 2014 +0300
     5.3 @@ -0,0 +1,33 @@
     5.4 +#ifndef VR_H_
     5.5 +#define VR_H_
     5.6 +
     5.7 +#ifdef __cplusplus
     5.8 +extern "C" {
     5.9 +#endif
    5.10 +
    5.11 +int vr_init(void);
    5.12 +void vr_shutdown(void);
    5.13 +
    5.14 +int vr_module_count(void);
    5.15 +const char *vr_module_name(int idx);
    5.16 +
    5.17 +int vr_use_module(int idx);
    5.18 +int vr_use_module_named(const char *name);
    5.19 +
    5.20 +/* returns non-zero if the active vr module provides this kind of matrix
    5.21 + * information, otherwise it returns zero, and sets mat to identity
    5.22 + */
    5.23 +int vr_view_matrix(int eye, float *mat);
    5.24 +int vr_proj_matrix(int eye, float *mat);
    5.25 +
    5.26 +/* fbtex should be a texture containing both eye views side by side (LR) */
    5.27 +void vr_present(unsigned int fbtex);
    5.28 +
    5.29 +/* set the area of the framebuffer texture to be used */
    5.30 +void vr_fbrect(float u, float umax, float v, float vmax);
    5.31 +
    5.32 +#ifdef __cplusplus
    5.33 +}
    5.34 +#endif
    5.35 +
    5.36 +#endif	/* VR_H_ */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/vr/vr_impl.h	Wed Aug 20 06:33:43 2014 +0300
     6.3 @@ -0,0 +1,29 @@
     6.4 +#ifndef VR_IMPL_H_
     6.5 +#define VR_IMPL_H_
     6.6 +
     6.7 +struct vr_module {
     6.8 +	char *name;
     6.9 +
    6.10 +	int (*init)(void);
    6.11 +	void (*cleanup)(void);
    6.12 +
    6.13 +	void (*view_matrix)(int eye, float *mat);
    6.14 +	void (*proj_matrix)(int eye, float *mat);
    6.15 +
    6.16 +	void (*draw)(unsigned int fbtex, float u, float maxu, float v, float maxv);
    6.17 +};
    6.18 +
    6.19 +void vr_init_modules(void);
    6.20 +
    6.21 +void vr_clear_modules(void);
    6.22 +void vr_register_module(struct vr_module *mod);
    6.23 +
    6.24 +int vr_get_num_modules(void);
    6.25 +struct vr_module *vr_get_module(int idx);
    6.26 +
    6.27 +void vr_activate_module(int idx);
    6.28 +
    6.29 +int vr_get_num_active_modules(void);
    6.30 +struct vr_module *vr_get_active_module(int idx);
    6.31 +
    6.32 +#endif	/* VR_IMPL_H_ */
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/vr/vr_libovr.c	Wed Aug 20 06:33:43 2014 +0300
     7.3 @@ -0,0 +1,37 @@
     7.4 +#include "vr_impl.h"
     7.5 +
     7.6 +static int init(void)
     7.7 +{
     7.8 +	return -1;
     7.9 +}
    7.10 +
    7.11 +static void cleanup(void)
    7.12 +{
    7.13 +}
    7.14 +
    7.15 +static void view_matrix(int eye, float *mat)
    7.16 +{
    7.17 +}
    7.18 +
    7.19 +static void proj_matrix(int eye, float *mat)
    7.20 +{
    7.21 +}
    7.22 +
    7.23 +static void draw(unsigned int fbtex, float u, float maxu, float v, float maxv)
    7.24 +{
    7.25 +}
    7.26 +
    7.27 +struct vr_module *vr_module_libovr(void)
    7.28 +{
    7.29 +	static struct vr_module m;
    7.30 +
    7.31 +	if(!m.init) {
    7.32 +		m.name = "libovr";
    7.33 +		m.init = init;
    7.34 +		m.cleanup = cleanup;
    7.35 +		m.view_matrix = view_matrix;
    7.36 +		m.proj_matrix = proj_matrix;
    7.37 +		m.draw = draw;
    7.38 +	}
    7.39 +	return &m;
    7.40 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/vr/vr_modules.c	Wed Aug 20 06:33:43 2014 +0300
     8.3 @@ -0,0 +1,91 @@
     8.4 +/* XXX this might become partly auto-generated in the future */
     8.5 +#include <stdio.h>
     8.6 +#include <stdlib.h>
     8.7 +#include "vr_impl.h"
     8.8 +
     8.9 +struct vr_module *vr_module_libovr(void);
    8.10 +struct vr_module *vr_module_null(void);
    8.11 +
    8.12 +static struct vr_module *modules;
    8.13 +static int num_modules, modules_max_size;
    8.14 +
    8.15 +static int *active_modules;
    8.16 +static int num_act_modules, act_modules_max_size;
    8.17 +
    8.18 +
    8.19 +void vr_init_modules(void)
    8.20 +{
    8.21 +	struct vr_module *m;
    8.22 +
    8.23 +	vr_clear_modules();
    8.24 +
    8.25 +	if((m = vr_module_libovr())) {
    8.26 +		vr_register_module(m);
    8.27 +	}
    8.28 +
    8.29 +	if((m = vr_module_null())) {
    8.30 +		vr_register_module(m);
    8.31 +	}
    8.32 +
    8.33 +	/* more ... */
    8.34 +}
    8.35 +
    8.36 +void vr_clear_modules(void)
    8.37 +{
    8.38 +	free(modules);
    8.39 +	free(active_modules);
    8.40 +	modules = 0;
    8.41 +	num_modules = modules_max_size = 0;
    8.42 +	active_modules = 0;
    8.43 +	num_act_modules = act_modules_max_size = 0;
    8.44 +}
    8.45 +
    8.46 +void vr_register_module(struct vr_module *mod)
    8.47 +{
    8.48 +	if(num_modules >= modules_max_size) {
    8.49 +		int newsz = modules_max_size ? modules_max_size * 2 : 2;
    8.50 +		struct vr_module *newmods = realloc(modules, newsz * sizeof *newmods);
    8.51 +		if(!newmods) {
    8.52 +			fprintf(stderr, "failed to resize modules array up to %d\n", newsz);
    8.53 +			return;
    8.54 +		}
    8.55 +		modules = newmods;
    8.56 +		modules_max_size = newsz;
    8.57 +	}
    8.58 +	modules[num_modules++] = *mod;
    8.59 +}
    8.60 +
    8.61 +int vr_get_num_modules(void)
    8.62 +{
    8.63 +	return num_modules;
    8.64 +}
    8.65 +
    8.66 +struct vr_module *vr_get_module(int idx)
    8.67 +{
    8.68 +	return modules + idx;
    8.69 +}
    8.70 +
    8.71 +void vr_activate_module(int idx)
    8.72 +{
    8.73 +	if(num_act_modules >= act_modules_max_size) {
    8.74 +		int newsz = act_modules_max_size ? act_modules_max_size * 2 : 2;
    8.75 +		int *newact = realloc(active_modules, newsz * sizeof *newact);
    8.76 +		if(!newact) {
    8.77 +			fprintf(stderr, "failed to resize active modules array up to %d\n", newsz);
    8.78 +			return;
    8.79 +		}
    8.80 +		active_modules = newact;
    8.81 +		act_modules_max_size = newsz;
    8.82 +	}
    8.83 +	active_modules[num_act_modules++] = idx;
    8.84 +}
    8.85 +
    8.86 +int vr_get_num_active_modules(void)
    8.87 +{
    8.88 +	return num_act_modules;
    8.89 +}
    8.90 +
    8.91 +struct vr_module *vr_get_active_module(int idx)
    8.92 +{
    8.93 +	return modules + active_modules[idx];
    8.94 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/vr/vr_null.c	Wed Aug 20 06:33:43 2014 +0300
     9.3 @@ -0,0 +1,61 @@
     9.4 +#ifdef WIN32
     9.5 +#define WIN32_LEAN_AND_MEAN
     9.6 +#include <windows.h>
     9.7 +#endif
     9.8 +
     9.9 +#ifdef __APPLE__
    9.10 +#include <OpenGL/gl.h>
    9.11 +#else
    9.12 +#include <GL/gl.h>
    9.13 +#endif
    9.14 +
    9.15 +#include "vr_impl.h"
    9.16 +
    9.17 +static int init(void)
    9.18 +{
    9.19 +	return 0;
    9.20 +}
    9.21 +
    9.22 +static void draw(unsigned int fbtex, float u, float maxu, float v, float maxv)
    9.23 +{
    9.24 +	glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
    9.25 +
    9.26 +	glDisable(GL_LIGHTING);
    9.27 +	glDisable(GL_DEPTH_TEST);
    9.28 +	glDisable(GL_FOG);
    9.29 +	glDisable(GL_CULL_FACE);
    9.30 +
    9.31 +	glMatrixMode(GL_MODELVIEW);
    9.32 +	glLoadIdentity();
    9.33 +	glMatrixMode(GL_PROJECTION);
    9.34 +	glLoadIdentity();
    9.35 +
    9.36 +	glBegin(GL_QUADS);
    9.37 +	glTexCoord2f(u, v);
    9.38 +	glVertex2f(-1, -1);
    9.39 +	glTexCoord2f((u + maxu) / 2, v);
    9.40 +	glVertex2f(1, -1);
    9.41 +	glTexCoord2f((u + maxu) / 2, maxv);
    9.42 +	glVertex2f(1, 1);
    9.43 +	glTexCoord2f(u, maxv);
    9.44 +	glVertex2f(-1, 1);
    9.45 +	glEnd();
    9.46 +
    9.47 +	glPopMatrix();
    9.48 +	glMatrixMode(GL_MODELVIEW);
    9.49 +	glPopMatrix();
    9.50 +
    9.51 +	glPopAttrib();
    9.52 +}
    9.53 +
    9.54 +struct vr_module *vr_module_null(void)
    9.55 +{
    9.56 +	static struct vr_module m;
    9.57 +
    9.58 +	if(!m.init) {
    9.59 +		m.name = "null";
    9.60 +		m.init = init;
    9.61 +		m.draw = draw;
    9.62 +	}
    9.63 +	return &m;
    9.64 +}