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