dungeon_crawler

annotate prototype/src/camera.cc @ 18:5c41e6fcb300

- commandline arguments - stereoscopic rendering - FBO fixed
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 03:17:48 +0300
parents 77c302306206
children aa9e28670ae2
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 const Matrix4x4 &Camera::matrix() const
nuclear@1 19 {
nuclear@1 20 if(!mcache.valid) {
nuclear@1 21 calc_matrix(&mcache.mat);
nuclear@1 22 mcache.valid = true;
nuclear@1 23 }
nuclear@1 24 return mcache.mat;
nuclear@1 25 }
nuclear@1 26
nuclear@1 27 const Matrix4x4 &Camera::inv_matrix() const
nuclear@1 28 {
nuclear@1 29 if(!mcache_inv.valid) {
nuclear@1 30 calc_inv_matrix(&mcache_inv.mat);
nuclear@1 31 mcache_inv.valid = true;
nuclear@1 32 }
nuclear@1 33 return mcache_inv.mat;
nuclear@1 34 }
nuclear@1 35
nuclear@1 36 void Camera::use() const
nuclear@1 37 {
nuclear@18 38 mult_matrix(matrix());
nuclear@1 39 }
nuclear@1 40
nuclear@1 41 void Camera::use_inverse() const
nuclear@1 42 {
nuclear@18 43 mult_matrix(inv_matrix());
nuclear@1 44 }
nuclear@1 45
nuclear@1 46 void Camera::input_move(float x, float y, float z)
nuclear@1 47 {
nuclear@1 48 }
nuclear@1 49
nuclear@1 50 void Camera::input_rotate(float x, float y, float z)
nuclear@1 51 {
nuclear@1 52 }
nuclear@1 53
nuclear@1 54 void Camera::input_zoom(float factor)
nuclear@1 55 {
nuclear@1 56 }
nuclear@1 57
nuclear@1 58
nuclear@1 59 // ---- orbit camera ----
nuclear@1 60
nuclear@1 61 OrbitCamera::OrbitCamera()
nuclear@1 62 {
nuclear@1 63 theta = 0.0;
nuclear@1 64 phi = 0.0;
nuclear@1 65 rad = 10.0;
nuclear@1 66 }
nuclear@1 67
nuclear@1 68 OrbitCamera::~OrbitCamera()
nuclear@1 69 {
nuclear@1 70 }
nuclear@1 71
nuclear@1 72 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@1 73 {
nuclear@1 74 mat->reset_identity();
nuclear@1 75 mat->translate(Vector3(0, 0, -rad));
nuclear@1 76 mat->rotate(Vector3(phi, 0, 0));
nuclear@1 77 mat->rotate(Vector3(0, theta, 0));
nuclear@1 78 }
nuclear@1 79
nuclear@1 80 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@1 81 {
nuclear@1 82 mat->reset_identity();
nuclear@1 83 mat->rotate(Vector3(0, theta, 0));
nuclear@1 84 mat->rotate(Vector3(phi, 0, 0));
nuclear@1 85 mat->translate(Vector3(0, 0, -rad));
nuclear@1 86 }
nuclear@1 87
nuclear@1 88 void OrbitCamera::input_rotate(float x, float y, float z)
nuclear@1 89 {
nuclear@7 90 theta += y;
nuclear@7 91 phi += x;
nuclear@1 92
nuclear@7 93 if(phi < -M_PI / 2)
nuclear@7 94 phi = -M_PI / 2;
nuclear@7 95 if(phi > M_PI)
nuclear@7 96 phi = M_PI;
nuclear@1 97
nuclear@1 98 inval_cache();
nuclear@1 99 }
nuclear@1 100
nuclear@1 101 void OrbitCamera::input_zoom(float factor)
nuclear@1 102 {
nuclear@1 103 rad += factor;
nuclear@1 104 if(rad < 0.0)
nuclear@1 105 rad = 0.0;
nuclear@1 106
nuclear@1 107 inval_cache();
nuclear@1 108 }
nuclear@1 109
nuclear@1 110
nuclear@7 111 void FpsCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@7 112 {
nuclear@7 113 mat->reset_identity();
nuclear@8 114 mat->translate(Vector3(pos.x, pos.y, pos.z));
nuclear@8 115 mat->rotate(Vector3(0, theta, 0));
nuclear@7 116 mat->rotate(Vector3(phi, 0, 0));
nuclear@7 117 }
nuclear@7 118
nuclear@7 119 void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@7 120 {
nuclear@7 121 mat->reset_identity();
nuclear@7 122 mat->rotate(Vector3(phi, 0, 0));
nuclear@7 123 mat->rotate(Vector3(0, theta, 0));
nuclear@7 124 mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
nuclear@7 125 }
nuclear@7 126
nuclear@7 127 void FpsCamera::input_move(float x, float y, float z)
nuclear@7 128 {
nuclear@7 129 pos.x += x * cos(theta) - z * sin(theta);
nuclear@7 130 pos.z += x * sin(theta) + z * cos(theta);
nuclear@7 131 pos.y += y;
nuclear@7 132 inval_cache();
nuclear@7 133 }
nuclear@7 134
nuclear@7 135
nuclear@7 136
nuclear@1 137 FlyCamera::FlyCamera()
nuclear@1 138 {
nuclear@1 139 pos.z = 10.0f;
nuclear@1 140 }
nuclear@1 141
nuclear@1 142 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@1 143 {
nuclear@1 144 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
nuclear@1 145 Matrix4x4 tmat;
nuclear@1 146 tmat.set_translation(pos);
nuclear@1 147 *mat = tmat * Matrix4x4(rmat);
nuclear@1 148 }
nuclear@1 149
nuclear@1 150 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@1 151 {
nuclear@1 152 }*/
nuclear@1 153
nuclear@1 154 const Vector3 &FlyCamera::get_position() const
nuclear@1 155 {
nuclear@1 156 return pos;
nuclear@1 157 }
nuclear@1 158
nuclear@1 159 const Quaternion &FlyCamera::get_rotation() const
nuclear@1 160 {
nuclear@1 161 return rot;
nuclear@1 162 }
nuclear@1 163
nuclear@1 164 void FlyCamera::input_move(float x, float y, float z)
nuclear@1 165 {
nuclear@1 166 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
nuclear@1 167
nuclear@1 168 Vector3 k = vfwd.transformed(rot);
nuclear@1 169 Vector3 i = vright.transformed(rot);
nuclear@1 170 Vector3 j = cross_product(k, i);
nuclear@1 171
nuclear@1 172 pos += i * x + j * y + k * z;
nuclear@1 173 inval_cache();
nuclear@1 174 }
nuclear@1 175
nuclear@1 176 void FlyCamera::input_rotate(float x, float y, float z)
nuclear@1 177 {
nuclear@1 178 Vector3 axis(x, y, z);
nuclear@1 179 float axis_len = axis.length();
nuclear@1 180 rot.rotate(axis / axis_len, axis_len);
nuclear@1 181 rot.normalize();
nuclear@1 182
nuclear@1 183 inval_cache();
nuclear@1 184 }