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 // Get camera scene node.
74 // It has the eye transformation, pitch and view bobbing applied.
75 inline scene::ICameraSceneNode* getCameraNode() const
76 {
77 return m_cameranode;
78 }
79
80 // Get the camera position (in absolute scene coordinates).
81 // This has view bobbing applied.
82 inline v3f getPosition() const
83 {
84 return m_camera_position;
85 }
86
87 // Returns the absolute position of the head SceneNode in the world
88 inline v3f getHeadPosition() const
89 {
90 return m_headnode->getAbsolutePosition();
91 }
92
93 // Get the camera direction (in absolute camera coordinates).
94 // This has view bobbing applied.
95 inline v3f getDirection() const
96 {
97 return m_camera_direction;
98 }
99
100 // Get the camera offset
101 inline v3s16 getOffset() const
102 {
103 return m_camera_offset;
104 }
105
106 // Horizontal field of view
107 inline f32 getFovX() const
108 {
109 return m_fov_x;
110 }
111
112 // Vertical field of view
113 inline f32 getFovY() const
114 {
115 return m_fov_y;
116 }
117
118 // Get maximum of getFovX() and getFovY()
119 inline f32 getFovMax() const
120 {
121 return MYMAX(m_fov_x, m_fov_y);
122 }
123
124 // Returns a lambda that when called with an object's position and bounding-sphere
125 // radius (both in BS space) returns true if, and only if the object should be
126 // frustum-culled.
127 auto getFrustumCuller() const
128 {
129 return [planes = getFrustumCullPlanes(),
130 camera_offset = intToFloat(m_camera_offset, BS)
131 ](v3f position, f32 radius) {
132 v3f pos_camspace = position - camera_offset;
133 for (auto &plane : planes) {
134 if (plane.getDistanceTo(pos_camspace) > radius)
135 return true;
136 }
137 return false;
138 };
139 }
140
141 // Notify about new server-sent FOV and initialize smooth FOV transition
142 void notifyFovChange();
143
144 // Step the camera: updates the viewing range and view bobbing.
145 void step(f32 dtime);
146
147 // Update the camera from the local player's position.
148 void update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio);
149
150 // Update render distance
151 void updateViewingRange();
152
153 // Start digging animation
154 // Pass 0 for left click, 1 for right click
155 void setDigging(s32 button);
156
157 // Replace the wielded item mesh
158 void wield(const ItemStack &item);
159
160 // Draw the wielded tool.
161 // This has to happen *after* the main scene is drawn.
162 // Warning: This clears the Z buffer.
163 void drawWieldedTool(irr::core::matrix4* translation=NULL);
164
165 // Toggle the current camera mode
174
175 // Set the current camera mode
176 inline void setCameraMode(CameraMode mode)
177 {
178 m_camera_mode = mode;
179 }
180
181 //read the current camera mode
183 {
184 return m_camera_mode;
185 }
186
187 Nametag *addNametag(scene::ISceneNode *parent_node,
188 const std::string &text, video::SColor textcolor,
189 std::optional<video::SColor> bgcolor, const v3f &pos);
190
191 void removeNametag(Nametag *nametag);
192
193 void drawNametags();
194
195 inline void addArmInertia(f32 player_yaw);
196
197private:
198 // Use getFrustumCuller().
199 // This helper just exists to decrease the header's number of includes.
200 std::array<core::plane3d<f32>, 4> getFrustumCullPlanes() const;
201
202 // Nodes
203 scene::ISceneNode *m_playernode = nullptr;
204 scene::ISceneNode *m_headnode = nullptr;
205 scene::ICameraSceneNode *m_cameranode = nullptr;
206
207 scene::ISceneManager *m_wieldmgr = nullptr;
209
210 // draw control
212
214
215 // Default Client FOV (as defined by the "fov" setting)
217
218 // Absolute camera position
220 // Absolute camera direction
222 // Camera offset
224
226
227 // Server-sent FOV variables
228 bool m_server_sent_fov = false;
230
231 // FOV transition variables
234
235 v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
240
241 // Field of view and aspect ratio stuff
242 f32 m_aspect = 1.0f;
243 f32 m_fov_x = 1.0f;
244 f32 m_fov_y = 1.0f;
245
246 // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
248 // If 0, view bobbing is off (e.g. player is standing).
249 // If 1, view bobbing is on (player is walking).
250 // If 2, view bobbing is getting switched off.
252 // Speed of view bobbing animation
254 // Fall view bobbing
256
257 // Digging animation frame (0 <= m_digging_anim < 1)
258 f32 m_digging_anim = 0.0f;
259 // If -1, no digging animation
260 // If 0, left-click digging animation
261 // If 1, right-click digging animation
263
264 // Animation when changing wielded item
267
269
273
274 std::list<Nametag *> m_nametags;
276
277 // Last known light color of the player
278 video::SColor m_player_light_color;
279};
#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:101
v2f m_wieldmesh_offset
Definition camera.h:235
bool m_show_nametag_backgrounds
Definition camera.h:275
auto getFrustumCuller() const
Definition camera.h:127
scene::ISceneManager * m_wieldmgr
Definition camera.h:207
std::array< core::plane3d< f32 >, 4 > getFrustumCullPlanes() const
Definition camera.cpp:687
s32 m_digging_button
Definition camera.h:262
f32 m_cache_fall_bobbing_amount
Definition camera.h:270
ItemStack m_wield_item_next
Definition camera.h:266
bool m_arm_inertia
Definition camera.h:272
f32 m_view_bobbing_speed
Definition camera.h:253
f32 m_target_fov_degrees
Definition camera.h:229
f32 m_curr_fov_degrees
Definition camera.h:229
MapDrawControl & m_draw_control
Definition camera.h:211
v3s16 m_camera_offset
Definition camera.h:223
v3f m_camera_direction
Definition camera.h:221
v2f m_last_cam_pos
Definition camera.h:239
scene::ISceneNode * m_playernode
Definition camera.h:203
Nametag * addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, std::optional< video::SColor > bgcolor, const v3f &pos)
Definition camera.cpp:672
f32 getFovX() const
Definition camera.h:107
f32 getFovY() const
Definition camera.h:113
void toggleCameraMode()
Definition camera.h:166
s32 m_view_bobbing_state
Definition camera.h:251
void notifyFovChange()
Definition camera.cpp:80
CameraMode getCameraMode()
Definition camera.h:182
void wield(const ItemStack &item)
Definition camera.cpp:591
CameraMode m_camera_mode
Definition camera.h:268
f32 m_fov_y
Definition camera.h:244
video::SColor m_player_light_color
Definition camera.h:278
void drawWieldedTool(irr::core::matrix4 *translation=NULL)
Definition camera.cpp:603
void step(f32 dtime)
Definition camera.cpp:115
v3f getDirection() const
Definition camera.h:95
f32 getFovMax() const
Definition camera.h:119
v3f getPosition() const
Definition camera.h:82
f32 m_transition_time
Definition camera.h:233
f32 m_view_bobbing_fall
Definition camera.h:255
f32 m_cache_view_bobbing_amount
Definition camera.h:271
f32 m_view_bobbing_anim
Definition camera.h:247
void setDigging(s32 button)
Definition camera.cpp:585
bool m_stepheight_smooth_active
Definition camera.h:225
f32 m_digging_anim
Definition camera.h:258
void addArmInertia(f32 player_yaw)
Definition camera.cpp:212
f32 m_fov_x
Definition camera.h:243
v2f m_arm_dir
Definition camera.h:236
bool m_fov_transition_active
Definition camera.h:232
scene::ICameraSceneNode * m_cameranode
Definition camera.h:205
Client * m_client
Definition camera.h:213
v2f m_cam_vel_old
Definition camera.h:238
v3f getHeadPosition() const
Definition camera.h:88
Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine)
Definition camera.cpp:35
void drawNametags()
Definition camera.cpp:630
f32 m_wield_change_timer
Definition camera.h:265
void removeNametag(Nametag *nametag)
Definition camera.cpp:681
f32 m_cache_fov
Definition camera.h:216
void update(LocalPlayer *player, f32 frametime, f32 tool_reload_ratio)
Definition camera.cpp:284
void updateViewingRange()
Definition camera.cpp:571
~Camera()
Definition camera.cpp:75
scene::ICameraSceneNode * getCameraNode() const
Definition camera.h:75
WieldMeshSceneNode * m_wieldnode
Definition camera.h:208
f32 m_aspect
Definition camera.h:242
f32 m_fov_diff
Definition camera.h:233
v3f m_camera_position
Definition camera.h:219
v2f m_cam_vel
Definition camera.h:237
void setCameraMode(CameraMode mode)
Definition camera.h:176
bool m_server_sent_fov
Definition camera.h:228
scene::ISceneNode * m_headnode
Definition camera.h:204
std::list< Nametag * > m_nametags
Definition camera.h:274
Definition client.h:105
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