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),
52 m_player_light_color(0xFFFFFFFF)
54 auto smgr = rendering_engine->get_scene_manager();
55 // note: making the camera node a child of the player node
56 // would lead to unexpected behaviour, so we don't do that.
57 m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
58 m_headnode = smgr->addEmptySceneNode(m_playernode);
59 m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
60 m_cameranode->bindTargetAndRotation(true);
62 // This needs to be in its own scene manager. It is drawn after
63 // all other 3D scene nodes and before the GUI.
64 m_wieldmgr = smgr->createNewSceneManager();
65 m_wieldmgr->addCameraSceneNode();
66 m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
67 m_wieldnode->setItem(ItemStack(), m_client);
68 m_wieldnode->drop(); // m_wieldmgr grabbed it
70 /* TODO: Add a callback function so these can be updated when a setting
71 * changes. At this point in time it doesn't matter (e.g. /set
72 * is documented to change server settings only)
74 * TODO: Local caching of settings is not optimal and should at some stage
75 * be updated to use a global settings object for getting thse values
76 * (as opposed to the this local caching). This can be addressed in
79 m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
80 m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
81 // 45 degrees is the lowest FOV that doesn't cause the server to treat this
82 // as a zoom FOV and load world beyond the set server limits.
83 m_cache_fov = std::fmax(g_settings->getFloat("fov"), 45.0f);
84 m_arm_inertia = g_settings->getBool("arm_inertia");
86 m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds");
94 void Camera::notifyFovChange()
96 LocalPlayer *player = m_client->getEnv().getLocalPlayer();
99 PlayerFovSpec spec = player->getFov();
102 * Update m_old_fov_degrees first - it serves as the starting point of the
103 * upcoming transition.
105 * If an FOV transition is already active, mark current FOV as the start of
106 * the new transition. If not, set it to the previous transition's target FOV.
108 if (m_fov_transition_active)
109 m_old_fov_degrees = m_curr_fov_degrees;
111 m_old_fov_degrees = m_server_sent_fov ? m_target_fov_degrees : m_cache_fov;
114 * Update m_server_sent_fov next - it corresponds to the target FOV of the
115 * upcoming transition.
117 * Set it to m_cache_fov, if server-sent FOV is 0. Otherwise check if
118 * server-sent FOV is a multiplier, and multiply it with m_cache_fov instead
121 if (spec.fov == 0.0f) {
122 m_server_sent_fov = false;
123 m_target_fov_degrees = m_cache_fov;
125 m_server_sent_fov = true;
126 m_target_fov_degrees = spec.is_multiplier ? m_cache_fov * spec.fov : spec.fov;
129 if (spec.transition_time > 0.0f)
130 m_fov_transition_active = true;
132 // If FOV smooth transition is active, initialize required variables
133 if (m_fov_transition_active) {
134 m_transition_time = spec.transition_time;
135 m_fov_diff = m_target_fov_degrees - m_old_fov_degrees;
139 // Returns the fractional part of x
140 inline f32 my_modf(f32 x)
143 return modf(x, &dummy);
146 void Camera::step(f32 dtime)
148 if(m_view_bobbing_fall > 0)
150 m_view_bobbing_fall -= 3 * dtime;
151 if(m_view_bobbing_fall <= 0)
152 m_view_bobbing_fall = -1; // Mark the effect as finished
155 bool was_under_zero = m_wield_change_timer < 0;
156 m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
158 if (m_wield_change_timer >= 0 && was_under_zero) {
159 m_wieldnode->setItem(m_wield_item_next, m_client);
160 m_wieldnode->setNodeLightColor(m_player_light_color);
163 if (m_view_bobbing_state != 0)
165 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
166 f32 offset = dtime * m_view_bobbing_speed * 0.030;
167 if (m_view_bobbing_state == 2) {
168 // Animation is getting turned off
169 if (m_view_bobbing_anim < 0.25) {
170 m_view_bobbing_anim -= offset;
171 } else if (m_view_bobbing_anim > 0.75) {
172 m_view_bobbing_anim += offset;
173 } else if (m_view_bobbing_anim < 0.5) {
174 m_view_bobbing_anim += offset;
175 if (m_view_bobbing_anim > 0.5)
176 m_view_bobbing_anim = 0.5;
178 m_view_bobbing_anim -= offset;
179 if (m_view_bobbing_anim < 0.5)
180 m_view_bobbing_anim = 0.5;
183 if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
184 fabs(m_view_bobbing_anim - 0.5) < 0.01) {
185 m_view_bobbing_anim = 0;
186 m_view_bobbing_state = 0;
190 float was = m_view_bobbing_anim;
191 m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
192 bool step = (was == 0 ||
193 (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
194 (was > 0.5f && m_view_bobbing_anim <= 0.5f));
196 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
201 if (m_digging_button != -1) {
202 f32 offset = dtime * 3.5f;
203 float m_digging_anim_was = m_digging_anim;
204 m_digging_anim += offset;
205 if (m_digging_anim >= 1)
208 m_digging_button = -1;
211 if(m_digging_anim_was < lim && m_digging_anim >= lim)
213 if (m_digging_button == 0) {
214 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
215 } else if(m_digging_button == 1) {
216 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
222 static inline v2f dir(const v2f &pos_dist)
224 f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
225 f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
227 f32 x_abs = std::fabs(x);
228 f32 y_abs = std::fabs(y);
230 if (x_abs >= y_abs) {
235 if (y_abs >= x_abs) {
240 return v2f(std::fabs(x), std::fabs(y));
243 void Camera::addArmInertia(f32 player_yaw)
245 m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
246 -100.0f, 100.0f) / 0.016f) * 0.01f;
247 m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
248 f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
249 f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
251 if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
253 The arm moves relative to the camera speed,
254 with an acceleration factor.
257 if (m_cam_vel.X > 1.0f) {
258 if (m_cam_vel.X > m_cam_vel_old.X)
259 m_cam_vel_old.X = m_cam_vel.X;
261 f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
262 m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
264 if (m_last_cam_pos.X != player_yaw)
265 m_last_cam_pos.X = player_yaw;
267 m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
268 WIELDMESH_OFFSET_X - (WIELDMESH_AMPLITUDE_X * 0.5f),
269 WIELDMESH_OFFSET_X + (WIELDMESH_AMPLITUDE_X * 0.5f));
272 if (m_cam_vel.Y > 1.0f) {
273 if (m_cam_vel.Y > m_cam_vel_old.Y)
274 m_cam_vel_old.Y = m_cam_vel.Y;
276 f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
277 m_wieldmesh_offset.Y +=
278 m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
280 if (m_last_cam_pos.Y != m_camera_direction.Y)
281 m_last_cam_pos.Y = m_camera_direction.Y;
283 m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
284 WIELDMESH_OFFSET_Y - (WIELDMESH_AMPLITUDE_Y * 0.5f),
285 WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
288 m_arm_dir = dir(m_wieldmesh_offset);
291 Now the arm gets back to its default position when the camera stops,
292 following a vector, with a smooth deceleration factor.
295 f32 dec_X = 0.35f * (std::min(15.0f, m_cam_vel_old.X) * (1.0f +
296 (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
298 f32 dec_Y = 0.25f * (std::min(15.0f, m_cam_vel_old.Y) * (1.0f +
299 (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
302 m_cam_vel_old.X = 0.0f;
304 m_wieldmesh_offset.X -=
305 m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
308 m_cam_vel_old.Y = 0.0f;
310 m_wieldmesh_offset.Y -=
311 m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
315 void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
317 // Get player position
318 // Smooth the movement when walking up stairs
319 v3f old_player_position = m_playernode->getPosition();
320 v3f player_position = player->getPosition();
322 // This is worse than `LocalPlayer::getPosition()` but
323 // mods expect the player head to be at the parent's position
325 if (player->getParent())
326 player_position = player->getParent()->getPosition() + v3f(0, g_settings->getBool("float_above_parent") ? BS : 0, 0);
328 // Smooth the camera movement after the player instantly moves upward due to stepheight.
329 // The smoothing usually continues until the camera position reaches the player position.
330 float player_stepheight = player->getCAO() ? player->getCAO()->getStepHeight() : HUGE_VALF;
331 float upward_movement = player_position.Y - old_player_position.Y;
332 if (upward_movement < 0.01f || upward_movement > player_stepheight) {
333 m_stepheight_smooth_active = false;
334 } else if (player->touching_ground) {
335 m_stepheight_smooth_active = true;
337 if (m_stepheight_smooth_active) {
338 f32 oldy = old_player_position.Y;
339 f32 newy = player_position.Y;
340 f32 t = std::exp(-23 * frametime);
341 player_position.Y = oldy * t + newy * (1-t);
344 // Set player node transformation
345 m_playernode->setPosition(player_position);
346 m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
347 m_playernode->updateAbsolutePosition();
349 // Get camera tilt timer (hurt animation)
350 float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
352 // Fall bobbing animation
353 float fall_bobbing = 0;
354 if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
356 if(m_view_bobbing_fall == -1) // Effect took place and has finished
357 player->camera_impact = m_view_bobbing_fall = 0;
358 else if(m_view_bobbing_fall == 0) // Initialize effect
359 m_view_bobbing_fall = 1;
361 // Convert 0 -> 1 to 0 -> 1 -> 0
362 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
363 // Smoothen and invert the above
364 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
365 // Amplify according to the intensity of the impact
366 if (player->camera_impact > 0.0f)
367 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
369 fall_bobbing *= m_cache_fall_bobbing_amount;
372 // Calculate and translate the head SceneNode offsets
374 v3f eye_offset = player->getEyeOffset();
375 if (m_camera_mode == CAMERA_MODE_FIRST)
376 eye_offset += player->eye_offset_first;
378 eye_offset += player->eye_offset_third;
380 // Set head node transformation
381 eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
382 m_headnode->setPosition(eye_offset);
383 m_headnode->setRotation(v3f(player->getPitch(), 0,
384 cameratilt * player->hurt_tilt_strength));
385 m_headnode->updateAbsolutePosition();
388 // Compute relative camera position and target
389 v3f rel_cam_pos = v3f(0,0,0);
390 v3f rel_cam_target = v3f(0,0,1);
391 v3f rel_cam_up = v3f(0,1,0);
393 if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
394 m_camera_mode < CAMERA_MODE_THIRD) {
395 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
396 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
399 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
402 0.3 * bobdir * sin(bobfrac * M_PI),
403 -0.28 * bobtmp * bobtmp,
406 rel_cam_pos += bobvec * m_cache_view_bobbing_amount;
407 rel_cam_target += bobvec * m_cache_view_bobbing_amount;
408 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * m_cache_view_bobbing_amount);
411 // Compute absolute camera position and target
412 m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
413 m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
416 m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
418 // Seperate camera position for calculation
419 v3f my_cp = m_camera_position;
421 // Reposition the camera for third person view
422 if (m_camera_mode > CAMERA_MODE_FIRST)
424 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
425 m_camera_direction *= -1;
429 // Calculate new position
431 for (int i = BS; i <= BS * 2.75; i++) {
432 my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
433 my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
435 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
437 // Prevent camera positioned inside nodes
438 const NodeDefManager *nodemgr = m_client->ndef();
439 MapNode n = m_client->getEnv().getClientMap()
440 .getNode(floatToInt(my_cp, BS));
442 const ContentFeatures& features = nodemgr->get(n);
443 if (features.walkable) {
444 my_cp.X += m_camera_direction.X*-1*-BS/2;
445 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
446 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
452 // If node blocks camera position don't move y to heigh
453 if (abort && my_cp.Y > player_position.Y+BS*2)
454 my_cp.Y = player_position.Y+BS*2;
457 // Update offset if too far away from the center of the map
458 m_camera_offset.X += CAMERA_OFFSET_STEP*
459 (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
460 m_camera_offset.Y += CAMERA_OFFSET_STEP*
461 (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
462 m_camera_offset.Z += CAMERA_OFFSET_STEP*
463 (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
465 // Set camera node transformation
466 m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
467 m_cameranode->setUpVector(abs_cam_up);
468 // *100.0 helps in large map coordinates
469 m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
471 // update the camera position in third-person mode to render blocks behind player
472 // and correctly apply liquid post FX.
473 if (m_camera_mode != CAMERA_MODE_FIRST)
474 m_camera_position = my_cp;
477 * Apply server-sent FOV, instantaneous or smooth transition.
478 * If not, check for zoom and set to zoom FOV.
479 * Otherwise, default to m_cache_fov.
481 if (m_fov_transition_active) {
482 // Smooth FOV transition
483 // Dynamically calculate FOV delta based on frametimes
484 f32 delta = (frametime / m_transition_time) * m_fov_diff;
485 m_curr_fov_degrees += delta;
487 // Mark transition as complete if target FOV has been reached
488 if ((m_fov_diff > 0.0f && m_curr_fov_degrees >= m_target_fov_degrees) ||
489 (m_fov_diff < 0.0f && m_curr_fov_degrees <= m_target_fov_degrees)) {
490 m_fov_transition_active = false;
491 m_curr_fov_degrees = m_target_fov_degrees;
493 } else if (m_server_sent_fov) {
494 // Instantaneous FOV change
495 m_curr_fov_degrees = m_target_fov_degrees;
496 } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
497 // Player requests zoom, apply zoom FOV
498 m_curr_fov_degrees = player->getZoomFOV();
500 // Set to client's selected FOV
501 m_curr_fov_degrees = m_cache_fov;
503 m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
505 // FOV and aspect ratio
506 const v2u32 &window_size = RenderingEngine::getWindowSize();
507 m_aspect = (f32) window_size.X / (f32) window_size.Y;
508 m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
509 // Increase vertical FOV on lower aspect ratios (<16:10)
510 m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
511 m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
512 m_cameranode->setAspectRatio(m_aspect);
513 m_cameranode->setFOV(m_fov_y);
516 addArmInertia(player->getYaw());
518 // Position the wielded item
519 //v3f wield_position = v3f(45, -35, 65);
520 v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
521 //v3f wield_rotation = v3f(-100, 120, -100);
522 v3f wield_rotation = v3f(-100, 120, -100);
523 wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
524 if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
527 if(m_digging_anim > 0.5)
528 frac = 2.0 * (m_digging_anim - 0.5);
529 // This value starts from 1 and settles to 0
530 f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
531 //f32 ratiothing2 = pow(ratiothing, 0.5f);
532 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
533 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
534 //wield_position.Z += frac * 5.0 * ratiothing2;
535 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
536 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
537 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
538 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
540 if (m_digging_button != -1)
542 f32 digfrac = m_digging_anim;
543 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
544 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
545 wield_position.Z += 25 * 0.5;
547 // Euler angles are PURE EVIL, so why not use quaternions?
548 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
549 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
550 core::quaternion quat_slerp;
551 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
552 quat_slerp.toEuler(wield_rotation);
553 wield_rotation *= core::RADTODEG;
555 f32 bobfrac = my_modf(m_view_bobbing_anim);
556 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
557 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
559 m_wieldnode->setPosition(wield_position);
560 m_wieldnode->setRotation(wield_rotation);
562 m_player_light_color = player->light_color;
563 m_wieldnode->setNodeLightColor(m_player_light_color);
565 // Set render distance
566 updateViewingRange();
568 // If the player is walking, swimming, or climbing,
569 // view bobbing is enabled and free_move is off,
570 // start (or continue) the view bobbing animation.
571 const v3f &speed = player->getSpeed();
572 const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
573 const bool movement_Y = fabs(speed.Y) > BS;
575 const bool walking = movement_XZ && player->touching_ground;
576 const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
577 const bool climbing = movement_Y && player->is_climbing;
578 const bool flying = g_settings->getBool("free_move")
579 && m_client->checkLocalPrivilege("fly");
580 if ((walking || swimming || climbing) && !flying) {
582 m_view_bobbing_state = 1;
583 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
584 } else if (m_view_bobbing_state == 1) {
586 m_view_bobbing_state = 2;
587 m_view_bobbing_speed = 60;
591 void Camera::updateViewingRange()
593 f32 viewing_range = g_settings->getFloat("viewing_range");
595 // Ignore near_plane setting on all other platforms to prevent abuse
597 m_cameranode->setNearValue(rangelim(
598 g_settings->getFloat("near_plane"), 0.0f, 0.25f) * BS);
600 m_cameranode->setNearValue(0.1f * BS);
603 m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
604 if (m_draw_control.range_all) {
605 m_cameranode->setFarValue(100000.0);
608 m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
611 void Camera::setDigging(s32 button)
613 if (m_digging_button == -1)
614 m_digging_button = button;
617 void Camera::wield(const ItemStack &item)
619 if (item.name != m_wield_item_next.name ||
620 item.metadata != m_wield_item_next.metadata) {
621 m_wield_item_next = item;
622 if (m_wield_change_timer > 0)
623 m_wield_change_timer = -m_wield_change_timer;
624 else if (m_wield_change_timer == 0)
625 m_wield_change_timer = -0.001;
629 void Camera::drawWieldedTool(irr::core::matrix4* translation)
631 // Clear Z buffer so that the wielded tool stays in front of world geometry
632 m_wieldmgr->getVideoDriver()->clearBuffers(video::ECBF_DEPTH);
634 // Draw the wielded node (in a separate scene manager)
635 scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
636 cam->setAspectRatio(m_cameranode->getAspectRatio());
637 cam->setFOV(72.0*M_PI/180.0);
638 cam->setNearValue(10);
639 cam->setFarValue(1000);
640 if (translation != NULL)
642 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
643 irr::core::vector3df focusPoint = (cam->getTarget()
644 - cam->getAbsolutePosition()).setLength(1)
645 + cam->getAbsolutePosition();
647 irr::core::vector3df camera_pos =
648 (startMatrix * *translation).getTranslation();
649 cam->setPosition(camera_pos);
650 cam->setTarget(focusPoint);
652 m_wieldmgr->drawAll();
655 void Camera::drawNametags()
657 core::matrix4 trans = m_cameranode->getProjectionMatrix();
658 trans *= m_cameranode->getViewMatrix();
660 gui::IGUIFont *font = g_fontengine->getFont();
661 video::IVideoDriver *driver = RenderingEngine::get_video_driver();
662 v2u32 screensize = driver->getScreenSize();
664 for (const Nametag *nametag : m_nametags) {
665 // Nametags are hidden in GenericCAO::updateNametag()
667 v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
668 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
669 trans.multiplyWith1x4Matrix(transformed_pos);
670 if (transformed_pos[3] > 0) {
671 std::wstring nametag_colorless =
672 unescape_translate(utf8_to_wide(nametag->text));
673 core::dimension2d<u32> textsize = font->getDimension(
674 nametag_colorless.c_str());
675 f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
676 core::reciprocal(transformed_pos[3]);
678 screen_pos.X = screensize.X *
679 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
680 screen_pos.Y = screensize.Y *
681 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
682 core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
683 core::rect<s32> bg_size(-2, 0, std::max(textsize.Width+2, (u32) nametag->images_dim.Width), textsize.Height + nametag->images_dim.Height);
685 auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
686 if (bgcolor.getAlpha() != 0)
687 driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
690 translate_string(utf8_to_wide(nametag->text)).c_str(),
691 size + screen_pos, nametag->textcolor);
693 v2s32 image_pos(screen_pos);
694 image_pos.Y += textsize.Height;
696 const video::SColor color(255, 255, 255, 255);
697 const video::SColor colors[] = {color, color, color, color};
699 for (video::ITexture *texture : nametag->images) {
700 core::dimension2di imgsize(texture->getOriginalSize());
701 core::rect<s32> rect(core::position2d<s32>(0, 0), imgsize);
702 draw2DImageFilterScaled(driver, texture, rect + image_pos, rect, NULL, colors, true);
703 image_pos += core::dimension2di(imgsize.Width, 0);
709 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
710 const std::string &text, video::SColor textcolor,
711 Optional<video::SColor> bgcolor, const v3f &pos,
712 const std::vector<std::string> &images)
714 Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos, m_client->tsrc(), images);
715 m_nametags.push_back(nametag);
719 void Camera::removeNametag(Nametag *nametag)
721 m_nametags.remove(nametag);