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