Add animated by step texture matrix for kart model

This commit is contained in:
Benau 2023-07-27 08:11:51 +08:00
parent 3b043b7f47
commit a47e69d74e
4 changed files with 61 additions and 34 deletions

View File

@ -71,20 +71,22 @@ MovingTexture::MovingTexture(core::matrix4 *matrix, float dx, float dy)
m_x = v.X; m_x = v.X;
m_y = v.Y; m_y = v.Y;
m_count = 0.0f; m_count = 0.0f;
m_dt = 0.0f;
m_sp_tm = NULL; m_sp_tm = NULL;
} // MovingTexture } // MovingTexture
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
MovingTexture::MovingTexture(float dx, float dy) MovingTexture::MovingTexture(float dx, float dy, float dt,
bool animated_by_step)
{ {
// by default the animation by step is disabled m_isAnimatedByStep = animated_by_step;
m_isAnimatedByStep = false;
m_dx = dx; m_dx = dx;
m_dy = dy; m_dy = dy;
m_x = 0; m_x = 0;
m_y = 0; m_y = 0;
m_count = 0.0f; m_count = 0.0f;
m_dt = dt;
m_matrix = NULL; m_matrix = NULL;
m_sp_tm = NULL; m_sp_tm = NULL;
} // MovingTexture } // MovingTexture
@ -160,5 +162,3 @@ void MovingTexture::update(float dt)
} }
} }
} // update } // update

View File

@ -30,7 +30,7 @@ class XMLNode;
* \brief Handles animated textures (textures that move) * \brief Handles animated textures (textures that move)
* \ingroup graphics * \ingroup graphics
*/ */
class MovingTexture : public NoCopy class MovingTexture
{ {
private: private:
/** Translation increment per second. */ /** Translation increment per second. */
@ -52,7 +52,8 @@ private:
public: public:
MovingTexture(core::matrix4 *matrix, const XMLNode &node); MovingTexture(core::matrix4 *matrix, const XMLNode &node);
MovingTexture(core::matrix4 *matrix, float dx, float dy); MovingTexture(core::matrix4 *matrix, float dx, float dy);
MovingTexture(float dx, float dy); MovingTexture(float dx, float dy, float dt = 0.0f,
bool animated_by_step = false);
virtual ~MovingTexture(); virtual ~MovingTexture();
/** Sets the speed of the animation. */ /** Sets the speed of the animation. */
@ -62,6 +63,8 @@ public:
void setSPTM(float* sp_tm) { m_sp_tm = sp_tm; } void setSPTM(float* sp_tm) { m_sp_tm = sp_tm; }
virtual void update (float dt); virtual void update (float dt);
virtual void reset (); virtual void reset ();
float getCurrentX() const { return m_x; }
float getCurrentY() const { return m_y; }
} }
; // MovingTexture ; // MovingTexture

View File

@ -31,6 +31,7 @@
#include "graphics/material.hpp" #include "graphics/material.hpp"
#include "graphics/material_manager.hpp" #include "graphics/material_manager.hpp"
#include "graphics/mesh_tools.hpp" #include "graphics/mesh_tools.hpp"
#include "graphics/moving_texture.hpp"
#include "graphics/sp/sp_mesh.hpp" #include "graphics/sp/sp_mesh.hpp"
#include "graphics/sp/sp_mesh_buffer.hpp" #include "graphics/sp/sp_mesh_buffer.hpp"
#include "graphics/sp/sp_mesh_node.hpp" #include "graphics/sp/sp_mesh_node.hpp"
@ -62,17 +63,32 @@ SpeedWeightedObject::Properties::Properties()
{ {
m_strength_factor = -1.0f; m_strength_factor = -1.0f;
m_speed_factor = 0.0f; m_speed_factor = 0.0f;
m_texture_speed.X = 0.0f; m_moving_texture = NULL;
m_texture_speed.Y = 0.0f;
} // SpeedWeightedObject::Properties::Properties } // SpeedWeightedObject::Properties::Properties
// ----------------------------------------------------------------------------
SpeedWeightedObject::Properties::~Properties()
{
delete m_moving_texture;
} // SpeedWeightedObject::Properties::~Properties
// ----------------------------------------------------------------------------
SpeedWeightedObject::Properties& SpeedWeightedObject::Properties::
operator=(const SpeedWeightedObject::Properties& other)
{
m_strength_factor = other.m_strength_factor;
m_speed_factor = other.m_speed_factor;
m_moving_texture = NULL;
if (other.m_moving_texture)
m_moving_texture = new MovingTexture(*other.m_moving_texture);
return *this;
} // SpeedWeightedObject::Properties::Properties& operator=
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void SpeedWeightedObject::Properties::loadFromXMLNode(const XMLNode* xml_node) void SpeedWeightedObject::Properties::loadFromXMLNode(const XMLNode* xml_node)
{ {
xml_node->get("strength-factor", &m_strength_factor); xml_node->get("strength-factor", &m_strength_factor);
xml_node->get("speed-factor", &m_speed_factor); xml_node->get("speed-factor", &m_speed_factor);
xml_node->get("texture-speed-x", &m_texture_speed.X);
xml_node->get("texture-speed-y", &m_texture_speed.Y);
} // SpeedWeightedObject::Properties::loadFromXMLNode } // SpeedWeightedObject::Properties::loadFromXMLNode
// ============================================================================ // ============================================================================
@ -658,8 +674,7 @@ bool KartModel::loadModels(const KartProperties &kart_properties)
(obj.m_model->getMeshBuffer(j)); (obj.m_model->getMeshBuffer(j));
// Pre-upload gl meshes and textures for kart screen // Pre-upload gl meshes and textures for kart screen
mb->uploadGLMesh(); mb->uploadGLMesh();
if (obj.m_properties.m_texture_speed != if (obj.m_properties.m_moving_texture)
core::vector2df(0.0f, 0.0f))
{ {
for (unsigned k = 0; k < mb->getAllSTKMaterials().size(); for (unsigned k = 0; k < mb->getAllSTKMaterials().size();
k++) k++)
@ -810,8 +825,21 @@ void KartModel::loadSpeedWeightedInfo(const XMLNode* speed_weighted_node)
if (!obj.m_name.empty()) if (!obj.m_name.empty())
{ {
m_speed_weighted_objects.push_back(obj); m_speed_weighted_objects.push_back(obj);
float dx = 0.0f;
float dy = 0.0f;
float dt = 0.0f;
bool step = false;
speed_weighted_node->get("texture-speed-x", &dx);
speed_weighted_node->get("texture-speed-y", &dy);
speed_weighted_node->get("texture-speed-dt", &dt);
speed_weighted_node->get("animated-by-step", &step);
if (dx != 0.0f || dy != 0.0f)
{
m_speed_weighted_objects.back().m_properties.m_moving_texture =
new MovingTexture(dx, dy, dt, step);
}
} }
} } // loadSpeedWeightedInfo
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Loads a single wheel node. Currently this is the name of the wheel model /** Loads a single wheel node. Currently this is the name of the wheel model
@ -1171,24 +1199,17 @@ void KartModel::update(float dt, float distance, float steer, float speed,
obj.m_node->setAnimationSpeed(anim_speed); obj.m_node->setAnimationSpeed(anim_speed);
} }
// Texture animation if (obj.m_properties.m_moving_texture)
core::vector2df tex_speed;
tex_speed.X = obj.m_properties.m_texture_speed.X;
tex_speed.Y = obj.m_properties.m_texture_speed.Y;
if (tex_speed != core::vector2df(0.0f, 0.0f))
{ {
obj.m_texture_cur_offset += speed * tex_speed * dt; obj.m_properties.m_moving_texture->update(speed * dt);
if (obj.m_texture_cur_offset.X > 1.0f) obj.m_texture_cur_offset.X = fmod(obj.m_texture_cur_offset.X, 1.0f);
if (obj.m_texture_cur_offset.Y > 1.0f) obj.m_texture_cur_offset.Y = fmod(obj.m_texture_cur_offset.Y, 1.0f);
SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(obj.m_node); SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(obj.m_node);
if (spmn) if (spmn)
{ {
for (unsigned i = 0; i < spmn->getSPM()->getMeshBufferCount(); i++) for (unsigned i = 0; i < spmn->getSPM()->getMeshBufferCount(); i++)
{ {
auto& ret = spmn->getTextureMatrix(i); auto& ret = spmn->getTextureMatrix(i);
ret[0] = obj.m_texture_cur_offset.X; ret[0] = obj.m_properties.m_moving_texture->getCurrentX();
ret[1] = obj.m_texture_cur_offset.Y; ret[1] = obj.m_properties.m_moving_texture->getCurrentY();
} }
} }
else else
@ -1205,11 +1226,13 @@ void KartModel::update(float dt, float distance, float steer, float speed,
if (!t) continue; if (!t) continue;
core::matrix4 *m = core::matrix4 *m =
&irrMaterial.getTextureMatrix(j); &irrMaterial.getTextureMatrix(j);
m->setTextureTranslate(obj.m_texture_cur_offset.X, m->setTextureTranslate(
obj.m_texture_cur_offset.Y); obj.m_properties.m_moving_texture->getCurrentX(),
obj.m_properties.m_moving_texture->getCurrentY());
} // for j<MATERIAL_MAX_TEXTURES } // for j<MATERIAL_MAX_TEXTURES
} // for i<getMaterialCount } // for i<getMaterialCount
} }
} }
} }
} }

View File

@ -31,13 +31,14 @@ namespace irr
class ISceneNode; class IMeshSceneNode; } class ISceneNode; class IMeshSceneNode; }
} }
using namespace irr; using namespace irr;
namespace GE { class GERenderInfo; }
#include "utils/no_copy.hpp" #include "utils/no_copy.hpp"
#include "utils/vec3.hpp" #include "utils/vec3.hpp"
class AbstractKart; class AbstractKart;
class KartProperties; class KartProperties;
namespace GE { class GERenderInfo; } class MovingTexture;
class XMLNode; class XMLNode;
/** A speed-weighted object is an object whose characteristics are influenced by the kart's speed */ /** A speed-weighted object is an object whose characteristics are influenced by the kart's speed */
@ -48,14 +49,17 @@ struct SpeedWeightedObject
{ {
Properties(); Properties();
~Properties();
Properties& operator=(const Properties& other);
/** Strength factor: how much the kart speed affects the animation's distance from a static pose (-1 to disable) */ /** Strength factor: how much the kart speed affects the animation's distance from a static pose (-1 to disable) */
float m_strength_factor; float m_strength_factor;
/** Speed factor: how much the kart speed affects the animation's speed (-1 to disable) */ /** Speed factor: how much the kart speed affects the animation's speed (-1 to disable) */
float m_speed_factor; float m_speed_factor;
/** Texture speed, in UV coordinates */ MovingTexture* m_moving_texture;
core::vector2df m_texture_speed;
void loadFromXMLNode(const XMLNode* xml_node); void loadFromXMLNode(const XMLNode* xml_node);
@ -78,9 +82,6 @@ struct SpeedWeightedObject
/** Attach to which bone in kart model if not empty. */ /** Attach to which bone in kart model if not empty. */
std::string m_bone_name; std::string m_bone_name;
/** Current uv translation in the texture matrix for speed-weighted texture animations */
core::vector2df m_texture_cur_offset;
/** Specific properties for this given speed-weighted object, /** Specific properties for this given speed-weighted object,
* otherwise just a copy of the values from the kart's properties */ * otherwise just a copy of the values from the kart's properties */
Properties m_properties; Properties m_properties;