3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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.
22 #include "irrlichttypes_extrabloated.h"
23 #include "inventory.h"
24 #include "client/tile.h"
25 #include <ICameraSceneNode.h>
26 #include <ISceneNode.h>
28 #include "util/Optional.h"
31 struct MapDrawControl;
33 class RenderingEngine;
34 class WieldMeshSceneNode;
38 scene::ISceneNode *parent_node;
40 video::SColor textcolor;
41 Optional<video::SColor> bgcolor;
44 Nametag(scene::ISceneNode *a_parent_node,
45 const std::string &text,
46 const video::SColor &textcolor,
47 const Optional<video::SColor> &bgcolor,
49 parent_node(a_parent_node),
57 video::SColor getBgColor(bool use_fallback) const
60 return bgcolor.value();
61 else if (!use_fallback)
62 return video::SColor(0, 0, 0, 0);
63 else if (textcolor.getLuminance() > 186)
64 // Dark background for light text
65 return video::SColor(50, 50, 50, 50);
67 // Light background for dark text
68 return video::SColor(50, 255, 255, 255);
72 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
75 Client camera class, manages the player and camera scene nodes, the viewing distance
76 and performs view bobbing etc. It also displays the wielded tool in front of the
82 Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine);
85 // Get camera scene node.
86 // It has the eye transformation, pitch and view bobbing applied.
87 inline scene::ICameraSceneNode* getCameraNode() const
92 // Get the camera position (in absolute scene coordinates).
93 // This has view bobbing applied.
94 inline v3f getPosition() const
96 return m_camera_position;
99 // Returns the absolute position of the head SceneNode in the world
100 inline v3f getHeadPosition() const
102 return m_headnode->getAbsolutePosition();
105 // Get the camera direction (in absolute camera coordinates).
106 // This has view bobbing applied.
107 inline v3f getDirection() const
109 return m_camera_direction;
112 // Get the camera offset
113 inline v3s16 getOffset() const
115 return m_camera_offset;
118 // Horizontal field of view
119 inline f32 getFovX() const
124 // Vertical field of view
125 inline f32 getFovY() const
130 // Get maximum of getFovX() and getFovY()
131 inline f32 getFovMax() const
133 return MYMAX(m_fov_x, m_fov_y);
136 // Notify about new server-sent FOV and initialize smooth FOV transition
137 void notifyFovChange();
139 // Checks if the constructor was able to create the scene nodes
140 bool successfullyCreated(std::string &error_message);
142 // Step the camera: updates the viewing range and view bobbing.
143 void step(f32 dtime);
145 // Update the camera from the local player's position.
146 // busytime is used to adjust the viewing range.
147 void update(LocalPlayer* player, f32 frametime, f32 busytime,
148 f32 tool_reload_ratio);
150 // Update render distance
151 void updateViewingRange();
153 // Start digging animation
154 // Pass 0 for left click, 1 for right click
155 void setDigging(s32 button);
157 // Replace the wielded item mesh
158 void wield(const ItemStack &item);
160 // Draw the wielded tool.
161 // This has to happen *after* the main scene is drawn.
162 // Warning: This clears the Z buffer.
163 void drawWieldedTool(irr::core::matrix4* translation=NULL);
165 // Toggle the current camera mode
166 void toggleCameraMode() {
167 if (m_camera_mode == CAMERA_MODE_FIRST)
168 m_camera_mode = CAMERA_MODE_THIRD;
169 else if (m_camera_mode == CAMERA_MODE_THIRD)
170 m_camera_mode = CAMERA_MODE_THIRD_FRONT;
172 m_camera_mode = CAMERA_MODE_FIRST;
175 // Set the current camera mode
176 inline void setCameraMode(CameraMode mode)
178 m_camera_mode = mode;
181 //read the current camera mode
182 inline CameraMode getCameraMode()
184 return m_camera_mode;
187 Nametag *addNametag(scene::ISceneNode *parent_node,
188 const std::string &text, video::SColor textcolor,
189 Optional<video::SColor> bgcolor, const v3f &pos);
191 void removeNametag(Nametag *nametag);
195 inline void addArmInertia(f32 player_yaw);
199 scene::ISceneNode *m_playernode = nullptr;
200 scene::ISceneNode *m_headnode = nullptr;
201 scene::ICameraSceneNode *m_cameranode = nullptr;
203 scene::ISceneManager *m_wieldmgr = nullptr;
204 WieldMeshSceneNode *m_wieldnode = nullptr;
207 MapDrawControl& m_draw_control;
211 // Default Client FOV (as defined by the "fov" setting)
214 // Absolute camera position
215 v3f m_camera_position;
216 // Absolute camera direction
217 v3f m_camera_direction;
219 v3s16 m_camera_offset;
221 // Server-sent FOV variables
222 bool m_server_sent_fov = false;
223 f32 m_curr_fov_degrees, m_old_fov_degrees, m_target_fov_degrees;
225 // FOV transition variables
226 bool m_fov_transition_active = false;
227 f32 m_fov_diff, m_transition_time;
229 v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
235 // Field of view and aspect ratio stuff
240 // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
241 f32 m_view_bobbing_anim = 0.0f;
242 // If 0, view bobbing is off (e.g. player is standing).
243 // If 1, view bobbing is on (player is walking).
244 // If 2, view bobbing is getting switched off.
245 s32 m_view_bobbing_state = 0;
246 // Speed of view bobbing animation
247 f32 m_view_bobbing_speed = 0.0f;
249 f32 m_view_bobbing_fall = 0.0f;
251 // Digging animation frame (0 <= m_digging_anim < 1)
252 f32 m_digging_anim = 0.0f;
253 // If -1, no digging animation
254 // If 0, left-click digging animation
255 // If 1, right-click digging animation
256 s32 m_digging_button = -1;
258 // Animation when changing wielded item
259 f32 m_wield_change_timer = 0.125f;
260 ItemStack m_wield_item_next;
262 CameraMode m_camera_mode = CAMERA_MODE_FIRST;
264 f32 m_cache_fall_bobbing_amount;
265 f32 m_cache_view_bobbing_amount;
268 std::list<Nametag *> m_nametags;
269 bool m_show_nametag_backgrounds;