Bubba-3D  0.9.0
Awesome game engine!
PerspectiveCamera.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include "PerspectiveCamera.h"
18 #include "linmath/float3x3.h"
19 
20 PerspectiveCamera::PerspectiveCamera(chag::float3 position, chag::float3 lookAt, chag::float3 up,
21  float fov, float ratio, float nearPlane, float farPlane)
22  : Camera(position, lookAt, up, fov, ratio, nearPlane, farPlane)
23 {
24 
25 }
26 
27 PerspectiveCamera::~PerspectiveCamera() {
28 }
29 
30 chag::float4x4 PerspectiveCamera::getViewMatrix() {
31  return lookAt(m_vPosition, m_vLookAt, m_vUp);
32 }
33 
34 void PerspectiveCamera::update(float dt) { }
35 
36 chag::float4x4 PerspectiveCamera::lookAt(const chag::float3 &eye,
37  const chag::float3 &center,
38  const chag::float3 &up)
39 {
40  chag::float3 dir = chag::normalize(eye - center);
41  chag::float3 right = chag::normalize(chag::cross(up, chag::normalize(dir)));
42  chag::float3 newup = chag::normalize(chag::cross(dir, right));
43 
44  chag::float3x3 R = chag::make_matrix(right, newup, dir);
45  chag::float4x4 invrot = chag::make_matrix(chag::transpose(R), chag::make_vector(0.0f,0.0f,0.0f));
46 
47  return invrot * chag::make_translation(-eye);
48 }
49 
50 chag::float4x4 PerspectiveCamera::getProjectionMatrix() {
51  return perspectiveMatrix(m_fFov, m_fRatio, m_fNearPlane, m_fFarPlane);
52 }
53 
54 chag::float4x4 PerspectiveCamera::perspectiveMatrix(float fov, float aspectRatio, float n, float f) {
55  return chag::make_perspective(fov, aspectRatio, n, f);
56 }
57 
58 void PerspectiveCamera::setPosition(chag::float3 position) {
59  this->m_vPosition = position;
60 }
61 
62 void PerspectiveCamera::setLookAt(chag::float3 lookAt){
63  this->m_vLookAt = lookAt;
64 }
65 
66 void PerspectiveCamera::setUpVector(chag::float3 up){
67  this->m_vUp = up;
68 }
Definition: Camera.h:26