Luanti 5.15.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);
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 // Nodes
190 scene::ISceneNode *m_playernode = nullptr;
191 scene::ISceneNode *m_headnode = nullptr;
192 scene::ICameraSceneNode *m_cameranode = nullptr;
193
194 scene::ISceneManager *m_wieldmgr = nullptr;
196
197 // draw control
199
201
202 // Default Client FOV (as defined by the "fov" setting)
204
205 // Absolute camera position
207 // Absolute camera direction
209 // Camera offset
211
213
214 // Server-sent FOV variables
215 bool m_server_sent_fov = false;
217
218 // FOV transition variables
221
222 v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
227
228 // Field of view and aspect ratio stuff
229 f32 m_aspect = 1.0f;
230 f32 m_fov_x = 1.0f;
231 f32 m_fov_y = 1.0f;
232
233 // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
235 // If 0, view bobbing is off (e.g. player is standing).
236 // If 1, view bobbing is on (player is walking).
237 // If 2, view bobbing is getting switched off.
239 // Speed of view bobbing animation
241
242 // Digging animation frame (0 <= m_digging_anim < 1)
243 f32 m_digging_anim = 0.0f;
244 // If -1, no digging animation
245 // If 0, left-click digging animation
246 // If 1, right-click digging animation
248
249 // Animation when changing wielded item
252
254
257
258 std::vector<Nametag*> m_nametags;
260
261 // Last known light color of the player
262 video::SColor m_player_light_color;
263};
#define MYMAX(a, b)
Definition basic_macros.h:11
Definition camera.h:61
v3s16 getOffset() const
Definition camera.h:94
v2f m_wieldmesh_offset
Definition camera.h:222
bool m_show_nametag_backgrounds
Definition camera.h:259
auto getFrustumCuller() const
Definition camera.h:120
scene::ISceneManager * m_wieldmgr
Definition camera.h:194
std::array< core::plane3d< f32 >, 4 > getFrustumCullPlanes() const
Definition camera.cpp:739
s32 m_digging_button
Definition camera.h:247
ItemStack m_wield_item_next
Definition camera.h:251
void readSettings()
Definition camera.cpp:76
bool m_arm_inertia
Definition camera.h:256
f32 m_view_bobbing_speed
Definition camera.h:240
f32 m_target_fov_degrees
Definition camera.h:216
f32 m_curr_fov_degrees
Definition camera.h:216
MapDrawControl & m_draw_control
Definition camera.h:198
v3s16 m_camera_offset
Definition camera.h:210
v3f m_camera_direction
Definition camera.h:208
v2f m_last_cam_pos
Definition camera.h:226
scene::ISceneNode * m_playernode
Definition camera.h:190
CameraMode getCameraMode() const
Definition camera.h:171
f32 getFovX() const
Definition camera.h:100
std::vector< Nametag * > m_nametags
Definition camera.h:258
f32 getFovY() const
Definition camera.h:106
void toggleCameraMode()
Definition camera.cpp:636
s32 m_view_bobbing_state
Definition camera.h:238
void notifyFovChange()
Definition camera.cpp:102
void wield(const ItemStack &item)
Definition camera.cpp:597
CameraMode m_camera_mode
Definition camera.h:253
f32 m_fov_y
Definition camera.h:231
video::SColor m_player_light_color
Definition camera.h:262
Nametag * addNametag(const Nametag &params)
Definition camera.cpp:723
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:220
f32 m_cache_view_bobbing_amount
Definition camera.h:255
f32 m_view_bobbing_anim
Definition camera.h:234
void setDigging(s32 button)
Definition camera.cpp:591
void updateOffset()
Definition camera.cpp:300
bool m_stepheight_smooth_active
Definition camera.h:212
f32 m_digging_anim
Definition camera.h:243
void addArmInertia(f32 player_yaw)
Definition camera.cpp:228
f32 m_fov_x
Definition camera.h:230
v2f m_arm_dir
Definition camera.h:223
bool m_fov_transition_active
Definition camera.h:219
scene::ICameraSceneNode * m_cameranode
Definition camera.h:192
Client * m_client
Definition camera.h:200
v2f m_cam_vel_old
Definition camera.h:225
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:646
f32 m_wield_change_timer
Definition camera.h:250
void removeNametag(Nametag *nametag)
Definition camera.cpp:731
f32 m_cache_fov
Definition camera.h:203
void update(LocalPlayer *player, f32 frametime, f32 tool_reload_ratio)
Definition camera.cpp:314
void updateViewingRange()
Definition camera.cpp:577
~Camera()
Definition camera.cpp:91
scene::ICameraSceneNode * getCameraNode() const
Definition camera.h:71
WieldMeshSceneNode * m_wieldnode
Definition camera.h:195
f32 m_aspect
Definition camera.h:229
f32 m_fov_diff
Definition camera.h:220
v3f m_camera_position
Definition camera.h:206
v2f m_cam_vel
Definition camera.h:224
void drawWieldedTool(core::matrix4 *translation=NULL)
Definition camera.cpp:609
void setCameraMode(CameraMode mode)
Definition camera.h:165
bool m_server_sent_fov
Definition camera.h:215
scene::ISceneNode * m_headnode
Definition camera.h:191
static void settingChangedCallback(const std::string &name, void *data)
Definition camera.cpp:71
Definition client.h:106
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