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