C++ modernize: Pragma once (#6264)
[oweals/minetest.git] / src / camera.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "inventory.h"
24 #include "mesh.h"
25 #include "client/tile.h"
26 #include <ICameraSceneNode.h>
27 #include <ISceneNode.h>
28 #include <list>
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class Client;
33 class WieldMeshSceneNode;
34
35 struct Nametag {
36         Nametag(scene::ISceneNode *a_parent_node,
37                         const std::string &a_nametag_text,
38                         const video::SColor &a_nametag_color,
39                         const v3f a_nametag_pos):
40                 parent_node(a_parent_node),
41                 nametag_text(a_nametag_text),
42                 nametag_color(a_nametag_color),
43                 nametag_pos(a_nametag_pos)
44         {
45         }
46         scene::ISceneNode *parent_node;
47         std::string nametag_text;
48         video::SColor nametag_color;
49         v3f nametag_pos;
50 };
51
52 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
53
54 /*
55         Client camera class, manages the player and camera scene nodes, the viewing distance
56         and performs view bobbing etc. It also displays the wielded tool in front of the
57         first-person camera.
58 */
59 class Camera
60 {
61 public:
62         Camera(MapDrawControl &draw_control, Client *client);
63         ~Camera();
64
65         // Get camera scene node.
66         // It has the eye transformation, pitch and view bobbing applied.
67         inline scene::ICameraSceneNode* getCameraNode() const
68         {
69                 return m_cameranode;
70         }
71
72         // Get the camera position (in absolute scene coordinates).
73         // This has view bobbing applied.
74         inline v3f getPosition() const
75         {
76                 return m_camera_position;
77         }
78
79         // Get the camera direction (in absolute camera coordinates).
80         // This has view bobbing applied.
81         inline v3f getDirection() const
82         {
83                 return m_camera_direction;
84         }
85
86         // Get the camera offset
87         inline v3s16 getOffset() const
88         {
89                 return m_camera_offset;
90         }
91
92         // Horizontal field of view
93         inline f32 getFovX() const
94         {
95                 return m_fov_x;
96         }
97
98         // Vertical field of view
99         inline f32 getFovY() const
100         {
101                 return m_fov_y;
102         }
103
104         // Get maximum of getFovX() and getFovY()
105         inline f32 getFovMax() const
106         {
107                 return MYMAX(m_fov_x, m_fov_y);
108         }
109
110         // Checks if the constructor was able to create the scene nodes
111         bool successfullyCreated(std::string &error_message);
112
113         // Step the camera: updates the viewing range and view bobbing.
114         void step(f32 dtime);
115
116         // Update the camera from the local player's position.
117         // busytime is used to adjust the viewing range.
118         void update(LocalPlayer* player, f32 frametime, f32 busytime,
119                         f32 tool_reload_ratio);
120
121         // Update render distance
122         void updateViewingRange();
123
124         // Start digging animation
125         // Pass 0 for left click, 1 for right click
126         void setDigging(s32 button);
127
128         // Replace the wielded item mesh
129         void wield(const ItemStack &item);
130
131         // Draw the wielded tool.
132         // This has to happen *after* the main scene is drawn.
133         // Warning: This clears the Z buffer.
134         void drawWieldedTool(irr::core::matrix4* translation=NULL);
135
136         // Toggle the current camera mode
137         void toggleCameraMode() {
138                 if (m_camera_mode == CAMERA_MODE_FIRST)
139                         m_camera_mode = CAMERA_MODE_THIRD;
140                 else if (m_camera_mode == CAMERA_MODE_THIRD)
141                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
142                 else
143                         m_camera_mode = CAMERA_MODE_FIRST;
144         }
145
146         // Set the current camera mode
147         inline void setCameraMode(CameraMode mode)
148         {
149                 m_camera_mode = mode;
150         }
151
152         //read the current camera mode
153         inline CameraMode getCameraMode()
154         {
155                 return m_camera_mode;
156         }
157
158         Nametag *addNametag(scene::ISceneNode *parent_node,
159                 const std::string &nametag_text, video::SColor nametag_color,
160                 const v3f &pos);
161
162         void removeNametag(Nametag *nametag);
163
164         const std::list<Nametag *> &getNametags() { return m_nametags; }
165
166         void drawNametags();
167
168         inline void addArmInertia(f32 player_yaw);
169
170 private:
171         // Nodes
172         scene::ISceneNode *m_playernode = nullptr;
173         scene::ISceneNode *m_headnode = nullptr;
174         scene::ICameraSceneNode *m_cameranode = nullptr;
175
176         scene::ISceneManager *m_wieldmgr = nullptr;
177         WieldMeshSceneNode *m_wieldnode = nullptr;
178
179         // draw control
180         MapDrawControl& m_draw_control;
181
182         Client *m_client;
183
184         // Absolute camera position
185         v3f m_camera_position;
186         // Absolute camera direction
187         v3f m_camera_direction;
188         // Camera offset
189         v3s16 m_camera_offset;
190
191         v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
192         v2f m_arm_dir;
193         v2f m_cam_vel;
194         v2f m_cam_vel_old;
195         v2f m_last_cam_pos;
196
197         // Field of view and aspect ratio stuff
198         f32 m_aspect = 1.0f;
199         f32 m_fov_x = 1.0f;
200         f32 m_fov_y = 1.0f;
201
202         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
203         f32 m_view_bobbing_anim = 0.0f;
204         // If 0, view bobbing is off (e.g. player is standing).
205         // If 1, view bobbing is on (player is walking).
206         // If 2, view bobbing is getting switched off.
207         s32 m_view_bobbing_state = 0;
208         // Speed of view bobbing animation
209         f32 m_view_bobbing_speed = 0.0f;
210         // Fall view bobbing
211         f32 m_view_bobbing_fall = 0.0f;
212
213         // Digging animation frame (0 <= m_digging_anim < 1)
214         f32 m_digging_anim = 0.0f;
215         // If -1, no digging animation
216         // If 0, left-click digging animation
217         // If 1, right-click digging animation
218         s32 m_digging_button = -1;
219
220         // Animation when changing wielded item
221         f32 m_wield_change_timer = 0.125f;
222         ItemStack m_wield_item_next;
223
224         CameraMode m_camera_mode = CAMERA_MODE_FIRST;
225
226         f32 m_cache_fall_bobbing_amount;
227         f32 m_cache_view_bobbing_amount;
228         f32 m_cache_fov;
229         f32 m_cache_zoom_fov;
230         bool m_arm_inertia;
231
232         std::list<Nametag *> m_nametags;
233 };