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.
24 #include "clientmap.h" // MapDrawControl
27 #include "client/renderingengine.h"
29 #include "wieldmesh.h"
30 #include "noise.h" // easeCurve
34 #include "util/numeric.h"
35 #include "constants.h"
36 #include "fontengine.h"
37 #include "script/scripting_client.h"
39 #define CAMERA_OFFSET_STEP 200
40 #define WIELDMESH_OFFSET_X 55.0f
41 #define WIELDMESH_OFFSET_Y -35.0f
43 Camera::Camera(MapDrawControl &draw_control, Client *client):
44 m_draw_control(draw_control),
47 scene::ISceneManager *smgr = RenderingEngine::get_scene_manager();
48 // note: making the camera node a child of the player node
49 // would lead to unexpected behaviour, so we don't do that.
50 m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
51 m_headnode = smgr->addEmptySceneNode(m_playernode);
52 m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
53 m_cameranode->bindTargetAndRotation(true);
55 // This needs to be in its own scene manager. It is drawn after
56 // all other 3D scene nodes and before the GUI.
57 m_wieldmgr = smgr->createNewSceneManager();
58 m_wieldmgr->addCameraSceneNode();
59 m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
60 m_wieldnode->setItem(ItemStack(), m_client);
61 m_wieldnode->drop(); // m_wieldmgr grabbed it
63 /* TODO: Add a callback function so these can be updated when a setting
64 * changes. At this point in time it doesn't matter (e.g. /set
65 * is documented to change server settings only)
67 * TODO: Local caching of settings is not optimal and should at some stage
68 * be updated to use a global settings object for getting thse values
69 * (as opposed to the this local caching). This can be addressed in
72 m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
73 m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
74 // 45 degrees is the lowest FOV that doesn't cause the server to treat this
75 // as a zoom FOV and load world beyond the set server limits.
76 m_cache_fov = std::fmax(g_settings->getFloat("fov"), 45.0f);
77 m_arm_inertia = g_settings->getBool("arm_inertia");
86 bool Camera::successfullyCreated(std::string &error_message)
89 error_message = "Failed to create the player scene node";
90 } else if (!m_headnode) {
91 error_message = "Failed to create the head scene node";
92 } else if (!m_cameranode) {
93 error_message = "Failed to create the camera scene node";
94 } else if (!m_wieldmgr) {
95 error_message = "Failed to create the wielded item scene manager";
96 } else if (!m_wieldnode) {
97 error_message = "Failed to create the wielded item scene node";
99 error_message.clear();
102 if (m_client->modsLoaded())
103 m_client->getScript()->on_camera_ready(this);
105 return error_message.empty();
108 // Returns the fractional part of x
109 inline f32 my_modf(f32 x)
112 return modf(x, &dummy);
115 void Camera::step(f32 dtime)
117 if(m_view_bobbing_fall > 0)
119 m_view_bobbing_fall -= 3 * dtime;
120 if(m_view_bobbing_fall <= 0)
121 m_view_bobbing_fall = -1; // Mark the effect as finished
124 bool was_under_zero = m_wield_change_timer < 0;
125 m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
127 if (m_wield_change_timer >= 0 && was_under_zero)
128 m_wieldnode->setItem(m_wield_item_next, m_client);
130 if (m_view_bobbing_state != 0)
132 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
133 f32 offset = dtime * m_view_bobbing_speed * 0.030;
134 if (m_view_bobbing_state == 2) {
135 // Animation is getting turned off
136 if (m_view_bobbing_anim < 0.25) {
137 m_view_bobbing_anim -= offset;
138 } else if (m_view_bobbing_anim > 0.75) {
139 m_view_bobbing_anim += offset;
142 if (m_view_bobbing_anim < 0.5) {
143 m_view_bobbing_anim += offset;
144 if (m_view_bobbing_anim > 0.5)
145 m_view_bobbing_anim = 0.5;
147 m_view_bobbing_anim -= offset;
148 if (m_view_bobbing_anim < 0.5)
149 m_view_bobbing_anim = 0.5;
152 if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
153 fabs(m_view_bobbing_anim - 0.5) < 0.01) {
154 m_view_bobbing_anim = 0;
155 m_view_bobbing_state = 0;
159 float was = m_view_bobbing_anim;
160 m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
161 bool step = (was == 0 ||
162 (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
163 (was > 0.5f && m_view_bobbing_anim <= 0.5f));
165 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
170 if (m_digging_button != -1) {
171 f32 offset = dtime * 3.5f;
172 float m_digging_anim_was = m_digging_anim;
173 m_digging_anim += offset;
174 if (m_digging_anim >= 1)
177 m_digging_button = -1;
180 if(m_digging_anim_was < lim && m_digging_anim >= lim)
182 if (m_digging_button == 0) {
183 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
184 } else if(m_digging_button == 1) {
185 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
191 static inline v2f dir(const v2f &pos_dist)
193 f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
194 f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
196 f32 x_abs = std::fabs(x);
197 f32 y_abs = std::fabs(y);
199 if (x_abs >= y_abs) {
204 if (y_abs >= x_abs) {
209 return v2f(std::fabs(x), std::fabs(y));
212 void Camera::addArmInertia(f32 player_yaw)
214 m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
215 -100.0f, 100.0f) / 0.016f) * 0.01f;
216 m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
217 f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
218 f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
220 if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
222 The arm moves relative to the camera speed,
223 with an acceleration factor.
226 if (m_cam_vel.X > 1.0f) {
227 if (m_cam_vel.X > m_cam_vel_old.X)
228 m_cam_vel_old.X = m_cam_vel.X;
230 f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
231 m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
233 if (m_last_cam_pos.X != player_yaw)
234 m_last_cam_pos.X = player_yaw;
236 m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
237 WIELDMESH_OFFSET_X - 7.0f, WIELDMESH_OFFSET_X + 7.0f);
240 if (m_cam_vel.Y > 1.0f) {
241 if (m_cam_vel.Y > m_cam_vel_old.Y)
242 m_cam_vel_old.Y = m_cam_vel.Y;
244 f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
245 m_wieldmesh_offset.Y +=
246 m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
248 if (m_last_cam_pos.Y != m_camera_direction.Y)
249 m_last_cam_pos.Y = m_camera_direction.Y;
251 m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
252 WIELDMESH_OFFSET_Y - 10.0f, WIELDMESH_OFFSET_Y + 5.0f);
255 m_arm_dir = dir(m_wieldmesh_offset);
258 Now the arm gets back to its default position when the camera stops,
259 following a vector, with a smooth deceleration factor.
262 f32 dec_X = 0.12f * (m_cam_vel_old.X * (1.0f +
263 (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
265 f32 dec_Y = 0.06f * (m_cam_vel_old.Y * (1.0f +
266 (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
269 m_cam_vel_old.X = 0.0f;
271 m_wieldmesh_offset.X -=
272 m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
275 m_cam_vel_old.Y = 0.0f;
277 m_wieldmesh_offset.Y -=
278 m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
282 void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_reload_ratio)
284 // Get player position
285 // Smooth the movement when walking up stairs
286 v3f old_player_position = m_playernode->getPosition();
287 v3f player_position = player->getPosition();
289 // This is worse than `LocalPlayer::getPosition()` but
290 // mods expect the player head to be at the parent's position
292 if (player->getParent())
293 player_position = player->getParent()->getPosition();
295 if(player->touching_ground &&
296 player_position.Y > old_player_position.Y)
298 f32 oldy = old_player_position.Y;
299 f32 newy = player_position.Y;
300 f32 t = std::exp(-23 * frametime);
301 player_position.Y = oldy * t + newy * (1-t);
304 // Set player node transformation
305 m_playernode->setPosition(player_position);
306 m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
307 m_playernode->updateAbsolutePosition();
309 // Get camera tilt timer (hurt animation)
310 float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
312 // Fall bobbing animation
313 float fall_bobbing = 0;
314 if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
316 if(m_view_bobbing_fall == -1) // Effect took place and has finished
317 player->camera_impact = m_view_bobbing_fall = 0;
318 else if(m_view_bobbing_fall == 0) // Initialize effect
319 m_view_bobbing_fall = 1;
321 // Convert 0 -> 1 to 0 -> 1 -> 0
322 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
323 // Smoothen and invert the above
324 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
325 // Amplify according to the intensity of the impact
326 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
328 fall_bobbing *= m_cache_fall_bobbing_amount;
331 // Calculate players eye offset for different camera modes
332 v3f PlayerEyeOffset = player->getEyeOffset();
333 if (m_camera_mode == CAMERA_MODE_FIRST)
334 PlayerEyeOffset += player->eye_offset_first;
336 PlayerEyeOffset += player->eye_offset_third;
338 // Set head node transformation
339 m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
340 m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
341 m_headnode->updateAbsolutePosition();
343 // Compute relative camera position and target
344 v3f rel_cam_pos = v3f(0,0,0);
345 v3f rel_cam_target = v3f(0,0,1);
346 v3f rel_cam_up = v3f(0,1,0);
348 if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
349 m_camera_mode < CAMERA_MODE_THIRD) {
350 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
351 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
355 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
356 //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
359 0.3 * bobdir * sin(bobfrac * M_PI),
360 -0.28 * bobtmp * bobtmp,
363 //rel_cam_pos += 0.2 * bobvec;
364 //rel_cam_target += 0.03 * bobvec;
365 //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
367 f *= m_cache_view_bobbing_amount;
368 rel_cam_pos += bobvec * f;
369 //rel_cam_target += 0.995 * bobvec * f;
370 rel_cam_target += bobvec * f;
371 rel_cam_target.Z -= 0.005 * bobvec.Z * f;
372 //rel_cam_target.X -= 0.005 * bobvec.X * f;
373 //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
374 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
376 f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
377 f32 angle_rad = angle_deg * M_PI / 180;
381 r * (cos(angle_rad) - 1),
384 //rel_cam_target += off;
385 rel_cam_up.rotateXYBy(angle_deg);
390 // Compute absolute camera position and target
391 m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
392 m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
395 m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
397 // Seperate camera position for calculation
398 v3f my_cp = m_camera_position;
400 // Reposition the camera for third person view
401 if (m_camera_mode > CAMERA_MODE_FIRST)
403 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
404 m_camera_direction *= -1;
408 // Calculate new position
410 for (int i = BS; i <= BS * 2.75; i++) {
411 my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
412 my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
414 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
416 // Prevent camera positioned inside nodes
417 const NodeDefManager *nodemgr = m_client->ndef();
418 MapNode n = m_client->getEnv().getClientMap()
419 .getNode(floatToInt(my_cp, BS));
421 const ContentFeatures& features = nodemgr->get(n);
422 if (features.walkable) {
423 my_cp.X += m_camera_direction.X*-1*-BS/2;
424 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
425 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
431 // If node blocks camera position don't move y to heigh
432 if (abort && my_cp.Y > player_position.Y+BS*2)
433 my_cp.Y = player_position.Y+BS*2;
436 // Update offset if too far away from the center of the map
437 m_camera_offset.X += CAMERA_OFFSET_STEP*
438 (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
439 m_camera_offset.Y += CAMERA_OFFSET_STEP*
440 (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
441 m_camera_offset.Z += CAMERA_OFFSET_STEP*
442 (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
444 // Set camera node transformation
445 m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
446 m_cameranode->setUpVector(abs_cam_up);
447 // *100.0 helps in large map coordinates
448 m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
450 // update the camera position in third-person mode to render blocks behind player
451 // and correctly apply liquid post FX.
452 if (m_camera_mode != CAMERA_MODE_FIRST)
453 m_camera_position = my_cp;
456 * Apply server-sent FOV. If server doesn't enforce FOV,
457 * check for zoom and set to zoom FOV.
458 * Otherwise, default to m_cache_fov
462 PlayerFovSpec fov_spec = player->getFov();
463 if (fov_spec.fov > 0.0f) {
464 // If server-sent FOV is a multiplier, multiply
465 // it with m_cache_fov instead of overriding
466 if (fov_spec.is_multiplier)
467 fov_degrees = m_cache_fov * fov_spec.fov;
469 fov_degrees = fov_spec.fov;
470 } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
471 // Player requests zoom, apply zoom FOV
472 fov_degrees = player->getZoomFOV();
474 // Set to client's selected FOV
475 fov_degrees = m_cache_fov;
477 fov_degrees = rangelim(fov_degrees, 1.0f, 160.0f);
479 // FOV and aspect ratio
480 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
481 m_aspect = (f32) window_size.X / (f32) window_size.Y;
482 m_fov_y = fov_degrees * M_PI / 180.0;
483 // Increase vertical FOV on lower aspect ratios (<16:10)
484 m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
485 m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
486 m_cameranode->setAspectRatio(m_aspect);
487 m_cameranode->setFOV(m_fov_y);
490 addArmInertia(player->getYaw());
492 // Position the wielded item
493 //v3f wield_position = v3f(45, -35, 65);
494 v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
495 //v3f wield_rotation = v3f(-100, 120, -100);
496 v3f wield_rotation = v3f(-100, 120, -100);
497 wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
498 if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
501 if(m_digging_anim > 0.5)
502 frac = 2.0 * (m_digging_anim - 0.5);
503 // This value starts from 1 and settles to 0
504 f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
505 //f32 ratiothing2 = pow(ratiothing, 0.5f);
506 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
507 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
508 //wield_position.Z += frac * 5.0 * ratiothing2;
509 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
510 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
511 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
512 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
514 if (m_digging_button != -1)
516 f32 digfrac = m_digging_anim;
517 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
518 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
519 wield_position.Z += 25 * 0.5;
521 // Euler angles are PURE EVIL, so why not use quaternions?
522 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
523 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
524 core::quaternion quat_slerp;
525 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
526 quat_slerp.toEuler(wield_rotation);
527 wield_rotation *= core::RADTODEG;
529 f32 bobfrac = my_modf(m_view_bobbing_anim);
530 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
531 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
533 m_wieldnode->setPosition(wield_position);
534 m_wieldnode->setRotation(wield_rotation);
536 m_wieldnode->setColor(player->light_color);
538 // Set render distance
539 updateViewingRange();
541 // If the player is walking, swimming, or climbing,
542 // view bobbing is enabled and free_move is off,
543 // start (or continue) the view bobbing animation.
544 const v3f &speed = player->getSpeed();
545 const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
546 const bool movement_Y = fabs(speed.Y) > BS;
548 const bool walking = movement_XZ && player->touching_ground;
549 const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
550 const bool climbing = movement_Y && player->is_climbing;
551 if ((walking || swimming || climbing) &&
552 (!g_settings->getBool("free_move") || !m_client->checkLocalPrivilege("fly"))) {
554 m_view_bobbing_state = 1;
555 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
557 else if (m_view_bobbing_state == 1)
560 m_view_bobbing_state = 2;
561 m_view_bobbing_speed = 60;
565 void Camera::updateViewingRange()
567 f32 viewing_range = g_settings->getFloat("viewing_range");
568 f32 near_plane = g_settings->getFloat("near_plane");
570 m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
571 m_cameranode->setNearValue(rangelim(near_plane, 0.0f, 0.5f) * BS);
572 if (m_draw_control.range_all) {
573 m_cameranode->setFarValue(100000.0);
576 m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
579 void Camera::setDigging(s32 button)
581 if (m_digging_button == -1)
582 m_digging_button = button;
585 void Camera::wield(const ItemStack &item)
587 if (item.name != m_wield_item_next.name ||
588 item.metadata != m_wield_item_next.metadata) {
589 m_wield_item_next = item;
590 if (m_wield_change_timer > 0)
591 m_wield_change_timer = -m_wield_change_timer;
592 else if (m_wield_change_timer == 0)
593 m_wield_change_timer = -0.001;
597 void Camera::drawWieldedTool(irr::core::matrix4* translation)
599 // Clear Z buffer so that the wielded tool stay in front of world geometry
600 m_wieldmgr->getVideoDriver()->clearZBuffer();
602 // Draw the wielded node (in a separate scene manager)
603 scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
604 cam->setAspectRatio(m_cameranode->getAspectRatio());
605 cam->setFOV(72.0*M_PI/180.0);
606 cam->setNearValue(10);
607 cam->setFarValue(1000);
608 if (translation != NULL)
610 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
611 irr::core::vector3df focusPoint = (cam->getTarget()
612 - cam->getAbsolutePosition()).setLength(1)
613 + cam->getAbsolutePosition();
615 irr::core::vector3df camera_pos =
616 (startMatrix * *translation).getTranslation();
617 cam->setPosition(camera_pos);
618 cam->setTarget(focusPoint);
620 m_wieldmgr->drawAll();
623 void Camera::drawNametags()
625 core::matrix4 trans = m_cameranode->getProjectionMatrix();
626 trans *= m_cameranode->getViewMatrix();
628 for (std::list<Nametag *>::const_iterator
629 i = m_nametags.begin();
630 i != m_nametags.end(); ++i) {
631 Nametag *nametag = *i;
632 if (nametag->nametag_color.getAlpha() == 0) {
633 // Enforce hiding nametag,
634 // because if freetype is enabled, a grey
635 // shadow can remain.
638 v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->nametag_pos * BS;
639 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
640 trans.multiplyWith1x4Matrix(transformed_pos);
641 if (transformed_pos[3] > 0) {
642 std::wstring nametag_colorless =
643 unescape_translate(utf8_to_wide(nametag->nametag_text));
644 core::dimension2d<u32> textsize =
645 g_fontengine->getFont()->getDimension(
646 nametag_colorless.c_str());
647 f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
648 core::reciprocal(transformed_pos[3]);
649 v2u32 screensize = RenderingEngine::get_video_driver()->getScreenSize();
651 screen_pos.X = screensize.X *
652 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
653 screen_pos.Y = screensize.Y *
654 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
655 core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
656 g_fontengine->getFont()->draw(
657 translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
658 size + screen_pos, nametag->nametag_color);
663 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
664 const std::string &nametag_text, video::SColor nametag_color,
667 Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color, pos);
668 m_nametags.push_back(nametag);
672 void Camera::removeNametag(Nametag *nametag)
674 m_nametags.remove(nametag);