026f928741c41011e4cbe1346bc7cb63aa3bfc9b
[oweals/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 "tile.h"
26 #include "utility.h"
27
28 class LocalPlayer;
29 class MapDrawControl;
30 class ExtrudedSpriteSceneNode;
31
32 /*
33         Client camera class, manages the player and camera scene nodes, the viewing distance
34         and performs view bobbing etc. It also displays the wielded tool in front of the
35         first-person camera.
36 */
37 class Camera
38 {
39 public:
40         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
41         ~Camera();
42
43         // Get player scene node.
44         // This node is positioned at the player's torso (without any view bobbing),
45         // as given by Player::m_position. Yaw is applied but not pitch.
46         inline scene::ISceneNode* getPlayerNode() const
47         {
48                 return m_playernode;
49         }
50
51         // Get head scene node.
52         // It has the eye transformation and pitch applied,
53         // but no view bobbing.
54         inline scene::ISceneNode* getHeadNode() const
55         {
56                 return m_headnode;
57         }
58
59         // Get camera scene node.
60         // It has the eye transformation, pitch and view bobbing applied.
61         inline scene::ICameraSceneNode* getCameraNode() const
62         {
63                 return m_cameranode;
64         }
65
66         // Get wielded item scene node.
67         inline ExtrudedSpriteSceneNode* getWieldNode() const
68         {
69                 return m_wieldnode;
70         }
71
72         // Get the camera position (in absolute scene coordinates).
73         // This has view bobbing applied.
74         inline v3f getPosition() const
75         {
76                 return m_camera_position;
77         }
78
79         // Get the camera direction (in absolute camera coordinates).
80         // This has view bobbing applied.
81         inline v3f getDirection() const
82         {
83                 return m_camera_direction;
84         }
85
86         // Horizontal field of view
87         inline f32 getFovX() const
88         {
89                 return m_fov_x;
90         }
91
92         // Vertical field of view
93         inline f32 getFovY() const
94         {
95                 return m_fov_y;
96         }
97
98         // Get maximum of getFovX() and getFovY()
99         inline f32 getFovMax() const
100         {
101                 return MYMAX(m_fov_x, m_fov_y);
102         }
103
104         // Checks if the constructor was able to create the scene nodes
105         bool successfullyCreated(std::wstring& error_message);
106
107         // Step the camera: updates the viewing range and view bobbing.
108         void step(f32 dtime);
109
110         // Update the camera from the local player's position.
111         // frametime is used to adjust the viewing range.
112         void update(LocalPlayer* player, f32 frametime, v2u32 screensize);
113
114         // Render distance feedback loop
115         void updateViewingRange(f32 frametime_in);
116
117         // Update settings from g_settings
118         void updateSettings();
119
120         // Replace the wielded item mesh
121         void wield(const InventoryItem* item);
122
123         // Start digging animation
124         // Pass 0 for left click, 1 for right click
125         void setDigging(s32 button);
126
127 private:
128         // Scene manager and nodes
129         scene::ISceneManager* m_smgr;
130         scene::ISceneNode* m_playernode;
131         scene::ISceneNode* m_headnode;
132         scene::ICameraSceneNode* m_cameranode;
133         ExtrudedSpriteSceneNode* m_wieldnode;
134
135         // draw control
136         MapDrawControl& m_draw_control;
137
138         // viewing_range_min_nodes setting
139         f32 m_viewing_range_min;
140         // viewing_range_max_nodes setting
141         f32 m_viewing_range_max;
142
143         // Absolute camera position
144         v3f m_camera_position;
145         // Absolute camera direction
146         v3f m_camera_direction;
147
148         // Field of view and aspect ratio stuff
149         f32 m_aspect;
150         f32 m_fov_x;
151         f32 m_fov_y;
152
153         // Stuff for viewing range calculations
154         f32 m_wanted_frametime;
155         f32 m_added_frametime;
156         s16 m_added_frames;
157         f32 m_range_old;
158         f32 m_frametime_old;
159         f32 m_frametime_counter;
160         f32 m_time_per_range;
161
162         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
163         f32 m_view_bobbing_anim;
164         // If 0, view bobbing is off (e.g. player is standing).
165         // If 1, view bobbing is on (player is walking).
166         // If 2, view bobbing is getting switched off.
167         s32 m_view_bobbing_state;
168         // Speed of view bobbing animation
169         f32 m_view_bobbing_speed;
170
171         // Digging animation frame (0 <= m_digging_anim < 1)
172         f32 m_digging_anim;
173         // If -1, no digging animation
174         // If 0, left-click digging animation
175         // If 1, right-click digging animation
176         s32 m_digging_button;
177 };
178
179
180 /*
181         A scene node that displays a 2D mesh extruded into the third dimension,
182         to add an illusion of depth.
183
184         Since this class was created to display the wielded tool of the local
185         player, and only tools and items are rendered like this (but not solid
186         content like stone and mud, which are shown as cubes), the option to
187         draw a textured cube instead is provided.
188  */
189 class ExtrudedSpriteSceneNode: public scene::ISceneNode
190 {
191 public:
192         ExtrudedSpriteSceneNode(
193                 scene::ISceneNode* parent,
194                 scene::ISceneManager* mgr,
195                 s32 id = -1,
196                 const v3f& position = v3f(0,0,0),
197                 const v3f& rotation = v3f(0,0,0),
198                 const v3f& scale = v3f(1,1,1));
199         ~ExtrudedSpriteSceneNode();
200
201         void setSprite(video::ITexture* texture);
202         void setCube(const TileSpec tiles[6]);
203
204         f32 getSpriteThickness() const { return m_thickness; }
205         void setSpriteThickness(f32 thickness);
206
207         void removeSpriteFromCache(video::ITexture* texture);
208
209         virtual const core::aabbox3d<f32>& getBoundingBox() const;
210         virtual void OnRegisterSceneNode();
211         virtual void render();
212
213 private:
214         scene::IMeshSceneNode* m_meshnode;
215         f32 m_thickness;
216         scene::IMesh* m_cubemesh;
217         bool m_is_cube;
218
219         // internal extrusion helper methods
220         io::path getExtrudedName(video::ITexture* texture);
221         scene::IAnimatedMesh* extrudeARGB(u32 width, u32 height, u8* data);
222         scene::IAnimatedMesh* extrude(video::ITexture* texture);
223         scene::IMesh* createCubeMesh();
224 };
225
226 #endif