]> git.lizzy.rs Git - minetest.git/blob - src/camera.h
inventorycube: use all three specified textures; also moved mesh creation / modificat...
[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 #include <IMeshCache.h>
30 #include <IAnimatedMesh.h>
31
32 class LocalPlayer;
33 struct MapDrawControl;
34 class ExtrudedSpriteSceneNode;
35 class IGameDef;
36
37 /*
38         Client camera class, manages the player and camera scene nodes, the viewing distance
39         and performs view bobbing etc. It also displays the wielded tool in front of the
40         first-person camera.
41 */
42 class Camera
43 {
44 public:
45         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
46         ~Camera();
47
48         // Get player scene node.
49         // This node is positioned at the player's torso (without any view bobbing),
50         // as given by Player::m_position. Yaw is applied but not pitch.
51         inline scene::ISceneNode* getPlayerNode() const
52         {
53                 return m_playernode;
54         }
55
56         // Get head scene node.
57         // It has the eye transformation and pitch applied,
58         // but no view bobbing.
59         inline scene::ISceneNode* getHeadNode() const
60         {
61                 return m_headnode;
62         }
63
64         // Get camera scene node.
65         // It has the eye transformation, pitch and view bobbing applied.
66         inline scene::ICameraSceneNode* getCameraNode() const
67         {
68                 return m_cameranode;
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(const InventoryItem* item, IGameDef *gamedef);
121
122         // Start digging animation
123         // Pass 0 for left click, 1 for right click
124         void setDigging(s32 button);
125
126         // Draw the wielded tool.
127         // This has to happen *after* the main scene is drawn.
128         // Warning: This clears the Z buffer.
129         void drawWieldedTool();
130
131 private:
132         // Scene manager and nodes
133         scene::ISceneManager* m_smgr;
134         scene::ISceneNode* m_playernode;
135         scene::ISceneNode* m_headnode;
136         scene::ICameraSceneNode* m_cameranode;
137
138         scene::ISceneManager* m_wieldmgr;
139         ExtrudedSpriteSceneNode* m_wieldnode;
140
141         // draw control
142         MapDrawControl& m_draw_control;
143
144         // viewing_range_min_nodes setting
145         f32 m_viewing_range_min;
146         // viewing_range_max_nodes setting
147         f32 m_viewing_range_max;
148
149         // Absolute camera position
150         v3f m_camera_position;
151         // Absolute camera direction
152         v3f m_camera_direction;
153
154         // Field of view and aspect ratio stuff
155         f32 m_aspect;
156         f32 m_fov_x;
157         f32 m_fov_y;
158
159         // Stuff for viewing range calculations
160         f32 m_wanted_frametime;
161         f32 m_added_frametime;
162         s16 m_added_frames;
163         f32 m_range_old;
164         f32 m_frametime_old;
165         f32 m_frametime_counter;
166         f32 m_time_per_range;
167
168         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
169         f32 m_view_bobbing_anim;
170         // If 0, view bobbing is off (e.g. player is standing).
171         // If 1, view bobbing is on (player is walking).
172         // If 2, view bobbing is getting switched off.
173         s32 m_view_bobbing_state;
174         // Speed of view bobbing animation
175         f32 m_view_bobbing_speed;
176
177         // Digging animation frame (0 <= m_digging_anim < 1)
178         f32 m_digging_anim;
179         // If -1, no digging animation
180         // If 0, left-click digging animation
181         // If 1, right-click digging animation
182         s32 m_digging_button;
183 };
184
185
186 /*
187         A scene node that displays a 2D mesh extruded into the third dimension,
188         to add an illusion of depth.
189
190         Since this class was created to display the wielded tool of the local
191         player, and only tools and items are rendered like this (but not solid
192         content like stone and mud, which are shown as cubes), the option to
193         draw a textured cube instead is provided.
194  */
195 class ExtrudedSpriteSceneNode: public scene::ISceneNode
196 {
197 public:
198         ExtrudedSpriteSceneNode(
199                 scene::ISceneNode* parent,
200                 scene::ISceneManager* mgr,
201                 s32 id = -1,
202                 const v3f& position = v3f(0,0,0),
203                 const v3f& rotation = v3f(0,0,0),
204                 const v3f& scale = v3f(1,1,1));
205         ~ExtrudedSpriteSceneNode();
206
207         void setSprite(video::ITexture* texture);
208         void setCube(const TileSpec tiles[6]);
209
210         void updateLight(u8 light);
211
212         void removeSpriteFromCache(video::ITexture* texture);
213
214         virtual const core::aabbox3d<f32>& getBoundingBox() const;
215         virtual void OnRegisterSceneNode();
216         virtual void render();
217
218 private:
219         scene::IMeshSceneNode* m_meshnode;
220         scene::IMesh* m_cubemesh;
221         bool m_is_cube;
222         u8 m_light;
223
224         io::path getExtrudedName(video::ITexture* texture);
225 };
226
227 #endif