2 This file is part of the graphics core library.
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
6 the graphics core library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 the graphics core library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the graphics core library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "3dengfx_config.h"
23 #include "base_cam.hpp"
26 #include "3dengfx/3denginefx.hpp"
27 #endif // USING_3DENGFX
29 FrustumPlane::FrustumPlane() {
33 FrustumPlane::FrustumPlane(scalar_t a, scalar_t b, scalar_t c, scalar_t d) {
40 // frustum plane extraction from a projection (or mvp) matrix
41 FrustumPlane::FrustumPlane(const Matrix4x4 &mat, int plane) {
45 a = mat[3][0] + (neg ? -mat[i][0] : mat[i][0]);
46 b = mat[3][1] + (neg ? -mat[i][1] : mat[i][1]);
47 c = mat[3][2] + (neg ? -mat[i][2] : mat[i][2]);
48 d = mat[3][3] + (neg ? -mat[i][3] : mat[i][3]);
50 // normalize plane equation
51 scalar_t len = Vector3(a, b, c).length();
58 BaseCamera::BaseCamera(const Vector3 &trans, const Quaternion &rot) {
61 up = Vector3(0, 1, 0);
66 flip_view.x = flip_view.y = flip_view.z = false;
69 BaseCamera::~BaseCamera() {}
71 void BaseCamera::setup_frustum(const Matrix4x4 &m) {
72 for(int i=0; i<6; i++) {
73 frustum[i] = FrustumPlane(m, i);
77 void BaseCamera::set_up_vector(const Vector3 &up) {
81 void BaseCamera::set_fov(scalar_t angle) {
85 scalar_t BaseCamera::get_fov() const {
89 void BaseCamera::set_aspect(scalar_t aspect) {
90 this->aspect = aspect;
93 scalar_t BaseCamera::get_aspect() const {
97 void BaseCamera::set_clipping_planes(scalar_t near_clip, scalar_t far_clip) {
98 this->near_clip = near_clip;
99 this->far_clip = far_clip;
102 void BaseCamera::set_clipping_plane(scalar_t val, ClipPlane which) {
103 if(which == CLIP_NEAR) {
110 scalar_t BaseCamera::get_clipping_plane(ClipPlane which) const {
111 return which == CLIP_NEAR ? near_clip : far_clip;
115 void BaseCamera::zoom(scalar_t zoom_factor, unsigned long msec) {
116 Vector3 zoom_dir(0, 0, zoom_factor);
117 PRS prs = get_prs(msec);
119 zoom_dir.transform(prs.rotation.inverse());
120 translate(zoom_dir, msec);
123 void BaseCamera::pan(const Vector2 &dir, unsigned long msec) {
124 Vector3 i(1, 0, 0), j(0, 1, 0);
126 PRS prs = get_prs(msec);
128 i.transform(prs.rotation.inverse());
129 j.transform(prs.rotation.inverse());
131 translate(i * dir.x);
132 translate(j * dir.y);
135 void BaseCamera::roll(scalar_t angle, unsigned long msec) {
136 Vector3 dir(0, 0, 1);
137 dir.transform(get_prs(msec).rotation);
139 Quaternion q(dir, angle);
140 up = Vector3(0, 1, 0);
144 void BaseCamera::flip(bool x, bool y, bool z) {
150 const FrustumPlane *BaseCamera::get_frustum() const {
154 Matrix4x4 BaseCamera::get_projection_matrix() const {
156 return create_projection_matrix(fov, aspect, near_clip, far_clip);
158 return Matrix4x4::identity_matrix;
159 #endif // USING_3DENGFX
162 void BaseCamera::activate(unsigned long msec) const {
164 set_matrix(XFORM_VIEW, get_camera_matrix(msec));
166 Matrix4x4 proj = get_projection_matrix();
167 set_matrix(XFORM_PROJECTION, proj);
169 engfx_state::view_mat_camera = (const Camera*)this;
170 const_cast<BaseCamera*>(this)->setup_frustum(proj * engfx_state::view_matrix);
171 #endif // USING_3DENGFX