libgoatvr
diff src/vr.c @ 5:e63cb28fc644
working on the linux side a bit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 18 Sep 2014 10:56:45 +0300 |
parents | ded3d0a74e19 |
children | 6896f9cf9621 |
line diff
1.1 --- a/src/vr.c Fri Aug 29 07:37:19 2014 +0300 1.2 +++ b/src/vr.c Thu Sep 18 10:56:45 2014 +0300 1.3 @@ -1,11 +1,12 @@ 1.4 #include <stdio.h> 1.5 #include <string.h> 1.6 +#include "opengl.h" 1.7 #include "vr.h" 1.8 #include "vr_impl.h" 1.9 #include "mathutil.h" 1.10 1.11 1.12 -static void swap_buffers(void); 1.13 +static void fallback_present(void); 1.14 1.15 1.16 static struct vr_module *vrm; 1.17 @@ -19,10 +20,24 @@ 1.18 0, 0, 1, 1 1.19 }; 1.20 1.21 +static void *defopt; /* default options db */ 1.22 + 1.23 +static struct { 1.24 + float umin, umax, vmin, vmax; 1.25 + int tex; 1.26 +} rtarg[2]; 1.27 + 1.28 + 1.29 int vr_init(void) 1.30 { 1.31 int i, nmodules; 1.32 1.33 + /* create the default options database */ 1.34 + if(!defopt && (defopt = create_options())) { 1.35 + set_option_float(defopt, VR_OPT_EYE_HEIGHT, 1.675); 1.36 + set_option_float(defopt, VR_OPT_IPD, 0.064); 1.37 + } 1.38 + 1.39 if(vrm) { 1.40 vr_shutdown(); 1.41 } 1.42 @@ -99,6 +114,8 @@ 1.43 { 1.44 if(vrm && vrm->set_option) { 1.45 vrm->set_option(optname, OTYPE_INT, &val); 1.46 + } else { 1.47 + set_option_int(defopt, optname, val); 1.48 } 1.49 } 1.50 1.51 @@ -106,6 +123,8 @@ 1.52 { 1.53 if(vrm && vrm->set_option) { 1.54 vrm->set_option(optname, OTYPE_FLOAT, &val); 1.55 + } else { 1.56 + set_option_float(defopt, optname, val); 1.57 } 1.58 } 1.59 1.60 @@ -113,8 +132,8 @@ 1.61 { 1.62 int res = 0; 1.63 1.64 - if(vrm && vrm->get_option) { 1.65 - vrm->get_option(optname, OTYPE_INT, &res); 1.66 + if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) { 1.67 + get_option_int(defopt, optname, &res); /* fallback */ 1.68 } 1.69 return res; 1.70 } 1.71 @@ -123,8 +142,8 @@ 1.72 { 1.73 float res = 0.0f; 1.74 1.75 - if(vrm && vrm->get_option) { 1.76 - vrm->get_option(optname, OTYPE_FLOAT, &res); 1.77 + if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) { 1.78 + get_option_float(defopt, optname, &res); /* fallback */ 1.79 } 1.80 return res; 1.81 } 1.82 @@ -166,6 +185,7 @@ 1.83 have_rot = vr_view_rotation(eye, quat); 1.84 1.85 if(!have_trans && !have_rot) { 1.86 + memcpy(mat, idmat, sizeof idmat); 1.87 return 0; 1.88 } 1.89 1.90 @@ -212,7 +232,8 @@ 1.91 } 1.92 1.93 if(!res) { 1.94 - swap_buffers(); 1.95 + fallback_present(); 1.96 + vr_gl_swap_buffers(); 1.97 } 1.98 return 0; 1.99 } 1.100 @@ -229,6 +250,12 @@ 1.101 { 1.102 if(vrm && vrm->set_eye_texture) { 1.103 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax); 1.104 + } else { 1.105 + rtarg[eye].tex = tex; 1.106 + rtarg[eye].umin = umin; 1.107 + rtarg[eye].umax = umax; 1.108 + rtarg[eye].vmin = vmin; 1.109 + rtarg[eye].vmax = vmax; 1.110 } 1.111 } 1.112 1.113 @@ -239,22 +266,48 @@ 1.114 } 1.115 } 1.116 1.117 +static void fallback_present(void) 1.118 +{ 1.119 + int i; 1.120 1.121 -#ifdef __unix__ 1.122 -#include <GL/glx.h> 1.123 + glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT); 1.124 1.125 -static void swap_buffers(void) 1.126 -{ 1.127 - glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable()); 1.128 + glDisable(GL_LIGHTING); 1.129 + glDisable(GL_DEPTH_TEST); 1.130 + glDisable(GL_ALPHA_TEST); 1.131 + glDisable(GL_STENCIL_TEST); 1.132 + glDisable(GL_FOG); 1.133 + 1.134 + glEnable(GL_TEXTURE_2D); 1.135 + 1.136 + glMatrixMode(GL_MODELVIEW); 1.137 + glPushMatrix(); 1.138 + glLoadIdentity(); 1.139 + glMatrixMode(GL_PROJECTION); 1.140 + glPushMatrix(); 1.141 + glLoadIdentity(); 1.142 + 1.143 + for(i=0; i<2; i++) { 1.144 + float x0 = i == 0 ? -1 : 0; 1.145 + float x1 = i == 0 ? 0 : 1; 1.146 + 1.147 + glBindTexture(GL_TEXTURE_2D, rtarg[i].tex); 1.148 + 1.149 + glBegin(GL_QUADS); 1.150 + glTexCoord2f(rtarg[i].umin, rtarg[i].vmin); 1.151 + glVertex2f(x0, -1); 1.152 + glTexCoord2f(rtarg[i].umax, rtarg[i].vmin); 1.153 + glVertex2f(x1, -1); 1.154 + glTexCoord2f(rtarg[i].umax, rtarg[i].vmax); 1.155 + glVertex2f(x1, 1); 1.156 + glTexCoord2f(rtarg[i].umin, rtarg[i].vmax); 1.157 + glVertex2f(x0, 1); 1.158 + glEnd(); 1.159 + } 1.160 + 1.161 + glPopMatrix(); 1.162 + glMatrixMode(GL_MODELVIEW); 1.163 + glPopMatrix(); 1.164 + 1.165 + glPopAttrib(); 1.166 } 1.167 - 1.168 -#endif 1.169 - 1.170 -#ifdef WIN32 1.171 -#include <windows.h> 1.172 - 1.173 -static void swap_buffers(void) 1.174 -{ 1.175 - SwapBuffers(wglGetCurrentDC()); 1.176 -} 1.177 -#endif