]> git.lizzy.rs Git - minetest.git/blob - src/client/camera.h
1018af55a7baae93b99a4b7c290ec273d7617eda
[minetest.git] / src / client / 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 "client/tile.h"
25 #include <ICameraSceneNode.h>
26 #include <ISceneNode.h>
27 #include <plane3d.h>
28 #include <array>
29 #include <list>
30 #include "util/Optional.h"
31
32 class LocalPlayer;
33 struct MapDrawControl;
34 class Client;
35 class RenderingEngine;
36 class WieldMeshSceneNode;
37
38 struct Nametag
39 {
40         scene::ISceneNode *parent_node;
41         std::string text;
42         video::SColor textcolor;
43         Optional<video::SColor> bgcolor;
44         v3f pos;
45
46         Nametag(scene::ISceneNode *a_parent_node,
47                         const std::string &text,
48                         const video::SColor &textcolor,
49                         const Optional<video::SColor> &bgcolor,
50                         const v3f &pos):
51                 parent_node(a_parent_node),
52                 text(text),
53                 textcolor(textcolor),
54                 bgcolor(bgcolor),
55                 pos(pos)
56         {
57         }
58
59         video::SColor getBgColor(bool use_fallback) const
60         {
61                 if (bgcolor)
62                         return bgcolor.value();
63                 else if (!use_fallback)
64                         return video::SColor(0, 0, 0, 0);
65                 else if (textcolor.getLuminance() > 186)
66                         // Dark background for light text
67                         return video::SColor(50, 50, 50, 50);
68                 else
69                         // Light background for dark text
70                         return video::SColor(50, 255, 255, 255);
71         }
72 };
73
74 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
75
76 /*
77         Client camera class, manages the player and camera scene nodes, the viewing distance
78         and performs view bobbing etc. It also displays the wielded tool in front of the
79         first-person camera.
80 */
81 class Camera
82 {
83 public:
84         Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine);
85         ~Camera();
86
87         // Get camera scene node.
88         // It has the eye transformation, pitch and view bobbing applied.
89         inline scene::ICameraSceneNode* getCameraNode() const
90         {
91                 return m_cameranode;
92         }
93
94         // Get the camera position (in absolute scene coordinates).
95         // This has view bobbing applied.
96         inline v3f getPosition() const
97         {
98                 return m_camera_position;
99         }
100
101         // Returns the absolute position of the head SceneNode in the world
102         inline v3f getHeadPosition() const
103         {
104                 return m_headnode->getAbsolutePosition();
105         }
106
107         // Get the camera direction (in absolute camera coordinates).
108         // This has view bobbing applied.
109         inline v3f getDirection() const
110         {
111                 return m_camera_direction;
112         }
113
114         // Get the camera offset
115         inline v3s16 getOffset() const
116         {
117                 return m_camera_offset;
118         }
119
120         // Horizontal field of view
121         inline f32 getFovX() const
122         {
123                 return m_fov_x;
124         }
125
126         // Vertical field of view
127         inline f32 getFovY() const
128         {
129                 return m_fov_y;
130         }
131
132         // Get maximum of getFovX() and getFovY()
133         inline f32 getFovMax() const
134         {
135                 return MYMAX(m_fov_x, m_fov_y);
136         }
137
138         // Returns a lambda that when called with an object's position and bounding-sphere
139         // radius (both in BS space) returns true if, and only if the object should be
140         // frustum-culled.
141         auto getFrustumCuller() const
142         {
143                 return [planes = getFrustumCullPlanes(),
144                                 camera_offset = intToFloat(m_camera_offset, BS)
145                                 ](v3f position, f32 radius) {
146                         v3f pos_camspace = position - camera_offset;
147                         for (auto &plane : planes) {
148                                 if (plane.getDistanceTo(pos_camspace) > radius)
149                                         return true;
150                         }
151                         return false;
152                 };
153         }
154
155         // Notify about new server-sent FOV and initialize smooth FOV transition
156         void notifyFovChange();
157
158         // Step the camera: updates the viewing range and view bobbing.
159         void step(f32 dtime);
160
161         // Update the camera from the local player's position.
162         void update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio);
163
164         // Update render distance
165         void updateViewingRange();
166
167         // Start digging animation
168         // Pass 0 for left click, 1 for right click
169         void setDigging(s32 button);
170
171         // Replace the wielded item mesh
172         void wield(const ItemStack &item);
173
174         // Draw the wielded tool.
175         // This has to happen *after* the main scene is drawn.
176         // Warning: This clears the Z buffer.
177         void drawWieldedTool(irr::core::matrix4* translation=NULL);
178
179         // Toggle the current camera mode
180         void toggleCameraMode() {
181                 if (m_camera_mode == CAMERA_MODE_FIRST)
182                         m_camera_mode = CAMERA_MODE_THIRD;
183                 else if (m_camera_mode == CAMERA_MODE_THIRD)
184                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
185                 else
186                         m_camera_mode = CAMERA_MODE_FIRST;
187         }
188
189         // Set the current camera mode
190         inline void setCameraMode(CameraMode mode)
191         {
192                 m_camera_mode = mode;
193         }
194
195         //read the current camera mode
196         inline CameraMode getCameraMode()
197         {
198                 return m_camera_mode;
199         }
200
201         Nametag *addNametag(scene::ISceneNode *parent_node,
202                 const std::string &text, video::SColor textcolor,
203                 Optional<video::SColor> bgcolor, const v3f &pos);
204
205         void removeNametag(Nametag *nametag);
206
207         void drawNametags();
208
209         inline void addArmInertia(f32 player_yaw);
210
211 private:
212         // Use getFrustumCuller().
213         // This helper just exists to decrease the header's number of includes.
214         std::array<core::plane3d<f32>, 4> getFrustumCullPlanes() const;
215
216         // Nodes
217         scene::ISceneNode *m_playernode = nullptr;
218         scene::ISceneNode *m_headnode = nullptr;
219         scene::ICameraSceneNode *m_cameranode = nullptr;
220
221         scene::ISceneManager *m_wieldmgr = nullptr;
222         WieldMeshSceneNode *m_wieldnode = nullptr;
223
224         // draw control
225         MapDrawControl& m_draw_control;
226
227         Client *m_client;
228
229         // Default Client FOV (as defined by the "fov" setting)
230         f32 m_cache_fov;
231
232         // Absolute camera position
233         v3f m_camera_position;
234         // Absolute camera direction
235         v3f m_camera_direction;
236         // Camera offset
237         v3s16 m_camera_offset;
238
239         bool m_stepheight_smooth_active = false;
240
241         // Server-sent FOV variables
242         bool m_server_sent_fov = false;
243         f32 m_curr_fov_degrees, m_old_fov_degrees, m_target_fov_degrees;
244
245         // FOV transition variables
246         bool m_fov_transition_active = false;
247         f32 m_fov_diff, m_transition_time;
248
249         v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
250         v2f m_arm_dir;
251         v2f m_cam_vel;
252         v2f m_cam_vel_old;
253         v2f m_last_cam_pos;
254
255         // Field of view and aspect ratio stuff
256         f32 m_aspect = 1.0f;
257         f32 m_fov_x = 1.0f;
258         f32 m_fov_y = 1.0f;
259
260         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
261         f32 m_view_bobbing_anim = 0.0f;
262         // If 0, view bobbing is off (e.g. player is standing).
263         // If 1, view bobbing is on (player is walking).
264         // If 2, view bobbing is getting switched off.
265         s32 m_view_bobbing_state = 0;
266         // Speed of view bobbing animation
267         f32 m_view_bobbing_speed = 0.0f;
268         // Fall view bobbing
269         f32 m_view_bobbing_fall = 0.0f;
270
271         // Digging animation frame (0 <= m_digging_anim < 1)
272         f32 m_digging_anim = 0.0f;
273         // If -1, no digging animation
274         // If 0, left-click digging animation
275         // If 1, right-click digging animation
276         s32 m_digging_button = -1;
277
278         // Animation when changing wielded item
279         f32 m_wield_change_timer = 0.125f;
280         ItemStack m_wield_item_next;
281
282         CameraMode m_camera_mode = CAMERA_MODE_FIRST;
283
284         f32 m_cache_fall_bobbing_amount;
285         f32 m_cache_view_bobbing_amount;
286         bool m_arm_inertia;
287
288         std::list<Nametag *> m_nametags;
289         bool m_show_nametag_backgrounds;
290
291         // Last known light color of the player
292         video::SColor m_player_light_color;
293 };