]> git.lizzy.rs Git - dragonfireclient.git/blob - src/camera.h
Add player:set_eye_offset() by @MirceaKitsune and clean up
[dragonfireclient.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
36 enum CameraModes {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
37
38 /*
39         Client camera class, manages the player and camera scene nodes, the viewing distance
40         and performs view bobbing etc. It also displays the wielded tool in front of the
41         first-person camera.
42 */
43 class Camera
44 {
45 public:
46         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
47                         IGameDef *gamedef);
48         ~Camera();
49
50         // Get player scene node.
51         // This node is positioned at the player's torso (without any view bobbing),
52         // as given by Player::m_position. Yaw is applied but not pitch.
53         inline scene::ISceneNode* getPlayerNode() const
54         {
55                 return m_playernode;
56         }
57
58         // Get head scene node.
59         // It has the eye transformation and pitch applied,
60         // but no view bobbing.
61         inline scene::ISceneNode* getHeadNode() const
62         {
63                 return m_headnode;
64         }
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::wstring& 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                         v2u32 screensize, f32 tool_reload_ratio,
121                         int current_camera_mode, 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, u16 playeritem);
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();
137
138 private:
139         // Scene manager and nodes
140         scene::ISceneManager* m_smgr;
141         scene::ISceneNode* m_playernode;
142         scene::ISceneNode* m_headnode;
143         scene::ICameraSceneNode* m_cameranode;
144
145         scene::ISceneManager* m_wieldmgr;
146         scene::IMeshSceneNode* m_wieldnode;
147         u8 m_wieldlight;
148
149         // draw control
150         MapDrawControl& m_draw_control;
151         
152         IGameDef *m_gamedef;
153
154         // Absolute camera position
155         v3f m_camera_position;
156         // Absolute camera direction
157         v3f m_camera_direction;
158         // Camera offset
159         v3s16 m_camera_offset;
160
161         // Field of view and aspect ratio stuff
162         f32 m_aspect;
163         f32 m_fov_x;
164         f32 m_fov_y;
165
166         // Stuff for viewing range calculations
167         f32 m_added_busytime;
168         s16 m_added_frames;
169         f32 m_range_old;
170         f32 m_busytime_old;
171         f32 m_frametime_counter;
172         f32 m_time_per_range;
173
174         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
175         f32 m_view_bobbing_anim;
176         // If 0, view bobbing is off (e.g. player is standing).
177         // If 1, view bobbing is on (player is walking).
178         // If 2, view bobbing is getting switched off.
179         s32 m_view_bobbing_state;
180         // Speed of view bobbing animation
181         f32 m_view_bobbing_speed;
182         // Fall view bobbing
183         f32 m_view_bobbing_fall;
184
185         // Digging animation frame (0 <= m_digging_anim < 1)
186         f32 m_digging_anim;
187         // If -1, no digging animation
188         // If 0, left-click digging animation
189         // If 1, right-click digging animation
190         s32 m_digging_button;
191
192         //dummymesh for camera
193         irr::scene::IAnimatedMesh* m_dummymesh;
194
195         // Animation when changing wielded item
196         f32 m_wield_change_timer;
197         scene::IMesh *m_wield_mesh_next;
198         u16 m_previous_playeritem;
199         std::string m_previous_itemname;
200 };
201
202 #endif