]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/camera.cpp
Semi-transparent background for nametags (#10152)
[dragonfireclient.git] / src / client / 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 "config.h"
24 #include "map.h"
25 #include "clientmap.h"     // MapDrawControl
26 #include "player.h"
27 #include <cmath>
28 #include "client/renderingengine.h"
29 #include "settings.h"
30 #include "wieldmesh.h"
31 #include "noise.h"         // easeCurve
32 #include "sound.h"
33 #include "mtevent.h"
34 #include "nodedef.h"
35 #include "util/numeric.h"
36 #include "constants.h"
37 #include "fontengine.h"
38 #include "script/scripting_client.h"
39
40 #define CAMERA_OFFSET_STEP 200
41 #define WIELDMESH_OFFSET_X 55.0f
42 #define WIELDMESH_OFFSET_Y -35.0f
43 #define WIELDMESH_AMPLITUDE_X 7.0f
44 #define WIELDMESH_AMPLITUDE_Y 10.0f
45
46 Camera::Camera(MapDrawControl &draw_control, Client *client):
47         m_draw_control(draw_control),
48         m_client(client)
49 {
50         scene::ISceneManager *smgr = RenderingEngine::get_scene_manager();
51         // note: making the camera node a child of the player node
52         // would lead to unexpected behaviour, so we don't do that.
53         m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
54         m_headnode = smgr->addEmptySceneNode(m_playernode);
55         m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
56         m_cameranode->bindTargetAndRotation(true);
57
58         // This needs to be in its own scene manager. It is drawn after
59         // all other 3D scene nodes and before the GUI.
60         m_wieldmgr = smgr->createNewSceneManager();
61         m_wieldmgr->addCameraSceneNode();
62         m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
63         m_wieldnode->setItem(ItemStack(), m_client);
64         m_wieldnode->drop(); // m_wieldmgr grabbed it
65
66         /* TODO: Add a callback function so these can be updated when a setting
67          *       changes.  At this point in time it doesn't matter (e.g. /set
68          *       is documented to change server settings only)
69          *
70          * TODO: Local caching of settings is not optimal and should at some stage
71          *       be updated to use a global settings object for getting thse values
72          *       (as opposed to the this local caching). This can be addressed in
73          *       a later release.
74          */
75         m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
76         m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
77         // 45 degrees is the lowest FOV that doesn't cause the server to treat this
78         // as a zoom FOV and load world beyond the set server limits.
79         m_cache_fov                 = std::fmax(g_settings->getFloat("fov"), 45.0f);
80         m_arm_inertia               = g_settings->getBool("arm_inertia");
81         m_nametags.clear();
82 }
83
84 Camera::~Camera()
85 {
86         m_wieldmgr->drop();
87 }
88
89 void Camera::notifyFovChange()
90 {
91         LocalPlayer *player = m_client->getEnv().getLocalPlayer();
92         assert(player);
93
94         PlayerFovSpec spec = player->getFov();
95
96         /*
97          * Update m_old_fov_degrees first - it serves as the starting point of the
98          * upcoming transition.
99          *
100          * If an FOV transition is already active, mark current FOV as the start of
101          * the new transition. If not, set it to the previous transition's target FOV.
102          */
103         if (m_fov_transition_active)
104                 m_old_fov_degrees = m_curr_fov_degrees;
105         else
106                 m_old_fov_degrees = m_server_sent_fov ? m_target_fov_degrees : m_cache_fov;
107
108         /*
109          * Update m_server_sent_fov next - it corresponds to the target FOV of the
110          * upcoming transition.
111          *
112          * Set it to m_cache_fov, if server-sent FOV is 0. Otherwise check if
113          * server-sent FOV is a multiplier, and multiply it with m_cache_fov instead
114          * of overriding.
115          */
116         if (spec.fov == 0.0f) {
117                 m_server_sent_fov = false;
118                 m_target_fov_degrees = m_cache_fov;
119         } else {
120                 m_server_sent_fov = true;
121                 m_target_fov_degrees = spec.is_multiplier ? m_cache_fov * spec.fov : spec.fov;
122         }
123
124         if (spec.transition_time > 0.0f)
125                 m_fov_transition_active = true;
126
127         // If FOV smooth transition is active, initialize required variables
128         if (m_fov_transition_active) {
129                 m_transition_time = spec.transition_time;
130                 m_fov_diff = m_target_fov_degrees - m_old_fov_degrees;
131         }
132 }
133
134 bool Camera::successfullyCreated(std::string &error_message)
135 {
136         if (!m_playernode) {
137                 error_message = "Failed to create the player scene node";
138         } else if (!m_headnode) {
139                 error_message = "Failed to create the head scene node";
140         } else if (!m_cameranode) {
141                 error_message = "Failed to create the camera scene node";
142         } else if (!m_wieldmgr) {
143                 error_message = "Failed to create the wielded item scene manager";
144         } else if (!m_wieldnode) {
145                 error_message = "Failed to create the wielded item scene node";
146         } else {
147                 error_message.clear();
148         }
149
150         if (m_client->modsLoaded())
151                 m_client->getScript()->on_camera_ready(this);
152
153         return error_message.empty();
154 }
155
156 // Returns the fractional part of x
157 inline f32 my_modf(f32 x)
158 {
159         double dummy;
160         return modf(x, &dummy);
161 }
162
163 void Camera::step(f32 dtime)
164 {
165         if(m_view_bobbing_fall > 0)
166         {
167                 m_view_bobbing_fall -= 3 * dtime;
168                 if(m_view_bobbing_fall <= 0)
169                         m_view_bobbing_fall = -1; // Mark the effect as finished
170         }
171
172         bool was_under_zero = m_wield_change_timer < 0;
173         m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
174
175         if (m_wield_change_timer >= 0 && was_under_zero)
176                 m_wieldnode->setItem(m_wield_item_next, m_client);
177
178         if (m_view_bobbing_state != 0)
179         {
180                 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
181                 f32 offset = dtime * m_view_bobbing_speed * 0.030;
182                 if (m_view_bobbing_state == 2) {
183                         // Animation is getting turned off
184                         if (m_view_bobbing_anim < 0.25) {
185                                 m_view_bobbing_anim -= offset;
186                         } else if (m_view_bobbing_anim > 0.75) {
187                                 m_view_bobbing_anim += offset;
188                         }
189
190                         if (m_view_bobbing_anim < 0.5) {
191                                 m_view_bobbing_anim += offset;
192                                 if (m_view_bobbing_anim > 0.5)
193                                         m_view_bobbing_anim = 0.5;
194                         } else {
195                                 m_view_bobbing_anim -= offset;
196                                 if (m_view_bobbing_anim < 0.5)
197                                         m_view_bobbing_anim = 0.5;
198                         }
199
200                         if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
201                                         fabs(m_view_bobbing_anim - 0.5) < 0.01) {
202                                 m_view_bobbing_anim = 0;
203                                 m_view_bobbing_state = 0;
204                         }
205                 }
206                 else {
207                         float was = m_view_bobbing_anim;
208                         m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
209                         bool step = (was == 0 ||
210                                         (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
211                                         (was > 0.5f && m_view_bobbing_anim <= 0.5f));
212                         if(step) {
213                                 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
214                         }
215                 }
216         }
217
218         if (m_digging_button != -1) {
219                 f32 offset = dtime * 3.5f;
220                 float m_digging_anim_was = m_digging_anim;
221                 m_digging_anim += offset;
222                 if (m_digging_anim >= 1)
223                 {
224                         m_digging_anim = 0;
225                         m_digging_button = -1;
226                 }
227                 float lim = 0.15;
228                 if(m_digging_anim_was < lim && m_digging_anim >= lim)
229                 {
230                         if (m_digging_button == 0) {
231                                 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
232                         } else if(m_digging_button == 1) {
233                                 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
234                         }
235                 }
236         }
237 }
238
239 static inline v2f dir(const v2f &pos_dist)
240 {
241         f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
242         f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
243
244         f32 x_abs = std::fabs(x);
245         f32 y_abs = std::fabs(y);
246
247         if (x_abs >= y_abs) {
248                 y *= (1.0f / x_abs);
249                 x /= x_abs;
250         }
251
252         if (y_abs >= x_abs) {
253                 x *= (1.0f / y_abs);
254                 y /= y_abs;
255         }
256
257         return v2f(std::fabs(x), std::fabs(y));
258 }
259
260 void Camera::addArmInertia(f32 player_yaw)
261 {
262         m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
263                 -100.0f, 100.0f) / 0.016f) * 0.01f;
264         m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
265         f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
266         f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
267
268         if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
269                 /*
270                     The arm moves relative to the camera speed,
271                     with an acceleration factor.
272                 */
273
274                 if (m_cam_vel.X > 1.0f) {
275                         if (m_cam_vel.X > m_cam_vel_old.X)
276                                 m_cam_vel_old.X = m_cam_vel.X;
277
278                         f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
279                         m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
280
281                         if (m_last_cam_pos.X != player_yaw)
282                                 m_last_cam_pos.X = player_yaw;
283
284                         m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
285                                 WIELDMESH_OFFSET_X - (WIELDMESH_AMPLITUDE_X * 0.5f),
286                                 WIELDMESH_OFFSET_X + (WIELDMESH_AMPLITUDE_X * 0.5f));
287                 }
288
289                 if (m_cam_vel.Y > 1.0f) {
290                         if (m_cam_vel.Y > m_cam_vel_old.Y)
291                                 m_cam_vel_old.Y = m_cam_vel.Y;
292
293                         f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
294                         m_wieldmesh_offset.Y +=
295                                 m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
296
297                         if (m_last_cam_pos.Y != m_camera_direction.Y)
298                                 m_last_cam_pos.Y = m_camera_direction.Y;
299
300                         m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
301                                 WIELDMESH_OFFSET_Y - (WIELDMESH_AMPLITUDE_Y * 0.5f),
302                                 WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
303                 }
304
305                 m_arm_dir = dir(m_wieldmesh_offset);
306         } else {
307                 /*
308                     Now the arm gets back to its default position when the camera stops,
309                     following a vector, with a smooth deceleration factor.
310                 */
311
312                 f32 dec_X = 0.35f * (std::min(15.0f, m_cam_vel_old.X) * (1.0f +
313                         (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
314
315                 f32 dec_Y = 0.25f * (std::min(15.0f, m_cam_vel_old.Y) * (1.0f +
316                         (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
317
318                 if (gap_X < 0.1f)
319                         m_cam_vel_old.X = 0.0f;
320
321                 m_wieldmesh_offset.X -=
322                         m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
323
324                 if (gap_Y < 0.1f)
325                         m_cam_vel_old.Y = 0.0f;
326
327                 m_wieldmesh_offset.Y -=
328                         m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
329         }
330 }
331
332 void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_reload_ratio)
333 {
334         // Get player position
335         // Smooth the movement when walking up stairs
336         v3f old_player_position = m_playernode->getPosition();
337         v3f player_position = player->getPosition();
338
339         // This is worse than `LocalPlayer::getPosition()` but
340         // mods expect the player head to be at the parent's position
341         // plus eye height.
342         if (player->getParent())
343                 player_position = player->getParent()->getPosition();
344
345         // Smooth the camera movement when the player instantly moves upward due to stepheight.
346         // To smooth the 'not touching_ground' stepheight, smoothing is necessary when jumping
347         // or swimming (for when moving from liquid to land).
348         // Disable smoothing if climbing or flying, to avoid upwards offset of player model
349         // when seen in 3rd person view.
350         bool flying = g_settings->getBool("free_move") && m_client->checkLocalPrivilege("fly");
351         if (player_position.Y > old_player_position.Y && !player->is_climbing && !flying) {
352                 f32 oldy = old_player_position.Y;
353                 f32 newy = player_position.Y;
354                 f32 t = std::exp(-23 * frametime);
355                 player_position.Y = oldy * t + newy * (1-t);
356         }
357
358         // Set player node transformation
359         m_playernode->setPosition(player_position);
360         m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
361         m_playernode->updateAbsolutePosition();
362
363         // Get camera tilt timer (hurt animation)
364         float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
365
366         // Fall bobbing animation
367         float fall_bobbing = 0;
368         if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
369         {
370                 if(m_view_bobbing_fall == -1) // Effect took place and has finished
371                         player->camera_impact = m_view_bobbing_fall = 0;
372                 else if(m_view_bobbing_fall == 0) // Initialize effect
373                         m_view_bobbing_fall = 1;
374
375                 // Convert 0 -> 1 to 0 -> 1 -> 0
376                 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
377                 // Smoothen and invert the above
378                 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
379                 // Amplify according to the intensity of the impact
380                 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
381
382                 fall_bobbing *= m_cache_fall_bobbing_amount;
383         }
384
385         // Calculate and translate the head SceneNode offsets
386         {
387                 v3f eye_offset = player->getEyeOffset();
388                 if (m_camera_mode == CAMERA_MODE_FIRST)
389                         eye_offset += player->eye_offset_first;
390                 else
391                         eye_offset += player->eye_offset_third;
392
393                 // Set head node transformation
394                 eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
395                 m_headnode->setPosition(eye_offset);
396                 m_headnode->setRotation(v3f(player->getPitch(), 0,
397                         cameratilt * player->hurt_tilt_strength));
398                 m_headnode->updateAbsolutePosition();
399         }
400
401         // Compute relative camera position and target
402         v3f rel_cam_pos = v3f(0,0,0);
403         v3f rel_cam_target = v3f(0,0,1);
404         v3f rel_cam_up = v3f(0,1,0);
405
406         if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
407                 m_camera_mode < CAMERA_MODE_THIRD) {
408                 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
409                 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
410
411                 #if 1
412                 f32 bobknob = 1.2;
413                 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
414                 //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
415
416                 v3f bobvec = v3f(
417                         0.3 * bobdir * sin(bobfrac * M_PI),
418                         -0.28 * bobtmp * bobtmp,
419                         0.);
420
421                 //rel_cam_pos += 0.2 * bobvec;
422                 //rel_cam_target += 0.03 * bobvec;
423                 //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
424                 float f = 1.0;
425                 f *= m_cache_view_bobbing_amount;
426                 rel_cam_pos += bobvec * f;
427                 //rel_cam_target += 0.995 * bobvec * f;
428                 rel_cam_target += bobvec * f;
429                 rel_cam_target.Z -= 0.005 * bobvec.Z * f;
430                 //rel_cam_target.X -= 0.005 * bobvec.X * f;
431                 //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
432                 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
433                 #else
434                 f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
435                 f32 angle_rad = angle_deg * M_PI / 180;
436                 f32 r = 0.05;
437                 v3f off = v3f(
438                         r * sin(angle_rad),
439                         r * (cos(angle_rad) - 1),
440                         0);
441                 rel_cam_pos += off;
442                 //rel_cam_target += off;
443                 rel_cam_up.rotateXYBy(angle_deg);
444                 #endif
445
446         }
447
448         // Compute absolute camera position and target
449         m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
450         m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
451
452         v3f abs_cam_up;
453         m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
454
455         // Seperate camera position for calculation
456         v3f my_cp = m_camera_position;
457
458         // Reposition the camera for third person view
459         if (m_camera_mode > CAMERA_MODE_FIRST)
460         {
461                 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
462                         m_camera_direction *= -1;
463
464                 my_cp.Y += 2;
465
466                 // Calculate new position
467                 bool abort = false;
468                 for (int i = BS; i <= BS * 2.75; i++) {
469                         my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
470                         my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
471                         if (i > 12)
472                                 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
473
474                         // Prevent camera positioned inside nodes
475                         const NodeDefManager *nodemgr = m_client->ndef();
476                         MapNode n = m_client->getEnv().getClientMap()
477                                 .getNode(floatToInt(my_cp, BS));
478
479                         const ContentFeatures& features = nodemgr->get(n);
480                         if (features.walkable) {
481                                 my_cp.X += m_camera_direction.X*-1*-BS/2;
482                                 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
483                                 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
484                                 abort = true;
485                                 break;
486                         }
487                 }
488
489                 // If node blocks camera position don't move y to heigh
490                 if (abort && my_cp.Y > player_position.Y+BS*2)
491                         my_cp.Y = player_position.Y+BS*2;
492         }
493
494         // Update offset if too far away from the center of the map
495         m_camera_offset.X += CAMERA_OFFSET_STEP*
496                         (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
497         m_camera_offset.Y += CAMERA_OFFSET_STEP*
498                         (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
499         m_camera_offset.Z += CAMERA_OFFSET_STEP*
500                         (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
501
502         // Set camera node transformation
503         m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
504         m_cameranode->setUpVector(abs_cam_up);
505         // *100.0 helps in large map coordinates
506         m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
507
508         // update the camera position in third-person mode to render blocks behind player
509         // and correctly apply liquid post FX.
510         if (m_camera_mode != CAMERA_MODE_FIRST)
511                 m_camera_position = my_cp;
512
513         /*
514          * Apply server-sent FOV, instantaneous or smooth transition.
515          * If not, check for zoom and set to zoom FOV.
516          * Otherwise, default to m_cache_fov.
517          */
518         if (m_fov_transition_active) {
519                 // Smooth FOV transition
520                 // Dynamically calculate FOV delta based on frametimes
521                 f32 delta = (frametime / m_transition_time) * m_fov_diff;
522                 m_curr_fov_degrees += delta;
523
524                 // Mark transition as complete if target FOV has been reached
525                 if ((m_fov_diff > 0.0f && m_curr_fov_degrees >= m_target_fov_degrees) ||
526                                 (m_fov_diff < 0.0f && m_curr_fov_degrees <= m_target_fov_degrees)) {
527                         m_fov_transition_active = false;
528                         m_curr_fov_degrees = m_target_fov_degrees;
529                 }
530         } else if (m_server_sent_fov) {
531                 // Instantaneous FOV change
532                 m_curr_fov_degrees = m_target_fov_degrees;
533         } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
534                 // Player requests zoom, apply zoom FOV
535                 m_curr_fov_degrees = player->getZoomFOV();
536         } else {
537                 // Set to client's selected FOV
538                 m_curr_fov_degrees = m_cache_fov;
539         }
540         m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
541
542         // FOV and aspect ratio
543         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
544         m_aspect = (f32) window_size.X / (f32) window_size.Y;
545         m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
546         // Increase vertical FOV on lower aspect ratios (<16:10)
547         m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
548         m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
549         m_cameranode->setAspectRatio(m_aspect);
550         m_cameranode->setFOV(m_fov_y);
551
552         if (m_arm_inertia)
553                 addArmInertia(player->getYaw());
554
555         // Position the wielded item
556         //v3f wield_position = v3f(45, -35, 65);
557         v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
558         //v3f wield_rotation = v3f(-100, 120, -100);
559         v3f wield_rotation = v3f(-100, 120, -100);
560         wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
561         if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
562         {
563                 f32 frac = 1.0;
564                 if(m_digging_anim > 0.5)
565                         frac = 2.0 * (m_digging_anim - 0.5);
566                 // This value starts from 1 and settles to 0
567                 f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
568                 //f32 ratiothing2 = pow(ratiothing, 0.5f);
569                 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
570                 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
571                 //wield_position.Z += frac * 5.0 * ratiothing2;
572                 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
573                 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
574                 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
575                 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
576         }
577         if (m_digging_button != -1)
578         {
579                 f32 digfrac = m_digging_anim;
580                 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
581                 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
582                 wield_position.Z += 25 * 0.5;
583
584                 // Euler angles are PURE EVIL, so why not use quaternions?
585                 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
586                 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
587                 core::quaternion quat_slerp;
588                 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
589                 quat_slerp.toEuler(wield_rotation);
590                 wield_rotation *= core::RADTODEG;
591         } else {
592                 f32 bobfrac = my_modf(m_view_bobbing_anim);
593                 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
594                 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
595         }
596         m_wieldnode->setPosition(wield_position);
597         m_wieldnode->setRotation(wield_rotation);
598
599         m_wieldnode->setNodeLightColor(player->light_color);
600
601         // Set render distance
602         updateViewingRange();
603
604         // If the player is walking, swimming, or climbing,
605         // view bobbing is enabled and free_move is off,
606         // start (or continue) the view bobbing animation.
607         const v3f &speed = player->getSpeed();
608         const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
609         const bool movement_Y = fabs(speed.Y) > BS;
610
611         const bool walking = movement_XZ && player->touching_ground;
612         const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
613         const bool climbing = movement_Y && player->is_climbing;
614         if ((walking || swimming || climbing) && !flying) {
615                 // Start animation
616                 m_view_bobbing_state = 1;
617                 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
618         } else if (m_view_bobbing_state == 1) {
619                 // Stop animation
620                 m_view_bobbing_state = 2;
621                 m_view_bobbing_speed = 60;
622         }
623 }
624
625 void Camera::updateViewingRange()
626 {
627         f32 viewing_range = g_settings->getFloat("viewing_range");
628
629         // Ignore near_plane setting on all other platforms to prevent abuse
630 #if ENABLE_GLES
631         m_cameranode->setNearValue(rangelim(
632                 g_settings->getFloat("near_plane"), 0.0f, 0.25f) * BS);
633 #else
634         m_cameranode->setNearValue(0.1f * BS);
635 #endif
636
637         m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
638         if (m_draw_control.range_all) {
639                 m_cameranode->setFarValue(100000.0);
640                 return;
641         }
642         m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
643 }
644
645 void Camera::setDigging(s32 button)
646 {
647         if (m_digging_button == -1)
648                 m_digging_button = button;
649 }
650
651 void Camera::wield(const ItemStack &item)
652 {
653         if (item.name != m_wield_item_next.name ||
654                         item.metadata != m_wield_item_next.metadata) {
655                 m_wield_item_next = item;
656                 if (m_wield_change_timer > 0)
657                         m_wield_change_timer = -m_wield_change_timer;
658                 else if (m_wield_change_timer == 0)
659                         m_wield_change_timer = -0.001;
660         }
661 }
662
663 void Camera::drawWieldedTool(irr::core::matrix4* translation)
664 {
665         // Clear Z buffer so that the wielded tool stays in front of world geometry
666         m_wieldmgr->getVideoDriver()->clearZBuffer();
667
668         // Draw the wielded node (in a separate scene manager)
669         scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
670         cam->setAspectRatio(m_cameranode->getAspectRatio());
671         cam->setFOV(72.0*M_PI/180.0);
672         cam->setNearValue(10);
673         cam->setFarValue(1000);
674         if (translation != NULL)
675         {
676                 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
677                 irr::core::vector3df focusPoint = (cam->getTarget()
678                                 - cam->getAbsolutePosition()).setLength(1)
679                                 + cam->getAbsolutePosition();
680
681                 irr::core::vector3df camera_pos =
682                                 (startMatrix * *translation).getTranslation();
683                 cam->setPosition(camera_pos);
684                 cam->setTarget(focusPoint);
685         }
686         m_wieldmgr->drawAll();
687 }
688
689 void Camera::drawNametags()
690 {
691         core::matrix4 trans = m_cameranode->getProjectionMatrix();
692         trans *= m_cameranode->getViewMatrix();
693
694         gui::IGUIFont *font = g_fontengine->getFont();
695         video::IVideoDriver *driver = RenderingEngine::get_video_driver();
696         v2u32 screensize = driver->getScreenSize();
697
698         for (const Nametag *nametag : m_nametags) {
699                 if (nametag->nametag_color.getAlpha() == 0) {
700                         // Enforce hiding nametag,
701                         // because if freetype is enabled, a grey
702                         // shadow can remain.
703                         continue;
704                 }
705                 v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->nametag_pos * BS;
706                 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
707                 trans.multiplyWith1x4Matrix(transformed_pos);
708                 if (transformed_pos[3] > 0) {
709                         std::wstring nametag_colorless =
710                                 unescape_translate(utf8_to_wide(nametag->nametag_text));
711                         core::dimension2d<u32> textsize = font->getDimension(
712                                 nametag_colorless.c_str());
713                         f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
714                                 core::reciprocal(transformed_pos[3]);
715                         v2s32 screen_pos;
716                         screen_pos.X = screensize.X *
717                                 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
718                         screen_pos.Y = screensize.Y *
719                                 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
720                         core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
721                         core::rect<s32> bg_size(-2, 0, textsize.Width+2, textsize.Height);
722
723                         video::SColor textColor = nametag->nametag_color;
724
725                         bool darkBackground = textColor.getLuminance() > 186;
726                         video::SColor backgroundColor = darkBackground
727                                         ? video::SColor(50, 50, 50, 50)
728                                         : video::SColor(50, 255, 255, 255);
729                         driver->draw2DRectangle(backgroundColor, bg_size + screen_pos);
730
731                         font->draw(
732                                 translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
733                                 size + screen_pos, textColor);
734                 }
735         }
736 }
737
738 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
739                 const std::string &nametag_text, video::SColor nametag_color,
740                 const v3f &pos)
741 {
742         Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color, pos);
743         m_nametags.push_back(nametag);
744         return nametag;
745 }
746
747 void Camera::removeNametag(Nametag *nametag)
748 {
749         m_nametags.remove(nametag);
750         delete nametag;
751 }