dungeon_crawler

diff prototype/src/camera.cc @ 1:96de911d05d4

started a rough prototype
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 28 Jun 2012 06:05:50 +0300
parents
children 8fb37db44fd8
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/camera.cc	Thu Jun 28 06:05:50 2012 +0300
     1.3 @@ -0,0 +1,177 @@
     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.0)
   1.116 +		phi = -M_PI / 2.0;
   1.117 +	if(phi > M_PI / 2.0)
   1.118 +		phi = M_PI / 2.0;
   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 +	Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
   1.141 +	Matrix4x4 tmat;
   1.142 +	tmat.set_translation(pos);
   1.143 +	*mat = tmat * Matrix4x4(rmat);
   1.144 +}
   1.145 +
   1.146 +/*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
   1.147 +{
   1.148 +}*/
   1.149 +
   1.150 +const Vector3 &FlyCamera::get_position() const
   1.151 +{
   1.152 +	return pos;
   1.153 +}
   1.154 +
   1.155 +const Quaternion &FlyCamera::get_rotation() const
   1.156 +{
   1.157 +	return rot;
   1.158 +}
   1.159 +
   1.160 +void FlyCamera::input_move(float x, float y, float z)
   1.161 +{
   1.162 +	static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
   1.163 +
   1.164 +	Vector3 k = vfwd.transformed(rot);
   1.165 +	Vector3	i = vright.transformed(rot);
   1.166 +	Vector3 j = cross_product(k, i);
   1.167 +
   1.168 +	pos += i * x + j * y + k * z;
   1.169 +	inval_cache();
   1.170 +}
   1.171 +
   1.172 +void FlyCamera::input_rotate(float x, float y, float z)
   1.173 +{
   1.174 +	Vector3 axis(x, y, z);
   1.175 +	float axis_len = axis.length();
   1.176 +	rot.rotate(axis / axis_len, axis_len);
   1.177 +	rot.normalize();
   1.178 +
   1.179 +	inval_cache();
   1.180 +}