Skip to content
Permalink
Browse files

Implement WieldMeshSceneNode which improves wield mesh rendering

- Don't create and cache an extruded mesh for every (non-node) item.
  Instead use a single one per image resolution.

- For cubic nodes reuse a single wield mesh too

- Improve lighting of the wielded item

- Increase far value of wield mesh scene camera, fixes #1770

- Also includes some minor refactorings of Camera and GenericCAO.
  • Loading branch information
kahrl committed Nov 8, 2014
1 parent cc8d7b8 commit 9b551d5cbcaf71a8c39bbf7e886290649aed4799
@@ -209,8 +209,9 @@ LOCAL_SRC_FILES := \
jni/src/util/string.cpp \
jni/src/util/timetaker.cpp \
jni/src/touchscreengui.cpp \
jni/src/database-leveldb.cpp \
jni/src/settings.cpp
jni/src/database-leveldb.cpp \
jni/src/settings.cpp \
jni/src/wieldmesh.cpp

# lua api
LOCAL_SRC_FILES += \
@@ -466,6 +466,7 @@ set(minetest_SRCS
shader.cpp
sky.cpp
tile.cpp
wieldmesh.cpp
${minetest_SCRIPT_SRCS}
)
list(SORT minetest_SRCS)
@@ -23,12 +23,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "main.h" // for g_settings
#include "map.h"
#include "clientmap.h" // MapDrawControl
#include "mesh.h"
#include "player.h"
#include "tile.h"
#include <cmath>
#include "settings.h"
#include "itemdef.h" // For wield visualization
#include "wieldmesh.h"
#include "noise.h" // easeCurve
#include "gamedef.h"
#include "sound.h"
@@ -50,7 +48,6 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,

m_wieldmgr(NULL),
m_wieldnode(NULL),
m_wieldlight(0),

m_draw_control(draw_control),
m_gamedef(gamedef),
@@ -77,12 +74,9 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,

m_digging_anim(0),
m_digging_button(-1),
m_dummymesh(createCubeMesh(v3f(1,1,1))),

m_wield_change_timer(0.125),
m_wield_mesh_next(NULL),
m_previous_playeritem(-1),
m_previous_itemname(""),
m_wield_item_next(),

m_camera_mode(CAMERA_MODE_FIRST)
{
@@ -99,14 +93,15 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
// all other 3D scene nodes and before the GUI.
m_wieldmgr = smgr->createNewSceneManager();
m_wieldmgr->addCameraSceneNode();
m_wieldnode = m_wieldmgr->addMeshSceneNode(m_dummymesh, NULL); // need a dummy mesh
m_wieldnode = new WieldMeshSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr, -1, true);
m_wieldnode->setItem(ItemStack(), m_gamedef);
m_wieldnode->drop(); // m_wieldmgr grabbed it
m_wieldlightnode = m_wieldmgr->addLightSceneNode(NULL, v3f(0.0, 50.0, 0.0));
}

Camera::~Camera()
{
m_wieldmgr->drop();

delete m_dummymesh;
}

bool Camera::successfullyCreated(std::wstring& error_message)
@@ -156,22 +151,10 @@ void Camera::step(f32 dtime)
}

bool was_under_zero = m_wield_change_timer < 0;
if(m_wield_change_timer < 0.125)
m_wield_change_timer += dtime;
if(m_wield_change_timer > 0.125)
m_wield_change_timer = 0.125;
m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);

if(m_wield_change_timer >= 0 && was_under_zero)
{
if(m_wield_mesh_next)
{
m_wieldnode->setMesh(m_wield_mesh_next);
m_wieldnode->setVisible(true);
} else {
m_wieldnode->setVisible(false);
}
m_wield_mesh_next = NULL;
}
if (m_wield_change_timer >= 0 && was_under_zero)
m_wieldnode->setItem(m_wield_item_next, m_gamedef);

if (m_view_bobbing_state != 0)
{
@@ -445,10 +428,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
v3f wield_position = v3f(55, -35, 65);
//v3f wield_rotation = v3f(-100, 120, -100);
v3f wield_rotation = v3f(-100, 120, -100);
if(m_wield_change_timer < 0)
wield_position.Y -= 40 + m_wield_change_timer*320;
else
wield_position.Y -= 40 - m_wield_change_timer*320;
wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
{
f32 frac = 1.0;
@@ -486,7 +466,12 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
}
m_wieldnode->setPosition(wield_position);
m_wieldnode->setRotation(wield_rotation);
m_wieldlight = player->light;

// Shine light upon the wield mesh
video::SColor black(255,0,0,0);
m_wieldmgr->setAmbientLight(player->light_color.getInterpolated(black, 0.7));
m_wieldlightnode->getLightData().DiffuseColor = player->light_color.getInterpolated(black, 0.3);
m_wieldlightnode->setPosition(v3f(30+5*sin(2*player->getYaw()*M_PI/180), -50, 0));

// Render distance feedback loop
updateViewingRange(frametime, busytime);
@@ -658,56 +643,28 @@ void Camera::setDigging(s32 button)
m_digging_button = button;
}

void Camera::wield(const ItemStack &item, u16 playeritem)
void Camera::wield(const ItemStack &item)
{
IItemDefManager *idef = m_gamedef->idef();
std::string itemname = item.getDefinition(idef).name;
m_wield_mesh_next = idef->getWieldMesh(itemname, m_gamedef);
if(playeritem != m_previous_playeritem &&
!(m_previous_itemname == "" && itemname == ""))
{
m_previous_playeritem = playeritem;
m_previous_itemname = itemname;
if(m_wield_change_timer >= 0.125)
m_wield_change_timer = -0.125;
else if(m_wield_change_timer > 0)
{
if (item.name != m_wield_item_next.name) {
m_wield_item_next = item;
if (m_wield_change_timer > 0)
m_wield_change_timer = -m_wield_change_timer;
}
} else {
if(m_wield_mesh_next) {
m_wieldnode->setMesh(m_wield_mesh_next);
m_wieldnode->setVisible(true);
} else {
m_wieldnode->setVisible(false);
}
m_wield_mesh_next = NULL;
if(m_previous_itemname != itemname)
{
m_previous_itemname = itemname;
m_wield_change_timer = 0;
}
else
m_wield_change_timer = 0.125;
else if (m_wield_change_timer == 0)
m_wield_change_timer = -0.001;
}
}

void Camera::drawWieldedTool(irr::core::matrix4* translation)
{
// Set vertex colors of wield mesh according to light level
u8 li = m_wieldlight;
video::SColor color(255,li,li,li);
setMeshColor(m_wieldnode->getMesh(), color);

// Clear Z buffer
// Clear Z buffer so that the wielded tool stay in front of world geometry
m_wieldmgr->getVideoDriver()->clearZBuffer();

// Draw the wielded node (in a separate scene manager)
scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
cam->setAspectRatio(m_cameranode->getAspectRatio());
cam->setFOV(72.0*M_PI/180.0);
cam->setNearValue(0.1);
cam->setFarValue(100);
cam->setFarValue(1000);
if (translation != NULL)
{
irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class LocalPlayer;
struct MapDrawControl;
class IGameDef;
class WieldMeshSceneNode;

enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};

@@ -127,7 +128,7 @@ class Camera
void setDigging(s32 button);

// Replace the wielded item mesh
void wield(const ItemStack &item, u16 playeritem);
void wield(const ItemStack &item);

// Draw the wielded tool.
// This has to happen *after* the main scene is drawn.
@@ -157,8 +158,8 @@ class Camera
scene::ICameraSceneNode* m_cameranode;

scene::ISceneManager* m_wieldmgr;
scene::IMeshSceneNode* m_wieldnode;
u8 m_wieldlight;
WieldMeshSceneNode* m_wieldnode;
scene::ILightSceneNode* m_wieldlightnode;

// draw control
MapDrawControl& m_draw_control;
@@ -203,14 +204,9 @@ class Camera
// If 1, right-click digging animation
s32 m_digging_button;

//dummymesh for camera
irr::scene::IAnimatedMesh* m_dummymesh;

// Animation when changing wielded item
f32 m_wield_change_timer;
scene::IMesh *m_wield_mesh_next;
u16 m_previous_playeritem;
std::string m_previous_itemname;
ItemStack m_wield_item_next;

CameraMode m_camera_mode;
};
@@ -41,6 +41,7 @@ class ITextureSource;
class IGameDef;
class LocalPlayer;
struct ItemStack;
class WieldMeshSceneNode;

class ClientActiveObject : public ActiveObject
{
@@ -58,8 +59,10 @@ class ClientActiveObject : public ActiveObject
virtual bool getCollisionBox(aabb3f *toset){return false;}
virtual bool collideWithObjects(){return false;}
virtual v3f getPosition(){return v3f(0,0,0);}
virtual scene::ISceneNode *getSceneNode(){return NULL;}
virtual scene::IMeshSceneNode *getMeshSceneNode(){return NULL;}
virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode(){return NULL;}
virtual WieldMeshSceneNode *getWieldMeshSceneNode(){return NULL;}
virtual scene::IBillboardSceneNode *getSpriteSceneNode(){return NULL;}
virtual bool isPlayer() const {return false;}
virtual bool isLocalPlayer() const {return false;}

0 comments on commit 9b551d5

Please sign in to comment.
You can’t perform that action at this time.