camera.h 6.7 KB

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