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