camera.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "client/tile.h"
  20. #include <ICameraSceneNode.h>
  21. #include <ISceneNode.h>
  22. #include <list>
  23. class LocalPlayer;
  24. struct MapDrawControl;
  25. class Client;
  26. class WieldMeshSceneNode;
  27. struct Nametag {
  28. Nametag(scene::ISceneNode *a_parent_node,
  29. const std::string &a_nametag_text,
  30. const video::SColor &a_nametag_color,
  31. const v3f &a_nametag_pos):
  32. parent_node(a_parent_node),
  33. nametag_text(a_nametag_text),
  34. nametag_color(a_nametag_color),
  35. nametag_pos(a_nametag_pos)
  36. {
  37. }
  38. scene::ISceneNode *parent_node;
  39. std::string nametag_text;
  40. video::SColor nametag_color;
  41. v3f nametag_pos;
  42. };
  43. enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
  44. /*
  45. Client camera class, manages the player and camera scene nodes, the viewing distance
  46. and performs view bobbing etc. It also displays the wielded tool in front of the
  47. first-person camera.
  48. */
  49. class Camera
  50. {
  51. public:
  52. Camera(MapDrawControl &draw_control, Client *client);
  53. ~Camera();
  54. // Get camera scene node.
  55. // It has the eye transformation, pitch and view bobbing applied.
  56. inline scene::ICameraSceneNode* getCameraNode() const
  57. {
  58. return m_cameranode;
  59. }
  60. // Get the camera position (in absolute scene coordinates).
  61. // This has view bobbing applied.
  62. inline v3f getPosition() const
  63. {
  64. return m_camera_position;
  65. }
  66. // Get the camera direction (in absolute camera coordinates).
  67. // This has view bobbing applied.
  68. inline v3f getDirection() const
  69. {
  70. return m_camera_direction;
  71. }
  72. // Get the camera offset
  73. inline v3s16 getOffset() const
  74. {
  75. return m_camera_offset;
  76. }
  77. // Horizontal field of view
  78. inline f32 getFovX() const
  79. {
  80. return m_fov_x;
  81. }
  82. // Vertical field of view
  83. inline f32 getFovY() const
  84. {
  85. return m_fov_y;
  86. }
  87. // Get maximum of getFovX() and getFovY()
  88. inline f32 getFovMax() const
  89. {
  90. return MYMAX(m_fov_x, m_fov_y);
  91. }
  92. // Checks if the constructor was able to create the scene nodes
  93. bool successfullyCreated(std::string &error_message);
  94. // Step the camera: updates the viewing range and view bobbing.
  95. void step(f32 dtime);
  96. // Update the camera from the local player's position.
  97. // busytime is used to adjust the viewing range.
  98. void update(LocalPlayer* player, f32 frametime, f32 busytime,
  99. f32 tool_reload_ratio);
  100. // Update render distance
  101. void updateViewingRange();
  102. // Start digging animation
  103. // Pass 0 for left click, 1 for right click
  104. void setDigging(s32 button);
  105. // Replace the wielded item mesh
  106. void wield(const ItemStack &item);
  107. // Draw the wielded tool.
  108. // This has to happen *after* the main scene is drawn.
  109. // Warning: This clears the Z buffer.
  110. void drawWieldedTool(irr::core::matrix4* translation=NULL);
  111. // Toggle the current camera mode
  112. void toggleCameraMode() {
  113. if (m_camera_mode == CAMERA_MODE_FIRST)
  114. m_camera_mode = CAMERA_MODE_THIRD;
  115. else if (m_camera_mode == CAMERA_MODE_THIRD)
  116. m_camera_mode = CAMERA_MODE_THIRD_FRONT;
  117. else
  118. m_camera_mode = CAMERA_MODE_FIRST;
  119. }
  120. // Set the current camera mode
  121. inline void setCameraMode(CameraMode mode)
  122. {
  123. m_camera_mode = mode;
  124. }
  125. //read the current camera mode
  126. inline CameraMode getCameraMode()
  127. {
  128. return m_camera_mode;
  129. }
  130. Nametag *addNametag(scene::ISceneNode *parent_node,
  131. const std::string &nametag_text, video::SColor nametag_color,
  132. const v3f &pos);
  133. void removeNametag(Nametag *nametag);
  134. const std::list<Nametag *> &getNametags() { return m_nametags; }
  135. void drawNametags();
  136. inline void addArmInertia(f32 player_yaw);
  137. private:
  138. // Nodes
  139. scene::ISceneNode *m_playernode = nullptr;
  140. scene::ISceneNode *m_headnode = nullptr;
  141. scene::ICameraSceneNode *m_cameranode = nullptr;
  142. scene::ISceneManager *m_wieldmgr = nullptr;
  143. WieldMeshSceneNode *m_wieldnode = nullptr;
  144. // draw control
  145. MapDrawControl& m_draw_control;
  146. Client *m_client;
  147. // Absolute camera position
  148. v3f m_camera_position;
  149. // Absolute camera direction
  150. v3f m_camera_direction;
  151. // Camera offset
  152. v3s16 m_camera_offset;
  153. v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
  154. v2f m_arm_dir;
  155. v2f m_cam_vel;
  156. v2f m_cam_vel_old;
  157. v2f m_last_cam_pos;
  158. // Field of view and aspect ratio stuff
  159. f32 m_aspect = 1.0f;
  160. f32 m_fov_x = 1.0f;
  161. f32 m_fov_y = 1.0f;
  162. // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
  163. f32 m_view_bobbing_anim = 0.0f;
  164. // If 0, view bobbing is off (e.g. player is standing).
  165. // If 1, view bobbing is on (player is walking).
  166. // If 2, view bobbing is getting switched off.
  167. s32 m_view_bobbing_state = 0;
  168. // Speed of view bobbing animation
  169. f32 m_view_bobbing_speed = 0.0f;
  170. // Fall view bobbing
  171. f32 m_view_bobbing_fall = 0.0f;
  172. // Digging animation frame (0 <= m_digging_anim < 1)
  173. f32 m_digging_anim = 0.0f;
  174. // If -1, no digging animation
  175. // If 0, left-click digging animation
  176. // If 1, right-click digging animation
  177. s32 m_digging_button = -1;
  178. // Animation when changing wielded item
  179. f32 m_wield_change_timer = 0.125f;
  180. ItemStack m_wield_item_next;
  181. CameraMode m_camera_mode = CAMERA_MODE_FIRST;
  182. f32 m_cache_fall_bobbing_amount;
  183. f32 m_cache_view_bobbing_amount;
  184. f32 m_cache_fov;
  185. bool m_arm_inertia;
  186. std::list<Nametag *> m_nametags;
  187. };