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