camera.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "camera.h"
  17. #include "debug.h"
  18. #include "client.h"
  19. #include "config.h"
  20. #include "map.h"
  21. #include "clientmap.h" // MapDrawControl
  22. #include "player.h"
  23. #include <cmath>
  24. #include "client/renderingengine.h"
  25. #include "client/content_cao.h"
  26. #include "settings.h"
  27. #include "wieldmesh.h"
  28. #include "noise.h" // easeCurve
  29. #include "mtevent.h"
  30. #include "nodedef.h"
  31. #include "util/numeric.h"
  32. #include "constants.h"
  33. #include "fontengine.h"
  34. #include "script/scripting_client.h"
  35. #include "gettext.h"
  36. #include <SViewFrustum.h>
  37. #define CAMERA_OFFSET_STEP 200
  38. #define WIELDMESH_OFFSET_X 55.0f
  39. #define WIELDMESH_OFFSET_Y -35.0f
  40. #define WIELDMESH_AMPLITUDE_X 7.0f
  41. #define WIELDMESH_AMPLITUDE_Y 10.0f
  42. Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine):
  43. m_draw_control(draw_control),
  44. m_client(client),
  45. m_player_light_color(0xFFFFFFFF)
  46. {
  47. auto smgr = rendering_engine->get_scene_manager();
  48. // note: making the camera node a child of the player node
  49. // would lead to unexpected behavior, 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);
  54. // This needs to be in its own scene manager. It is drawn after
  55. // all other 3D scene nodes and before the GUI.
  56. m_wieldmgr = smgr->createNewSceneManager();
  57. m_wieldmgr->addCameraSceneNode();
  58. m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
  59. m_wieldnode->setItem(ItemStack(), m_client);
  60. m_wieldnode->drop(); // m_wieldmgr grabbed it
  61. /* TODO: Add a callback function so these can be updated when a setting
  62. * changes. At this point in time it doesn't matter (e.g. /set
  63. * is documented to change server settings only)
  64. *
  65. * TODO: Local caching of settings is not optimal and should at some stage
  66. * be updated to use a global settings object for getting thse values
  67. * (as opposed to the this local caching). This can be addressed in
  68. * a later release.
  69. */
  70. m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount", 0.0f, 100.0f);
  71. m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount", 0.0f, 7.9f);
  72. // 45 degrees is the lowest FOV that doesn't cause the server to treat this
  73. // as a zoom FOV and load world beyond the set server limits.
  74. m_cache_fov = g_settings->getFloat("fov", 45.0f, 160.0f);
  75. m_arm_inertia = g_settings->getBool("arm_inertia");
  76. m_nametags.clear();
  77. m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds");
  78. }
  79. Camera::~Camera()
  80. {
  81. m_wieldmgr->drop();
  82. }
  83. void Camera::notifyFovChange()
  84. {
  85. LocalPlayer *player = m_client->getEnv().getLocalPlayer();
  86. assert(player);
  87. PlayerFovSpec spec = player->getFov();
  88. // Remember old FOV in case a transition is wanted
  89. f32 m_old_fov_degrees = m_fov_transition_active
  90. ? m_curr_fov_degrees // FOV is overridden with transition
  91. : m_server_sent_fov
  92. ? m_target_fov_degrees // FOV is overridden without transition
  93. : m_cache_fov; // FOV is not overridden
  94. m_server_sent_fov = spec.fov > 0.0f;
  95. m_target_fov_degrees = m_server_sent_fov
  96. ? spec.is_multiplier
  97. ? m_cache_fov * spec.fov // apply multiplier to client-set FOV
  98. : spec.fov // absolute override
  99. : m_cache_fov; // reset to client-set FOV
  100. m_fov_transition_active = spec.transition_time > 0.0f;
  101. if (m_fov_transition_active) {
  102. m_transition_time = spec.transition_time;
  103. m_fov_diff = m_target_fov_degrees - m_old_fov_degrees;
  104. }
  105. }
  106. // Returns the fractional part of x
  107. inline f32 my_modf(f32 x)
  108. {
  109. float dummy;
  110. return std::modf(x, &dummy);
  111. }
  112. void Camera::step(f32 dtime)
  113. {
  114. if(m_view_bobbing_fall > 0)
  115. {
  116. m_view_bobbing_fall -= 3 * dtime;
  117. if(m_view_bobbing_fall <= 0)
  118. m_view_bobbing_fall = -1; // Mark the effect as finished
  119. }
  120. bool was_under_zero = m_wield_change_timer < 0;
  121. m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
  122. if (m_wield_change_timer >= 0 && was_under_zero) {
  123. m_wieldnode->setItem(m_wield_item_next, m_client);
  124. m_wieldnode->setNodeLightColor(m_player_light_color);
  125. }
  126. if (m_view_bobbing_state != 0)
  127. {
  128. //f32 offset = dtime * m_view_bobbing_speed * 0.035;
  129. f32 offset = dtime * m_view_bobbing_speed * 0.030;
  130. if (m_view_bobbing_state == 2) {
  131. // Animation is getting turned off
  132. if (m_view_bobbing_anim < 0.25) {
  133. m_view_bobbing_anim -= offset;
  134. } else if (m_view_bobbing_anim > 0.75) {
  135. m_view_bobbing_anim += offset;
  136. } else if (m_view_bobbing_anim < 0.5) {
  137. m_view_bobbing_anim += offset;
  138. if (m_view_bobbing_anim > 0.5)
  139. m_view_bobbing_anim = 0.5;
  140. } else {
  141. m_view_bobbing_anim -= offset;
  142. if (m_view_bobbing_anim < 0.5)
  143. m_view_bobbing_anim = 0.5;
  144. }
  145. if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
  146. fabs(m_view_bobbing_anim - 0.5) < 0.01) {
  147. m_view_bobbing_anim = 0;
  148. m_view_bobbing_state = 0;
  149. }
  150. }
  151. else {
  152. float was = m_view_bobbing_anim;
  153. m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
  154. bool step = (was == 0 ||
  155. (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
  156. (was > 0.5f && m_view_bobbing_anim <= 0.5f));
  157. if(step) {
  158. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
  159. }
  160. }
  161. }
  162. if (m_digging_button != -1) {
  163. f32 offset = dtime * 3.5f;
  164. float m_digging_anim_was = m_digging_anim;
  165. m_digging_anim += offset;
  166. if (m_digging_anim >= 1)
  167. {
  168. m_digging_anim = 0;
  169. m_digging_button = -1;
  170. }
  171. float lim = 0.15;
  172. if(m_digging_anim_was < lim && m_digging_anim >= lim)
  173. {
  174. if (m_digging_button == 0) {
  175. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
  176. } else if(m_digging_button == 1) {
  177. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
  178. }
  179. }
  180. }
  181. }
  182. static inline v2f dir(const v2f &pos_dist)
  183. {
  184. f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
  185. f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
  186. f32 x_abs = std::fabs(x);
  187. f32 y_abs = std::fabs(y);
  188. if (x_abs >= y_abs) {
  189. y *= (1.0f / x_abs);
  190. x /= x_abs;
  191. }
  192. if (y_abs >= x_abs) {
  193. x *= (1.0f / y_abs);
  194. y /= y_abs;
  195. }
  196. return v2f(std::fabs(x), std::fabs(y));
  197. }
  198. void Camera::addArmInertia(f32 player_yaw)
  199. {
  200. m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
  201. -100.0f, 100.0f) / 0.016f) * 0.01f;
  202. m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
  203. f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
  204. f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
  205. if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
  206. /*
  207. The arm moves relative to the camera speed,
  208. with an acceleration factor.
  209. */
  210. if (m_cam_vel.X > 1.0f) {
  211. if (m_cam_vel.X > m_cam_vel_old.X)
  212. m_cam_vel_old.X = m_cam_vel.X;
  213. f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
  214. m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
  215. if (m_last_cam_pos.X != player_yaw)
  216. m_last_cam_pos.X = player_yaw;
  217. m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
  218. WIELDMESH_OFFSET_X - (WIELDMESH_AMPLITUDE_X * 0.5f),
  219. WIELDMESH_OFFSET_X + (WIELDMESH_AMPLITUDE_X * 0.5f));
  220. }
  221. if (m_cam_vel.Y > 1.0f) {
  222. if (m_cam_vel.Y > m_cam_vel_old.Y)
  223. m_cam_vel_old.Y = m_cam_vel.Y;
  224. f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
  225. m_wieldmesh_offset.Y +=
  226. m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
  227. if (m_last_cam_pos.Y != m_camera_direction.Y)
  228. m_last_cam_pos.Y = m_camera_direction.Y;
  229. m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
  230. WIELDMESH_OFFSET_Y - (WIELDMESH_AMPLITUDE_Y * 0.5f),
  231. WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
  232. }
  233. m_arm_dir = dir(m_wieldmesh_offset);
  234. } else {
  235. /*
  236. Now the arm gets back to its default position when the camera stops,
  237. following a vector, with a smooth deceleration factor.
  238. */
  239. f32 dec_X = 0.35f * (std::min(15.0f, m_cam_vel_old.X) * (1.0f +
  240. (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
  241. f32 dec_Y = 0.25f * (std::min(15.0f, m_cam_vel_old.Y) * (1.0f +
  242. (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
  243. if (gap_X < 0.1f)
  244. m_cam_vel_old.X = 0.0f;
  245. m_wieldmesh_offset.X -=
  246. m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
  247. if (gap_Y < 0.1f)
  248. m_cam_vel_old.Y = 0.0f;
  249. m_wieldmesh_offset.Y -=
  250. m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
  251. }
  252. }
  253. void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
  254. {
  255. // Get player position
  256. // Smooth the movement when walking up stairs
  257. v3f old_player_position = m_playernode->getPosition();
  258. v3f player_position = player->getPosition();
  259. f32 yaw = player->getYaw();
  260. f32 pitch = player->getPitch();
  261. // This is worse than `LocalPlayer::getPosition()` but
  262. // mods expect the player head to be at the parent's position
  263. // plus eye height.
  264. if (player->getParent())
  265. player_position = player->getParent()->getPosition();
  266. // Smooth the camera movement after the player instantly moves upward due to stepheight.
  267. // The smoothing usually continues until the camera position reaches the player position.
  268. float player_stepheight = player->getCAO() ? player->getCAO()->getStepHeight() : HUGE_VALF;
  269. float upward_movement = player_position.Y - old_player_position.Y;
  270. if (upward_movement < 0.01f || upward_movement > player_stepheight) {
  271. m_stepheight_smooth_active = false;
  272. } else if (player->touching_ground) {
  273. m_stepheight_smooth_active = true;
  274. }
  275. if (m_stepheight_smooth_active) {
  276. f32 oldy = old_player_position.Y;
  277. f32 newy = player_position.Y;
  278. f32 t = std::exp(-23 * frametime);
  279. player_position.Y = oldy * t + newy * (1-t);
  280. }
  281. // Set player node transformation
  282. m_playernode->setPosition(player_position);
  283. m_playernode->setRotation(v3f(0, -1 * yaw, 0));
  284. m_playernode->updateAbsolutePosition();
  285. // Get camera tilt timer (hurt animation)
  286. float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
  287. // Fall bobbing animation
  288. float fall_bobbing = 0;
  289. if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
  290. {
  291. if(m_view_bobbing_fall == -1) // Effect took place and has finished
  292. player->camera_impact = m_view_bobbing_fall = 0;
  293. else if(m_view_bobbing_fall == 0) // Initialize effect
  294. m_view_bobbing_fall = 1;
  295. // Convert 0 -> 1 to 0 -> 1 -> 0
  296. fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
  297. // Smoothen and invert the above
  298. fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
  299. // Amplify according to the intensity of the impact
  300. if (player->camera_impact > 0.0f)
  301. fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
  302. fall_bobbing *= m_cache_fall_bobbing_amount;
  303. }
  304. // Calculate and translate the head SceneNode offsets
  305. {
  306. v3f eye_offset = player->getEyeOffset();
  307. switch(m_camera_mode) {
  308. case CAMERA_MODE_FIRST:
  309. eye_offset += player->eye_offset_first;
  310. break;
  311. case CAMERA_MODE_THIRD:
  312. eye_offset += player->eye_offset_third;
  313. break;
  314. case CAMERA_MODE_THIRD_FRONT:
  315. eye_offset.X += player->eye_offset_third_front.X;
  316. eye_offset.Y += player->eye_offset_third_front.Y;
  317. eye_offset.Z -= player->eye_offset_third_front.Z;
  318. break;
  319. }
  320. // Set head node transformation
  321. eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
  322. m_headnode->setPosition(eye_offset);
  323. m_headnode->setRotation(v3f(pitch, 0,
  324. cameratilt * player->hurt_tilt_strength));
  325. m_headnode->updateAbsolutePosition();
  326. }
  327. // Compute relative camera position and target
  328. v3f rel_cam_pos = v3f(0,0,0);
  329. v3f rel_cam_target = v3f(0,0,1);
  330. v3f rel_cam_up = v3f(0,1,0);
  331. if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
  332. m_camera_mode < CAMERA_MODE_THIRD) {
  333. f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
  334. f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
  335. f32 bobknob = 1.2;
  336. f32 bobtmp = std::sin(std::pow(bobfrac, bobknob) * M_PI);
  337. v3f bobvec = v3f(
  338. 0.3 * bobdir * std::sin(bobfrac * M_PI),
  339. -0.28 * bobtmp * bobtmp,
  340. 0.);
  341. rel_cam_pos += bobvec * m_cache_view_bobbing_amount;
  342. rel_cam_target += bobvec * m_cache_view_bobbing_amount;
  343. rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * m_cache_view_bobbing_amount);
  344. }
  345. // Compute absolute camera position and target
  346. m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
  347. m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
  348. v3f abs_cam_up;
  349. m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
  350. // Separate camera position for calculation
  351. v3f my_cp = m_camera_position;
  352. // Reposition the camera for third person view
  353. if (m_camera_mode > CAMERA_MODE_FIRST)
  354. {
  355. if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
  356. m_camera_direction *= -1;
  357. my_cp.Y += 2;
  358. // Calculate new position
  359. bool abort = false;
  360. for (int i = BS; i <= BS * 2.75; i++) {
  361. my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
  362. my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
  363. if (i > 12)
  364. my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
  365. // Prevent camera positioned inside nodes
  366. const NodeDefManager *nodemgr = m_client->ndef();
  367. MapNode n = m_client->getEnv().getClientMap()
  368. .getNode(floatToInt(my_cp, BS));
  369. const ContentFeatures& features = nodemgr->get(n);
  370. if (features.walkable) {
  371. my_cp.X += m_camera_direction.X*-1*-BS/2;
  372. my_cp.Z += m_camera_direction.Z*-1*-BS/2;
  373. my_cp.Y += m_camera_direction.Y*-1*-BS/2;
  374. abort = true;
  375. break;
  376. }
  377. }
  378. // If node blocks camera position don't move y to heigh
  379. if (abort && my_cp.Y > player_position.Y+BS*2)
  380. my_cp.Y = player_position.Y+BS*2;
  381. }
  382. // Update offset if too far away from the center of the map
  383. m_camera_offset.X += CAMERA_OFFSET_STEP*
  384. (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
  385. m_camera_offset.Y += CAMERA_OFFSET_STEP*
  386. (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
  387. m_camera_offset.Z += CAMERA_OFFSET_STEP*
  388. (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
  389. // Set camera node transformation
  390. m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
  391. m_cameranode->updateAbsolutePosition();
  392. m_cameranode->setUpVector(abs_cam_up);
  393. // *100.0 helps in large map coordinates
  394. m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
  395. // update the camera position in third-person mode to render blocks behind player
  396. // and correctly apply liquid post FX.
  397. if (m_camera_mode != CAMERA_MODE_FIRST)
  398. m_camera_position = my_cp;
  399. /*
  400. * Apply server-sent FOV, instantaneous or smooth transition.
  401. * If not, check for zoom and set to zoom FOV.
  402. * Otherwise, default to m_cache_fov.
  403. */
  404. if (m_fov_transition_active) {
  405. // Smooth FOV transition
  406. // Dynamically calculate FOV delta based on frametimes
  407. f32 delta = (frametime / m_transition_time) * m_fov_diff;
  408. m_curr_fov_degrees += delta;
  409. // Mark transition as complete if target FOV has been reached
  410. if ((m_fov_diff > 0.0f && m_curr_fov_degrees >= m_target_fov_degrees) ||
  411. (m_fov_diff < 0.0f && m_curr_fov_degrees <= m_target_fov_degrees)) {
  412. m_fov_transition_active = false;
  413. m_curr_fov_degrees = m_target_fov_degrees;
  414. }
  415. } else if (m_server_sent_fov) {
  416. // Instantaneous FOV change
  417. m_curr_fov_degrees = m_target_fov_degrees;
  418. } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
  419. // Player requests zoom, apply zoom FOV
  420. m_curr_fov_degrees = player->getZoomFOV();
  421. } else {
  422. // Set to client's selected FOV
  423. m_curr_fov_degrees = m_cache_fov;
  424. }
  425. m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
  426. // FOV and aspect ratio
  427. const v2u32 &window_size = RenderingEngine::getWindowSize();
  428. m_aspect = (f32) window_size.X / (f32) window_size.Y;
  429. m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
  430. // Increase vertical FOV on lower aspect ratios (<16:10)
  431. m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
  432. m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
  433. m_cameranode->setAspectRatio(m_aspect);
  434. m_cameranode->setFOV(m_fov_y);
  435. // Make new matrices and frustum
  436. m_cameranode->updateMatrices();
  437. if (m_arm_inertia)
  438. addArmInertia(yaw);
  439. // Position the wielded item
  440. v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
  441. v3f wield_rotation = v3f(-100, 120, -100);
  442. wield_position.Y += std::abs(m_wield_change_timer)*320 - 40;
  443. if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
  444. {
  445. f32 frac = 1.0;
  446. if(m_digging_anim > 0.5)
  447. frac = 2.0 * (m_digging_anim - 0.5);
  448. // This value starts from 1 and settles to 0
  449. f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
  450. f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
  451. wield_position.Y -= frac * 25.0f * std::pow(ratiothing2, 1.7f);
  452. wield_position.X -= frac * 35.0f * std::pow(ratiothing2, 1.1f);
  453. wield_rotation.Y += frac * 70.0f * std::pow(ratiothing2, 1.4f);
  454. }
  455. if (m_digging_button != -1)
  456. {
  457. f32 digfrac = m_digging_anim;
  458. wield_position.X -= 50 * std::sin(std::pow(digfrac, 0.8f) * M_PI);
  459. wield_position.Y += 24 * std::sin(digfrac * 1.8 * M_PI);
  460. wield_position.Z += 25 * 0.5;
  461. // Euler angles are PURE EVIL, so why not use quaternions?
  462. core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
  463. core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
  464. core::quaternion quat_slerp;
  465. quat_slerp.slerp(quat_begin, quat_end, std::sin(digfrac * M_PI));
  466. quat_slerp.toEuler(wield_rotation);
  467. wield_rotation *= core::RADTODEG;
  468. } else {
  469. f32 bobfrac = my_modf(m_view_bobbing_anim);
  470. wield_position.X -= std::sin(bobfrac*M_PI*2.0) * 3.0;
  471. wield_position.Y += std::sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
  472. }
  473. m_wieldnode->setPosition(wield_position);
  474. m_wieldnode->setRotation(wield_rotation);
  475. m_player_light_color = player->light_color;
  476. m_wieldnode->setNodeLightColor(m_player_light_color);
  477. // Set render distance
  478. updateViewingRange();
  479. // If the player is walking, swimming, or climbing,
  480. // view bobbing is enabled and free_move is off,
  481. // start (or continue) the view bobbing animation.
  482. const v3f &speed = player->getSpeed();
  483. const bool movement_XZ = std::hypot(speed.X, speed.Z) > BS;
  484. const bool movement_Y = std::abs(speed.Y) > BS;
  485. const bool walking = movement_XZ && player->touching_ground;
  486. const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
  487. const bool climbing = movement_Y && player->is_climbing;
  488. const bool flying = g_settings->getBool("free_move")
  489. && m_client->checkLocalPrivilege("fly");
  490. if ((walking || swimming || climbing) && !flying) {
  491. // Start animation
  492. m_view_bobbing_state = 1;
  493. m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
  494. } else if (m_view_bobbing_state == 1) {
  495. // Stop animation
  496. m_view_bobbing_state = 2;
  497. m_view_bobbing_speed = 60;
  498. }
  499. }
  500. void Camera::updateViewingRange()
  501. {
  502. f32 viewing_range = g_settings->getFloat("viewing_range");
  503. m_cameranode->setNearValue(0.1f * BS);
  504. m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
  505. if (m_draw_control.range_all) {
  506. m_cameranode->setFarValue(100000.0);
  507. return;
  508. }
  509. m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
  510. }
  511. void Camera::setDigging(s32 button)
  512. {
  513. if (m_digging_button == -1)
  514. m_digging_button = button;
  515. }
  516. void Camera::wield(const ItemStack &item)
  517. {
  518. if (item.name != m_wield_item_next.name ||
  519. item.metadata != m_wield_item_next.metadata) {
  520. m_wield_item_next = item;
  521. if (m_wield_change_timer > 0)
  522. m_wield_change_timer = -m_wield_change_timer;
  523. else if (m_wield_change_timer == 0)
  524. m_wield_change_timer = -0.001;
  525. }
  526. }
  527. void Camera::drawWieldedTool(irr::core::matrix4* translation)
  528. {
  529. // Clear Z buffer so that the wielded tool stays in front of world geometry
  530. m_wieldmgr->getVideoDriver()->clearBuffers(video::ECBF_DEPTH);
  531. // Draw the wielded node (in a separate scene manager)
  532. scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
  533. cam->setAspectRatio(m_cameranode->getAspectRatio());
  534. cam->setFOV(72.0*M_PI/180.0);
  535. cam->setNearValue(10);
  536. cam->setFarValue(1000);
  537. if (translation != NULL)
  538. {
  539. irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
  540. irr::core::vector3df focusPoint = (cam->getTarget()
  541. - cam->getAbsolutePosition()).setLength(1)
  542. + cam->getAbsolutePosition();
  543. irr::core::vector3df camera_pos =
  544. (startMatrix * *translation).getTranslation();
  545. cam->setPosition(camera_pos);
  546. cam->updateAbsolutePosition();
  547. cam->setTarget(focusPoint);
  548. }
  549. m_wieldmgr->drawAll();
  550. }
  551. void Camera::drawNametags()
  552. {
  553. core::matrix4 trans = m_cameranode->getProjectionMatrix();
  554. trans *= m_cameranode->getViewMatrix();
  555. gui::IGUIFont *font = g_fontengine->getFont();
  556. video::IVideoDriver *driver = RenderingEngine::get_video_driver();
  557. v2u32 screensize = driver->getScreenSize();
  558. for (const Nametag *nametag : m_nametags) {
  559. // Nametags are hidden in GenericCAO::updateNametag()
  560. v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
  561. f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
  562. trans.multiplyWith1x4Matrix(transformed_pos);
  563. if (transformed_pos[3] > 0) {
  564. std::wstring nametag_colorless =
  565. unescape_translate(utf8_to_wide(nametag->text));
  566. core::dimension2d<u32> textsize = font->getDimension(
  567. nametag_colorless.c_str());
  568. f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
  569. core::reciprocal(transformed_pos[3]);
  570. v2s32 screen_pos;
  571. screen_pos.X = screensize.X *
  572. (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
  573. screen_pos.Y = screensize.Y *
  574. (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
  575. core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
  576. auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
  577. if (bgcolor.getAlpha() != 0) {
  578. core::rect<s32> bg_size(-2, 0, textsize.Width + 2, textsize.Height);
  579. driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
  580. }
  581. font->draw(
  582. translate_string(utf8_to_wide(nametag->text)).c_str(),
  583. size + screen_pos, nametag->textcolor);
  584. }
  585. }
  586. }
  587. Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
  588. const std::string &text, video::SColor textcolor,
  589. std::optional<video::SColor> bgcolor, const v3f &pos)
  590. {
  591. Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
  592. m_nametags.push_back(nametag);
  593. return nametag;
  594. }
  595. void Camera::removeNametag(Nametag *nametag)
  596. {
  597. m_nametags.remove(nametag);
  598. delete nametag;
  599. }
  600. std::array<core::plane3d<f32>, 4> Camera::getFrustumCullPlanes() const
  601. {
  602. using irr::scene::SViewFrustum;
  603. const auto &frustum_planes = m_cameranode->getViewFrustum()->planes;
  604. return {
  605. frustum_planes[SViewFrustum::VF_LEFT_PLANE],
  606. frustum_planes[SViewFrustum::VF_RIGHT_PLANE],
  607. frustum_planes[SViewFrustum::VF_BOTTOM_PLANE],
  608. frustum_planes[SViewFrustum::VF_TOP_PLANE],
  609. };
  610. }