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