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