oculus1
diff src/vr.cc @ 26:75ab0d4ce2bb
backed out the shaderless oculus distortion method, as it's completely pointless
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 04 Oct 2013 14:57:33 +0300 |
parents | ef4c9d8eeca7 |
children |
line diff
1.1 --- a/src/vr.cc Fri Oct 04 14:50:50 2013 +0300 1.2 +++ b/src/vr.cc Fri Oct 04 14:57:33 2013 +0300 1.3 @@ -10,40 +10,13 @@ 1.4 #include <malloc.h> 1.5 #endif 1.6 1.7 -#define USUB 28 1.8 -#define VSUB 40 1.9 - 1.10 -/* these are just used for the shaderless precomputed distortion method */ 1.11 -struct Mesh { 1.12 - int prim; 1.13 - int num_verts, num_faces; 1.14 - unsigned int vbo; 1.15 - unsigned int ibo; 1.16 -}; 1.17 -struct Vertex { 1.18 - float x, y, z; 1.19 - float tx, ty; 1.20 -}; 1.21 - 1.22 - 1.23 static void init_ctx(); 1.24 static bool init_ovr(); 1.25 static bool init_sdr(); 1.26 1.27 -static Mesh gen_view_mesh(int usub, int vsub, float aspect, float lens_center_offset, 1.28 - float scale, const float *dist_factors, float tex_scale_x, float tex_scale_y); 1.29 -static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale, const float *dist_factors); 1.30 -static float barrel_scale(float rad, const float *k); 1.31 - 1.32 VRContext vr_ctx; 1.33 static unsigned int sdrprog; 1.34 1.35 -static Mesh wrapmesh[2]; 1.36 -static bool mesh_valid; 1.37 - 1.38 -bool dbg_enable; 1.39 - 1.40 - 1.41 extern "C" int vr_init(enum vr_init_mode mode) 1.42 { 1.43 glewInit(); 1.44 @@ -365,204 +338,49 @@ 1.45 {-1, -1, 0, 1}, 1.46 {0, -1, 1, 1} 1.47 }; 1.48 - static const float quad_trans[3] = {0, -1, 1}; 1.49 - static const float quad_scale[3] = {1, 0.5, 0.5}; 1.50 static const float offs_scale[3] = {0.0, -1.0, 1.0}; 1.51 - static int prev_tex_scale_x, prev_tex_scale_y; 1.52 1.53 glPushAttrib(GL_ENABLE_BIT); 1.54 glDisable(GL_DEPTH_TEST); 1.55 glDisable(GL_LIGHTING); 1.56 - glDisable(GL_CULL_FACE); 1.57 + glEnable(GL_TEXTURE_2D); 1.58 + 1.59 + glMatrixMode(GL_MODELVIEW); 1.60 + glPushMatrix(); 1.61 + glLoadIdentity(); 1.62 1.63 glMatrixMode(GL_PROJECTION); 1.64 glPushMatrix(); 1.65 glLoadIdentity(); 1.66 1.67 - glMatrixMode(GL_MODELVIEW); 1.68 - glPushMatrix(); 1.69 - glLoadIdentity(); 1.70 + glUseProgram(sdrprog); 1.71 1.72 - if(!dbg_enable) { 1.73 - glUseProgram(sdrprog); 1.74 - 1.75 - if(sdrprog) { 1.76 - int loc; 1.77 - if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) { 1.78 - float offset = vr_ctx.info.lens_center_offset * offs_scale[eye]; 1.79 - glUniform1f(loc, offset); 1.80 - } 1.81 - 1.82 - if((loc = glGetUniformLocation(sdrprog, "tex_scale")) != -1) { 1.83 - glUniform2f(loc, tex_scale_x, tex_scale_y); 1.84 - } 1.85 + if(sdrprog) { 1.86 + int loc; 1.87 + if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) { 1.88 + float offset = vr_ctx.info.lens_center_offset * offs_scale[eye]; 1.89 + glUniform1f(loc, offset); 1.90 } 1.91 1.92 - glBindTexture(GL_TEXTURE_2D, tex); 1.93 - glBegin(GL_QUADS); 1.94 - glColor4f(1, 1, 1, 1); 1.95 - glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]); 1.96 - glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]); 1.97 - glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]); 1.98 - glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]); 1.99 - glEnd(); 1.100 - 1.101 - glUseProgram(0); 1.102 - 1.103 - } else { 1.104 - 1.105 - if(!mesh_valid || tex_scale_x != prev_tex_scale_x || tex_scale_y != prev_tex_scale_y) { 1.106 - for(int i=0; i<2; i++) { 1.107 - int eye = i + VR_EYE_LEFT; 1.108 - 1.109 - if(wrapmesh[i].vbo) { 1.110 - glDeleteBuffers(1, &wrapmesh[i].vbo); 1.111 - } 1.112 - if(wrapmesh[i].ibo) { 1.113 - glDeleteBuffers(1, &wrapmesh[i].ibo); 1.114 - } 1.115 - 1.116 - float aspect = vr_ctx.info.aspect / 2.0; 1.117 - float offset = vr_ctx.info.lens_center_offset * offs_scale[eye]; 1.118 - wrapmesh[i] = gen_view_mesh(USUB, VSUB, aspect, offset, vr_ctx.info.scale, vr_ctx.info.distort, 1.119 - tex_scale_x, tex_scale_y); 1.120 - } 1.121 - mesh_valid = true; 1.122 - prev_tex_scale_x = tex_scale_x; 1.123 - prev_tex_scale_y = tex_scale_y; 1.124 + if((loc = glGetUniformLocation(sdrprog, "tex_scale")) != -1) { 1.125 + glUniform2f(loc, tex_scale_x, tex_scale_y); 1.126 } 1.127 - 1.128 - glScalef(quad_scale[eye], 1.0, 1.0); 1.129 - glTranslatef(quad_trans[eye], 0, 0); 1.130 - 1.131 - glUseProgram(0); 1.132 - glBindTexture(GL_TEXTURE_2D, tex); 1.133 - glEnable(GL_TEXTURE_2D); 1.134 - 1.135 - glColor3f(1, 1, 1); 1.136 - 1.137 - glEnableClientState(GL_VERTEX_ARRAY); 1.138 - glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1.139 - 1.140 - int meshidx = eye - VR_EYE_LEFT; 1.141 - glBindBuffer(GL_ARRAY_BUFFER, wrapmesh[meshidx].vbo); 1.142 - glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0); 1.143 - glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, tx)); 1.144 - 1.145 - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wrapmesh[meshidx].ibo); 1.146 - glDrawElements(GL_TRIANGLES, wrapmesh[meshidx].num_faces * 3, GL_UNSIGNED_INT, 0); 1.147 - 1.148 - glBindBuffer(GL_ARRAY_BUFFER, 0); 1.149 - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1.150 - 1.151 - glDisableClientState(GL_VERTEX_ARRAY); 1.152 - glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1.153 } 1.154 1.155 - glMatrixMode(GL_PROJECTION); 1.156 + glBindTexture(GL_TEXTURE_2D, tex); 1.157 + glBegin(GL_QUADS); 1.158 + glColor4f(1, 1, 1, 1); 1.159 + glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]); 1.160 + glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]); 1.161 + glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]); 1.162 + glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]); 1.163 + glEnd(); 1.164 + 1.165 + glUseProgram(0); 1.166 + 1.167 glPopMatrix(); 1.168 glMatrixMode(GL_MODELVIEW); 1.169 glPopMatrix(); 1.170 1.171 glPopAttrib(); 1.172 } 1.173 - 1.174 -static Mesh gen_view_mesh(int usub, int vsub, float aspect, float lens_center_offset, 1.175 - float scale, const float *dist_factors, float tex_scale_x, float tex_scale_y) 1.176 -{ 1.177 - int uverts = usub + 1; 1.178 - int vverts = vsub + 1; 1.179 - 1.180 - int num_verts = uverts * vverts; 1.181 - int num_quads = usub * vsub; 1.182 - int num_tris = num_quads * 2; 1.183 - 1.184 - Vertex *varr = new Vertex[num_verts]; 1.185 - unsigned int *iarr = new unsigned int[num_tris * 3]; 1.186 - 1.187 - float du = 1.0 / (float)usub; 1.188 - float dv = 1.0 / (float)vsub; 1.189 - 1.190 - Vertex *vptr = varr; 1.191 - for(int i=0; i<vverts; i++) { 1.192 - float v = (float)i * dv; 1.193 - float y = 2.0 * v - 1.0; 1.194 - 1.195 - for(int j=0; j<uverts; j++) { 1.196 - float u = (float)j * du; 1.197 - float x = 2.0 * u - 1.0; 1.198 - float tc[2] = {u, v}; 1.199 - 1.200 - distort_texcoords(tc, aspect, lens_center_offset, scale, dist_factors); 1.201 - 1.202 - vptr->x = x; 1.203 - vptr->y = y; 1.204 - vptr->z = 0; 1.205 - vptr->tx = tc[0] * tex_scale_x; 1.206 - vptr->ty = tc[1] * tex_scale_y; 1.207 - vptr++; 1.208 - } 1.209 - } 1.210 - 1.211 - unsigned int *iptr = iarr; 1.212 - for(int i=0; i<vsub; i++) { 1.213 - for(int j=0; j<usub; j++) { 1.214 - *iptr++ = i * uverts + j; 1.215 - *iptr++ = (i + 1) * uverts + j; 1.216 - *iptr++ = (i + 1) * uverts + (j + 1); 1.217 - 1.218 - *iptr++ = i * uverts + j; 1.219 - *iptr++ = (i + 1) * uverts + (j + 1); 1.220 - *iptr++ = i * uverts + (j + 1); 1.221 - } 1.222 - } 1.223 - 1.224 - unsigned int buf[2]; 1.225 - glGenBuffers(2, buf); 1.226 - glBindBuffer(GL_ARRAY_BUFFER, buf[0]); 1.227 - glBufferData(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW); 1.228 - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf[1]); 1.229 - glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW); 1.230 - 1.231 - delete [] varr; 1.232 - delete [] iarr; 1.233 - 1.234 - Mesh mesh; 1.235 - mesh.prim = GL_TRIANGLES; 1.236 - mesh.num_verts = num_verts; 1.237 - mesh.num_faces = num_tris; 1.238 - mesh.vbo = buf[0]; 1.239 - mesh.ibo = buf[1]; 1.240 - return mesh; 1.241 -} 1.242 - 1.243 -static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale, const float *dist_factors) 1.244 -{ 1.245 - // map tc [0, 1] -> [-1, 1] 1.246 - float ptx = tc[0] * 2.0 - 1.0; 1.247 - float pty = tc[1] * 2.0 - 1.0; 1.248 - 1.249 - ptx += lens_center_offset * 2.0; 1.250 - pty /= aspect; // correct for aspect ratio 1.251 - 1.252 - float rad = barrel_scale(ptx * ptx + pty * pty, dist_factors); 1.253 - ptx *= rad; // scale the point by the computer distortion radius 1.254 - pty *= rad; 1.255 - 1.256 - ptx /= scale; 1.257 - pty /= scale; 1.258 - 1.259 - pty *= aspect; 1.260 - ptx -= lens_center_offset * 2.0; 1.261 - 1.262 - // map back to range [0, 1] 1.263 - tc[0] = ptx * 0.5 + 0.5; 1.264 - tc[1] = pty * 0.5 + 0.5; 1.265 -} 1.266 - 1.267 -static float barrel_scale(float rad, const float *k) 1.268 -{ 1.269 - float radsq = rad * rad; 1.270 - float radquad = radsq * radsq; 1.271 - return k[0] + k[1] * radsq + k[2] * radquad + k[3] * radquad * radsq; 1.272 -}