dungeon_crawler

annotate prototype/src/camera.cc @ 8:77c302306206

forgot to uncomment something
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Aug 2012 14:30:25 +0300
parents 8fb37db44fd8
children 5c41e6fcb300
rev   line source
nuclear@1 1 #include "opengl.h"
nuclear@1 2 #include "camera.h"
nuclear@1 3
nuclear@1 4 Camera::Camera()
nuclear@1 5 {
nuclear@1 6 inval_cache();
nuclear@1 7 }
nuclear@1 8
nuclear@1 9 Camera::~Camera()
nuclear@1 10 {
nuclear@1 11 }
nuclear@1 12
nuclear@1 13 void Camera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@1 14 {
nuclear@1 15 *mat = matrix().inverse();
nuclear@1 16 }
nuclear@1 17
nuclear@1 18 void Camera::set_glmat(const Matrix4x4 &mat) const
nuclear@1 19 {
nuclear@1 20 #ifdef SINGLE_PRECISION_MATH
nuclear@1 21 if(glLoadTransposeMatrixfARB) {
nuclear@1 22 glLoadTransposeMatrixfARB((float*)&mat);
nuclear@1 23 } else {
nuclear@1 24 Matrix4x4 tmat = mat.transposed();
nuclear@1 25 glLoadMatrixf((float*)&tmat);
nuclear@1 26 }
nuclear@1 27 #else
nuclear@1 28 if(glLoadTransposeMatrixdARB) {
nuclear@1 29 glLoadTransposeMatrixdARB((double*)&mat);
nuclear@1 30 } else {
nuclear@1 31 Matrix4x4 tmat = mat.transposed();
nuclear@1 32 glLoadMatrixd((double*)&tmat);
nuclear@1 33 }
nuclear@1 34 #endif
nuclear@1 35 }
nuclear@1 36
nuclear@1 37 const Matrix4x4 &Camera::matrix() const
nuclear@1 38 {
nuclear@1 39 if(!mcache.valid) {
nuclear@1 40 calc_matrix(&mcache.mat);
nuclear@1 41 mcache.valid = true;
nuclear@1 42 }
nuclear@1 43 return mcache.mat;
nuclear@1 44 }
nuclear@1 45
nuclear@1 46 const Matrix4x4 &Camera::inv_matrix() const
nuclear@1 47 {
nuclear@1 48 if(!mcache_inv.valid) {
nuclear@1 49 calc_inv_matrix(&mcache_inv.mat);
nuclear@1 50 mcache_inv.valid = true;
nuclear@1 51 }
nuclear@1 52 return mcache_inv.mat;
nuclear@1 53 }
nuclear@1 54
nuclear@1 55 void Camera::use() const
nuclear@1 56 {
nuclear@1 57 set_glmat(matrix());
nuclear@1 58 }
nuclear@1 59
nuclear@1 60 void Camera::use_inverse() const
nuclear@1 61 {
nuclear@1 62 set_glmat(inv_matrix());
nuclear@1 63 }
nuclear@1 64
nuclear@1 65 void Camera::input_move(float x, float y, float z)
nuclear@1 66 {
nuclear@1 67 }
nuclear@1 68
nuclear@1 69 void Camera::input_rotate(float x, float y, float z)
nuclear@1 70 {
nuclear@1 71 }
nuclear@1 72
nuclear@1 73 void Camera::input_zoom(float factor)
nuclear@1 74 {
nuclear@1 75 }
nuclear@1 76
nuclear@1 77
nuclear@1 78 // ---- orbit camera ----
nuclear@1 79
nuclear@1 80 OrbitCamera::OrbitCamera()
nuclear@1 81 {
nuclear@1 82 theta = 0.0;
nuclear@1 83 phi = 0.0;
nuclear@1 84 rad = 10.0;
nuclear@1 85 }
nuclear@1 86
nuclear@1 87 OrbitCamera::~OrbitCamera()
nuclear@1 88 {
nuclear@1 89 }
nuclear@1 90
nuclear@1 91 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@1 92 {
nuclear@1 93 mat->reset_identity();
nuclear@1 94 mat->translate(Vector3(0, 0, -rad));
nuclear@1 95 mat->rotate(Vector3(phi, 0, 0));
nuclear@1 96 mat->rotate(Vector3(0, theta, 0));
nuclear@1 97 }
nuclear@1 98
nuclear@1 99 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@1 100 {
nuclear@1 101 mat->reset_identity();
nuclear@1 102 mat->rotate(Vector3(0, theta, 0));
nuclear@1 103 mat->rotate(Vector3(phi, 0, 0));
nuclear@1 104 mat->translate(Vector3(0, 0, -rad));
nuclear@1 105 }
nuclear@1 106
nuclear@1 107 void OrbitCamera::input_rotate(float x, float y, float z)
nuclear@1 108 {
nuclear@7 109 theta += y;
nuclear@7 110 phi += x;
nuclear@1 111
nuclear@7 112 if(phi < -M_PI / 2)
nuclear@7 113 phi = -M_PI / 2;
nuclear@7 114 if(phi > M_PI)
nuclear@7 115 phi = M_PI;
nuclear@1 116
nuclear@1 117 inval_cache();
nuclear@1 118 }
nuclear@1 119
nuclear@1 120 void OrbitCamera::input_zoom(float factor)
nuclear@1 121 {
nuclear@1 122 rad += factor;
nuclear@1 123 if(rad < 0.0)
nuclear@1 124 rad = 0.0;
nuclear@1 125
nuclear@1 126 inval_cache();
nuclear@1 127 }
nuclear@1 128
nuclear@1 129
nuclear@7 130 void FpsCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@7 131 {
nuclear@7 132 mat->reset_identity();
nuclear@8 133 mat->translate(Vector3(pos.x, pos.y, pos.z));
nuclear@8 134 mat->rotate(Vector3(0, theta, 0));
nuclear@7 135 mat->rotate(Vector3(phi, 0, 0));
nuclear@7 136 }
nuclear@7 137
nuclear@7 138 void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@7 139 {
nuclear@7 140 mat->reset_identity();
nuclear@7 141 mat->rotate(Vector3(phi, 0, 0));
nuclear@7 142 mat->rotate(Vector3(0, theta, 0));
nuclear@7 143 mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
nuclear@7 144 }
nuclear@7 145
nuclear@7 146 void FpsCamera::input_move(float x, float y, float z)
nuclear@7 147 {
nuclear@7 148 pos.x += x * cos(theta) - z * sin(theta);
nuclear@7 149 pos.z += x * sin(theta) + z * cos(theta);
nuclear@7 150 pos.y += y;
nuclear@7 151 inval_cache();
nuclear@7 152 }
nuclear@7 153
nuclear@7 154
nuclear@7 155
nuclear@1 156 FlyCamera::FlyCamera()
nuclear@1 157 {
nuclear@1 158 pos.z = 10.0f;
nuclear@1 159 }
nuclear@1 160
nuclear@1 161 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@1 162 {
nuclear@1 163 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
nuclear@1 164 Matrix4x4 tmat;
nuclear@1 165 tmat.set_translation(pos);
nuclear@1 166 *mat = tmat * Matrix4x4(rmat);
nuclear@1 167 }
nuclear@1 168
nuclear@1 169 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@1 170 {
nuclear@1 171 }*/
nuclear@1 172
nuclear@1 173 const Vector3 &FlyCamera::get_position() const
nuclear@1 174 {
nuclear@1 175 return pos;
nuclear@1 176 }
nuclear@1 177
nuclear@1 178 const Quaternion &FlyCamera::get_rotation() const
nuclear@1 179 {
nuclear@1 180 return rot;
nuclear@1 181 }
nuclear@1 182
nuclear@1 183 void FlyCamera::input_move(float x, float y, float z)
nuclear@1 184 {
nuclear@1 185 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
nuclear@1 186
nuclear@1 187 Vector3 k = vfwd.transformed(rot);
nuclear@1 188 Vector3 i = vright.transformed(rot);
nuclear@1 189 Vector3 j = cross_product(k, i);
nuclear@1 190
nuclear@1 191 pos += i * x + j * y + k * z;
nuclear@1 192 inval_cache();
nuclear@1 193 }
nuclear@1 194
nuclear@1 195 void FlyCamera::input_rotate(float x, float y, float z)
nuclear@1 196 {
nuclear@1 197 Vector3 axis(x, y, z);
nuclear@1 198 float axis_len = axis.length();
nuclear@1 199 rot.rotate(axis / axis_len, axis_len);
nuclear@1 200 rot.normalize();
nuclear@1 201
nuclear@1 202 inval_cache();
nuclear@1 203 }