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 "guiscalingfilter.h"
40 #include "script/scripting_client.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 Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine):
50 m_draw_control(draw_control),
53 auto smgr = rendering_engine->get_scene_manager();
54 // note: making the camera node a child of the player node
55 // would lead to unexpected behaviour, so we don't do that.
56 m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
57 m_headnode = smgr->addEmptySceneNode(m_playernode);
58 m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
59 m_cameranode->bindTargetAndRotation(true);
61 // This needs to be in its own scene manager. It is drawn after
62 // all other 3D scene nodes and before the GUI.
63 m_wieldmgr = smgr->createNewSceneManager();
64 m_wieldmgr->addCameraSceneNode();
65 m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
66 m_wieldnode->setItem(ItemStack(), m_client);
67 m_wieldnode->drop(); // m_wieldmgr grabbed it
69 /* TODO: Add a callback function so these can be updated when a setting
70 * changes. At this point in time it doesn't matter (e.g. /set
71 * is documented to change server settings only)
73 * TODO: Local caching of settings is not optimal and should at some stage
74 * be updated to use a global settings object for getting thse values
75 * (as opposed to the this local caching). This can be addressed in
78 m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
79 m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
80 // 45 degrees is the lowest FOV that doesn't cause the server to treat this
81 // as a zoom FOV and load world beyond the set server limits.
82 m_cache_fov = std::fmax(g_settings->getFloat("fov"), 45.0f);
83 m_arm_inertia = g_settings->getBool("arm_inertia");
85 m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds");
93 void Camera::notifyFovChange()
95 LocalPlayer *player = m_client->getEnv().getLocalPlayer();
98 PlayerFovSpec spec = player->getFov();
101 * Update m_old_fov_degrees first - it serves as the starting point of the
102 * upcoming transition.
104 * If an FOV transition is already active, mark current FOV as the start of
105 * the new transition. If not, set it to the previous transition's target FOV.
107 if (m_fov_transition_active)
108 m_old_fov_degrees = m_curr_fov_degrees;
110 m_old_fov_degrees = m_server_sent_fov ? m_target_fov_degrees : m_cache_fov;
113 * Update m_server_sent_fov next - it corresponds to the target FOV of the
114 * upcoming transition.
116 * Set it to m_cache_fov, if server-sent FOV is 0. Otherwise check if
117 * server-sent FOV is a multiplier, and multiply it with m_cache_fov instead
120 if (spec.fov == 0.0f) {
121 m_server_sent_fov = false;
122 m_target_fov_degrees = m_cache_fov;
124 m_server_sent_fov = true;
125 m_target_fov_degrees = spec.is_multiplier ? m_cache_fov * spec.fov : spec.fov;
128 if (spec.transition_time > 0.0f)
129 m_fov_transition_active = true;
131 // If FOV smooth transition is active, initialize required variables
132 if (m_fov_transition_active) {
133 m_transition_time = spec.transition_time;
134 m_fov_diff = m_target_fov_degrees - m_old_fov_degrees;
138 // Returns the fractional part of x
139 inline f32 my_modf(f32 x)
142 return modf(x, &dummy);
145 void Camera::step(f32 dtime)
147 if(m_view_bobbing_fall > 0)
149 m_view_bobbing_fall -= 3 * dtime;
150 if(m_view_bobbing_fall <= 0)
151 m_view_bobbing_fall = -1; // Mark the effect as finished
154 bool was_under_zero = m_wield_change_timer < 0;
155 m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
157 if (m_wield_change_timer >= 0 && was_under_zero)
158 m_wieldnode->setItem(m_wield_item_next, m_client);
160 if (m_view_bobbing_state != 0)
162 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
163 f32 offset = dtime * m_view_bobbing_speed * 0.030;
164 if (m_view_bobbing_state == 2) {
165 // Animation is getting turned off
166 if (m_view_bobbing_anim < 0.25) {
167 m_view_bobbing_anim -= offset;
168 } else if (m_view_bobbing_anim > 0.75) {
169 m_view_bobbing_anim += offset;
170 } else if (m_view_bobbing_anim < 0.5) {
171 m_view_bobbing_anim += offset;
172 if (m_view_bobbing_anim > 0.5)
173 m_view_bobbing_anim = 0.5;
175 m_view_bobbing_anim -= offset;
176 if (m_view_bobbing_anim < 0.5)
177 m_view_bobbing_anim = 0.5;
180 if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
181 fabs(m_view_bobbing_anim - 0.5) < 0.01) {
182 m_view_bobbing_anim = 0;
183 m_view_bobbing_state = 0;
187 float was = m_view_bobbing_anim;
188 m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
189 bool step = (was == 0 ||
190 (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
191 (was > 0.5f && m_view_bobbing_anim <= 0.5f));
193 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
198 if (m_digging_button != -1) {
199 f32 offset = dtime * 3.5f;
200 float m_digging_anim_was = m_digging_anim;
201 m_digging_anim += offset;
202 if (m_digging_anim >= 1)
205 m_digging_button = -1;
208 if(m_digging_anim_was < lim && m_digging_anim >= lim)
210 if (m_digging_button == 0) {
211 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
212 } else if(m_digging_button == 1) {
213 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
219 static inline v2f dir(const v2f &pos_dist)
221 f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
222 f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
224 f32 x_abs = std::fabs(x);
225 f32 y_abs = std::fabs(y);
227 if (x_abs >= y_abs) {
232 if (y_abs >= x_abs) {
237 return v2f(std::fabs(x), std::fabs(y));
240 void Camera::addArmInertia(f32 player_yaw)
242 m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
243 -100.0f, 100.0f) / 0.016f) * 0.01f;
244 m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
245 f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
246 f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
248 if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
250 The arm moves relative to the camera speed,
251 with an acceleration factor.
254 if (m_cam_vel.X > 1.0f) {
255 if (m_cam_vel.X > m_cam_vel_old.X)
256 m_cam_vel_old.X = m_cam_vel.X;
258 f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
259 m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
261 if (m_last_cam_pos.X != player_yaw)
262 m_last_cam_pos.X = player_yaw;
264 m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
265 WIELDMESH_OFFSET_X - (WIELDMESH_AMPLITUDE_X * 0.5f),
266 WIELDMESH_OFFSET_X + (WIELDMESH_AMPLITUDE_X * 0.5f));
269 if (m_cam_vel.Y > 1.0f) {
270 if (m_cam_vel.Y > m_cam_vel_old.Y)
271 m_cam_vel_old.Y = m_cam_vel.Y;
273 f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
274 m_wieldmesh_offset.Y +=
275 m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
277 if (m_last_cam_pos.Y != m_camera_direction.Y)
278 m_last_cam_pos.Y = m_camera_direction.Y;
280 m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
281 WIELDMESH_OFFSET_Y - (WIELDMESH_AMPLITUDE_Y * 0.5f),
282 WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
285 m_arm_dir = dir(m_wieldmesh_offset);
288 Now the arm gets back to its default position when the camera stops,
289 following a vector, with a smooth deceleration factor.
292 f32 dec_X = 0.35f * (std::min(15.0f, m_cam_vel_old.X) * (1.0f +
293 (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
295 f32 dec_Y = 0.25f * (std::min(15.0f, m_cam_vel_old.Y) * (1.0f +
296 (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
299 m_cam_vel_old.X = 0.0f;
301 m_wieldmesh_offset.X -=
302 m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
305 m_cam_vel_old.Y = 0.0f;
307 m_wieldmesh_offset.Y -=
308 m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
312 void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
314 // Get player position
315 // Smooth the movement when walking up stairs
316 v3f old_player_position = m_playernode->getPosition();
317 v3f player_position = player->getPosition();
319 // This is worse than `LocalPlayer::getPosition()` but
320 // mods expect the player head to be at the parent's position
322 if (player->getParent())
323 player_position = player->getParent()->getPosition() + v3f(0, g_settings->getBool("float_above_parent") ? BS : 0, 0);
325 // Smooth the camera movement after the player instantly moves upward due to stepheight.
326 // The smoothing usually continues until the camera position reaches the player position.
327 float player_stepheight = player->getCAO() ? player->getCAO()->getStepHeight() : HUGE_VALF;
328 float upward_movement = player_position.Y - old_player_position.Y;
329 if (upward_movement < 0.01f || upward_movement > player_stepheight) {
330 m_stepheight_smooth_active = false;
331 } else if (player->touching_ground) {
332 m_stepheight_smooth_active = true;
334 if (m_stepheight_smooth_active) {
335 f32 oldy = old_player_position.Y;
336 f32 newy = player_position.Y;
337 f32 t = std::exp(-23 * frametime);
338 player_position.Y = oldy * t + newy * (1-t);
341 // Set player node transformation
342 m_playernode->setPosition(player_position);
343 m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
344 m_playernode->updateAbsolutePosition();
346 // Get camera tilt timer (hurt animation)
347 float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
349 // Fall bobbing animation
350 float fall_bobbing = 0;
351 if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
353 if(m_view_bobbing_fall == -1) // Effect took place and has finished
354 player->camera_impact = m_view_bobbing_fall = 0;
355 else if(m_view_bobbing_fall == 0) // Initialize effect
356 m_view_bobbing_fall = 1;
358 // Convert 0 -> 1 to 0 -> 1 -> 0
359 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
360 // Smoothen and invert the above
361 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
362 // Amplify according to the intensity of the impact
363 if (player->camera_impact > 0.0f)
364 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
366 fall_bobbing *= m_cache_fall_bobbing_amount;
369 // Calculate and translate the head SceneNode offsets
371 v3f eye_offset = player->getEyeOffset();
372 if (m_camera_mode == CAMERA_MODE_FIRST)
373 eye_offset += player->eye_offset_first;
375 eye_offset += player->eye_offset_third;
377 // Set head node transformation
378 eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
379 m_headnode->setPosition(eye_offset);
380 m_headnode->setRotation(v3f(player->getPitch(), 0,
381 cameratilt * player->hurt_tilt_strength));
382 m_headnode->updateAbsolutePosition();
385 // Compute relative camera position and target
386 v3f rel_cam_pos = v3f(0,0,0);
387 v3f rel_cam_target = v3f(0,0,1);
388 v3f rel_cam_up = v3f(0,1,0);
390 if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
391 m_camera_mode < CAMERA_MODE_THIRD) {
392 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
393 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
396 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
399 0.3 * bobdir * sin(bobfrac * M_PI),
400 -0.28 * bobtmp * bobtmp,
403 rel_cam_pos += bobvec * m_cache_view_bobbing_amount;
404 rel_cam_target += bobvec * m_cache_view_bobbing_amount;
405 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * m_cache_view_bobbing_amount);
408 // Compute absolute camera position and target
409 m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
410 m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
413 m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
415 // Seperate camera position for calculation
416 v3f my_cp = m_camera_position;
418 // Reposition the camera for third person view
419 if (m_camera_mode > CAMERA_MODE_FIRST)
421 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
422 m_camera_direction *= -1;
426 // Calculate new position
428 for (int i = BS; i <= BS * 2.75; i++) {
429 my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
430 my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
432 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
434 // Prevent camera positioned inside nodes
435 const NodeDefManager *nodemgr = m_client->ndef();
436 MapNode n = m_client->getEnv().getClientMap()
437 .getNode(floatToInt(my_cp, BS));
439 const ContentFeatures& features = nodemgr->get(n);
440 if (features.walkable) {
441 my_cp.X += m_camera_direction.X*-1*-BS/2;
442 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
443 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
449 // If node blocks camera position don't move y to heigh
450 if (abort && my_cp.Y > player_position.Y+BS*2)
451 my_cp.Y = player_position.Y+BS*2;
454 // Update offset if too far away from the center of the map
455 m_camera_offset.X += CAMERA_OFFSET_STEP*
456 (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
457 m_camera_offset.Y += CAMERA_OFFSET_STEP*
458 (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
459 m_camera_offset.Z += CAMERA_OFFSET_STEP*
460 (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
462 // Set camera node transformation
463 m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
464 m_cameranode->setUpVector(abs_cam_up);
465 // *100.0 helps in large map coordinates
466 m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
468 // update the camera position in third-person mode to render blocks behind player
469 // and correctly apply liquid post FX.
470 if (m_camera_mode != CAMERA_MODE_FIRST)
471 m_camera_position = my_cp;
474 * Apply server-sent FOV, instantaneous or smooth transition.
475 * If not, check for zoom and set to zoom FOV.
476 * Otherwise, default to m_cache_fov.
478 if (m_fov_transition_active) {
479 // Smooth FOV transition
480 // Dynamically calculate FOV delta based on frametimes
481 f32 delta = (frametime / m_transition_time) * m_fov_diff;
482 m_curr_fov_degrees += delta;
484 // Mark transition as complete if target FOV has been reached
485 if ((m_fov_diff > 0.0f && m_curr_fov_degrees >= m_target_fov_degrees) ||
486 (m_fov_diff < 0.0f && m_curr_fov_degrees <= m_target_fov_degrees)) {
487 m_fov_transition_active = false;
488 m_curr_fov_degrees = m_target_fov_degrees;
490 } else if (m_server_sent_fov) {
491 // Instantaneous FOV change
492 m_curr_fov_degrees = m_target_fov_degrees;
493 } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
494 // Player requests zoom, apply zoom FOV
495 m_curr_fov_degrees = player->getZoomFOV();
497 // Set to client's selected FOV
498 m_curr_fov_degrees = m_cache_fov;
500 m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
502 // FOV and aspect ratio
503 const v2u32 &window_size = RenderingEngine::getWindowSize();
504 m_aspect = (f32) window_size.X / (f32) window_size.Y;
505 m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
506 // Increase vertical FOV on lower aspect ratios (<16:10)
507 m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
508 m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
509 m_cameranode->setAspectRatio(m_aspect);
510 m_cameranode->setFOV(m_fov_y);
513 addArmInertia(player->getYaw());
515 // Position the wielded item
516 //v3f wield_position = v3f(45, -35, 65);
517 v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
518 //v3f wield_rotation = v3f(-100, 120, -100);
519 v3f wield_rotation = v3f(-100, 120, -100);
520 wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
521 if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
524 if(m_digging_anim > 0.5)
525 frac = 2.0 * (m_digging_anim - 0.5);
526 // This value starts from 1 and settles to 0
527 f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
528 //f32 ratiothing2 = pow(ratiothing, 0.5f);
529 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
530 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
531 //wield_position.Z += frac * 5.0 * ratiothing2;
532 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
533 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
534 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
535 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
537 if (m_digging_button != -1)
539 f32 digfrac = m_digging_anim;
540 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
541 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
542 wield_position.Z += 25 * 0.5;
544 // Euler angles are PURE EVIL, so why not use quaternions?
545 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
546 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
547 core::quaternion quat_slerp;
548 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
549 quat_slerp.toEuler(wield_rotation);
550 wield_rotation *= core::RADTODEG;
552 f32 bobfrac = my_modf(m_view_bobbing_anim);
553 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
554 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
556 m_wieldnode->setPosition(wield_position);
557 m_wieldnode->setRotation(wield_rotation);
559 m_wieldnode->setNodeLightColor(player->light_color);
561 // Set render distance
562 updateViewingRange();
564 // If the player is walking, swimming, or climbing,
565 // view bobbing is enabled and free_move is off,
566 // start (or continue) the view bobbing animation.
567 const v3f &speed = player->getSpeed();
568 const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
569 const bool movement_Y = fabs(speed.Y) > BS;
571 const bool walking = movement_XZ && player->touching_ground;
572 const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
573 const bool climbing = movement_Y && player->is_climbing;
574 const bool flying = g_settings->getBool("free_move")
575 && m_client->checkLocalPrivilege("fly");
576 if ((walking || swimming || climbing) && !flying) {
578 m_view_bobbing_state = 1;
579 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
580 } else if (m_view_bobbing_state == 1) {
582 m_view_bobbing_state = 2;
583 m_view_bobbing_speed = 60;
587 void Camera::updateViewingRange()
589 f32 viewing_range = g_settings->getFloat("viewing_range");
591 // Ignore near_plane setting on all other platforms to prevent abuse
593 m_cameranode->setNearValue(rangelim(
594 g_settings->getFloat("near_plane"), 0.0f, 0.25f) * BS);
596 m_cameranode->setNearValue(0.1f * BS);
599 m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
600 if (m_draw_control.range_all) {
601 m_cameranode->setFarValue(100000.0);
604 m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
607 void Camera::setDigging(s32 button)
609 if (m_digging_button == -1)
610 m_digging_button = button;
613 void Camera::wield(const ItemStack &item)
615 if (item.name != m_wield_item_next.name ||
616 item.metadata != m_wield_item_next.metadata) {
617 m_wield_item_next = item;
618 if (m_wield_change_timer > 0)
619 m_wield_change_timer = -m_wield_change_timer;
620 else if (m_wield_change_timer == 0)
621 m_wield_change_timer = -0.001;
625 void Camera::drawWieldedTool(irr::core::matrix4* translation)
627 // Clear Z buffer so that the wielded tool stays in front of world geometry
628 m_wieldmgr->getVideoDriver()->clearBuffers(video::ECBF_DEPTH);
630 // Draw the wielded node (in a separate scene manager)
631 scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
632 cam->setAspectRatio(m_cameranode->getAspectRatio());
633 cam->setFOV(72.0*M_PI/180.0);
634 cam->setNearValue(10);
635 cam->setFarValue(1000);
636 if (translation != NULL)
638 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
639 irr::core::vector3df focusPoint = (cam->getTarget()
640 - cam->getAbsolutePosition()).setLength(1)
641 + cam->getAbsolutePosition();
643 irr::core::vector3df camera_pos =
644 (startMatrix * *translation).getTranslation();
645 cam->setPosition(camera_pos);
646 cam->setTarget(focusPoint);
648 m_wieldmgr->drawAll();
651 void Camera::drawNametags()
653 core::matrix4 trans = m_cameranode->getProjectionMatrix();
654 trans *= m_cameranode->getViewMatrix();
656 gui::IGUIFont *font = g_fontengine->getFont();
657 video::IVideoDriver *driver = RenderingEngine::get_video_driver();
658 v2u32 screensize = driver->getScreenSize();
660 for (const Nametag *nametag : m_nametags) {
661 // Nametags are hidden in GenericCAO::updateNametag()
663 v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
664 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
665 trans.multiplyWith1x4Matrix(transformed_pos);
666 if (transformed_pos[3] > 0) {
667 std::wstring nametag_colorless =
668 unescape_translate(utf8_to_wide(nametag->text));
669 core::dimension2d<u32> textsize = font->getDimension(
670 nametag_colorless.c_str());
671 f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
672 core::reciprocal(transformed_pos[3]);
674 screen_pos.X = screensize.X *
675 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
676 screen_pos.Y = screensize.Y *
677 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
678 core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
679 core::rect<s32> bg_size(-2, 0, std::max(textsize.Width+2, (u32) nametag->images_dim.Width), textsize.Height + nametag->images_dim.Height);
681 auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
682 if (bgcolor.getAlpha() != 0)
683 driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
686 translate_string(utf8_to_wide(nametag->text)).c_str(),
687 size + screen_pos, nametag->textcolor);
689 v2s32 image_pos(screen_pos);
690 image_pos.Y += textsize.Height;
692 const video::SColor color(255, 255, 255, 255);
693 const video::SColor colors[] = {color, color, color, color};
695 for (video::ITexture *texture : nametag->images) {
696 core::dimension2di imgsize(texture->getOriginalSize());
697 core::rect<s32> rect(core::position2d<s32>(0, 0), imgsize);
698 draw2DImageFilterScaled(driver, texture, rect + image_pos, rect, NULL, colors, true);
699 image_pos += core::dimension2di(imgsize.Width, 0);
705 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
706 const std::string &text, video::SColor textcolor,
707 Optional<video::SColor> bgcolor, const v3f &pos,
708 const std::vector<std::string> &images)
710 Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos, m_client->tsrc(), images);
711 m_nametags.push_back(nametag);
715 void Camera::removeNametag(Nametag *nametag)
717 m_nametags.remove(nametag);