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