oculus1

diff src/camera.cc @ 5:681046a82ed2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 05:01:38 +0300
parents
children b66b54a68dfd
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/camera.cc	Sun Sep 15 05:01:38 2013 +0300
     1.3 @@ -0,0 +1,190 @@
     1.4 +#include "opengl.h"
     1.5 +#include "camera.h"
     1.6 +
     1.7 +Camera::Camera()
     1.8 +{
     1.9 +	inval_cache();
    1.10 +}
    1.11 +
    1.12 +Camera::~Camera()
    1.13 +{
    1.14 +}
    1.15 +
    1.16 +void Camera::calc_inv_matrix(Matrix4x4 *mat) const
    1.17 +{
    1.18 +	*mat = matrix().inverse();
    1.19 +}
    1.20 +
    1.21 +void Camera::set_glmat(const Matrix4x4 &mat) const
    1.22 +{
    1.23 +#ifdef SINGLE_PRECISION_MATH
    1.24 +	if(glLoadTransposeMatrixfARB) {
    1.25 +		glLoadTransposeMatrixfARB((float*)&mat);
    1.26 +	} else {
    1.27 +		Matrix4x4 tmat = mat.transposed();
    1.28 +		glLoadMatrixf((float*)&tmat);
    1.29 +	}
    1.30 +#else
    1.31 +	if(glLoadTransposeMatrixdARB) {
    1.32 +		glLoadTransposeMatrixdARB((double*)&mat);
    1.33 +	} else {
    1.34 +		Matrix4x4 tmat = mat.transposed();
    1.35 +		glLoadMatrixd((double*)&tmat);
    1.36 +	}
    1.37 +#endif
    1.38 +}
    1.39 +
    1.40 +const Matrix4x4 &Camera::matrix() const
    1.41 +{
    1.42 +	if(!mcache.valid) {
    1.43 +		calc_matrix(&mcache.mat);
    1.44 +		mcache.valid = true;
    1.45 +	}
    1.46 +	return mcache.mat;
    1.47 +}
    1.48 +
    1.49 +const Matrix4x4 &Camera::inv_matrix() const
    1.50 +{
    1.51 +	if(!mcache_inv.valid) {
    1.52 +		calc_inv_matrix(&mcache_inv.mat);
    1.53 +		mcache_inv.valid = true;
    1.54 +	}
    1.55 +	return mcache_inv.mat;
    1.56 +}
    1.57 +
    1.58 +void Camera::use() const
    1.59 +{
    1.60 +	set_glmat(matrix());
    1.61 +}
    1.62 +
    1.63 +void Camera::use_inverse() const
    1.64 +{
    1.65 +	set_glmat(inv_matrix());
    1.66 +}
    1.67 +
    1.68 +void Camera::input_move(float x, float y, float z)
    1.69 +{
    1.70 +}
    1.71 +
    1.72 +void Camera::input_rotate(float x, float y, float z)
    1.73 +{
    1.74 +}
    1.75 +
    1.76 +void Camera::input_zoom(float factor)
    1.77 +{
    1.78 +}
    1.79 +
    1.80 +
    1.81 +// ---- orbit camera ----
    1.82 +
    1.83 +OrbitCamera::OrbitCamera()
    1.84 +{
    1.85 +	theta = 0.0;
    1.86 +	phi = 0.0;
    1.87 +	rad = 10.0;
    1.88 +}
    1.89 +
    1.90 +OrbitCamera::~OrbitCamera()
    1.91 +{
    1.92 +}
    1.93 +
    1.94 +void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
    1.95 +{
    1.96 +	mat->reset_identity();
    1.97 +	mat->translate(Vector3(0, 0, -rad));
    1.98 +	mat->rotate(Vector3(phi, 0, 0));
    1.99 +	mat->rotate(Vector3(0, theta, 0));
   1.100 +}
   1.101 +
   1.102 +void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
   1.103 +{
   1.104 +	mat->reset_identity();
   1.105 +	mat->rotate(Vector3(0, theta, 0));
   1.106 +	mat->rotate(Vector3(phi, 0, 0));
   1.107 +	mat->translate(Vector3(0, 0, -rad));
   1.108 +}
   1.109 +
   1.110 +void OrbitCamera::input_rotate(float x, float y, float z)
   1.111 +{
   1.112 +	theta += x;
   1.113 +	phi += y;
   1.114 +
   1.115 +	if(phi < -M_PI / 2)
   1.116 +		phi = -M_PI / 2;
   1.117 +	if(phi > M_PI)
   1.118 +		phi = M_PI;
   1.119 +
   1.120 +	inval_cache();
   1.121 +}
   1.122 +
   1.123 +void OrbitCamera::input_zoom(float factor)
   1.124 +{
   1.125 +	rad += factor;
   1.126 +	if(rad < 0.0)
   1.127 +		rad = 0.0;
   1.128 +
   1.129 +	inval_cache();
   1.130 +}
   1.131 +
   1.132 +
   1.133 +FlyCamera::FlyCamera()
   1.134 +{
   1.135 +	pos.z = 10.0f;
   1.136 +}
   1.137 +
   1.138 +void FlyCamera::calc_matrix(Matrix4x4 *mat) const
   1.139 +{
   1.140 +	/*mat->reset_identity();
   1.141 +	mat->translate(-pos);
   1.142 +	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
   1.143 +	mat->translate(pos);*/
   1.144 +	//mat->translate(-pos.transformed(rot));
   1.145 +
   1.146 +	Matrix3x3 qmat = rot.get_rotation_matrix();
   1.147 +
   1.148 +	Vector3 ivec = qmat.get_row_vector(0);
   1.149 +	Vector3 jvec = qmat.get_row_vector(1);
   1.150 +	Vector3 kvec = qmat.get_row_vector(2);
   1.151 +
   1.152 +	*mat = Matrix4x4(qmat);
   1.153 +	/*Vector3 trans_x = ivec * pos;
   1.154 +	Vector3 trans_y = jvec * pos;
   1.155 +	Vector3 trans_z = kvec * pos;
   1.156 +	Vector3 trans = trans_x + trans_y + trans_z;*/
   1.157 +	Vector3 trans;
   1.158 +	trans.x = dot_product(ivec, pos);
   1.159 +	trans.y = dot_product(jvec, pos);
   1.160 +	trans.z = dot_product(kvec, pos);
   1.161 +	mat->set_column_vector(-trans, 3);
   1.162 +}
   1.163 +
   1.164 +/*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
   1.165 +{
   1.166 +	mat->set_translation(pos);
   1.167 +	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
   1.168 +}*/
   1.169 +
   1.170 +const Vector3 &FlyCamera::get_position() const
   1.171 +{
   1.172 +	return pos;
   1.173 +}
   1.174 +
   1.175 +const Quaternion &FlyCamera::get_rotation() const
   1.176 +{
   1.177 +	return rot;
   1.178 +}
   1.179 +
   1.180 +void FlyCamera::input_move(float x, float y, float z)
   1.181 +{
   1.182 +	pos += Vector3(x, y, z);
   1.183 +	inval_cache();
   1.184 +}
   1.185 +
   1.186 +void FlyCamera::input_rotate(float x, float y, float z)
   1.187 +{
   1.188 +	Vector3 axis(x, y, z);
   1.189 +	float axis_len = axis.length();
   1.190 +	rot.rotate(axis / axis_len, axis_len);
   1.191 +	rot.normalize();
   1.192 +	inval_cache();
   1.193 +}