Move object nametags to camera
[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 "util/numeric.h"
28 #include <ICameraSceneNode.h>
29 #include <ISceneNode.h>
30 #include <list>
31
32 #include "client.h"
33
34 class LocalPlayer;
35 struct MapDrawControl;
36 class IGameDef;
37 class WieldMeshSceneNode;
38
39 struct Nametag {
40         Nametag(scene::ISceneNode *a_parent_node,
41                         const std::string &a_nametag_text,
42                         const video::SColor &a_nametag_color):
43                 parent_node(a_parent_node),
44                 nametag_text(a_nametag_text),
45                 nametag_color(a_nametag_color)
46         {
47         }
48         scene::ISceneNode *parent_node;
49         std::string nametag_text;
50         video::SColor nametag_color;
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(scene::ISceneManager* smgr, MapDrawControl& draw_control,
64                         IGameDef *gamedef);
65         ~Camera();
66
67         // Get player scene node.
68         // This node is positioned at the player's torso (without any view bobbing),
69         // as given by Player::m_position. Yaw is applied but not pitch.
70         inline scene::ISceneNode* getPlayerNode() const
71         {
72                 return m_playernode;
73         }
74
75         // Get head scene node.
76         // It has the eye transformation and pitch applied,
77         // but no view bobbing.
78         inline scene::ISceneNode* getHeadNode() const
79         {
80                 return m_headnode;
81         }
82
83         // Get camera scene node.
84         // It has the eye transformation, pitch and view bobbing applied.
85         inline scene::ICameraSceneNode* getCameraNode() const
86         {
87                 return m_cameranode;
88         }
89
90         // Get the camera position (in absolute scene coordinates).
91         // This has view bobbing applied.
92         inline v3f getPosition() const
93         {
94                 return m_camera_position;
95         }
96
97         // Get the camera direction (in absolute camera coordinates).
98         // This has view bobbing applied.
99         inline v3f getDirection() const
100         {
101                 return m_camera_direction;
102         }
103
104         // Get the camera offset
105         inline v3s16 getOffset() const
106         {
107                 return m_camera_offset;
108         }
109
110         // Horizontal field of view
111         inline f32 getFovX() const
112         {
113                 return m_fov_x;
114         }
115
116         // Vertical field of view
117         inline f32 getFovY() const
118         {
119                 return m_fov_y;
120         }
121
122         // Get maximum of getFovX() and getFovY()
123         inline f32 getFovMax() const
124         {
125                 return MYMAX(m_fov_x, m_fov_y);
126         }
127
128         // Checks if the constructor was able to create the scene nodes
129         bool successfullyCreated(std::string &error_message);
130
131         // Step the camera: updates the viewing range and view bobbing.
132         void step(f32 dtime);
133
134         // Update the camera from the local player's position.
135         // busytime is used to adjust the viewing range.
136         void update(LocalPlayer* player, f32 frametime, f32 busytime,
137                         f32 tool_reload_ratio, ClientEnvironment &c_env);
138
139         // Render distance feedback loop
140         void updateViewingRange(f32 frametime_in, f32 busytime_in);
141
142         // Start digging animation
143         // Pass 0 for left click, 1 for right click
144         void setDigging(s32 button);
145
146         // Replace the wielded item mesh
147         void wield(const ItemStack &item);
148
149         // Draw the wielded tool.
150         // This has to happen *after* the main scene is drawn.
151         // Warning: This clears the Z buffer.
152         void drawWieldedTool(irr::core::matrix4* translation=NULL);
153
154         // Toggle the current camera mode
155         void toggleCameraMode() {
156                 if (m_camera_mode == CAMERA_MODE_FIRST)
157                         m_camera_mode = CAMERA_MODE_THIRD;
158                 else if (m_camera_mode == CAMERA_MODE_THIRD)
159                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
160                 else
161                         m_camera_mode = CAMERA_MODE_FIRST;
162         }
163
164         //read the current camera mode
165         inline CameraMode getCameraMode()
166         {
167                 return m_camera_mode;
168         }
169
170         Nametag *addNametag(scene::ISceneNode *parent_node,
171                 std::string nametag_text, video::SColor nametag_color);
172
173         void removeNametag(Nametag *nametag);
174
175         void drawNametags();
176
177 private:
178         // Nodes
179         scene::ISceneNode* m_playernode;
180         scene::ISceneNode* m_headnode;
181         scene::ICameraSceneNode* m_cameranode;
182
183         scene::ISceneManager* m_wieldmgr;
184         WieldMeshSceneNode* m_wieldnode;
185
186         // draw control
187         MapDrawControl& m_draw_control;
188
189         IGameDef *m_gamedef;
190         video::IVideoDriver *m_driver;
191
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
199         // Field of view and aspect ratio stuff
200         f32 m_aspect;
201         f32 m_fov_x;
202         f32 m_fov_y;
203
204         // Stuff for viewing range calculations
205         f32 m_added_busytime;
206         s16 m_added_frames;
207         f32 m_range_old;
208         f32 m_busytime_old;
209         f32 m_frametime_counter;
210         f32 m_time_per_range;
211
212         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
213         f32 m_view_bobbing_anim;
214         // If 0, view bobbing is off (e.g. player is standing).
215         // If 1, view bobbing is on (player is walking).
216         // If 2, view bobbing is getting switched off.
217         s32 m_view_bobbing_state;
218         // Speed of view bobbing animation
219         f32 m_view_bobbing_speed;
220         // Fall view bobbing
221         f32 m_view_bobbing_fall;
222
223         // Digging animation frame (0 <= m_digging_anim < 1)
224         f32 m_digging_anim;
225         // If -1, no digging animation
226         // If 0, left-click digging animation
227         // If 1, right-click digging animation
228         s32 m_digging_button;
229
230         // Animation when changing wielded item
231         f32 m_wield_change_timer;
232         ItemStack m_wield_item_next;
233
234         CameraMode m_camera_mode;
235
236         f32 m_cache_fall_bobbing_amount;
237         f32 m_cache_view_bobbing_amount;
238         f32 m_cache_wanted_fps;
239         f32 m_cache_fov;
240         bool m_cache_view_bobbing;
241
242         std::list<Nametag *> m_nametags;
243 };
244
245 #endif