3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "common_irrlicht.h"
24 #include "inventory.h"
30 class ExtrudedSpriteSceneNode;
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
40 Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
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
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
59 // Get camera scene node.
60 // It has the eye transformation, pitch and view bobbing applied.
61 inline scene::ICameraSceneNode* getCameraNode() const
66 // Get wielded item scene node.
67 inline ExtrudedSpriteSceneNode* getWieldNode() const
72 // Get the camera position (in absolute scene coordinates).
73 // This has view bobbing applied.
74 inline v3f getPosition() const
76 return m_camera_position;
79 // Get the camera direction (in absolute camera coordinates).
80 // This has view bobbing applied.
81 inline v3f getDirection() const
83 return m_camera_direction;
86 // Horizontal field of view
87 inline f32 getFovX() const
92 // Vertical field of view
93 inline f32 getFovY() const
98 // Get maximum of getFovX() and getFovY()
99 inline f32 getFovMax() const
101 return MYMAX(m_fov_x, m_fov_y);
104 // Checks if the constructor was able to create the scene nodes
105 bool successfullyCreated(std::wstring& error_message);
107 // Step the camera: updates the viewing range and view bobbing.
108 void step(f32 dtime);
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);
114 // Render distance feedback loop
115 void updateViewingRange(f32 frametime_in);
117 // Update settings from g_settings
118 void updateSettings();
120 // Replace the wielded item mesh
121 void wield(const InventoryItem* item);
123 // Start digging animation
124 // Pass 0 for left click, 1 for right click
125 void setDigging(s32 button);
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;
136 MapDrawControl& m_draw_control;
138 // viewing_range_min_nodes setting
139 f32 m_viewing_range_min;
140 // viewing_range_max_nodes setting
141 f32 m_viewing_range_max;
143 // Absolute camera position
144 v3f m_camera_position;
145 // Absolute camera direction
146 v3f m_camera_direction;
148 // Field of view and aspect ratio stuff
153 // Stuff for viewing range calculations
154 f32 m_wanted_frametime;
155 f32 m_added_frametime;
159 f32 m_frametime_counter;
160 f32 m_time_per_range;
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;
171 // Digging animation frame (0 <= m_digging_anim < 1)
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;
181 A scene node that displays a 2D mesh extruded into the third dimension,
182 to add an illusion of depth.
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.
189 class ExtrudedSpriteSceneNode: public scene::ISceneNode
192 ExtrudedSpriteSceneNode(
193 scene::ISceneNode* parent,
194 scene::ISceneManager* mgr,
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();
201 void setSprite(video::ITexture* texture);
202 void setCube(const TileSpec tiles[6]);
204 f32 getSpriteThickness() const { return m_thickness; }
205 void setSpriteThickness(f32 thickness);
207 void removeSpriteFromCache(video::ITexture* texture);
209 virtual const core::aabbox3d<f32>& getBoundingBox() const;
210 virtual void OnRegisterSceneNode();
211 virtual void render();
214 scene::IMeshSceneNode* m_meshnode;
216 scene::IMesh* m_cubemesh;
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();