]> git.lizzy.rs Git - minetest-m13.git/blob - src/camera.h
bb7bffe39fdd24b479e8887b96d76bd9f15a4ff1
[minetest-m13.git] / src / camera.h
1 /*
2 Minetest-m13
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 "inventory.h"
25 #include "mesh.h"
26 #include "tile.h"
27 #include "utility.h"
28 #include <ICameraSceneNode.h>
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class IGameDef;
33
34 /*
35         Client camera class, manages the player and camera scene nodes, the viewing distance
36         and performs view bobbing etc. It also displays the wielded tool in front of the
37         first-person camera.
38 */
39 class Camera
40 {
41 public:
42         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
43         ~Camera();
44
45         // Get player scene node.
46         // This node is positioned at the player's torso (without any view bobbing),
47         // as given by Player::m_position. Yaw is applied but not pitch.
48         inline scene::ISceneNode* getPlayerNode() const
49         {
50                 return m_playernode;
51         }
52
53         // Get head scene node.
54         // It has the eye transformation and pitch applied,
55         // but no view bobbing.
56         inline scene::ISceneNode* getHeadNode() const
57         {
58                 return m_headnode;
59         }
60
61         // Get camera scene node.
62         // It has the eye transformation, pitch and view bobbing applied.
63         inline scene::ICameraSceneNode* getCameraNode() const
64         {
65                 return m_cameranode;
66         }
67
68         // Get the camera position (in absolute scene coordinates).
69         // This has view bobbing applied.
70         inline v3f getPosition() const
71         {
72                 return m_camera_position;
73         }
74
75         // Get the camera direction (in absolute camera coordinates).
76         // This has view bobbing applied.
77         inline v3f getDirection() const
78         {
79                 return m_camera_direction;
80         }
81
82         // Horizontal field of view
83         inline f32 getFovX() const
84         {
85                 return m_fov_x;
86         }
87
88         // Vertical field of view
89         inline f32 getFovY() const
90         {
91                 return m_fov_y;
92         }
93
94         // Get maximum of getFovX() and getFovY()
95         inline f32 getFovMax() const
96         {
97                 return MYMAX(m_fov_x, m_fov_y);
98         }
99
100         // Checks if the constructor was able to create the scene nodes
101         bool successfullyCreated(std::wstring& error_message);
102
103         // Step the camera: updates the viewing range and view bobbing.
104         void step(f32 dtime);
105
106         // Update the camera from the local player's position.
107         // frametime is used to adjust the viewing range.
108         void update(LocalPlayer* player, f32 frametime, v2u32 screensize);
109
110         // Render distance feedback loop
111         void updateViewingRange(f32 frametime_in);
112
113         // Update settings from g_settings
114         void updateSettings();
115
116         // Start digging animation
117         // Pass 0 for left click, 1 for right click
118         void setDigging(s32 button);
119
120         // Replace the wielded item mesh
121         void wield(const ItemStack &item, IGameDef *gamedef);
122
123         // Draw the wielded tool.
124         // This has to happen *after* the main scene is drawn.
125         // Warning: This clears the Z buffer.
126         void drawWieldedTool();
127
128 private:
129         // Scene manager and nodes
130         scene::ISceneManager* m_smgr;
131         scene::ISceneNode* m_playernode;
132         scene::ISceneNode* m_headnode;
133         scene::ICameraSceneNode* m_cameranode;
134
135         scene::ISceneManager* m_wieldmgr;
136         scene::IMeshSceneNode* m_wieldnode;
137         u8 m_wieldlight;
138
139         // draw control
140         MapDrawControl& m_draw_control;
141
142         // viewing_range_min_nodes setting
143         f32 m_viewing_range_min;
144         // viewing_range_max_nodes setting
145         f32 m_viewing_range_max;
146
147         // Absolute camera position
148         v3f m_camera_position;
149         // Absolute camera direction
150         v3f m_camera_direction;
151
152         // Field of view and aspect ratio stuff
153         f32 m_aspect;
154         f32 m_fov_x;
155         f32 m_fov_y;
156
157         // Stuff for viewing range calculations
158         f32 m_wanted_frametime;
159         f32 m_added_frametime;
160         s16 m_added_frames;
161         f32 m_range_old;
162         f32 m_frametime_old;
163         f32 m_frametime_counter;
164         f32 m_time_per_range;
165
166         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
167         f32 m_view_bobbing_anim;
168         // If 0, view bobbing is off (e.g. player is standing).
169         // If 1, view bobbing is on (player is walking).
170         // If 2, view bobbing is getting switched off.
171         s32 m_view_bobbing_state;
172         // Speed of view bobbing animation
173         f32 m_view_bobbing_speed;
174
175         // Digging animation frame (0 <= m_digging_anim < 1)
176         f32 m_digging_anim;
177         // If -1, no digging animation
178         // If 0, left-click digging animation
179         // If 1, right-click digging animation
180         s32 m_digging_button;
181 };
182
183 #endif