hx3d  1
2D/3D Simple Game Framework
camera.cpp
1 /*
2  Base camera class.
3  Inspired by LibGDX camera.
4 
5  Copyright (C) 2015 Denis BOURGE
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  USA
21 */
22 
23 #include "hx3d/graphics/cameras/camera.hpp"
24 
25 #include "hx3d/graphics/gl.hpp"
26 #include "hx3d/math/vector_utils.hpp"
27 
28 namespace hx3d {
29 namespace graphics {
30 
31 Camera::Camera(const float width, const float height, const float near, const float far):
32  position(0.f),
33  direction(0.f, 0.f, -1.f),
34  up(0.f, 1.f, 0.f),
35  near(near),
36  far(far),
37  viewportWidth(width),
38  viewportHeight(height)
39 {}
40 
41 void Camera::lookAt(glm::vec3 target) {
42  target -= position;
43  target = glm::normalize(target);
44 
45  if (target != glm::vec3(0)) {
46  float dot = glm::dot(target, up);
47  if (std::abs(dot - 1) < 0.00001f) {
48  up = direction;
49  up *= -1;
50  } else if (std::abs(dot + 1) < 0.00001f) {
51  up = direction;
52  }
53 
54  direction = target;
55 
56  // normalize up
57  glm::vec3 tmp(direction);
58  tmp = glm::cross(tmp, up);
59  tmp = glm::normalize(tmp);
60 
61  up = tmp;
62  up = glm::cross(up, direction);
63  up = glm::normalize(up);
64  }
65 }
66 
67 void Camera::translate(const glm::vec3 vec) {
68  position += vec;
69 }
70 
71 void Camera::rotate(const float angle, const glm::vec3 axis) {
72  direction = math::rotate(direction, glm::radians(angle), axis);
73  up = math::rotate(up, angle, axis);
74 }
75 
76 void Camera::rotateAround(const glm::vec3 center, const float angle, const glm::vec3 axis) {
77  glm::vec3 tmp(center);
78  tmp -= position;
79  translate(tmp);
80  rotate(angle, axis);
81 
82  tmp = math::rotate(tmp, glm::radians(angle), axis);
83  translate(-tmp);
84 }
85 
87 
88 } /* graphics */
89 } /* hx3d */
void rotate(const float angle, const glm::vec3 axis)
Rotate the camera on one/multiple axes.
Definition: camera.cpp:71
glm::vec3 up
Camera up vector.
Definition: camera.hpp:96
Camera(const float width, const float height, const float near, const float far)
Create a camera with a viewport width and height.
Definition: camera.cpp:31
void translate(const glm::vec3 vec)
Translate the camera.
Definition: camera.cpp:67
hx3d framework namespace
Definition: audio.hpp:26
glm::vec3 rotate(glm::vec3 vector, float angle, glm::vec3 axis)
Rotate a vector of an angle on one/multiple axis.
void lookAt(glm::vec3 target)
Look at target.
Definition: camera.cpp:41
glm::vec3 position
Camera position.
Definition: camera.hpp:92
void rotateAround(const glm::vec3 center, const float angle, const glm::vec3 axis)
Rotate the camera around one point, on one/multiple axes.
Definition: camera.cpp:76
glm::vec3 direction
Camera direction.
Definition: camera.hpp:94