]> git.lizzy.rs Git - minetest.git/blob - src/camera.h
Implement WieldMeshSceneNode which improves wield mesh rendering
[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 "tile.h"
27 #include "util/numeric.h"
28 #include <ICameraSceneNode.h>
29
30 #include "client.h"
31
32 class LocalPlayer;
33 struct MapDrawControl;
34 class IGameDef;
35 class WieldMeshSceneNode;
36
37 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
38
39 /*
40         Client camera class, manages the player and camera scene nodes, the viewing distance
41         and performs view bobbing etc. It also displays the wielded tool in front of the
42         first-person camera.
43 */
44 class Camera
45 {
46 public:
47         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
48                         IGameDef *gamedef);
49         ~Camera();
50
51         // Get player scene node.
52         // This node is positioned at the player's torso (without any view bobbing),
53         // as given by Player::m_position. Yaw is applied but not pitch.
54         inline scene::ISceneNode* getPlayerNode() const
55         {
56                 return m_playernode;
57         }
58
59         // Get head scene node.
60         // It has the eye transformation and pitch applied,
61         // but no view bobbing.
62         inline scene::ISceneNode* getHeadNode() const
63         {
64                 return m_headnode;
65         }
66
67         // Get camera scene node.
68         // It has the eye transformation, pitch and view bobbing applied.
69         inline scene::ICameraSceneNode* getCameraNode() const
70         {
71                 return m_cameranode;
72         }
73
74         // Get the camera position (in absolute scene coordinates).
75         // This has view bobbing applied.
76         inline v3f getPosition() const
77         {
78                 return m_camera_position;
79         }
80
81         // Get the camera direction (in absolute camera coordinates).
82         // This has view bobbing applied.
83         inline v3f getDirection() const
84         {
85                 return m_camera_direction;
86         }
87         
88         // Get the camera offset
89         inline v3s16 getOffset() const
90         {
91                 return m_camera_offset;
92         }
93
94         // Horizontal field of view
95         inline f32 getFovX() const
96         {
97                 return m_fov_x;
98         }
99
100         // Vertical field of view
101         inline f32 getFovY() const
102         {
103                 return m_fov_y;
104         }
105
106         // Get maximum of getFovX() and getFovY()
107         inline f32 getFovMax() const
108         {
109                 return MYMAX(m_fov_x, m_fov_y);
110         }
111
112         // Checks if the constructor was able to create the scene nodes
113         bool successfullyCreated(std::wstring& error_message);
114
115         // Step the camera: updates the viewing range and view bobbing.
116         void step(f32 dtime);
117
118         // Update the camera from the local player's position.
119         // busytime is used to adjust the viewing range.
120         void update(LocalPlayer* player, f32 frametime, f32 busytime,
121                         f32 tool_reload_ratio, ClientEnvironment &c_env);
122
123         // Render distance feedback loop
124         void updateViewingRange(f32 frametime_in, f32 busytime_in);
125
126         // Start digging animation
127         // Pass 0 for left click, 1 for right click
128         void setDigging(s32 button);
129
130         // Replace the wielded item mesh
131         void wield(const ItemStack &item);
132
133         // Draw the wielded tool.
134         // This has to happen *after* the main scene is drawn.
135         // Warning: This clears the Z buffer.
136         void drawWieldedTool(irr::core::matrix4* translation=NULL);
137
138         // Toggle the current camera mode
139         void toggleCameraMode() {
140                 if (m_camera_mode == CAMERA_MODE_FIRST)
141                         m_camera_mode = CAMERA_MODE_THIRD;
142                 else if (m_camera_mode == CAMERA_MODE_THIRD)
143                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
144                 else
145                         m_camera_mode = CAMERA_MODE_FIRST;
146         }
147
148         //read the current camera mode
149         inline CameraMode getCameraMode()
150         {
151                 return m_camera_mode;
152         }
153
154 private:
155         // Nodes
156         scene::ISceneNode* m_playernode;
157         scene::ISceneNode* m_headnode;
158         scene::ICameraSceneNode* m_cameranode;
159
160         scene::ISceneManager* m_wieldmgr;
161         WieldMeshSceneNode* m_wieldnode;
162         scene::ILightSceneNode* m_wieldlightnode;
163
164         // draw control
165         MapDrawControl& m_draw_control;
166         
167         IGameDef *m_gamedef;
168
169         // Absolute camera position
170         v3f m_camera_position;
171         // Absolute camera direction
172         v3f m_camera_direction;
173         // Camera offset
174         v3s16 m_camera_offset;
175
176         // Field of view and aspect ratio stuff
177         f32 m_aspect;
178         f32 m_fov_x;
179         f32 m_fov_y;
180
181         // Stuff for viewing range calculations
182         f32 m_added_busytime;
183         s16 m_added_frames;
184         f32 m_range_old;
185         f32 m_busytime_old;
186         f32 m_frametime_counter;
187         f32 m_time_per_range;
188
189         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
190         f32 m_view_bobbing_anim;
191         // If 0, view bobbing is off (e.g. player is standing).
192         // If 1, view bobbing is on (player is walking).
193         // If 2, view bobbing is getting switched off.
194         s32 m_view_bobbing_state;
195         // Speed of view bobbing animation
196         f32 m_view_bobbing_speed;
197         // Fall view bobbing
198         f32 m_view_bobbing_fall;
199
200         // Digging animation frame (0 <= m_digging_anim < 1)
201         f32 m_digging_anim;
202         // If -1, no digging animation
203         // If 0, left-click digging animation
204         // If 1, right-click digging animation
205         s32 m_digging_button;
206
207         // Animation when changing wielded item
208         f32 m_wield_change_timer;
209         ItemStack m_wield_item_next;
210
211         CameraMode m_camera_mode;
212 };
213
214 #endif