From baf7da9d4a7fc0566840b159903999d76d99a228 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Thu, 8 Sep 2011 01:08:47 +0200 Subject: Collected and moved existing camera infrastructure from game.cpp to camera.cpp and camera.h. Introduced configuration settings 'fov' which chooses the camera's (vertical) field of view and 'view_bobbing' which currently does nothing. Other code refactored to not expect the FOV to be a build time constant. --- src/camera.h | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/camera.h (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h new file mode 100644 index 000000000..88f03e66d --- /dev/null +++ b/src/camera.h @@ -0,0 +1,141 @@ +/* +Minetest-c55 +Copyright (C) 2010-2011 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef CAMERA_HEADER +#define CAMERA_HEADER + +#include "common_irrlicht.h" +#include "utility.h" + +class LocalPlayer; +class MapDrawControl; + +/* + Client camera class, manages the player and camera scene nodes, the viewing distance + and performs view bobbing etc. +*/ +class Camera +{ +public: + Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control); + ~Camera(); + + // Get player scene node. + // This node is positioned at the player's torso (without any view bobbing), + // as given by Player::m_position, Player::m_pitch and Player::m_yaw. + // Things like wielded tools should be positioned relative to this node. + inline scene::ISceneNode* getPlayerNode() const + { + return m_playernode; + } + + // Get camera scene node. + // The camera node is a child of the player node. + // It has the eye transformation and view bobbing applied. + inline scene::ICameraSceneNode* getCameraNode() const + { + return m_cameranode; + } + + // Get the camera position (in absolute scene coordinates). + // This has view bobbing applied. + inline v3f getPosition() const + { + return m_camera_position; + } + + // Get the camera direction (in absolute camera coordinates). + // This has view bobbing applied. + inline v3f getDirection() const + { + return m_camera_direction; + } + + // Horizontal field of view + inline f32 getFovX() const + { + return m_fov_x; + } + + // Vertical field of view + inline f32 getFovY() const + { + return m_fov_y; + } + + // Get maximum of getFovX() and getFovY() + inline f32 getFovMax() const + { + return MYMAX(m_fov_x, m_fov_y); + } + + // Step the camera: updates the viewing range and view bobbing. + void step(f32 dtime); + + // Update the camera from the local player's position. + // frametime is used to adjust the viewing range. + void update(LocalPlayer* player, f32 frametime, v2u32 screensize); + + // Render distance feedback loop + void updateViewingRange(f32 frametime_in); + + // Update settings from g_settings + void updateSettings(); + +private: + // Scene manager and nodes + scene::ISceneManager* m_smgr; + scene::ISceneNode* m_playernode; + scene::ICameraSceneNode* m_cameranode; + + // draw control + MapDrawControl& m_draw_control; + + // viewing_range_min_nodes setting + f32 m_viewing_range_min; + // viewing_range_max_nodes setting + f32 m_viewing_range_max; + + // Absolute camera position + v3f m_camera_position; + // Absolute camera direction + v3f m_camera_direction; + + // Field of view and aspect ratio stuff + f32 m_aspect; + f32 m_fov_x; + f32 m_fov_y; + + // Stuff for viewing range calculations + f32 m_wanted_frametime; + f32 m_added_frametime; + s16 m_added_frames; + f32 m_range_old; + f32 m_frametime_old; + f32 m_frametime_counter; + f32 m_time_per_range; + + // View bobbing animation frame (0 <= m_view_bobbing < 0x10000) + u32 m_view_bobbing_anim; + // Number of frames to continue the view bobbing animation. + u32 m_view_bobbing_anim_left; +}; + +#endif + -- cgit v1.2.3 From ccadebabba18226b1664af35888ee3aff6861be7 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Thu, 8 Sep 2011 13:03:55 +0200 Subject: Implemented view bobbing (testing simple lemniscate shape) --- src/camera.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index 88f03e66d..500168f3d 100644 --- a/src/camera.h +++ b/src/camera.h @@ -38,7 +38,7 @@ public: // Get player scene node. // This node is positioned at the player's torso (without any view bobbing), - // as given by Player::m_position, Player::m_pitch and Player::m_yaw. + // as given by Player::m_position. Yaw is applied but not pitch. // Things like wielded tools should be positioned relative to this node. inline scene::ISceneNode* getPlayerNode() const { @@ -46,7 +46,6 @@ public: } // Get camera scene node. - // The camera node is a child of the player node. // It has the eye transformation and view bobbing applied. inline scene::ICameraSceneNode* getCameraNode() const { @@ -131,10 +130,12 @@ private: f32 m_frametime_counter; f32 m_time_per_range; - // View bobbing animation frame (0 <= m_view_bobbing < 0x10000) - u32 m_view_bobbing_anim; - // Number of frames to continue the view bobbing animation. - u32 m_view_bobbing_anim_left; + // View bobbing animation frame (0 <= m_view_bobbing < 0x1000000) + s32 m_view_bobbing_anim; + // If 0, view bobbing is off (e.g. player is standing). + // If 1, view bobbing is on (player is walking). + // If 2, view bobbing is getting switched off. + s32 m_view_bobbing_state; }; #endif -- cgit v1.2.3 From fc92da432401051a8cc17618941625aec6b12d20 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Thu, 8 Sep 2011 23:31:48 +0200 Subject: View bobbing is slower in the water. --- src/camera.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index 500168f3d..ba5d72bf4 100644 --- a/src/camera.h +++ b/src/camera.h @@ -136,6 +136,8 @@ private: // If 1, view bobbing is on (player is walking). // If 2, view bobbing is getting switched off. s32 m_view_bobbing_state; + // If true, view bobbing is slown down (player is swimming) + bool m_view_bobbing_slow; }; #endif -- cgit v1.2.3 From 63266928a5e2b6f4b712695eb56cb1de3899ba21 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Thu, 15 Sep 2011 00:32:11 +0200 Subject: Made wielded tool move slightly (and smoothly) during view bobbing. Making the tool be a child node of an empty scene node instead of the camera scene node seemingly fixed the uncontrollable tool jitter, too. --- src/camera.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index ba5d72bf4..a6dd7476f 100644 --- a/src/camera.h +++ b/src/camera.h @@ -39,14 +39,21 @@ public: // Get player scene node. // This node is positioned at the player's torso (without any view bobbing), // as given by Player::m_position. Yaw is applied but not pitch. - // Things like wielded tools should be positioned relative to this node. inline scene::ISceneNode* getPlayerNode() const { return m_playernode; } + // Get head scene node. + // It has the eye transformation and pitch applied, + // but no view bobbing. + inline scene::ISceneNode* getHeadNode() const + { + return m_headnode; + } + // Get camera scene node. - // It has the eye transformation and view bobbing applied. + // It has the eye transformation, pitch and view bobbing applied. inline scene::ICameraSceneNode* getCameraNode() const { return m_cameranode; @@ -84,6 +91,9 @@ public: return MYMAX(m_fov_x, m_fov_y); } + // Checks if the constructor was able to create the scene nodes + bool successfullyCreated(std::wstring& error_message); + // Step the camera: updates the viewing range and view bobbing. void step(f32 dtime); @@ -101,6 +111,7 @@ private: // Scene manager and nodes scene::ISceneManager* m_smgr; scene::ISceneNode* m_playernode; + scene::ISceneNode* m_headnode; scene::ICameraSceneNode* m_cameranode; // draw control -- cgit v1.2.3 From ae66d611f2020e0fddb45f40af2d42c13e7ba585 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Sun, 18 Sep 2011 02:17:39 +0200 Subject: This looks more like MC view bobbing, but still not even close --- src/camera.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index a6dd7476f..dd9f30551 100644 --- a/src/camera.h +++ b/src/camera.h @@ -147,8 +147,8 @@ private: // If 1, view bobbing is on (player is walking). // If 2, view bobbing is getting switched off. s32 m_view_bobbing_state; - // If true, view bobbing is slown down (player is swimming) - bool m_view_bobbing_slow; + // Speed of view bobbing animation + f32 m_view_bobbing_speed; }; #endif -- cgit v1.2.3 From 36bcbca9acabbc47976d3d7625ffb1c9396b8fdc Mon Sep 17 00:00:00 2001 From: Kahrl Date: Mon, 19 Sep 2011 03:01:11 +0200 Subject: Added sprite extruder --- src/camera.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index dd9f30551..08c03dd19 100644 --- a/src/camera.h +++ b/src/camera.h @@ -21,14 +21,17 @@ with this program; if not, write to the Free Software Foundation, Inc., #define CAMERA_HEADER #include "common_irrlicht.h" +#include "inventory.h" #include "utility.h" class LocalPlayer; class MapDrawControl; +class ExtrudedSpriteSceneNode; /* Client camera class, manages the player and camera scene nodes, the viewing distance - and performs view bobbing etc. + and performs view bobbing etc. It also displays the wielded tool in front of the + first-person camera. */ class Camera { @@ -59,6 +62,12 @@ public: return m_cameranode; } + // Get wielded item scene node. + inline ExtrudedSpriteSceneNode* getWieldNode() const + { + return m_wieldnode; + } + // Get the camera position (in absolute scene coordinates). // This has view bobbing applied. inline v3f getPosition() const @@ -107,12 +116,19 @@ public: // Update settings from g_settings void updateSettings(); + // Replace the wielded item mesh + void wield(InventoryItem* item); + + // Start or stop digging animation + void setDigging(bool digging); + private: // Scene manager and nodes scene::ISceneManager* m_smgr; scene::ISceneNode* m_playernode; scene::ISceneNode* m_headnode; scene::ICameraSceneNode* m_cameranode; + ExtrudedSpriteSceneNode* m_wieldnode; // draw control MapDrawControl& m_draw_control; @@ -151,5 +167,51 @@ private: f32 m_view_bobbing_speed; }; -#endif +/* + A scene node that displays a 2D mesh extruded into the third dimension, + to add an illusion of depth. + + Since this class was created to display the wielded tool of the local + player, and only tools and items are rendered like this (but not solid + content like stone and mud, which are shown as cubes), the option to + draw a textured cube instead is provided. + */ +class ExtrudedSpriteSceneNode: public scene::ISceneNode +{ +public: + ExtrudedSpriteSceneNode( + scene::ISceneNode* parent, + scene::ISceneManager* mgr, + s32 id = -1, + const v3f& position = v3f(0,0,0), + const v3f& rotation = v3f(0,0,0), + const v3f& scale = v3f(0,0,0)); + ~ExtrudedSpriteSceneNode(); + + void setSprite(video::ITexture* texture); + void setCube(const TileSpec faces[6]); + + f32 getSpriteThickness() const { return m_thickness; } + void setSpriteThickness(f32 thickness); + + void removeSpriteFromCache(video::ITexture* texture); + + virtual const core::aabbox3d& getBoundingBox() const; + virtual void OnRegisterSceneNode(); + virtual void render(); + +private: + scene::IMeshSceneNode* m_meshnode; + f32 m_thickness; + scene::IMesh* m_cubemesh; + bool m_is_cube; + + // internal extrusion helper methods + io::path getExtrudedName(video::ITexture* texture); + scene::IAnimatedMesh* extrudeARGB(u32 width, u32 height, u8* data); + scene::IAnimatedMesh* extrude(video::ITexture* texture); + scene::IMesh* createCubeMesh(); +}; + +#endif -- cgit v1.2.3 From 02726f00031c8ffadf5298b037b595372be202ef Mon Sep 17 00:00:00 2001 From: Kahrl Date: Mon, 19 Sep 2011 06:37:24 +0200 Subject: Convert any inventory item into a mesh, bring back InventoryItem::getImageRay(), some const-correctness fixes --- src/camera.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index 08c03dd19..ccc224e3c 100644 --- a/src/camera.h +++ b/src/camera.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include "inventory.h" +#include "tile.h" #include "utility.h" class LocalPlayer; @@ -117,7 +118,7 @@ public: void updateSettings(); // Replace the wielded item mesh - void wield(InventoryItem* item); + void wield(const InventoryItem* item); // Start or stop digging animation void setDigging(bool digging); @@ -190,7 +191,7 @@ public: ~ExtrudedSpriteSceneNode(); void setSprite(video::ITexture* texture); - void setCube(const TileSpec faces[6]); + void setCube(const TileSpec tiles[6]); f32 getSpriteThickness() const { return m_thickness; } void setSpriteThickness(f32 thickness); -- cgit v1.2.3 From ab42549b1e48ede22d96e9f4ac017bb5d1853724 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Mon, 19 Sep 2011 17:08:42 +0200 Subject: Wielded tool updates, leaves and glass work now --- src/camera.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index ccc224e3c..f960e748e 100644 --- a/src/camera.h +++ b/src/camera.h @@ -166,6 +166,11 @@ private: s32 m_view_bobbing_state; // Speed of view bobbing animation f32 m_view_bobbing_speed; + + // Digging animation + s32 m_digging_anim; + // Speed of digging animation + s32 m_digging_speed; }; @@ -187,7 +192,7 @@ public: s32 id = -1, const v3f& position = v3f(0,0,0), const v3f& rotation = v3f(0,0,0), - const v3f& scale = v3f(0,0,0)); + const v3f& scale = v3f(1,1,1)); ~ExtrudedSpriteSceneNode(); void setSprite(video::ITexture* texture); -- cgit v1.2.3 From 01f3ae1c5089f36f1f2a164bcaa36fb2fc8642be Mon Sep 17 00:00:00 2001 From: Kahrl Date: Tue, 20 Sep 2011 11:06:16 +0200 Subject: Digging animation --- src/camera.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index f960e748e..026f92874 100644 --- a/src/camera.h +++ b/src/camera.h @@ -120,8 +120,9 @@ public: // Replace the wielded item mesh void wield(const InventoryItem* item); - // Start or stop digging animation - void setDigging(bool digging); + // Start digging animation + // Pass 0 for left click, 1 for right click + void setDigging(s32 button); private: // Scene manager and nodes @@ -158,8 +159,8 @@ private: f32 m_frametime_counter; f32 m_time_per_range; - // View bobbing animation frame (0 <= m_view_bobbing < 0x1000000) - s32 m_view_bobbing_anim; + // View bobbing animation frame (0 <= m_view_bobbing_anim < 1) + f32 m_view_bobbing_anim; // If 0, view bobbing is off (e.g. player is standing). // If 1, view bobbing is on (player is walking). // If 2, view bobbing is getting switched off. @@ -167,10 +168,12 @@ private: // Speed of view bobbing animation f32 m_view_bobbing_speed; - // Digging animation - s32 m_digging_anim; - // Speed of digging animation - s32 m_digging_speed; + // Digging animation frame (0 <= m_digging_anim < 1) + f32 m_digging_anim; + // If -1, no digging animation + // If 0, left-click digging animation + // If 1, right-click digging animation + s32 m_digging_button; }; -- cgit v1.2.3 From 36af9bb027ed6846227282587e3414ea3d30f6c5 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Wed, 21 Sep 2011 01:42:52 +0200 Subject: Create a separate scene manager for the wielded tool. This fixes the glitchyness in large map coordinates and some depth buffer problems. (The tool doesn't bob anymore when walking, this will be fixed later.) Fix MSVC build (thanks to dannydark). --- src/camera.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index 026f92874..fbee4a378 100644 --- a/src/camera.h +++ b/src/camera.h @@ -63,12 +63,6 @@ public: return m_cameranode; } - // Get wielded item scene node. - inline ExtrudedSpriteSceneNode* getWieldNode() const - { - return m_wieldnode; - } - // Get the camera position (in absolute scene coordinates). // This has view bobbing applied. inline v3f getPosition() const @@ -124,12 +118,19 @@ public: // Pass 0 for left click, 1 for right click void setDigging(s32 button); + // Draw the wielded tool. + // This has to happen *after* the main scene is drawn. + // Warning: This clears the Z buffer. + void drawWieldedTool(); + private: // Scene manager and nodes scene::ISceneManager* m_smgr; scene::ISceneNode* m_playernode; scene::ISceneNode* m_headnode; scene::ICameraSceneNode* m_cameranode; + + scene::ISceneManager* m_wieldmgr; ExtrudedSpriteSceneNode* m_wieldnode; // draw control -- cgit v1.2.3 From 3e012122d1607a6269af4e4522305d8b5efcd9d4 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Wed, 21 Sep 2011 18:37:29 +0200 Subject: Simplistic wielded tool lighting, added setMeshVerticesColor to utility.h and refactored some other code into calls of that --- src/camera.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/camera.h') diff --git a/src/camera.h b/src/camera.h index fbee4a378..66825e372 100644 --- a/src/camera.h +++ b/src/camera.h @@ -205,6 +205,8 @@ public: f32 getSpriteThickness() const { return m_thickness; } void setSpriteThickness(f32 thickness); + void updateLight(u8 light); + void removeSpriteFromCache(video::ITexture* texture); virtual const core::aabbox3d& getBoundingBox() const; @@ -216,6 +218,7 @@ private: f32 m_thickness; scene::IMesh* m_cubemesh; bool m_is_cube; + u8 m_light; // internal extrusion helper methods io::path getExtrudedName(video::ITexture* texture); -- cgit v1.2.3