Tooltips: Unify the tooltip[] and list[] description tooltip display functions (...
[oweals/minetest.git] / src / camera.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "camera.h"
21 #include "debug.h"
22 #include "client.h"
23 #include "map.h"
24 #include "clientmap.h"     // MapDrawControl
25 #include "player.h"
26 #include <cmath>
27 #include "settings.h"
28 #include "wieldmesh.h"
29 #include "noise.h"         // easeCurve
30 #include "sound.h"
31 #include "event.h"
32 #include "profiler.h"
33 #include "util/numeric.h"
34 #include "constants.h"
35 #include "fontengine.h"
36 #include "script/scripting_client.h"
37
38 #define CAMERA_OFFSET_STEP 200
39
40 #include "nodedef.h"
41
42 Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
43                 Client *client):
44         m_playernode(NULL),
45         m_headnode(NULL),
46         m_cameranode(NULL),
47
48         m_wieldmgr(NULL),
49         m_wieldnode(NULL),
50
51         m_draw_control(draw_control),
52         m_client(client),
53
54         m_camera_position(0,0,0),
55         m_camera_direction(0,0,0),
56         m_camera_offset(0,0,0),
57
58         m_aspect(1.0),
59         m_fov_x(1.0),
60         m_fov_y(1.0),
61
62         m_view_bobbing_anim(0),
63         m_view_bobbing_state(0),
64         m_view_bobbing_speed(0),
65         m_view_bobbing_fall(0),
66
67         m_digging_anim(0),
68         m_digging_button(-1),
69
70         m_wield_change_timer(0.125),
71         m_wield_item_next(),
72
73         m_camera_mode(CAMERA_MODE_FIRST)
74 {
75         //dstream<<FUNCTION_NAME<<std::endl;
76
77         m_driver = smgr->getVideoDriver();
78         // note: making the camera node a child of the player node
79         // would lead to unexpected behaviour, so we don't do that.
80         m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
81         m_headnode = smgr->addEmptySceneNode(m_playernode);
82         m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
83         m_cameranode->bindTargetAndRotation(true);
84
85         // This needs to be in its own scene manager. It is drawn after
86         // all other 3D scene nodes and before the GUI.
87         m_wieldmgr = smgr->createNewSceneManager();
88         m_wieldmgr->addCameraSceneNode();
89         m_wieldnode = new WieldMeshSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr, -1, false);
90         m_wieldnode->setItem(ItemStack(), m_client);
91         m_wieldnode->drop(); // m_wieldmgr grabbed it
92
93         /* TODO: Add a callback function so these can be updated when a setting
94          *       changes.  At this point in time it doesn't matter (e.g. /set
95          *       is documented to change server settings only)
96          *
97          * TODO: Local caching of settings is not optimal and should at some stage
98          *       be updated to use a global settings object for getting thse values
99          *       (as opposed to the this local caching). This can be addressed in
100          *       a later release.
101          */
102         m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
103         m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
104         m_cache_fov                 = g_settings->getFloat("fov");
105         m_cache_zoom_fov            = g_settings->getFloat("zoom_fov");
106         m_nametags.clear();
107 }
108
109 Camera::~Camera()
110 {
111         m_wieldmgr->drop();
112 }
113
114 bool Camera::successfullyCreated(std::string &error_message)
115 {
116         if (!m_playernode) {
117                 error_message = "Failed to create the player scene node";
118         } else if (!m_headnode) {
119                 error_message = "Failed to create the head scene node";
120         } else if (!m_cameranode) {
121                 error_message = "Failed to create the camera scene node";
122         } else if (!m_wieldmgr) {
123                 error_message = "Failed to create the wielded item scene manager";
124         } else if (!m_wieldnode) {
125                 error_message = "Failed to create the wielded item scene node";
126         } else {
127                 error_message.clear();
128         }
129         
130         if (g_settings->getBool("enable_client_modding")) {
131                 m_client->getScript()->on_camera_ready(this);
132         }
133         return error_message.empty();
134 }
135
136 // Returns the fractional part of x
137 inline f32 my_modf(f32 x)
138 {
139         double dummy;
140         return modf(x, &dummy);
141 }
142
143 void Camera::step(f32 dtime)
144 {
145         if(m_view_bobbing_fall > 0)
146         {
147                 m_view_bobbing_fall -= 3 * dtime;
148                 if(m_view_bobbing_fall <= 0)
149                         m_view_bobbing_fall = -1; // Mark the effect as finished
150         }
151
152         bool was_under_zero = m_wield_change_timer < 0;
153         m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
154
155         if (m_wield_change_timer >= 0 && was_under_zero)
156                 m_wieldnode->setItem(m_wield_item_next, m_client);
157
158         if (m_view_bobbing_state != 0)
159         {
160                 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
161                 f32 offset = dtime * m_view_bobbing_speed * 0.030;
162                 if (m_view_bobbing_state == 2) {
163                         // Animation is getting turned off
164                         if (m_view_bobbing_anim < 0.25) {
165                                 m_view_bobbing_anim -= offset;
166                         } else if (m_view_bobbing_anim > 0.75) {
167                                 m_view_bobbing_anim += offset;
168                         }
169
170                         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;
174                         } else {
175                                 m_view_bobbing_anim -= offset;
176                                 if (m_view_bobbing_anim < 0.5)
177                                         m_view_bobbing_anim = 0.5;
178                         }
179
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;
184                         }
185                 }
186                 else {
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));
192                         if(step) {
193                                 MtEvent *e = new SimpleTriggerEvent("ViewBobbingStep");
194                                 m_client->event()->put(e);
195                         }
196                 }
197         }
198
199         if (m_digging_button != -1)
200         {
201                 f32 offset = dtime * 3.5;
202                 float m_digging_anim_was = m_digging_anim;
203                 m_digging_anim += offset;
204                 if (m_digging_anim >= 1)
205                 {
206                         m_digging_anim = 0;
207                         m_digging_button = -1;
208                 }
209                 float lim = 0.15;
210                 if(m_digging_anim_was < lim && m_digging_anim >= lim)
211                 {
212                         if(m_digging_button == 0)
213                         {
214                                 MtEvent *e = new SimpleTriggerEvent("CameraPunchLeft");
215                                 m_client->event()->put(e);
216                         } else if(m_digging_button == 1) {
217                                 MtEvent *e = new SimpleTriggerEvent("CameraPunchRight");
218                                 m_client->event()->put(e);
219                         }
220                 }
221         }
222 }
223
224 void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
225                 f32 tool_reload_ratio, ClientEnvironment &c_env)
226 {
227         // Get player position
228         // Smooth the movement when walking up stairs
229         v3f old_player_position = m_playernode->getPosition();
230         v3f player_position = player->getPosition();
231         if (player->isAttached && player->parent)
232                 player_position = player->parent->getPosition();
233         //if(player->touching_ground && player_position.Y > old_player_position.Y)
234         if(player->touching_ground &&
235                         player_position.Y > old_player_position.Y)
236         {
237                 f32 oldy = old_player_position.Y;
238                 f32 newy = player_position.Y;
239                 f32 t = exp(-23*frametime);
240                 player_position.Y = oldy * t + newy * (1-t);
241         }
242
243         // Set player node transformation
244         m_playernode->setPosition(player_position);
245         m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
246         m_playernode->updateAbsolutePosition();
247
248         // Get camera tilt timer (hurt animation)
249         float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
250
251         // Fall bobbing animation
252         float fall_bobbing = 0;
253         if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
254         {
255                 if(m_view_bobbing_fall == -1) // Effect took place and has finished
256                         player->camera_impact = m_view_bobbing_fall = 0;
257                 else if(m_view_bobbing_fall == 0) // Initialize effect
258                         m_view_bobbing_fall = 1;
259
260                 // Convert 0 -> 1 to 0 -> 1 -> 0
261                 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
262                 // Smoothen and invert the above
263                 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
264                 // Amplify according to the intensity of the impact
265                 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
266
267                 fall_bobbing *= m_cache_fall_bobbing_amount;
268         }
269
270         // Calculate players eye offset for different camera modes
271         v3f PlayerEyeOffset = player->getEyeOffset();
272         if (m_camera_mode == CAMERA_MODE_FIRST)
273                 PlayerEyeOffset += player->eye_offset_first;
274         else
275                 PlayerEyeOffset += player->eye_offset_third;
276
277         // Set head node transformation
278         m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
279         m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
280         m_headnode->updateAbsolutePosition();
281
282         // Compute relative camera position and target
283         v3f rel_cam_pos = v3f(0,0,0);
284         v3f rel_cam_target = v3f(0,0,1);
285         v3f rel_cam_up = v3f(0,1,0);
286
287         if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
288                 m_camera_mode < CAMERA_MODE_THIRD) {
289                 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
290                 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
291
292                 #if 1
293                 f32 bobknob = 1.2;
294                 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
295                 //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
296
297                 v3f bobvec = v3f(
298                         0.3 * bobdir * sin(bobfrac * M_PI),
299                         -0.28 * bobtmp * bobtmp,
300                         0.);
301
302                 //rel_cam_pos += 0.2 * bobvec;
303                 //rel_cam_target += 0.03 * bobvec;
304                 //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
305                 float f = 1.0;
306                 f *= m_cache_view_bobbing_amount;
307                 rel_cam_pos += bobvec * f;
308                 //rel_cam_target += 0.995 * bobvec * f;
309                 rel_cam_target += bobvec * f;
310                 rel_cam_target.Z -= 0.005 * bobvec.Z * f;
311                 //rel_cam_target.X -= 0.005 * bobvec.X * f;
312                 //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
313                 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
314                 #else
315                 f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
316                 f32 angle_rad = angle_deg * M_PI / 180;
317                 f32 r = 0.05;
318                 v3f off = v3f(
319                         r * sin(angle_rad),
320                         r * (cos(angle_rad) - 1),
321                         0);
322                 rel_cam_pos += off;
323                 //rel_cam_target += off;
324                 rel_cam_up.rotateXYBy(angle_deg);
325                 #endif
326
327         }
328
329         // Compute absolute camera position and target
330         m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
331         m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
332
333         v3f abs_cam_up;
334         m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
335
336         // Seperate camera position for calculation
337         v3f my_cp = m_camera_position;
338
339         // Reposition the camera for third person view
340         if (m_camera_mode > CAMERA_MODE_FIRST)
341         {
342                 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
343                         m_camera_direction *= -1;
344
345                 my_cp.Y += 2;
346
347                 // Calculate new position
348                 bool abort = false;
349                 for (int i = BS; i <= BS*2.75; i++)
350                 {
351                         my_cp.X = m_camera_position.X + m_camera_direction.X*-i;
352                         my_cp.Z = m_camera_position.Z + m_camera_direction.Z*-i;
353                         if (i > 12)
354                                 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y*-i);
355
356                         // Prevent camera positioned inside nodes
357                         INodeDefManager *nodemgr = m_client->ndef();
358                         MapNode n = c_env.getClientMap().getNodeNoEx(floatToInt(my_cp, BS));
359                         const ContentFeatures& features = nodemgr->get(n);
360                         if(features.walkable)
361                         {
362                                 my_cp.X += m_camera_direction.X*-1*-BS/2;
363                                 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
364                                 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
365                                 abort = true;
366                                 break;
367                         }
368                 }
369
370                 // If node blocks camera position don't move y to heigh
371                 if (abort && my_cp.Y > player_position.Y+BS*2)
372                         my_cp.Y = player_position.Y+BS*2;
373         }
374
375         // Update offset if too far away from the center of the map
376         m_camera_offset.X += CAMERA_OFFSET_STEP*
377                         (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
378         m_camera_offset.Y += CAMERA_OFFSET_STEP*
379                         (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
380         m_camera_offset.Z += CAMERA_OFFSET_STEP*
381                         (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
382
383         // Set camera node transformation
384         m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
385         m_cameranode->setUpVector(abs_cam_up);
386         // *100.0 helps in large map coordinates
387         m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
388
389         // update the camera position in front-view mode to render blocks behind player
390         if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
391                 m_camera_position = my_cp;
392
393         // Get FOV
394         f32 fov_degrees;
395         if (player->getPlayerControl().zoom && m_client->checkLocalPrivilege("zoom")) {
396                 fov_degrees = m_cache_zoom_fov;
397         } else {
398                 fov_degrees = m_cache_fov;
399         }
400         fov_degrees = rangelim(fov_degrees, 7.0, 160.0);
401
402         // FOV and aspect ratio
403         m_aspect = (f32) porting::getWindowSize().X / (f32) porting::getWindowSize().Y;
404         m_fov_y = fov_degrees * M_PI / 180.0;
405         // Increase vertical FOV on lower aspect ratios (<16:10)
406         m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
407         m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
408         m_cameranode->setAspectRatio(m_aspect);
409         m_cameranode->setFOV(m_fov_y);
410
411         float wieldmesh_offset_Y = -35 + player->getPitch() * 0.05;
412         wieldmesh_offset_Y = rangelim(wieldmesh_offset_Y, -52, -32);
413
414         // Position the wielded item
415         //v3f wield_position = v3f(45, -35, 65);
416         v3f wield_position = v3f(55, wieldmesh_offset_Y, 65);
417         //v3f wield_rotation = v3f(-100, 120, -100);
418         v3f wield_rotation = v3f(-100, 120, -100);
419         wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
420         if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
421         {
422                 f32 frac = 1.0;
423                 if(m_digging_anim > 0.5)
424                         frac = 2.0 * (m_digging_anim - 0.5);
425                 // This value starts from 1 and settles to 0
426                 f32 ratiothing = pow((1.0f - tool_reload_ratio), 0.5f);
427                 //f32 ratiothing2 = pow(ratiothing, 0.5f);
428                 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
429                 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
430                 //wield_position.Z += frac * 5.0 * ratiothing2;
431                 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
432                 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
433                 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
434                 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
435         }
436         if (m_digging_button != -1)
437         {
438                 f32 digfrac = m_digging_anim;
439                 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
440                 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
441                 wield_position.Z += 25 * 0.5;
442
443                 // Euler angles are PURE EVIL, so why not use quaternions?
444                 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
445                 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
446                 core::quaternion quat_slerp;
447                 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
448                 quat_slerp.toEuler(wield_rotation);
449                 wield_rotation *= core::RADTODEG;
450         } else {
451                 f32 bobfrac = my_modf(m_view_bobbing_anim);
452                 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
453                 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
454         }
455         m_wieldnode->setPosition(wield_position);
456         m_wieldnode->setRotation(wield_rotation);
457
458         m_wieldnode->setColor(player->light_color);
459
460         // Set render distance
461         updateViewingRange();
462
463         // If the player is walking, swimming, or climbing,
464         // view bobbing is enabled and free_move is off,
465         // start (or continue) the view bobbing animation.
466         v3f speed = player->getSpeed();
467         const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
468         const bool movement_Y = fabs(speed.Y) > BS;
469
470         const bool walking = movement_XZ && player->touching_ground;
471         const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
472         const bool climbing = movement_Y && player->is_climbing;
473         if ((walking || swimming || climbing) &&
474                         (!g_settings->getBool("free_move") || !m_client->checkLocalPrivilege("fly"))) {
475                 // Start animation
476                 m_view_bobbing_state = 1;
477                 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
478         }
479         else if (m_view_bobbing_state == 1)
480         {
481                 // Stop animation
482                 m_view_bobbing_state = 2;
483                 m_view_bobbing_speed = 60;
484         }
485 }
486
487 void Camera::updateViewingRange()
488 {
489         f32 viewing_range = g_settings->getFloat("viewing_range");
490         m_draw_control.wanted_range = viewing_range;
491         if (m_draw_control.range_all) {
492                 m_cameranode->setFarValue(100000.0);
493                 return;
494         }
495         m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
496 }
497
498 void Camera::setDigging(s32 button)
499 {
500         if (m_digging_button == -1)
501                 m_digging_button = button;
502 }
503
504 void Camera::wield(const ItemStack &item)
505 {
506         if (item.name != m_wield_item_next.name ||
507                         item.metadata != m_wield_item_next.metadata) {
508                 m_wield_item_next = item;
509                 if (m_wield_change_timer > 0)
510                         m_wield_change_timer = -m_wield_change_timer;
511                 else if (m_wield_change_timer == 0)
512                         m_wield_change_timer = -0.001;
513         }
514 }
515
516 void Camera::drawWieldedTool(irr::core::matrix4* translation)
517 {
518         // Clear Z buffer so that the wielded tool stay in front of world geometry
519         m_wieldmgr->getVideoDriver()->clearZBuffer();
520
521         // Draw the wielded node (in a separate scene manager)
522         scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
523         cam->setAspectRatio(m_cameranode->getAspectRatio());
524         cam->setFOV(72.0*M_PI/180.0);
525         cam->setNearValue(10);
526         cam->setFarValue(1000);
527         if (translation != NULL)
528         {
529                 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
530                 irr::core::vector3df focusPoint = (cam->getTarget()
531                                 - cam->getAbsolutePosition()).setLength(1)
532                                 + cam->getAbsolutePosition();
533
534                 irr::core::vector3df camera_pos =
535                                 (startMatrix * *translation).getTranslation();
536                 cam->setPosition(camera_pos);
537                 cam->setTarget(focusPoint);
538         }
539         m_wieldmgr->drawAll();
540 }
541
542 void Camera::drawNametags()
543 {
544         core::matrix4 trans = m_cameranode->getProjectionMatrix();
545         trans *= m_cameranode->getViewMatrix();
546
547         for (std::list<Nametag *>::const_iterator
548                         i = m_nametags.begin();
549                         i != m_nametags.end(); ++i) {
550                 Nametag *nametag = *i;
551                 if (nametag->nametag_color.getAlpha() == 0) {
552                         // Enforce hiding nametag,
553                         // because if freetype is enabled, a grey
554                         // shadow can remain.
555                         continue;
556                 }
557                 v3f pos = nametag->parent_node->getAbsolutePosition() + v3f(0.0, 1.1 * BS, 0.0);
558                 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
559                 trans.multiplyWith1x4Matrix(transformed_pos);
560                 if (transformed_pos[3] > 0) {
561                         std::string nametag_colorless = unescape_enriched(nametag->nametag_text);
562                         core::dimension2d<u32> textsize =
563                                 g_fontengine->getFont()->getDimension(
564                                 utf8_to_wide(nametag_colorless).c_str());
565                         f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
566                                 core::reciprocal(transformed_pos[3]);
567                         v2u32 screensize = m_driver->getScreenSize();
568                         v2s32 screen_pos;
569                         screen_pos.X = screensize.X *
570                                 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
571                         screen_pos.Y = screensize.Y *
572                                 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
573                         core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
574                         g_fontengine->getFont()->draw(utf8_to_wide(nametag->nametag_text).c_str(),
575                                         size + screen_pos, nametag->nametag_color);
576                 }
577         }
578 }
579
580 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
581                 std::string nametag_text, video::SColor nametag_color)
582 {
583         Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color);
584         m_nametags.push_back(nametag);
585         return nametag;
586 }
587
588 void Camera::removeNametag(Nametag *nametag)
589 {
590         m_nametags.remove(nametag);
591         delete nametag;
592 }