3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
25 #include "clientmap.h" // MapDrawControl
28 #include "client/renderingengine.h"
29 #include "client/content_cao.h"
31 #include "wieldmesh.h"
32 #include "noise.h" // easeCurve
36 #include "util/numeric.h"
37 #include "constants.h"
38 #include "fontengine.h"
39 #include "script/scripting_client.h"
41 #include <SViewFrustum.h>
43 #define CAMERA_OFFSET_STEP 200
44 #define WIELDMESH_OFFSET_X 55.0f
45 #define WIELDMESH_OFFSET_Y -35.0f
46 #define WIELDMESH_AMPLITUDE_X 7.0f
47 #define WIELDMESH_AMPLITUDE_Y 10.0f
49 // Returns the fractional part of x
50 inline f32 my_modf(f32 x)
53 return modff(x, &dummy);
56 WieldNode::WieldNode(HandIndex index, Client *client, scene::ISceneManager *mgr) :
58 m_direction(index == MAINHAND ? +1 : -1),
60 m_meshnode(new WieldMeshSceneNode(mgr, -1, false)),
61 m_player_light_color(0xFFFFFFFF)
63 m_meshnode->setItem(ItemStack(), m_client);
64 m_meshnode->drop(); // mgr grabbed it
67 void WieldNode::step(f32 dtime)
69 bool was_under_zero = m_change_timer < 0;
70 m_change_timer = MYMIN(m_change_timer + dtime, 0.125);
72 if (m_change_timer >= 0 && was_under_zero) {
73 m_meshnode->setItem(m_item_next, m_client);
74 m_meshnode->setNodeLightColor(m_player_light_color);
77 if (m_digging_button == -1)
80 f32 offset = dtime * 3.5f;
81 float m_digging_anim_was = m_digging_anim;
82 m_digging_anim += offset;
83 if (m_digging_anim >= 1)
86 m_digging_button = -1;
89 if(m_digging_anim_was < lim && m_digging_anim >= lim)
91 if (m_digging_button == 0) {
92 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
93 } else if(m_digging_button == 1) {
94 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
99 static inline v2f dir(const v2f &pos_dist)
101 f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
102 f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
104 f32 x_abs = std::fabs(x);
105 f32 y_abs = std::fabs(y);
107 if (x_abs >= y_abs) {
112 if (y_abs >= x_abs) {
117 return v2f(std::fabs(x), std::fabs(y));
120 void WieldNode::addArmInertia(f32 player_yaw, v3f camera_direction)
122 m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
123 -100.0f, 100.0f) / 0.016f) * 0.01f;
124 m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - camera_direction.Y) / 0.016f);
125 f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_offset.X);
126 f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_offset.Y);
128 if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
130 The arm moves relative to the camera speed,
131 with an acceleration factor.
134 if (m_cam_vel.X > 1.0f) {
135 if (m_cam_vel.X > m_cam_vel_old.X)
136 m_cam_vel_old.X = m_cam_vel.X;
138 f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
139 m_offset.X += (m_last_cam_pos.X < player_yaw ? acc_X : -acc_X) * m_direction;
141 if (m_last_cam_pos.X != player_yaw)
142 m_last_cam_pos.X = player_yaw;
144 m_offset.X = rangelim(m_offset.X,
145 WIELDMESH_OFFSET_X - (WIELDMESH_AMPLITUDE_X * 0.5f),
146 WIELDMESH_OFFSET_X + (WIELDMESH_AMPLITUDE_X * 0.5f));
149 if (m_cam_vel.Y > 1.0f) {
150 if (m_cam_vel.Y > m_cam_vel_old.Y)
151 m_cam_vel_old.Y = m_cam_vel.Y;
153 f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
155 m_last_cam_pos.Y > camera_direction.Y ? acc_Y : -acc_Y;
157 if (m_last_cam_pos.Y != camera_direction.Y)
158 m_last_cam_pos.Y = camera_direction.Y;
160 m_offset.Y = rangelim(m_offset.Y,
161 WIELDMESH_OFFSET_Y - (WIELDMESH_AMPLITUDE_Y * 0.5f),
162 WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
165 m_arm_dir = dir(m_offset);
168 Now the arm gets back to its default position when the camera stops,
169 following a vector, with a smooth deceleration factor.
172 f32 dec_X = 0.35f * (std::min(15.0f, m_cam_vel_old.X) * (1.0f +
173 (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
175 f32 dec_Y = 0.25f * (std::min(15.0f, m_cam_vel_old.Y) * (1.0f +
176 (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
179 m_cam_vel_old.X = 0.0f;
182 m_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
185 m_cam_vel_old.Y = 0.0f;
188 m_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
192 void WieldNode::update(video::SColor player_light_color, f32 view_bobbing_anim, f32 tool_reload_ratio)
194 m_player_light_color = player_light_color;
196 // Position the wielded item
197 //v3f pos = v3f(45, -35, 65);
198 v3f pos = v3f(m_offset.X, m_offset.Y, 65);
199 //v3f rot = v3f(-100, 120, -100);
200 v3f rot = v3f(-100, 120, -100);
202 if (m_index == OFFHAND)
203 tool_reload_ratio = 1.0f;
205 pos.Y += fabs(m_change_timer)*320 - 40;
206 if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
209 if(m_digging_anim > 0.5)
210 frac = 2.0 * (m_digging_anim - 0.5);
211 // This value starts from 1 and settles to 0
212 f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
213 //f32 ratiothing2 = pow(ratiothing, 0.5f);
214 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
215 pos.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
216 //rot.Z += frac * 5.0 * ratiothing2;
217 pos.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
218 rot.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
219 //rot.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
220 //rot.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
222 if (m_digging_button != -1)
224 f32 digfrac = m_digging_anim;
225 pos.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
226 pos.Y += 24 * sin(digfrac * 1.8 * M_PI);
229 // Euler angles are PURE EVIL, so why not use quaternions?
230 core::quaternion quat_begin(rot * core::DEGTORAD);
231 //core::quaternion quat_end(v3f(s * 80, 30, s * 100) * core::DEGTORAD);
232 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
233 core::quaternion quat_slerp;
234 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
235 quat_slerp.W *= m_direction;
236 quat_slerp.X *= m_direction;
237 quat_slerp.toEuler(rot);
238 rot *= core::RADTODEG;
239 pos.X *= m_direction;
241 f32 bobfrac = my_modf(view_bobbing_anim);
242 pos.X *= m_direction;
243 pos.X -= sin(bobfrac*M_PI*2.0+M_PI*m_index) * 3.0 * m_direction;
244 pos.Y += sin(my_modf(bobfrac*2.0)*M_PI+M_PI*m_index) * 3.0;
247 m_meshnode->setPosition(pos);
248 m_meshnode->setRotation(rot);
250 m_meshnode->setNodeLightColor(m_player_light_color);
252 if (m_index == OFFHAND) {
253 m_meshnode->setVisible(
254 m_change_timer > 0 ? !m_item_next.name.empty() : m_item_old);
259 void WieldNode::setDigging(s32 button)
261 if (m_digging_button == -1)
262 m_digging_button = button;
265 void WieldNode::wield(const ItemStack &item)
267 if (item.name == m_item_next.name &&
268 item.metadata == m_item_next.metadata)
271 m_item_old = m_item_next.name != "";
273 if (m_change_timer > 0)
274 m_change_timer = -m_change_timer;
275 else if (m_change_timer == 0)
276 m_change_timer = -0.001;
279 Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine):
280 m_draw_control(draw_control),
282 m_player_light_color(0xFFFFFFFF)
284 auto smgr = rendering_engine->get_scene_manager();
285 // note: making the camera node a child of the player node
286 // would lead to unexpected behavior, so we don't do that.
287 m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
288 m_headnode = smgr->addEmptySceneNode(m_playernode);
289 m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
290 m_cameranode->bindTargetAndRotation(true);
292 // This needs to be in its own scene manager. It is drawn after
293 // all other 3D scene nodes and before the GUI.
294 m_wieldmgr = smgr->createNewSceneManager();
295 m_wieldmgr->addCameraSceneNode();
297 m_wieldnodes[MAINHAND] = new WieldNode(MAINHAND, m_client, m_wieldmgr);
298 m_wieldnodes[ OFFHAND] = new WieldNode( OFFHAND, m_client, m_wieldmgr);
300 /* TODO: Add a callback function so these can be updated when a setting
301 * changes. At this point in time it doesn't matter (e.g. /set
302 * is documented to change server settings only)
304 * TODO: Local caching of settings is not optimal and should at some stage
305 * be updated to use a global settings object for getting thse values
306 * (as opposed to the this local caching). This can be addressed in
309 m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount", 0.0f, 100.0f);
310 m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount", 0.0f, 7.9f);
311 // 45 degrees is the lowest FOV that doesn't cause the server to treat this
312 // as a zoom FOV and load world beyond the set server limits.
313 m_cache_fov = g_settings->getFloat("fov", 45.0f, 160.0f);
314 m_arm_inertia = g_settings->getBool("arm_inertia");
316 m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds");
321 for (auto node : m_wieldnodes)
326 void Camera::notifyFovChange()
328 LocalPlayer *player = m_client->getEnv().getLocalPlayer();
331 PlayerFovSpec spec = player->getFov();
334 * Update m_old_fov_degrees first - it serves as the starting point of the
335 * upcoming transition.
337 * If an FOV transition is already active, mark current FOV as the start of
338 * the new transition. If not, set it to the previous transition's target FOV.
340 if (m_fov_transition_active)
341 m_old_fov_degrees = m_curr_fov_degrees;
343 m_old_fov_degrees = m_server_sent_fov ? m_target_fov_degrees : m_cache_fov;
346 * Update m_server_sent_fov next - it corresponds to the target FOV of the
347 * upcoming transition.
349 * Set it to m_cache_fov, if server-sent FOV is 0. Otherwise check if
350 * server-sent FOV is a multiplier, and multiply it with m_cache_fov instead
353 if (spec.fov == 0.0f) {
354 m_server_sent_fov = false;
355 m_target_fov_degrees = m_cache_fov;
357 m_server_sent_fov = true;
358 m_target_fov_degrees = spec.is_multiplier ? m_cache_fov * spec.fov : spec.fov;
361 if (spec.transition_time > 0.0f)
362 m_fov_transition_active = true;
364 // If FOV smooth transition is active, initialize required variables
365 if (m_fov_transition_active) {
366 m_transition_time = spec.transition_time;
367 m_fov_diff = m_target_fov_degrees - m_old_fov_degrees;
371 void Camera::step(f32 dtime)
373 for (auto node : m_wieldnodes)
376 if(m_view_bobbing_fall > 0)
378 m_view_bobbing_fall -= 3 * dtime;
379 if(m_view_bobbing_fall <= 0)
380 m_view_bobbing_fall = -1; // Mark the effect as finished
383 if (m_view_bobbing_state != 0)
385 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
386 f32 offset = dtime * m_view_bobbing_speed * 0.030;
387 if (m_view_bobbing_state == 2) {
388 // Animation is getting turned off
389 if (m_view_bobbing_anim < 0.25) {
390 m_view_bobbing_anim -= offset;
391 } else if (m_view_bobbing_anim > 0.75) {
392 m_view_bobbing_anim += offset;
393 } else if (m_view_bobbing_anim < 0.5) {
394 m_view_bobbing_anim += offset;
395 if (m_view_bobbing_anim > 0.5)
396 m_view_bobbing_anim = 0.5;
398 m_view_bobbing_anim -= offset;
399 if (m_view_bobbing_anim < 0.5)
400 m_view_bobbing_anim = 0.5;
403 if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
404 fabs(m_view_bobbing_anim - 0.5) < 0.01) {
405 m_view_bobbing_anim = 0;
406 m_view_bobbing_state = 0;
410 float was = m_view_bobbing_anim;
411 m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
412 bool step = (was == 0 ||
413 (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
414 (was > 0.5f && m_view_bobbing_anim <= 0.5f));
416 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
422 void Camera::addArmInertia(f32 player_yaw)
424 for (auto node : m_wieldnodes)
425 node->addArmInertia(player_yaw, m_camera_direction);
428 void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
430 // Get player position
431 // Smooth the movement when walking up stairs
432 v3f old_player_position = m_playernode->getPosition();
433 v3f player_position = player->getPosition();
435 f32 yaw = player->getYaw();
436 f32 pitch = player->getPitch();
438 // This is worse than `LocalPlayer::getPosition()` but
439 // mods expect the player head to be at the parent's position
441 if (player->getParent())
442 player_position = player->getParent()->getPosition();
444 // Smooth the camera movement after the player instantly moves upward due to stepheight.
445 // The smoothing usually continues until the camera position reaches the player position.
446 float player_stepheight = player->getCAO() ? player->getCAO()->getStepHeight() : HUGE_VALF;
447 float upward_movement = player_position.Y - old_player_position.Y;
448 if (upward_movement < 0.01f || upward_movement > player_stepheight) {
449 m_stepheight_smooth_active = false;
450 } else if (player->touching_ground) {
451 m_stepheight_smooth_active = true;
453 if (m_stepheight_smooth_active) {
454 f32 oldy = old_player_position.Y;
455 f32 newy = player_position.Y;
456 f32 t = std::exp(-23 * frametime);
457 player_position.Y = oldy * t + newy * (1-t);
460 // Set player node transformation
461 m_playernode->setPosition(player_position);
462 m_playernode->setRotation(v3f(0, -1 * yaw, 0));
463 m_playernode->updateAbsolutePosition();
465 // Get camera tilt timer (hurt animation)
466 float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
468 // Fall bobbing animation
469 float fall_bobbing = 0;
470 if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
472 if(m_view_bobbing_fall == -1) // Effect took place and has finished
473 player->camera_impact = m_view_bobbing_fall = 0;
474 else if(m_view_bobbing_fall == 0) // Initialize effect
475 m_view_bobbing_fall = 1;
477 // Convert 0 -> 1 to 0 -> 1 -> 0
478 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
479 // Smoothen and invert the above
480 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
481 // Amplify according to the intensity of the impact
482 if (player->camera_impact > 0.0f)
483 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
485 fall_bobbing *= m_cache_fall_bobbing_amount;
488 // Calculate and translate the head SceneNode offsets
490 v3f eye_offset = player->getEyeOffset();
491 if (m_camera_mode == CAMERA_MODE_FIRST)
492 eye_offset += player->eye_offset_first;
494 eye_offset += player->eye_offset_third;
496 // Set head node transformation
497 eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
498 m_headnode->setPosition(eye_offset);
499 m_headnode->setRotation(v3f(pitch, 0,
500 cameratilt * player->hurt_tilt_strength));
501 m_headnode->updateAbsolutePosition();
504 // Compute relative camera position and target
505 v3f rel_cam_pos = v3f(0,0,0);
506 v3f rel_cam_target = v3f(0,0,1);
507 v3f rel_cam_up = v3f(0,1,0);
509 if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
510 m_camera_mode < CAMERA_MODE_THIRD) {
511 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
512 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
515 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
518 0.3 * bobdir * sin(bobfrac * M_PI),
519 -0.28 * bobtmp * bobtmp,
522 rel_cam_pos += bobvec * m_cache_view_bobbing_amount;
523 rel_cam_target += bobvec * m_cache_view_bobbing_amount;
524 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * m_cache_view_bobbing_amount);
527 // Compute absolute camera position and target
528 m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
529 m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
532 m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
534 // Separate camera position for calculation
535 v3f my_cp = m_camera_position;
537 // Reposition the camera for third person view
538 if (m_camera_mode > CAMERA_MODE_FIRST)
540 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
541 m_camera_direction *= -1;
545 // Calculate new position
547 for (int i = BS; i <= BS * 2.75; i++) {
548 my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
549 my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
551 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
553 // Prevent camera positioned inside nodes
554 const NodeDefManager *nodemgr = m_client->ndef();
555 MapNode n = m_client->getEnv().getClientMap()
556 .getNode(floatToInt(my_cp, BS));
558 const ContentFeatures& features = nodemgr->get(n);
559 if (features.walkable) {
560 my_cp.X += m_camera_direction.X*-1*-BS/2;
561 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
562 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
568 // If node blocks camera position don't move y to heigh
569 if (abort && my_cp.Y > player_position.Y+BS*2)
570 my_cp.Y = player_position.Y+BS*2;
573 // Update offset if too far away from the center of the map
574 m_camera_offset.X += CAMERA_OFFSET_STEP*
575 (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
576 m_camera_offset.Y += CAMERA_OFFSET_STEP*
577 (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
578 m_camera_offset.Z += CAMERA_OFFSET_STEP*
579 (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
581 // Set camera node transformation
582 m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
583 m_cameranode->updateAbsolutePosition();
584 m_cameranode->setUpVector(abs_cam_up);
585 // *100.0 helps in large map coordinates
586 m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
588 // update the camera position in third-person mode to render blocks behind player
589 // and correctly apply liquid post FX.
590 if (m_camera_mode != CAMERA_MODE_FIRST)
591 m_camera_position = my_cp;
594 * Apply server-sent FOV, instantaneous or smooth transition.
595 * If not, check for zoom and set to zoom FOV.
596 * Otherwise, default to m_cache_fov.
598 if (m_fov_transition_active) {
599 // Smooth FOV transition
600 // Dynamically calculate FOV delta based on frametimes
601 f32 delta = (frametime / m_transition_time) * m_fov_diff;
602 m_curr_fov_degrees += delta;
604 // Mark transition as complete if target FOV has been reached
605 if ((m_fov_diff > 0.0f && m_curr_fov_degrees >= m_target_fov_degrees) ||
606 (m_fov_diff < 0.0f && m_curr_fov_degrees <= m_target_fov_degrees)) {
607 m_fov_transition_active = false;
608 m_curr_fov_degrees = m_target_fov_degrees;
610 } else if (m_server_sent_fov) {
611 // Instantaneous FOV change
612 m_curr_fov_degrees = m_target_fov_degrees;
613 } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
614 // Player requests zoom, apply zoom FOV
615 m_curr_fov_degrees = player->getZoomFOV();
617 // Set to client's selected FOV
618 m_curr_fov_degrees = m_cache_fov;
620 m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
622 // FOV and aspect ratio
623 const v2u32 &window_size = RenderingEngine::getWindowSize();
624 m_aspect = (f32) window_size.X / (f32) window_size.Y;
625 m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
626 // Increase vertical FOV on lower aspect ratios (<16:10)
627 m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
628 m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
629 m_cameranode->setAspectRatio(m_aspect);
630 m_cameranode->setFOV(m_fov_y);
632 // Make new matrices and frustum
633 m_cameranode->updateMatrices();
638 m_player_light_color = player->light_color;
640 for (auto node : m_wieldnodes)
641 node->update(m_player_light_color, m_view_bobbing_anim, tool_reload_ratio);
643 // Set render distance
644 updateViewingRange();
646 // If the player is walking, swimming, or climbing,
647 // view bobbing is enabled and free_move is off,
648 // start (or continue) the view bobbing animation.
649 const v3f &speed = player->getSpeed();
650 const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
651 const bool movement_Y = fabs(speed.Y) > BS;
653 const bool walking = movement_XZ && player->touching_ground;
654 const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
655 const bool climbing = movement_Y && player->is_climbing;
656 const bool flying = g_settings->getBool("free_move")
657 && m_client->checkLocalPrivilege("fly");
658 if ((walking || swimming || climbing) && !flying) {
660 m_view_bobbing_state = 1;
661 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
662 } else if (m_view_bobbing_state == 1) {
664 m_view_bobbing_state = 2;
665 m_view_bobbing_speed = 60;
669 void Camera::updateViewingRange()
671 f32 viewing_range = g_settings->getFloat("viewing_range");
673 // Ignore near_plane setting on all other platforms to prevent abuse
675 m_cameranode->setNearValue(rangelim(
676 g_settings->getFloat("near_plane"), 0.0f, 0.25f) * BS);
678 m_cameranode->setNearValue(0.1f * BS);
681 m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
682 if (m_draw_control.range_all) {
683 m_cameranode->setFarValue(100000.0);
686 m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
689 void Camera::setDigging(s32 button, HandIndex hand)
691 m_wieldnodes[hand]->setDigging(button);
694 void Camera::wield(const ItemStack &item, HandIndex hand)
696 m_wieldnodes[hand]->wield(item);
699 void Camera::drawWieldedTool(irr::core::matrix4* translation)
701 // Draw the wielded node (in a separate scene manager)
702 scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
703 cam->setAspectRatio(m_cameranode->getAspectRatio());
704 cam->setFOV(72.0*M_PI/180.0);
705 cam->setNearValue(40); // give wield tool smaller z-depth than the world in most cases.
706 cam->setFarValue(1000);
707 if (translation != NULL)
709 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
710 irr::core::vector3df focusPoint = (cam->getTarget()
711 - cam->getAbsolutePosition()).setLength(1)
712 + cam->getAbsolutePosition();
714 irr::core::vector3df camera_pos =
715 (startMatrix * *translation).getTranslation();
716 cam->setPosition(camera_pos);
717 cam->updateAbsolutePosition();
718 cam->setTarget(focusPoint);
720 m_wieldmgr->drawAll();
723 void Camera::drawNametags()
725 core::matrix4 trans = m_cameranode->getProjectionMatrix();
726 trans *= m_cameranode->getViewMatrix();
728 gui::IGUIFont *font = g_fontengine->getFont();
729 video::IVideoDriver *driver = RenderingEngine::get_video_driver();
730 v2u32 screensize = driver->getScreenSize();
732 for (const Nametag *nametag : m_nametags) {
733 // Nametags are hidden in GenericCAO::updateNametag()
735 v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
736 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
737 trans.multiplyWith1x4Matrix(transformed_pos);
738 if (transformed_pos[3] > 0) {
739 std::wstring nametag_colorless =
740 unescape_translate(utf8_to_wide(nametag->text));
741 core::dimension2d<u32> textsize = font->getDimension(
742 nametag_colorless.c_str());
743 f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
744 core::reciprocal(transformed_pos[3]);
746 screen_pos.X = screensize.X *
747 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
748 screen_pos.Y = screensize.Y *
749 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
750 core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
752 auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
753 if (bgcolor.getAlpha() != 0) {
754 core::rect<s32> bg_size(-2, 0, textsize.Width + 2, textsize.Height);
755 driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
759 translate_string(utf8_to_wide(nametag->text)).c_str(),
760 size + screen_pos, nametag->textcolor);
765 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
766 const std::string &text, video::SColor textcolor,
767 Optional<video::SColor> bgcolor, const v3f &pos)
769 Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
770 m_nametags.push_back(nametag);
774 void Camera::removeNametag(Nametag *nametag)
776 m_nametags.remove(nametag);
780 std::array<core::plane3d<f32>, 4> Camera::getFrustumCullPlanes() const
782 using irr::scene::SViewFrustum;
783 const auto &frustum_planes = m_cameranode->getViewFrustum()->planes;
785 frustum_planes[SViewFrustum::VF_LEFT_PLANE],
786 frustum_planes[SViewFrustum::VF_RIGHT_PLANE],
787 frustum_planes[SViewFrustum::VF_BOTTOM_PLANE],
788 frustum_planes[SViewFrustum::VF_TOP_PLANE],