libgoatvr

diff src/vr.c @ 19:437fe32ac633

ops... wasn't handling the stereo eye separation correctly. also fixed a bug in vr_libovr.c causing an assertion inside LibOVR when ovrHmd_GetEyePose was called as a result of calls to view_rotation or view_translation outside of vr_begin/vr_end
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 04 Oct 2014 03:39:14 +0300
parents 1067274dc780
children d659cbedde1d
line diff
     1.1 --- a/src/vr.c	Fri Oct 03 23:07:27 2014 +0300
     1.2 +++ b/src/vr.c	Sat Oct 04 03:39:14 2014 +0300
     1.3 @@ -6,6 +6,7 @@
     1.4  #include "vr_impl.h"
     1.5  #include "mathutil.h"
     1.6  
     1.7 +#define DEF_IPD		0.064f
     1.8  
     1.9  static void fallback_present(void);
    1.10  
    1.11 @@ -36,9 +37,11 @@
    1.12  
    1.13  	/* create the default options database */
    1.14  	if(!defopt && (defopt = create_options())) {
    1.15 -		set_option_float(defopt, VR_RENDER_RES_SCALE, 1.0);
    1.16 -		set_option_float(defopt, VR_EYE_HEIGHT, 1.675);
    1.17 -		set_option_float(defopt, VR_IPD, 0.064);
    1.18 +		set_option_float(defopt, VR_RENDER_RES_SCALE, 1.0f);
    1.19 +		set_option_float(defopt, VR_EYE_HEIGHT, 1.675f);
    1.20 +		set_option_float(defopt, VR_IPD, DEF_IPD);
    1.21 +		set_option_vec3f(defopt, VR_LEYE_OFFSET, -DEF_IPD * 0.5f, 0.0f, 0.0f);
    1.22 +		set_option_vec3f(defopt, VR_REYE_OFFSET, DEF_IPD * 0.5f, 0.0f, 0.0f);
    1.23  		set_option_int(defopt, VR_NULL_STEREO, 0);
    1.24  	}
    1.25  
    1.26 @@ -168,6 +171,14 @@
    1.27  	return res;
    1.28  }
    1.29  
    1.30 +static float *def_option_vec(const char *optname, float *res)
    1.31 +{
    1.32 +	res[0] = res[1] = res[2] = res[3] = 0.0f;
    1.33 +
    1.34 +	get_option_vec(defopt, optname, res);
    1.35 +	return res;
    1.36 +}
    1.37 +
    1.38  int vr_geti(const char *optname)
    1.39  {
    1.40  	int res = 0;
    1.41 @@ -188,6 +199,17 @@
    1.42  	return res;
    1.43  }
    1.44  
    1.45 +float *vr_getfv(const char *optname, float *res)
    1.46 +{
    1.47 +	static float sres[4];
    1.48 +	if(!res) res = sres;
    1.49 +
    1.50 +	if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_VEC, res) == -1) {
    1.51 +		def_option_vec(optname, res);
    1.52 +	}
    1.53 +	return res;
    1.54 +}
    1.55 +
    1.56  int vr_geti_def(const char *optname, int def_val)
    1.57  {
    1.58  	int res = 0;
    1.59 @@ -214,17 +236,12 @@
    1.60  
    1.61  int vr_view_translation(int eye, float *vec)
    1.62  {
    1.63 -	float eye_offset;
    1.64 -
    1.65  	if(vrm && vrm->translation) {
    1.66  		vrm->translation(eye, vec);
    1.67  		return 1;
    1.68  	}
    1.69 -
    1.70 -	eye_offset = vr_getf(VR_IPD) / 2.0;
    1.71 -	vec[0] = eye == VR_EYE_LEFT ? -eye_offset : eye_offset;
    1.72 -	vec[1] = vec[2] = 0.0f;
    1.73 -	return 1;
    1.74 +	vec[0] = vec[1] = vec[2] = 0.0f;
    1.75 +	return 0;
    1.76  }
    1.77  
    1.78  int vr_view_rotation(int eye, float *quat)
    1.79 @@ -240,31 +257,36 @@
    1.80  
    1.81  int vr_view_matrix(int eye, float *mat)
    1.82  {
    1.83 -	float offs[3], quat[4];
    1.84 -	float rmat[16], tmat[16];
    1.85 +	float offs[3], quat[4], eye_offs[4];
    1.86 +	float rmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.87 +	float tmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.88  
    1.89  	if(vrm && vrm->view_matrix) {
    1.90  		vrm->view_matrix(eye, mat);
    1.91  		return 1;
    1.92  	}
    1.93  
    1.94 -	if(!vr_view_translation(eye, offs)) {
    1.95 -		float eye_offset = vr_getf(VR_IPD) / 2.0;
    1.96 -		offs[0] = eye == VR_EYE_LEFT ? -eye_offset : eye_offset;
    1.97 -		offs[1] = offs[2];
    1.98 +	memcpy(mat, idmat, sizeof idmat);
    1.99 +
   1.100 +	if(vr_view_translation(eye, offs)) {
   1.101 +		offs[0] = -offs[0];
   1.102 +		offs[1] = -offs[1];
   1.103 +		offs[2] = -offs[2];
   1.104 +
   1.105 +		vrimp_translation_matrix(offs, tmat);
   1.106 +		vrimp_mult_matrix(mat, mat, tmat);
   1.107  	}
   1.108 -	if(!vr_view_rotation(eye, quat)) {
   1.109 -		quat[0] = quat[1] = quat[2] = 0.0f;
   1.110 -		quat[3] = 1.0f;
   1.111 +	if(vr_view_rotation(eye, quat)) {
   1.112 +		vrimp_rotation_matrix(quat, rmat);
   1.113 +		vrimp_mult_matrix(mat, mat, rmat);
   1.114  	}
   1.115  
   1.116 -	offs[0] = -offs[0];
   1.117 -	offs[1] = -offs[1];
   1.118 -	offs[2] = -offs[2];
   1.119 -
   1.120 -	vrimp_translation_matrix(offs, tmat);
   1.121 -	vrimp_rotation_matrix(quat, rmat);
   1.122 -	vrimp_mult_matrix(mat, tmat, rmat);
   1.123 +	vr_getfv(eye == VR_EYE_LEFT ? VR_LEYE_OFFSET : VR_REYE_OFFSET, eye_offs);
   1.124 +	eye_offs[0] = -eye_offs[0];
   1.125 +	eye_offs[1] = -eye_offs[1];
   1.126 +	eye_offs[2] = -eye_offs[2];
   1.127 +	vrimp_translation_matrix(eye_offs, tmat);
   1.128 +	vrimp_mult_matrix(mat, mat, tmat);
   1.129  	return 1;
   1.130  }
   1.131