]> git.lizzy.rs Git - minetest.git/blob - src/camera.h
Merge pull request #46 from garrosh/master
[minetest.git] / src / camera.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
24 #include "inventory.h"
25 #include "mesh.h"
26 #include "tile.h"
27 #include "utility.h"
28 #include <ICameraSceneNode.h>
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class IGameDef;
33
34 /*
35         Client camera class, manages the player and camera scene nodes, the viewing distance
36         and performs view bobbing etc. It also displays the wielded tool in front of the
37         first-person camera.
38 */
39 class Camera
40 {
41 public:
42         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
43         ~Camera();
44
45         // Get player scene node.
46         // This node is positioned at the player's torso (without any view bobbing),
47         // as given by Player::m_position. Yaw is applied but not pitch.
48         inline scene::ISceneNode* getPlayerNode() const
49         {
50                 return m_playernode;
51         }
52
53         // Get head scene node.
54         // It has the eye transformation and pitch applied,
55         // but no view bobbing.
56         inline scene::ISceneNode* getHeadNode() const
57         {
58                 return m_headnode;
59         }
60
61         // Get camera scene node.
62         // It has the eye transformation, pitch and view bobbing applied.
63         inline scene::ICameraSceneNode* getCameraNode() const
64         {
65                 return m_cameranode;
66         }
67
68         // Get the camera position (in absolute scene coordinates).
69         // This has view bobbing applied.
70         inline v3f getPosition() const
71         {
72                 return m_camera_position;
73         }
74
75         // Get the camera direction (in absolute camera coordinates).
76         // This has view bobbing applied.
77         inline v3f getDirection() const
78         {
79                 return m_camera_direction;
80         }
81
82         // Horizontal field of view
83         inline f32 getFovX() const
84         {
85                 return m_fov_x;
86         }
87
88         // Vertical field of view
89         inline f32 getFovY() const
90         {
91                 return m_fov_y;
92         }
93
94         // Get maximum of getFovX() and getFovY()
95         inline f32 getFovMax() const
96         {
97                 return MYMAX(m_fov_x, m_fov_y);
98         }
99
100         // Checks if the constructor was able to create the scene nodes
101         bool successfullyCreated(std::wstring& error_message);
102
103         // Step the camera: updates the viewing range and view bobbing.
104         void step(f32 dtime);
105
106         // Update the camera from the local player's position.
107         // frametime is used to adjust the viewing range.
108         void update(LocalPlayer* player, f32 frametime, v2u32 screensize);
109
110         // Render distance feedback loop
111         void updateViewingRange(f32 frametime_in);
112
113         // Start digging animation
114         // Pass 0 for left click, 1 for right click
115         void setDigging(s32 button);
116
117         // Replace the wielded item mesh
118         void wield(const ItemStack &item, IGameDef *gamedef);
119
120         // Draw the wielded tool.
121         // This has to happen *after* the main scene is drawn.
122         // Warning: This clears the Z buffer.
123         void drawWieldedTool();
124
125 private:
126         // Scene manager and nodes
127         scene::ISceneManager* m_smgr;
128         scene::ISceneNode* m_playernode;
129         scene::ISceneNode* m_headnode;
130         scene::ICameraSceneNode* m_cameranode;
131
132         scene::ISceneManager* m_wieldmgr;
133         scene::IMeshSceneNode* m_wieldnode;
134         u8 m_wieldlight;
135
136         // draw control
137         MapDrawControl& m_draw_control;
138
139         // Absolute camera position
140         v3f m_camera_position;
141         // Absolute camera direction
142         v3f m_camera_direction;
143
144         // Field of view and aspect ratio stuff
145         f32 m_aspect;
146         f32 m_fov_x;
147         f32 m_fov_y;
148
149         // Stuff for viewing range calculations
150         f32 m_added_frametime;
151         s16 m_added_frames;
152         f32 m_range_old;
153         f32 m_frametime_old;
154         f32 m_frametime_counter;
155         f32 m_time_per_range;
156
157         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
158         f32 m_view_bobbing_anim;
159         // If 0, view bobbing is off (e.g. player is standing).
160         // If 1, view bobbing is on (player is walking).
161         // If 2, view bobbing is getting switched off.
162         s32 m_view_bobbing_state;
163         // Speed of view bobbing animation
164         f32 m_view_bobbing_speed;
165
166         // Digging animation frame (0 <= m_digging_anim < 1)
167         f32 m_digging_anim;
168         // If -1, no digging animation
169         // If 0, left-click digging animation
170         // If 1, right-click digging animation
171         s32 m_digging_button;
172 };
173
174 #endif