oculus1

changeset 28:cedf581048c7

merged with removal of the shaderless crap
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Oct 2013 04:24:16 +0300
parents 9f783284a503 75ab0d4ce2bb
children 9a973ef0e2a3
files src/main.cc
diffstat 3 files changed, 30 insertions(+), 231 deletions(-) [+]
line diff
     1.1 --- a/oculus1.vcproj	Sat Oct 26 03:54:35 2013 +0300
     1.2 +++ b/oculus1.vcproj	Sat Oct 26 04:24:16 2013 +0300
     1.3 @@ -553,22 +553,6 @@
     1.4  				</File>
     1.5  			</Filter>
     1.6  		</Filter>
     1.7 -		<Filter
     1.8 -			Name="sdr"
     1.9 -			>
    1.10 -			<File
    1.11 -				RelativePath=".\sdr\phong.p.glsl"
    1.12 -				>
    1.13 -			</File>
    1.14 -			<File
    1.15 -				RelativePath=".\sdr\phong.v.glsl"
    1.16 -				>
    1.17 -			</File>
    1.18 -			<File
    1.19 -				RelativePath=".\sdr\sdr.glsl"
    1.20 -				>
    1.21 -			</File>
    1.22 -		</Filter>
    1.23  	</Files>
    1.24  	<Globals>
    1.25  	</Globals>
     2.1 --- a/src/main.cc	Sat Oct 26 03:54:35 2013 +0300
     2.2 +++ b/src/main.cc	Sat Oct 26 04:24:16 2013 +0300
     2.3 @@ -431,9 +431,9 @@
     2.4  	}
     2.5  }
     2.6  
     2.7 +static bool fullscreen;
     2.8  static void toggle_fullscreen()
     2.9  {
    2.10 -	static bool fullscreen;
    2.11  	static int prev_x, prev_y;
    2.12  	static int prev_xsz, prev_ysz;
    2.13  
    2.14 @@ -515,13 +515,6 @@
    2.15  	case 'f':
    2.16  		toggle_fullscreen();
    2.17  		break;
    2.18 -
    2.19 -	case '\\':
    2.20 -		{
    2.21 -			extern bool dbg_enable;
    2.22 -			dbg_enable = !dbg_enable;
    2.23 -		}
    2.24 -		break;
    2.25  	}
    2.26  
    2.27  	keystate[key] = true;
    2.28 @@ -559,7 +552,11 @@
    2.29  	int center_y = height / 2;
    2.30  
    2.31  	int dx = x - center_x;
    2.32 -	int dy = use_vr ? 0 : y - center_y;
    2.33 +	int dy = y - center_y;
    2.34 +
    2.35 +	if(use_vr && fullscreen) {
    2.36 +		dy = 0;
    2.37 +	}
    2.38  
    2.39  	if(!dx && !dy) {
    2.40  		return;
     3.1 --- a/src/vr.cc	Sat Oct 26 03:54:35 2013 +0300
     3.2 +++ b/src/vr.cc	Sat Oct 26 04:24:16 2013 +0300
     3.3 @@ -10,40 +10,13 @@
     3.4  #include <malloc.h>
     3.5  #endif
     3.6  
     3.7 -#define USUB	28
     3.8 -#define VSUB	40
     3.9 -
    3.10 -/* these are just used for the shaderless precomputed distortion method */
    3.11 -struct Mesh {
    3.12 -	int prim;
    3.13 -	int num_verts, num_faces;
    3.14 -	unsigned int vbo;
    3.15 -	unsigned int ibo;
    3.16 -};
    3.17 -struct Vertex {
    3.18 -	float x, y, z;
    3.19 -	float tx, ty;
    3.20 -};
    3.21 -
    3.22 -
    3.23  static void init_ctx();
    3.24  static bool init_ovr();
    3.25  static bool init_sdr();
    3.26  
    3.27 -static Mesh gen_view_mesh(int usub, int vsub, float aspect, float lens_center_offset,
    3.28 -		float scale, const float *dist_factors, float tex_scale_x, float tex_scale_y);
    3.29 -static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale, const float *dist_factors);
    3.30 -static float barrel_scale(float rad, const float *k);
    3.31 -
    3.32  VRContext vr_ctx;
    3.33  static unsigned int sdrprog;
    3.34  
    3.35 -static Mesh wrapmesh[2];
    3.36 -static bool mesh_valid;
    3.37 -
    3.38 -bool dbg_enable;
    3.39 -
    3.40 -
    3.41  extern "C" int vr_init(enum vr_init_mode mode)
    3.42  {
    3.43  	glewInit();
    3.44 @@ -365,204 +338,49 @@
    3.45  		{-1, -1, 0, 1},
    3.46  		{0, -1, 1, 1}
    3.47  	};
    3.48 -	static const float quad_trans[3] = {0, -1, 1};
    3.49 -	static const float quad_scale[3] = {1, 0.5, 0.5};
    3.50  	static const float offs_scale[3] = {0.0, -1.0, 1.0};
    3.51 -	static int prev_tex_scale_x, prev_tex_scale_y;
    3.52  
    3.53  	glPushAttrib(GL_ENABLE_BIT);
    3.54  	glDisable(GL_DEPTH_TEST);
    3.55  	glDisable(GL_LIGHTING);
    3.56 -	glDisable(GL_CULL_FACE);
    3.57 +	glEnable(GL_TEXTURE_2D);
    3.58 +
    3.59 +	glMatrixMode(GL_MODELVIEW);
    3.60 +	glPushMatrix();
    3.61 +	glLoadIdentity();
    3.62  
    3.63  	glMatrixMode(GL_PROJECTION);
    3.64  	glPushMatrix();
    3.65  	glLoadIdentity();
    3.66  
    3.67 -	glMatrixMode(GL_MODELVIEW);
    3.68 -	glPushMatrix();
    3.69 -	glLoadIdentity();
    3.70 +	glUseProgram(sdrprog);
    3.71  
    3.72 -	if(!dbg_enable) {
    3.73 -		glUseProgram(sdrprog);
    3.74 -
    3.75 -		if(sdrprog) {
    3.76 -			int loc;
    3.77 -			if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) {
    3.78 -				float offset = vr_ctx.info.lens_center_offset * offs_scale[eye];
    3.79 -				glUniform1f(loc, offset);
    3.80 -			}
    3.81 -
    3.82 -			if((loc = glGetUniformLocation(sdrprog, "tex_scale")) != -1) {
    3.83 -				glUniform2f(loc, tex_scale_x, tex_scale_y);
    3.84 -			}
    3.85 +	if(sdrprog) {
    3.86 +		int loc;
    3.87 +		if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) {
    3.88 +			float offset = vr_ctx.info.lens_center_offset * offs_scale[eye];
    3.89 +			glUniform1f(loc, offset);
    3.90  		}
    3.91  
    3.92 -		glBindTexture(GL_TEXTURE_2D, tex);
    3.93 -		glBegin(GL_QUADS);
    3.94 -		glColor4f(1, 1, 1, 1);
    3.95 -		glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]);
    3.96 -		glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]);
    3.97 -		glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]);
    3.98 -		glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]);
    3.99 -		glEnd();
   3.100 -
   3.101 -		glUseProgram(0);
   3.102 -
   3.103 -	} else {
   3.104 -
   3.105 -		if(!mesh_valid || tex_scale_x != prev_tex_scale_x || tex_scale_y != prev_tex_scale_y) {
   3.106 -			for(int i=0; i<2; i++) {
   3.107 -				int eye = i + VR_EYE_LEFT;
   3.108 -
   3.109 -				if(wrapmesh[i].vbo) {
   3.110 -					glDeleteBuffers(1, &wrapmesh[i].vbo);
   3.111 -				}
   3.112 -				if(wrapmesh[i].ibo) {
   3.113 -					glDeleteBuffers(1, &wrapmesh[i].ibo);
   3.114 -				}
   3.115 -
   3.116 -				float aspect = vr_ctx.info.aspect / 2.0;
   3.117 -				float offset = vr_ctx.info.lens_center_offset * offs_scale[eye];
   3.118 -				wrapmesh[i] = gen_view_mesh(USUB, VSUB, aspect, offset, vr_ctx.info.scale, vr_ctx.info.distort,
   3.119 -					tex_scale_x, tex_scale_y);
   3.120 -			}
   3.121 -			mesh_valid = true;
   3.122 -			prev_tex_scale_x = tex_scale_x;
   3.123 -			prev_tex_scale_y = tex_scale_y;
   3.124 +		if((loc = glGetUniformLocation(sdrprog, "tex_scale")) != -1) {
   3.125 +			glUniform2f(loc, tex_scale_x, tex_scale_y);
   3.126  		}
   3.127 -
   3.128 -		glScalef(quad_scale[eye], 1.0, 1.0);
   3.129 -		glTranslatef(quad_trans[eye], 0, 0);
   3.130 -
   3.131 -		glUseProgram(0);
   3.132 -		glBindTexture(GL_TEXTURE_2D, tex);
   3.133 -		glEnable(GL_TEXTURE_2D);
   3.134 -
   3.135 -		glColor3f(1, 1, 1);
   3.136 -
   3.137 -		glEnableClientState(GL_VERTEX_ARRAY);
   3.138 -		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   3.139 -
   3.140 -		int meshidx = eye - VR_EYE_LEFT;
   3.141 -		glBindBuffer(GL_ARRAY_BUFFER, wrapmesh[meshidx].vbo);
   3.142 -		glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0);
   3.143 -		glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, tx));
   3.144 -
   3.145 -		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wrapmesh[meshidx].ibo);
   3.146 -		glDrawElements(GL_TRIANGLES, wrapmesh[meshidx].num_faces * 3, GL_UNSIGNED_INT, 0);
   3.147 -
   3.148 -		glBindBuffer(GL_ARRAY_BUFFER, 0);
   3.149 -		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
   3.150 -
   3.151 -		glDisableClientState(GL_VERTEX_ARRAY);
   3.152 -		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   3.153  	}
   3.154  
   3.155 -	glMatrixMode(GL_PROJECTION);
   3.156 +	glBindTexture(GL_TEXTURE_2D, tex);
   3.157 +	glBegin(GL_QUADS);
   3.158 +	glColor4f(1, 1, 1, 1);
   3.159 +	glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]);
   3.160 +	glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]);
   3.161 +	glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]);
   3.162 +	glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]);
   3.163 +	glEnd();
   3.164 +
   3.165 +	glUseProgram(0);
   3.166 +
   3.167  	glPopMatrix();
   3.168  	glMatrixMode(GL_MODELVIEW);
   3.169  	glPopMatrix();
   3.170  
   3.171  	glPopAttrib();
   3.172  }
   3.173 -
   3.174 -static Mesh gen_view_mesh(int usub, int vsub, float aspect, float lens_center_offset,
   3.175 -		float scale, const float *dist_factors, float tex_scale_x, float tex_scale_y)
   3.176 -{
   3.177 -	int uverts = usub + 1;
   3.178 -	int vverts = vsub + 1;
   3.179 -
   3.180 -	int num_verts = uverts * vverts;
   3.181 -	int num_quads = usub * vsub;
   3.182 -	int num_tris = num_quads * 2;
   3.183 -
   3.184 -	Vertex *varr = new Vertex[num_verts];
   3.185 -	unsigned int *iarr = new unsigned int[num_tris * 3];
   3.186 -
   3.187 -	float du = 1.0 / (float)usub;
   3.188 -	float dv = 1.0 / (float)vsub;
   3.189 -
   3.190 -	Vertex *vptr = varr;
   3.191 -	for(int i=0; i<vverts; i++) {
   3.192 -		float v = (float)i * dv;
   3.193 -		float y = 2.0 * v - 1.0;
   3.194 -
   3.195 -		for(int j=0; j<uverts; j++) {
   3.196 -			float u = (float)j * du;
   3.197 -			float x = 2.0 * u - 1.0;
   3.198 -			float tc[2] = {u, v};
   3.199 -
   3.200 -			distort_texcoords(tc, aspect, lens_center_offset, scale, dist_factors);
   3.201 -
   3.202 -			vptr->x = x;
   3.203 -			vptr->y = y;
   3.204 -			vptr->z = 0;
   3.205 -			vptr->tx = tc[0] * tex_scale_x;
   3.206 -			vptr->ty = tc[1] * tex_scale_y;
   3.207 -			vptr++;
   3.208 -		}
   3.209 -	}
   3.210 -
   3.211 -	unsigned int *iptr = iarr;
   3.212 -	for(int i=0; i<vsub; i++) {
   3.213 -		for(int j=0; j<usub; j++) {
   3.214 -			*iptr++ = i * uverts + j;
   3.215 -			*iptr++ = (i + 1) * uverts + j;
   3.216 -			*iptr++ = (i + 1) * uverts + (j + 1);
   3.217 -
   3.218 -			*iptr++ = i * uverts + j;
   3.219 -			*iptr++ = (i + 1) * uverts + (j + 1);
   3.220 -			*iptr++ = i * uverts + (j + 1);
   3.221 -		}
   3.222 -	}
   3.223 -
   3.224 -	unsigned int buf[2];
   3.225 -	glGenBuffers(2, buf);
   3.226 -	glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
   3.227 -	glBufferData(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW);
   3.228 -	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf[1]);
   3.229 -	glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW); 
   3.230 -
   3.231 -	delete [] varr;
   3.232 -	delete [] iarr;
   3.233 -
   3.234 -	Mesh mesh;
   3.235 -	mesh.prim = GL_TRIANGLES;
   3.236 -	mesh.num_verts = num_verts;
   3.237 -	mesh.num_faces = num_tris;
   3.238 -	mesh.vbo = buf[0];
   3.239 -	mesh.ibo = buf[1];
   3.240 -	return mesh;
   3.241 -}
   3.242 -
   3.243 -static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale, const float *dist_factors)
   3.244 -{
   3.245 -	// map tc [0, 1] -> [-1, 1]
   3.246 -	float ptx = tc[0] * 2.0 - 1.0;
   3.247 -	float pty = tc[1] * 2.0 - 1.0;
   3.248 -
   3.249 -	ptx += lens_center_offset * 2.0;
   3.250 -	pty /= aspect;	// correct for aspect ratio
   3.251 -
   3.252 -	float rad = barrel_scale(ptx * ptx + pty * pty, dist_factors);
   3.253 -	ptx *= rad;	// scale the point by the computer distortion radius
   3.254 -	pty *= rad;
   3.255 -
   3.256 -	ptx /= scale;
   3.257 -	pty /= scale;
   3.258 -
   3.259 -	pty *= aspect;
   3.260 -	ptx -= lens_center_offset * 2.0;
   3.261 -
   3.262 -	// map back to range [0, 1]
   3.263 -	tc[0] = ptx * 0.5 + 0.5;
   3.264 -	tc[1] = pty * 0.5 + 0.5;
   3.265 -}
   3.266 -
   3.267 -static float barrel_scale(float rad, const float *k)
   3.268 -{
   3.269 -	float radsq = rad * rad;
   3.270 -	float radquad = radsq * radsq;
   3.271 -	return k[0] + k[1] * radsq + k[2] * radquad + k[3] * radquad * radsq;
   3.272 -}