dungeon_crawler

annotate 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
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@1 109 theta += x;
nuclear@1 110 phi += y;
nuclear@1 111
nuclear@1 112 if(phi < -M_PI / 2.0)
nuclear@1 113 phi = -M_PI / 2.0;
nuclear@1 114 if(phi > M_PI / 2.0)
nuclear@1 115 phi = M_PI / 2.0;
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@1 130 FlyCamera::FlyCamera()
nuclear@1 131 {
nuclear@1 132 pos.z = 10.0f;
nuclear@1 133 }
nuclear@1 134
nuclear@1 135 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@1 136 {
nuclear@1 137 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
nuclear@1 138 Matrix4x4 tmat;
nuclear@1 139 tmat.set_translation(pos);
nuclear@1 140 *mat = tmat * Matrix4x4(rmat);
nuclear@1 141 }
nuclear@1 142
nuclear@1 143 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@1 144 {
nuclear@1 145 }*/
nuclear@1 146
nuclear@1 147 const Vector3 &FlyCamera::get_position() const
nuclear@1 148 {
nuclear@1 149 return pos;
nuclear@1 150 }
nuclear@1 151
nuclear@1 152 const Quaternion &FlyCamera::get_rotation() const
nuclear@1 153 {
nuclear@1 154 return rot;
nuclear@1 155 }
nuclear@1 156
nuclear@1 157 void FlyCamera::input_move(float x, float y, float z)
nuclear@1 158 {
nuclear@1 159 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
nuclear@1 160
nuclear@1 161 Vector3 k = vfwd.transformed(rot);
nuclear@1 162 Vector3 i = vright.transformed(rot);
nuclear@1 163 Vector3 j = cross_product(k, i);
nuclear@1 164
nuclear@1 165 pos += i * x + j * y + k * z;
nuclear@1 166 inval_cache();
nuclear@1 167 }
nuclear@1 168
nuclear@1 169 void FlyCamera::input_rotate(float x, float y, float z)
nuclear@1 170 {
nuclear@1 171 Vector3 axis(x, y, z);
nuclear@1 172 float axis_len = axis.length();
nuclear@1 173 rot.rotate(axis / axis_len, axis_len);
nuclear@1 174 rot.normalize();
nuclear@1 175
nuclear@1 176 inval_cache();
nuclear@1 177 }