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