]> git.lizzy.rs Git - minetest.git/blob - src/camera.h
Collected and moved existing camera infrastructure from game.cpp to camera.cpp and...
[minetest.git] / src / camera.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CAMERA_HEADER
21 #define CAMERA_HEADER
22
23 #include "common_irrlicht.h"
24 #include "utility.h"
25
26 class LocalPlayer;
27 class MapDrawControl;
28
29 /*
30         Client camera class, manages the player and camera scene nodes, the viewing distance
31         and performs view bobbing etc.
32 */
33 class Camera
34 {
35 public:
36         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
37         ~Camera();
38
39         // Get player scene node.
40         // This node is positioned at the player's torso (without any view bobbing),
41         // as given by Player::m_position, Player::m_pitch and Player::m_yaw.
42         // Things like wielded tools should be positioned relative to this node.
43         inline scene::ISceneNode* getPlayerNode() const
44         {
45                 return m_playernode;
46         }
47
48         // Get camera scene node.
49         // The camera node is a child of the player node.
50         // It has the eye transformation and view bobbing applied.
51         inline scene::ICameraSceneNode* getCameraNode() const
52         {
53                 return m_cameranode;
54         }
55
56         // Get the camera position (in absolute scene coordinates).
57         // This has view bobbing applied.
58         inline v3f getPosition() const
59         {
60                 return m_camera_position;
61         }
62
63         // Get the camera direction (in absolute camera coordinates).
64         // This has view bobbing applied.
65         inline v3f getDirection() const
66         {
67                 return m_camera_direction;
68         }
69
70         // Horizontal field of view
71         inline f32 getFovX() const
72         {
73                 return m_fov_x;
74         }
75
76         // Vertical field of view
77         inline f32 getFovY() const
78         {
79                 return m_fov_y;
80         }
81
82         // Get maximum of getFovX() and getFovY()
83         inline f32 getFovMax() const
84         {
85                 return MYMAX(m_fov_x, m_fov_y);
86         }
87
88         // Step the camera: updates the viewing range and view bobbing.
89         void step(f32 dtime);
90
91         // Update the camera from the local player's position.
92         // frametime is used to adjust the viewing range.
93         void update(LocalPlayer* player, f32 frametime, v2u32 screensize);
94
95         // Render distance feedback loop
96         void updateViewingRange(f32 frametime_in);
97
98         // Update settings from g_settings
99         void updateSettings();
100
101 private:
102         // Scene manager and nodes
103         scene::ISceneManager* m_smgr;
104         scene::ISceneNode* m_playernode;
105         scene::ICameraSceneNode* m_cameranode;
106
107         // draw control
108         MapDrawControl& m_draw_control;
109
110         // viewing_range_min_nodes setting
111         f32 m_viewing_range_min;
112         // viewing_range_max_nodes setting
113         f32 m_viewing_range_max;
114
115         // Absolute camera position
116         v3f m_camera_position;
117         // Absolute camera direction
118         v3f m_camera_direction;
119
120         // Field of view and aspect ratio stuff
121         f32 m_aspect;
122         f32 m_fov_x;
123         f32 m_fov_y;
124
125         // Stuff for viewing range calculations
126         f32 m_wanted_frametime;
127         f32 m_added_frametime;
128         s16 m_added_frames;
129         f32 m_range_old;
130         f32 m_frametime_old;
131         f32 m_frametime_counter;
132         f32 m_time_per_range;
133
134         // View bobbing animation frame (0 <= m_view_bobbing < 0x10000)
135         u32 m_view_bobbing_anim;
136         // Number of frames to continue the view bobbing animation.
137         u32 m_view_bobbing_anim_left;
138 };
139
140 #endif
141