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"
27 #include <ICameraSceneNode.h>
28 #include <IMeshCache.h>
29 #include <IAnimatedMesh.h>
33 class ExtrudedSpriteSceneNode;
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
44 Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
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
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
63 // Get camera scene node.
64 // It has the eye transformation, pitch and view bobbing applied.
65 inline scene::ICameraSceneNode* getCameraNode() const
70 // Get the camera position (in absolute scene coordinates).
71 // This has view bobbing applied.
72 inline v3f getPosition() const
74 return m_camera_position;
77 // Get the camera direction (in absolute camera coordinates).
78 // This has view bobbing applied.
79 inline v3f getDirection() const
81 return m_camera_direction;
84 // Horizontal field of view
85 inline f32 getFovX() const
90 // Vertical field of view
91 inline f32 getFovY() const
96 // Get maximum of getFovX() and getFovY()
97 inline f32 getFovMax() const
99 return MYMAX(m_fov_x, m_fov_y);
102 // Checks if the constructor was able to create the scene nodes
103 bool successfullyCreated(std::wstring& error_message);
105 // Step the camera: updates the viewing range and view bobbing.
106 void step(f32 dtime);
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);
112 // Render distance feedback loop
113 void updateViewingRange(f32 frametime_in);
115 // Update settings from g_settings
116 void updateSettings();
118 // Replace the wielded item mesh
119 void wield(const InventoryItem* item, IGameDef *gamedef);
121 // Start digging animation
122 // Pass 0 for left click, 1 for right click
123 void setDigging(s32 button);
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();
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;
137 scene::ISceneManager* m_wieldmgr;
138 ExtrudedSpriteSceneNode* m_wieldnode;
141 MapDrawControl& m_draw_control;
143 // viewing_range_min_nodes setting
144 f32 m_viewing_range_min;
145 // viewing_range_max_nodes setting
146 f32 m_viewing_range_max;
148 // Absolute camera position
149 v3f m_camera_position;
150 // Absolute camera direction
151 v3f m_camera_direction;
153 // Field of view and aspect ratio stuff
158 // Stuff for viewing range calculations
159 f32 m_wanted_frametime;
160 f32 m_added_frametime;
164 f32 m_frametime_counter;
165 f32 m_time_per_range;
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;
176 // Digging animation frame (0 <= m_digging_anim < 1)
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;
186 A scene node that displays a 2D mesh extruded into the third dimension,
187 to add an illusion of depth.
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.
194 class ExtrudedSpriteSceneNode: public scene::ISceneNode
197 ExtrudedSpriteSceneNode(
198 scene::ISceneNode* parent,
199 scene::ISceneManager* mgr,
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();
206 void setSprite(video::ITexture* texture);
207 void setCube(const TileSpec tiles[6]);
209 f32 getSpriteThickness() const { return m_thickness; }
210 void setSpriteThickness(f32 thickness);
212 void updateLight(u8 light);
214 void removeSpriteFromCache(video::ITexture* texture);
216 virtual const core::aabbox3d<f32>& getBoundingBox() const;
217 virtual void OnRegisterSceneNode();
218 virtual void render();
221 scene::IMeshSceneNode* m_meshnode;
223 scene::IMesh* m_cubemesh;
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();