conworlds

diff src/vr/vr.c @ 4:e6948e131526

adding a vr wrapper
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 20 Aug 2014 06:33:43 +0300
parents
children 3c36bc28c6c2
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vr/vr.c	Wed Aug 20 06:33:43 2014 +0300
     1.3 @@ -0,0 +1,124 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include "vr.h"
     1.7 +#include "vr_impl.h"
     1.8 +
     1.9 +
    1.10 +static struct vr_module *vrm;
    1.11 +static float idmat[] = {
    1.12 +	1, 0, 0, 0,
    1.13 +	0, 1, 0, 0,
    1.14 +	0, 0, 1, 0,
    1.15 +	0, 0, 0, 1
    1.16 +};
    1.17 +static float fbtex_rect[] = {
    1.18 +	0, 0, 1, 1
    1.19 +};
    1.20 +
    1.21 +int vr_init(void)
    1.22 +{
    1.23 +	int i, nmodules;
    1.24 +
    1.25 +	if(vrm) {
    1.26 +		vr_shutdown();
    1.27 +	}
    1.28 +
    1.29 +	vr_init_modules();
    1.30 +
    1.31 +	nmodules = vr_get_num_modules();
    1.32 +	for(i=0; i<nmodules; i++) {
    1.33 +		struct vr_module *m = vr_get_module(i);
    1.34 +		if(m->init() != -1) {
    1.35 +			/* add to the active modules array */
    1.36 +			vr_activate_module(i);
    1.37 +			if(!vrm) {
    1.38 +				vr_use_module(0);
    1.39 +			}
    1.40 +		}
    1.41 +	}
    1.42 +
    1.43 +	if(!vrm) {
    1.44 +		return -1;
    1.45 +	}
    1.46 +	return 0;
    1.47 +}
    1.48 +
    1.49 +void vr_shutdown(void)
    1.50 +{
    1.51 +	vr_clear_modules();
    1.52 +	vrm = 0;
    1.53 +	fbtex_rect[0] = fbtex_rect[1] = 0;
    1.54 +	fbtex_rect[2] = fbtex_rect[3] = 1;
    1.55 +}
    1.56 +
    1.57 +int vr_module_count(void)
    1.58 +{
    1.59 +	return vr_get_num_active_modules();
    1.60 +}
    1.61 +
    1.62 +const char *vr_module_name(int idx)
    1.63 +{
    1.64 +	struct vr_module *m = vr_get_active_module(idx);
    1.65 +	if(!m) {
    1.66 +		return 0;
    1.67 +	}
    1.68 +	return m->name;
    1.69 +}
    1.70 +
    1.71 +int vr_use_module(int idx)
    1.72 +{
    1.73 +	if(idx >= 0 && idx < vr_get_num_active_modules()) {
    1.74 +		vrm = vr_get_active_module(idx);
    1.75 +		printf("using vr module: %s\n", vrm->name);
    1.76 +		return 0;
    1.77 +	}
    1.78 +	return -1;
    1.79 +}
    1.80 +
    1.81 +int vr_use_module_named(const char *name)
    1.82 +{
    1.83 +	int i, count = vr_get_num_active_modules();
    1.84 +
    1.85 +	for(i=0; i<count; i++) {
    1.86 +		struct vr_module *m = vr_get_active_module(i);
    1.87 +		if(strcmp(m->name, name) == 0) {
    1.88 +			return vr_use_module(i);
    1.89 +		}
    1.90 +	}
    1.91 +	return -1;
    1.92 +}
    1.93 +
    1.94 +int vr_view_matrix(int eye, float *mat)
    1.95 +{
    1.96 +	if(vrm && vrm->view_matrix) {
    1.97 +		vrm->view_matrix(eye, mat);
    1.98 +		return 1;
    1.99 +	}
   1.100 +	memcpy(mat, idmat, sizeof idmat);
   1.101 +	return 0;
   1.102 +}
   1.103 +
   1.104 +int vr_proj_matrix(int eye, float *mat)
   1.105 +{
   1.106 +	if(vrm && vrm->proj_matrix) {
   1.107 +		vrm->proj_matrix(eye, mat);
   1.108 +		return 1;
   1.109 +	}
   1.110 +	memcpy(mat, idmat, sizeof idmat);
   1.111 +	return 0;
   1.112 +}
   1.113 +
   1.114 +void vr_present(unsigned int fbtex)
   1.115 +{
   1.116 +	if(vrm && vrm->draw) {
   1.117 +		vrm->draw(fbtex, fbtex_rect[0], fbtex_rect[2], fbtex_rect[1], fbtex_rect[3]);
   1.118 +	}
   1.119 +}
   1.120 +
   1.121 +void vr_fbrect(float u, float umax, float v, float vmax)
   1.122 +{
   1.123 +	fbtex_rect[0] = u;
   1.124 +	fbtex_rect[1] = v;
   1.125 +	fbtex_rect[2] = umax;
   1.126 +	fbtex_rect[3] = vmax;
   1.127 +}