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