oculus1

diff src/camera.cc @ 9:b66b54a68dfd

tracking almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 19 Sep 2013 06:36:48 +0300
parents 681046a82ed2
children b2abb08c8f94
line diff
     1.1 --- a/src/camera.cc	Wed Sep 18 22:15:04 2013 +0300
     1.2 +++ b/src/camera.cc	Thu Sep 19 06:36:48 2013 +0300
     1.3 @@ -15,25 +15,6 @@
     1.4  	*mat = matrix().inverse();
     1.5  }
     1.6  
     1.7 -void Camera::set_glmat(const Matrix4x4 &mat) const
     1.8 -{
     1.9 -#ifdef SINGLE_PRECISION_MATH
    1.10 -	if(glLoadTransposeMatrixfARB) {
    1.11 -		glLoadTransposeMatrixfARB((float*)&mat);
    1.12 -	} else {
    1.13 -		Matrix4x4 tmat = mat.transposed();
    1.14 -		glLoadMatrixf((float*)&tmat);
    1.15 -	}
    1.16 -#else
    1.17 -	if(glLoadTransposeMatrixdARB) {
    1.18 -		glLoadTransposeMatrixdARB((double*)&mat);
    1.19 -	} else {
    1.20 -		Matrix4x4 tmat = mat.transposed();
    1.21 -		glLoadMatrixd((double*)&tmat);
    1.22 -	}
    1.23 -#endif
    1.24 -}
    1.25 -
    1.26  const Matrix4x4 &Camera::matrix() const
    1.27  {
    1.28  	if(!mcache.valid) {
    1.29 @@ -54,12 +35,12 @@
    1.30  
    1.31  void Camera::use() const
    1.32  {
    1.33 -	set_glmat(matrix());
    1.34 +	mult_matrix(matrix());
    1.35  }
    1.36  
    1.37  void Camera::use_inverse() const
    1.38  {
    1.39 -	set_glmat(inv_matrix());
    1.40 +	mult_matrix(inv_matrix());
    1.41  }
    1.42  
    1.43  void Camera::input_move(float x, float y, float z)
    1.44 @@ -106,8 +87,8 @@
    1.45  
    1.46  void OrbitCamera::input_rotate(float x, float y, float z)
    1.47  {
    1.48 -	theta += x;
    1.49 -	phi += y;
    1.50 +	theta += y;
    1.51 +	phi += x;
    1.52  
    1.53  	if(phi < -M_PI / 2)
    1.54  		phi = -M_PI / 2;
    1.55 @@ -127,6 +108,36 @@
    1.56  }
    1.57  
    1.58  
    1.59 +void FpsCamera::calc_matrix(Matrix4x4 *mat) const
    1.60 +{
    1.61 +	mat->reset_identity();
    1.62 +	mat->translate(Vector3(pos.x, pos.y, pos.z));
    1.63 +	mat->rotate(Vector3(0, theta, 0));
    1.64 +	mat->rotate(Vector3(phi, 0, 0));
    1.65 +}
    1.66 +
    1.67 +void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
    1.68 +{
    1.69 +	mat->reset_identity();
    1.70 +	mat->rotate(Vector3(phi, 0, 0));
    1.71 +	mat->rotate(Vector3(0, theta, 0));
    1.72 +	mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
    1.73 +}
    1.74 +
    1.75 +void FpsCamera::input_move(float x, float y, float z)
    1.76 +{
    1.77 +	pos.x += x * cos(theta) - z * sin(theta);
    1.78 +	pos.z += x * sin(theta) + z * cos(theta);
    1.79 +	pos.y += y;
    1.80 +	inval_cache();
    1.81 +}
    1.82 +
    1.83 +const Vector3 &FpsCamera::get_position() const
    1.84 +{
    1.85 +	return pos;
    1.86 +}
    1.87 +
    1.88 +
    1.89  FlyCamera::FlyCamera()
    1.90  {
    1.91  	pos.z = 10.0f;
    1.92 @@ -134,34 +145,14 @@
    1.93  
    1.94  void FlyCamera::calc_matrix(Matrix4x4 *mat) const
    1.95  {
    1.96 -	/*mat->reset_identity();
    1.97 -	mat->translate(-pos);
    1.98 -	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
    1.99 -	mat->translate(pos);*/
   1.100 -	//mat->translate(-pos.transformed(rot));
   1.101 -
   1.102 -	Matrix3x3 qmat = rot.get_rotation_matrix();
   1.103 -
   1.104 -	Vector3 ivec = qmat.get_row_vector(0);
   1.105 -	Vector3 jvec = qmat.get_row_vector(1);
   1.106 -	Vector3 kvec = qmat.get_row_vector(2);
   1.107 -
   1.108 -	*mat = Matrix4x4(qmat);
   1.109 -	/*Vector3 trans_x = ivec * pos;
   1.110 -	Vector3 trans_y = jvec * pos;
   1.111 -	Vector3 trans_z = kvec * pos;
   1.112 -	Vector3 trans = trans_x + trans_y + trans_z;*/
   1.113 -	Vector3 trans;
   1.114 -	trans.x = dot_product(ivec, pos);
   1.115 -	trans.y = dot_product(jvec, pos);
   1.116 -	trans.z = dot_product(kvec, pos);
   1.117 -	mat->set_column_vector(-trans, 3);
   1.118 +	Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
   1.119 +	Matrix4x4 tmat;
   1.120 +	tmat.set_translation(pos);
   1.121 +	*mat = tmat * Matrix4x4(rmat);
   1.122  }
   1.123  
   1.124  /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
   1.125  {
   1.126 -	mat->set_translation(pos);
   1.127 -	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
   1.128  }*/
   1.129  
   1.130  const Vector3 &FlyCamera::get_position() const
   1.131 @@ -176,7 +167,13 @@
   1.132  
   1.133  void FlyCamera::input_move(float x, float y, float z)
   1.134  {
   1.135 -	pos += Vector3(x, y, z);
   1.136 +	static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
   1.137 +
   1.138 +	Vector3 k = vfwd.transformed(rot);
   1.139 +	Vector3	i = vright.transformed(rot);
   1.140 +	Vector3 j = cross_product(k, i);
   1.141 +
   1.142 +	pos += i * x + j * y + k * z;
   1.143  	inval_cache();
   1.144  }
   1.145  
   1.146 @@ -186,5 +183,6 @@
   1.147  	float axis_len = axis.length();
   1.148  	rot.rotate(axis / axis_len, axis_len);
   1.149  	rot.normalize();
   1.150 +
   1.151  	inval_cache();
   1.152  }