hx3d  1
2D/3D Simple Game Framework
perspective_camera.cpp
1 /*
2  Perspective camera.
3  Inspired by LibGDX perspective 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/perspective_camera.hpp"
24 
25 #include "hx3d/core/core.hpp"
26 #include "hx3d/window/application.hpp"
27 
28 #include "hx3d/utils/log.hpp"
29 
30 namespace hx3d {
31 namespace graphics {
32 
33 PerspectiveCamera::PerspectiveCamera(const float near, const float far):
34  PerspectiveCamera(Core::App()->getWidth(), Core::App()->getHeight(), near, far)
35 {}
36 
37 PerspectiveCamera::PerspectiveCamera(const float width, const float height, const float near, const float far):
38  PerspectiveCamera(width, height, near, far, 70)
39 {}
40 
41 PerspectiveCamera::PerspectiveCamera(const float width, const float height, const float near, const float far, const float fov):
42  Camera(width, height, near, far), fieldOfView(fov)
43 {
44  update();
45 }
46 
48  float aspect = viewportWidth / viewportHeight;
49  projection = glm::perspective(fieldOfView, aspect, near, far);
50  //
51  // for (int j = 0; j < 4; ++j) {
52  // for (int i = 0; i < 4; ++i) {
53  // Log.Info("[%d][%d]: %f", i, j, projection[i][j]);
54  // }
55  // }
56 
57  view = glm::lookAt(position, position + direction, up);
58 }
59 
60 } /* graphics */
61 } /* hx3d */
float near
Near pane.
Definition: camera.hpp:104
float viewportHeight
Viewport height.
Definition: camera.hpp:111
glm::vec3 up
Camera up vector.
Definition: camera.hpp:96
2D/3D camera.
Definition: camera.hpp:41
glm::mat4 view
View matrix.
Definition: camera.hpp:101
glm::mat4 projection
Projection matrix.
Definition: camera.hpp:99
hx3d framework namespace
Definition: audio.hpp:26
float far
Far pane.
Definition: camera.hpp:106
float viewportWidth
Viewport width.
Definition: camera.hpp:109
PerspectiveCamera(const float near, const float far)
Create a perspective camera with the screen size and the near and far.
virtual void update() override
Update the camera.
glm::vec3 position
Camera position.
Definition: camera.hpp:92
Centralized framework management.
Definition: core.hpp:50
float fieldOfView
Field of view (FOV)
glm::vec3 direction
Camera direction.
Definition: camera.hpp:94