camera.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #include "inventory.h"
  19. #include "util/numeric.h"
  20. #include "client/localplayer.h"
  21. #include <ICameraSceneNode.h>
  22. #include <ISceneNode.h>
  23. #include <plane3d.h>
  24. #include <array>
  25. #include <list>
  26. #include <optional>
  27. class LocalPlayer;
  28. struct MapDrawControl;
  29. class Client;
  30. class RenderingEngine;
  31. class WieldMeshSceneNode;
  32. struct Nametag
  33. {
  34. scene::ISceneNode *parent_node;
  35. std::string text;
  36. video::SColor textcolor;
  37. std::optional<video::SColor> bgcolor;
  38. v3f pos;
  39. Nametag(scene::ISceneNode *a_parent_node,
  40. const std::string &text,
  41. const video::SColor &textcolor,
  42. const std::optional<video::SColor> &bgcolor,
  43. const v3f &pos):
  44. parent_node(a_parent_node),
  45. text(text),
  46. textcolor(textcolor),
  47. bgcolor(bgcolor),
  48. pos(pos)
  49. {
  50. }
  51. video::SColor getBgColor(bool use_fallback) const
  52. {
  53. if (bgcolor)
  54. return bgcolor.value();
  55. else if (!use_fallback)
  56. return video::SColor(0, 0, 0, 0);
  57. else if (textcolor.getLuminance() > 186)
  58. // Dark background for light text
  59. return video::SColor(50, 50, 50, 50);
  60. else
  61. // Light background for dark text
  62. return video::SColor(50, 255, 255, 255);
  63. }
  64. };
  65. enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
  66. /*
  67. Client camera class, manages the player and camera scene nodes, the viewing distance
  68. and performs view bobbing etc. It also displays the wielded tool in front of the
  69. first-person camera.
  70. */
  71. class Camera
  72. {
  73. public:
  74. Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine);
  75. ~Camera();
  76. // Get camera scene node.
  77. // It has the eye transformation, pitch and view bobbing applied.
  78. inline scene::ICameraSceneNode* getCameraNode() const
  79. {
  80. return m_cameranode;
  81. }
  82. // Get the camera position (in absolute scene coordinates).
  83. // This has view bobbing applied.
  84. inline v3f getPosition() const
  85. {
  86. return m_camera_position;
  87. }
  88. // Returns the absolute position of the head SceneNode in the world
  89. inline v3f getHeadPosition() const
  90. {
  91. return m_headnode->getAbsolutePosition();
  92. }
  93. // Get the camera direction (in absolute camera coordinates).
  94. // This has view bobbing applied.
  95. inline v3f getDirection() const
  96. {
  97. return m_camera_direction;
  98. }
  99. // Get the camera offset
  100. inline v3s16 getOffset() const
  101. {
  102. return m_camera_offset;
  103. }
  104. // Horizontal field of view
  105. inline f32 getFovX() const
  106. {
  107. return m_fov_x;
  108. }
  109. // Vertical field of view
  110. inline f32 getFovY() const
  111. {
  112. return m_fov_y;
  113. }
  114. // Get maximum of getFovX() and getFovY()
  115. inline f32 getFovMax() const
  116. {
  117. return MYMAX(m_fov_x, m_fov_y);
  118. }
  119. // Returns a lambda that when called with an object's position and bounding-sphere
  120. // radius (both in BS space) returns true if, and only if the object should be
  121. // frustum-culled.
  122. auto getFrustumCuller() const
  123. {
  124. return [planes = getFrustumCullPlanes(),
  125. camera_offset = intToFloat(m_camera_offset, BS)
  126. ](v3f position, f32 radius) {
  127. v3f pos_camspace = position - camera_offset;
  128. for (auto &plane : planes) {
  129. if (plane.getDistanceTo(pos_camspace) > radius)
  130. return true;
  131. }
  132. return false;
  133. };
  134. }
  135. // Notify about new server-sent FOV and initialize smooth FOV transition
  136. void notifyFovChange();
  137. // Step the camera: updates the viewing range and view bobbing.
  138. void step(f32 dtime);
  139. // Update the camera from the local player's position.
  140. void update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio);
  141. // Update render distance
  142. void updateViewingRange();
  143. // Start digging animation
  144. // Pass 0 for left click, 1 for right click
  145. void setDigging(s32 button);
  146. // Replace the wielded item mesh
  147. void wield(const ItemStack &item);
  148. // Draw the wielded tool.
  149. // This has to happen *after* the main scene is drawn.
  150. // Warning: This clears the Z buffer.
  151. void drawWieldedTool(irr::core::matrix4* translation=NULL);
  152. // Toggle the current camera mode
  153. void toggleCameraMode() {
  154. if (m_camera_mode == CAMERA_MODE_FIRST)
  155. m_camera_mode = CAMERA_MODE_THIRD;
  156. else if (m_camera_mode == CAMERA_MODE_THIRD)
  157. m_camera_mode = CAMERA_MODE_THIRD_FRONT;
  158. else
  159. m_camera_mode = CAMERA_MODE_FIRST;
  160. }
  161. // Set the current camera mode
  162. inline void setCameraMode(CameraMode mode)
  163. {
  164. m_camera_mode = mode;
  165. }
  166. //read the current camera mode
  167. inline CameraMode getCameraMode()
  168. {
  169. return m_camera_mode;
  170. }
  171. Nametag *addNametag(scene::ISceneNode *parent_node,
  172. const std::string &text, video::SColor textcolor,
  173. std::optional<video::SColor> bgcolor, const v3f &pos);
  174. void removeNametag(Nametag *nametag);
  175. void drawNametags();
  176. inline void addArmInertia(f32 player_yaw);
  177. private:
  178. // Use getFrustumCuller().
  179. // This helper just exists to decrease the header's number of includes.
  180. std::array<core::plane3d<f32>, 4> getFrustumCullPlanes() const;
  181. // Nodes
  182. scene::ISceneNode *m_playernode = nullptr;
  183. scene::ISceneNode *m_headnode = nullptr;
  184. scene::ICameraSceneNode *m_cameranode = nullptr;
  185. scene::ISceneManager *m_wieldmgr = nullptr;
  186. WieldMeshSceneNode *m_wieldnode = nullptr;
  187. // draw control
  188. MapDrawControl& m_draw_control;
  189. Client *m_client;
  190. // Default Client FOV (as defined by the "fov" setting)
  191. f32 m_cache_fov;
  192. // Absolute camera position
  193. v3f m_camera_position;
  194. // Absolute camera direction
  195. v3f m_camera_direction;
  196. // Camera offset
  197. v3s16 m_camera_offset;
  198. bool m_stepheight_smooth_active = false;
  199. // Server-sent FOV variables
  200. bool m_server_sent_fov = false;
  201. f32 m_curr_fov_degrees, m_old_fov_degrees, m_target_fov_degrees;
  202. // FOV transition variables
  203. bool m_fov_transition_active = false;
  204. f32 m_fov_diff, m_transition_time;
  205. v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
  206. v2f m_arm_dir;
  207. v2f m_cam_vel;
  208. v2f m_cam_vel_old;
  209. v2f m_last_cam_pos;
  210. // Field of view and aspect ratio stuff
  211. f32 m_aspect = 1.0f;
  212. f32 m_fov_x = 1.0f;
  213. f32 m_fov_y = 1.0f;
  214. // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
  215. f32 m_view_bobbing_anim = 0.0f;
  216. // If 0, view bobbing is off (e.g. player is standing).
  217. // If 1, view bobbing is on (player is walking).
  218. // If 2, view bobbing is getting switched off.
  219. s32 m_view_bobbing_state = 0;
  220. // Speed of view bobbing animation
  221. f32 m_view_bobbing_speed = 0.0f;
  222. // Fall view bobbing
  223. f32 m_view_bobbing_fall = 0.0f;
  224. // Digging animation frame (0 <= m_digging_anim < 1)
  225. f32 m_digging_anim = 0.0f;
  226. // If -1, no digging animation
  227. // If 0, left-click digging animation
  228. // If 1, right-click digging animation
  229. s32 m_digging_button = -1;
  230. // Animation when changing wielded item
  231. f32 m_wield_change_timer = 0.125f;
  232. ItemStack m_wield_item_next;
  233. CameraMode m_camera_mode = CAMERA_MODE_FIRST;
  234. f32 m_cache_fall_bobbing_amount;
  235. f32 m_cache_view_bobbing_amount;
  236. bool m_arm_inertia;
  237. std::list<Nametag *> m_nametags;
  238. bool m_show_nametag_backgrounds;
  239. // Last known light color of the player
  240. video::SColor m_player_light_color;
  241. };