]> git.lizzy.rs Git - minetest.git/blob - src/camera.h
This looks more like MC view bobbing, but still not even close
[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. Yaw is applied but not pitch.
42         inline scene::ISceneNode* getPlayerNode() const
43         {
44                 return m_playernode;
45         }
46
47         // Get head scene node.
48         // It has the eye transformation and pitch applied,
49         // but no view bobbing.
50         inline scene::ISceneNode* getHeadNode() const
51         {
52                 return m_headnode;
53         }
54
55         // Get camera scene node.
56         // It has the eye transformation, pitch and view bobbing applied.
57         inline scene::ICameraSceneNode* getCameraNode() const
58         {
59                 return m_cameranode;
60         }
61
62         // Get the camera position (in absolute scene coordinates).
63         // This has view bobbing applied.
64         inline v3f getPosition() const
65         {
66                 return m_camera_position;
67         }
68
69         // Get the camera direction (in absolute camera coordinates).
70         // This has view bobbing applied.
71         inline v3f getDirection() const
72         {
73                 return m_camera_direction;
74         }
75
76         // Horizontal field of view
77         inline f32 getFovX() const
78         {
79                 return m_fov_x;
80         }
81
82         // Vertical field of view
83         inline f32 getFovY() const
84         {
85                 return m_fov_y;
86         }
87
88         // Get maximum of getFovX() and getFovY()
89         inline f32 getFovMax() const
90         {
91                 return MYMAX(m_fov_x, m_fov_y);
92         }
93
94         // Checks if the constructor was able to create the scene nodes
95         bool successfullyCreated(std::wstring& error_message);
96
97         // Step the camera: updates the viewing range and view bobbing.
98         void step(f32 dtime);
99
100         // Update the camera from the local player's position.
101         // frametime is used to adjust the viewing range.
102         void update(LocalPlayer* player, f32 frametime, v2u32 screensize);
103
104         // Render distance feedback loop
105         void updateViewingRange(f32 frametime_in);
106
107         // Update settings from g_settings
108         void updateSettings();
109
110 private:
111         // Scene manager and nodes
112         scene::ISceneManager* m_smgr;
113         scene::ISceneNode* m_playernode;
114         scene::ISceneNode* m_headnode;
115         scene::ICameraSceneNode* m_cameranode;
116
117         // draw control
118         MapDrawControl& m_draw_control;
119
120         // viewing_range_min_nodes setting
121         f32 m_viewing_range_min;
122         // viewing_range_max_nodes setting
123         f32 m_viewing_range_max;
124
125         // Absolute camera position
126         v3f m_camera_position;
127         // Absolute camera direction
128         v3f m_camera_direction;
129
130         // Field of view and aspect ratio stuff
131         f32 m_aspect;
132         f32 m_fov_x;
133         f32 m_fov_y;
134
135         // Stuff for viewing range calculations
136         f32 m_wanted_frametime;
137         f32 m_added_frametime;
138         s16 m_added_frames;
139         f32 m_range_old;
140         f32 m_frametime_old;
141         f32 m_frametime_counter;
142         f32 m_time_per_range;
143
144         // View bobbing animation frame (0 <= m_view_bobbing < 0x1000000)
145         s32 m_view_bobbing_anim;
146         // If 0, view bobbing is off (e.g. player is standing).
147         // If 1, view bobbing is on (player is walking).
148         // If 2, view bobbing is getting switched off.
149         s32 m_view_bobbing_state;
150         // Speed of view bobbing animation
151         f32 m_view_bobbing_speed;
152 };
153
154 #endif
155