vrchess

changeset 11:5dc4e2b8f6f5

LibOVR is broken
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 23 Aug 2014 00:24:20 +0300
parents e3f0ca1d008a
children 778ed91cb7fd
files src/game.cc src/main.cc src/vr/vr.c src/vr/vr.h src/vr/vr_libovr.c vrchess.vcxproj vrchess.vcxproj.filters
diffstat 7 files changed, 98 insertions(+), 33 deletions(-) [+]
line diff
     1.1 --- a/src/game.cc	Fri Aug 22 20:11:15 2014 +0300
     1.2 +++ b/src/game.cc	Sat Aug 23 00:24:20 2014 +0300
     1.3 @@ -87,7 +87,6 @@
     1.4  void game_render()
     1.5  {
     1.6  	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
     1.7 -	glClearColor(1, 0, 0, 1);
     1.8  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     1.9  
    1.10  	glViewport(0, 0, rtwidth / 2.0, rtheight);
    1.11 @@ -116,18 +115,18 @@
    1.12  
    1.13  	glMatrixMode(GL_PROJECTION);
    1.14  	glLoadIdentity();
    1.15 -	//if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
    1.16 +	if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
    1.17  		gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
    1.18 -	/*} else {
    1.19 +	} else {
    1.20  		glLoadMatrixf(mat);
    1.21 -	}*/
    1.22 +	}
    1.23  
    1.24  	glMatrixMode(GL_MODELVIEW);
    1.25 -	//if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
    1.26 +	if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
    1.27  		glLoadIdentity();
    1.28 -	/*} else {
    1.29 -		glLoadTransposeMatrixf(mat);
    1.30 -	}*/
    1.31 +	} else {
    1.32 +		glLoadMatrixf(mat);
    1.33 +	}
    1.34  	glMultTransposeMatrixf(view_matrix[0]);
    1.35  
    1.36  	draw_scene();
     2.1 --- a/src/main.cc	Fri Aug 22 20:11:15 2014 +0300
     2.2 +++ b/src/main.cc	Sat Aug 23 00:24:20 2014 +0300
     2.3 @@ -84,6 +84,30 @@
     2.4  
     2.5  static void keyb(unsigned char key, int x, int y)
     2.6  {
     2.7 +	static bool fullscr;
     2.8 +	static int prev_xsz, prev_ysz;
     2.9 +
    2.10 +	switch(key) {
    2.11 +	case 'f':
    2.12 +		fullscr = !fullscr;
    2.13 +		if(fullscr) {
    2.14 +			int xoffs, yoffs;
    2.15 +
    2.16 +			prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
    2.17 +			prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
    2.18 +
    2.19 +			xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
    2.20 +			yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
    2.21 +			if(xoffs || yoffs) {
    2.22 +				printf("repositioning: %d,%d\n", xoffs, yoffs);
    2.23 +				glutPositionWindow(xoffs + 1, yoffs + 1);
    2.24 +			}
    2.25 +			glutFullScreen();
    2.26 +		} else {
    2.27 +			glutReshapeWindow(prev_xsz, prev_ysz);
    2.28 +		}
    2.29 +		break;
    2.30 +	}
    2.31  	game_keyboard(key, true, x, y);
    2.32  }
    2.33  
     3.1 --- a/src/vr/vr.c	Fri Aug 22 20:11:15 2014 +0300
     3.2 +++ b/src/vr/vr.c	Sat Aug 23 00:24:20 2014 +0300
     3.3 @@ -2,6 +2,7 @@
     3.4  #include <string.h>
     3.5  #include "vr.h"
     3.6  #include "vr_impl.h"
     3.7 +#include "mathutil.h"
     3.8  
     3.9  
    3.10  static void swap_buffers(void);
    3.11 @@ -133,37 +134,49 @@
    3.12  {
    3.13  	if(vrm && vrm->translation) {
    3.14  		vrm->translation(eye, vec);
    3.15 -		return 0;
    3.16 +		return 1;
    3.17  	}
    3.18  	vec[0] = vec[1] = vec[2] = 0.0f;
    3.19 -	return -1;
    3.20 +	return 0;
    3.21  }
    3.22  
    3.23  int vr_view_rotation(int eye, float *quat)
    3.24  {
    3.25  	if(vrm && vrm->rotation) {
    3.26  		vrm->rotation(eye, quat);
    3.27 -		return 0;
    3.28 +		return 1;
    3.29  	}
    3.30  	quat[0] = quat[1] = quat[2] = 0.0f;
    3.31  	quat[3] = 1.0f;
    3.32 -	return -1;
    3.33 +	return 0;
    3.34  }
    3.35  
    3.36  int vr_view_matrix(int eye, float *mat)
    3.37  {
    3.38 +	int have_trans, have_rot;
    3.39  	float offs[3], quat[4];
    3.40 +	float rmat[16], tmat[16];
    3.41  
    3.42  	if(vrm && vrm->view_matrix) {
    3.43  		vrm->view_matrix(eye, mat);
    3.44  		return 1;
    3.45  	}
    3.46  
    3.47 -	if(!vr_view_translation(eye, offs) && !vr_view_rotation(eye, quat)) {
    3.48 +	have_trans = vr_view_translation(eye, offs);
    3.49 +	have_rot = vr_view_rotation(eye, quat);
    3.50 +
    3.51 +	if(!have_trans && !have_rot) {
    3.52  		return 0;
    3.53  	}
    3.54 -	/* TODO construct matrix */
    3.55 -	return 0;
    3.56 +
    3.57 +	offs[0] = -offs[0];
    3.58 +	offs[1] = -offs[1];
    3.59 +	offs[2] = -offs[2];
    3.60 +
    3.61 +	translation_matrix(offs, tmat);
    3.62 +	rotation_matrix(quat, rmat);
    3.63 +	mult_matrix(mat, tmat, rmat);
    3.64 +	return 1;
    3.65  }
    3.66  
    3.67  int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
     4.1 --- a/src/vr/vr.h	Fri Aug 22 20:11:15 2014 +0300
     4.2 +++ b/src/vr/vr.h	Sat Aug 23 00:24:20 2014 +0300
     4.3 @@ -9,6 +9,8 @@
     4.4  #define VR_OPT_REYE_YRES	"right-eye-yres"
     4.5  #define VR_OPT_EYE_HEIGHT	"eye-height"
     4.6  #define VR_OPT_IPD			"ipd"
     4.7 +#define VR_OPT_WIN_XOFFS	"win-xoffset"
     4.8 +#define VR_OPT_WIN_YOFFS	"win-yoffset"
     4.9  
    4.10  enum {
    4.11  	VR_EYE_LEFT,
     5.1 --- a/src/vr/vr_libovr.c	Fri Aug 22 20:11:15 2014 +0300
     5.2 +++ b/src/vr/vr_libovr.c	Sat Aug 23 00:24:20 2014 +0300
     5.3 @@ -13,6 +13,8 @@
     5.4  #include <OVR_CAPI.h>
     5.5  #include <OVR_CAPI_GL.h>
     5.6  
     5.7 +#define DISABLE_RETARDED_HEALTH_WARNING
     5.8 +
     5.9  /* just dropping the prototype here to avoid including CAPI_HSWDisplay.h */
    5.10  OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enabled);
    5.11  
    5.12 @@ -46,7 +48,7 @@
    5.13  		if(!(h = ovrHmd_Create(i))) {
    5.14  			break;
    5.15  		}
    5.16 -		printf(" [%d]: %s - %s\n", h->Manufacturer, h->ProductName);
    5.17 +		printf(" [%d]: %s - %s\n", i, h->Manufacturer, h->ProductName);
    5.18  
    5.19  		if(!hmd) {
    5.20  			hmd = h;
    5.21 @@ -60,6 +62,8 @@
    5.22  		return -1;
    5.23  	}
    5.24  
    5.25 +	ovrHmd_ConfigureTracking(hmd, 0xffffffff, 0);
    5.26 +
    5.27  	eye_fov[0] = hmd->DefaultEyeFov[0];
    5.28  	eye_fov[1] = hmd->DefaultEyeFov[1];
    5.29  
    5.30 @@ -76,6 +80,8 @@
    5.31  		set_option_int(optdb, VR_OPT_REYE_YRES, eye_res[1].h);
    5.32  		set_option_float(optdb, VR_OPT_EYE_HEIGHT, ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, OVR_DEFAULT_EYE_HEIGHT));
    5.33  		set_option_float(optdb, VR_OPT_IPD, ovrHmd_GetFloat(hmd, OVR_KEY_IPD, OVR_DEFAULT_IPD));
    5.34 +		set_option_int(optdb, VR_OPT_WIN_XOFFS, hmd->WindowsPos.x);
    5.35 +		set_option_int(optdb, VR_OPT_WIN_YOFFS, hmd->WindowsPos.y);
    5.36  	}
    5.37  
    5.38  	deferred_init_done = 0;
    5.39 @@ -90,30 +96,33 @@
    5.40  
    5.41  	deferred_init_done = 1;
    5.42  
    5.43 -	ovrHmd_ConfigureTracking(hmd, 0xffffffff, 0);
    5.44 -
    5.45 +	memset(&glcfg, 0, sizeof glcfg);
    5.46  	glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
    5.47  	glcfg.OGL.Header.RTSize = hmd->Resolution;
    5.48  	glcfg.OGL.Header.Multisample = 1;
    5.49  #ifdef WIN32
    5.50  	win = GetActiveWindow();
    5.51 -	glcfg.OGL.Window = win;
    5.52 +	/*glcfg.OGL.Window = win;
    5.53  	glcfg.OGL.DC = wglGetCurrentDC();
    5.54  	assert(glcfg.OGL.Window);
    5.55 -	assert(glcfg.OGL.DC);
    5.56 +	assert(glcfg.OGL.DC);*/
    5.57  #endif
    5.58  
    5.59 -	ovrHmd_AttachToWindow(hmd, win, 0, 0);
    5.60 -	/*ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);*/
    5.61 +	if(!(hmd->HmdCaps & ovrHmdCap_ExtendDesktop)) {
    5.62 +		ovrHmd_AttachToWindow(hmd, win, 0, 0);
    5.63 +	}
    5.64 +	ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
    5.65  
    5.66 -	dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette;/* | ovrDistortionCap_TimeWarp;// |
    5.67 -		ovrDistortionCap_Overdrive; */
    5.68 +	dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
    5.69 +		ovrDistortionCap_Overdrive | ovrDistortionCap_NoRestore;
    5.70  
    5.71  	if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, eye_fov, eye_render_desc)) {
    5.72  		fprintf(stderr, "failed to configure LibOVR distortion renderer\n");
    5.73  	}
    5.74  
    5.75 -	/* ovrhmd_EnableHSWDisplaySDKRender(hmd, 0); */
    5.76 +#ifdef DISABLE_RETARDED_HEALTH_WARNING
    5.77 +	ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
    5.78 +#endif
    5.79  }
    5.80  
    5.81  static void cleanup(void)
    5.82 @@ -153,14 +162,14 @@
    5.83  {
    5.84  	if(!hmd) {
    5.85  		vec[0] = vec[1] = vec[2] = 0;
    5.86 -		return -1;
    5.87 +		return 0;
    5.88  	}
    5.89  
    5.90  	pose[eye] = ovrHmd_GetEyePose(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
    5.91  	vec[0] = pose[eye].Position.x;
    5.92  	vec[1] = pose[eye].Position.y;
    5.93  	vec[2] = pose[eye].Position.z;
    5.94 -	return 0;
    5.95 +	return 1;
    5.96  }
    5.97  
    5.98  static int rotation(int eye, float *quat)
    5.99 @@ -168,7 +177,7 @@
   5.100  	if(!hmd) {
   5.101  		quat[0] = quat[1] = quat[2] = 0.0f;
   5.102  		quat[3] = 1.0f;
   5.103 -		return -1;
   5.104 +		return 0;
   5.105  	}
   5.106  
   5.107  	pose[eye] = ovrHmd_GetEyePose(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
   5.108 @@ -176,13 +185,14 @@
   5.109  	quat[1] = pose[eye].Orientation.y;
   5.110  	quat[2] = pose[eye].Orientation.z;
   5.111  	quat[3] = pose[eye].Orientation.w;
   5.112 -	return 0;
   5.113 +	return 1;
   5.114  }
   5.115  
   5.116  static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   5.117  
   5.118  static void proj_matrix(int eye, float znear, float zfar, float *mat)
   5.119  {
   5.120 +	int i, j;
   5.121  	ovrMatrix4f vmat;
   5.122  
   5.123  	if(!hmd) {
   5.124 @@ -191,7 +201,12 @@
   5.125  	}
   5.126  
   5.127  	vmat = ovrMatrix4f_Projection(eye_render_desc[eye].Fov, znear, zfar, 1);
   5.128 -	memcpy(mat, vmat.M[0], 16 * sizeof(float));
   5.129 +
   5.130 +	for(i=0; i<4; i++) {
   5.131 +		for(j=0; j<4; j++) {
   5.132 +			*mat++ = vmat.M[j][i];
   5.133 +		}
   5.134 +	}
   5.135  }
   5.136  
   5.137  static int new_frame = 1;
   5.138 @@ -227,6 +242,9 @@
   5.139  	ovrSizei texsz;
   5.140  	ovrRecti rect;
   5.141  
   5.142 +	vmin = 1.0 - vmax;
   5.143 +	vmax = 1.0 - vmin;
   5.144 +
   5.145  	glBindTexture(GL_TEXTURE_2D, tex);
   5.146  	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texsz.w);
   5.147  	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texsz.h);
   5.148 @@ -236,9 +254,6 @@
   5.149  	rect.Size.w = (int)((umax - umin) * texsz.w);
   5.150  	rect.Size.h = (int)((vmax - vmin) * texsz.h);
   5.151  
   5.152 -	printf("eye %d texture: %u [%g, %g] -> [%g, %g]\n", eye, tex, umin, vmin, umax, vmax);
   5.153 -	printf("    pixels [%d, %d] -> [%d, %d]\n", rect.Pos.x, rect.Pos.y, rect.Pos.x + rect.Size.w, rect.Pos.y + rect.Size.h);
   5.154 -
   5.155  	eye_tex[eye].OGL.Header.API = ovrRenderAPI_OpenGL;
   5.156  	eye_tex[eye].OGL.Header.TextureSize = texsz;
   5.157  	eye_tex[eye].OGL.Header.RenderViewport = rect;
     6.1 --- a/vrchess.vcxproj	Fri Aug 22 20:11:15 2014 +0300
     6.2 +++ b/vrchess.vcxproj	Sat Aug 23 00:24:20 2014 +0300
     6.3 @@ -89,12 +89,14 @@
     6.4      <ClCompile Include="src\opengl.cc" />
     6.5      <ClCompile Include="src\sdr.c" />
     6.6      <ClCompile Include="src\texture.cc" />
     6.7 +    <ClCompile Include="src\vr\mathutil.c" />
     6.8      <ClCompile Include="src\vr\opt.c" />
     6.9      <ClCompile Include="src\vr\rbtree.c" />
    6.10      <ClCompile Include="src\vr\vr.c" />
    6.11      <ClCompile Include="src\vr\vr_libovr.c" />
    6.12      <ClCompile Include="src\vr\vr_modules.c" />
    6.13      <ClCompile Include="src\vr\vr_null.c" />
    6.14 +    <ClCompile Include="src\vr\vr_openhmd.c" />
    6.15    </ItemGroup>
    6.16    <ItemGroup>
    6.17      <ClInclude Include="src\camera.h" />
    6.18 @@ -103,6 +105,7 @@
    6.19      <ClInclude Include="src\opengl.h" />
    6.20      <ClInclude Include="src\sdr.h" />
    6.21      <ClInclude Include="src\texture.h" />
    6.22 +    <ClInclude Include="src\vr\mathutil.h" />
    6.23      <ClInclude Include="src\vr\opt.h" />
    6.24      <ClInclude Include="src\vr\rbtree.h" />
    6.25      <ClInclude Include="src\vr\vr.h" />
     7.1 --- a/vrchess.vcxproj.filters	Fri Aug 22 20:11:15 2014 +0300
     7.2 +++ b/vrchess.vcxproj.filters	Sat Aug 23 00:24:20 2014 +0300
     7.3 @@ -49,6 +49,12 @@
     7.4      <ClCompile Include="src\vr\opt.c">
     7.5        <Filter>src\vr</Filter>
     7.6      </ClCompile>
     7.7 +    <ClCompile Include="src\vr\vr_openhmd.c">
     7.8 +      <Filter>src\vr</Filter>
     7.9 +    </ClCompile>
    7.10 +    <ClCompile Include="src\vr\mathutil.c">
    7.11 +      <Filter>src\vr</Filter>
    7.12 +    </ClCompile>
    7.13    </ItemGroup>
    7.14    <ItemGroup>
    7.15      <ClInclude Include="src\camera.h">
    7.16 @@ -81,5 +87,8 @@
    7.17      <ClInclude Include="src\vr\opt.h">
    7.18        <Filter>src\vr</Filter>
    7.19      </ClInclude>
    7.20 +    <ClInclude Include="src\vr\mathutil.h">
    7.21 +      <Filter>src\vr</Filter>
    7.22 +    </ClInclude>
    7.23    </ItemGroup>
    7.24  </Project>
    7.25 \ No newline at end of file