2009-01-14 04:37:51 -08:00
|
|
|
// Copyright (C) 2002-2009 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CWaterSurfaceSceneNode.h"
|
|
|
|
#include "ISceneManager.h"
|
|
|
|
#include "IMeshManipulator.h"
|
2007-09-14 15:25:59 -07:00
|
|
|
#include "IMeshCache.h"
|
2007-05-20 11:03:49 -07:00
|
|
|
#include "S3DVertex.h"
|
|
|
|
#include "SMesh.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! constructor
|
|
|
|
CWaterSurfaceSceneNode::CWaterSurfaceSceneNode(f32 waveHeight, f32 waveSpeed, f32 waveLength,
|
|
|
|
IMesh* mesh, ISceneNode* parent, ISceneManager* mgr, s32 id,
|
|
|
|
const core::vector3df& position, const core::vector3df& rotation,
|
|
|
|
const core::vector3df& scale)
|
2009-04-28 14:48:17 -07:00
|
|
|
: CMeshSceneNode(mesh, parent, mgr, id, position, rotation, scale),
|
|
|
|
WaveLength(waveLength), WaveSpeed(waveSpeed), WaveHeight(waveHeight),
|
|
|
|
OriginalMesh(0)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("CWaterSurfaceSceneNode");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// create copy of the mesh
|
|
|
|
if (!mesh)
|
|
|
|
return;
|
|
|
|
|
2007-09-14 15:25:59 -07:00
|
|
|
// Mesh is set in CMeshSceneNode constructor, now it is moved to OriginalMesh
|
2007-05-20 11:03:49 -07:00
|
|
|
IMesh* clone = SceneManager->getMeshManipulator()->createMeshCopy(mesh);
|
|
|
|
OriginalMesh = Mesh;
|
|
|
|
Mesh = clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! destructor
|
|
|
|
CWaterSurfaceSceneNode::~CWaterSurfaceSceneNode()
|
|
|
|
{
|
2007-09-14 15:25:59 -07:00
|
|
|
// Mesh is dropped in CMeshSceneNode destructor
|
2007-05-20 11:03:49 -07:00
|
|
|
if (OriginalMesh)
|
|
|
|
OriginalMesh->drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! frame
|
|
|
|
void CWaterSurfaceSceneNode::OnRegisterSceneNode()
|
|
|
|
{
|
2009-04-28 14:48:17 -07:00
|
|
|
CMeshSceneNode::OnRegisterSceneNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CWaterSurfaceSceneNode::OnAnimate(u32 timeMs)
|
|
|
|
{
|
|
|
|
if (Mesh && IsVisible)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2009-04-28 14:48:17 -07:00
|
|
|
const u32 meshBufferCount = Mesh->getMeshBufferCount();
|
|
|
|
const f32 time = timeMs / WaveSpeed;
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2009-04-28 14:48:17 -07:00
|
|
|
for (u32 b=0; b<meshBufferCount; ++b)
|
|
|
|
{
|
|
|
|
const u32 vtxCnt = Mesh->getMeshBuffer(b)->getVertexCount();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2009-04-28 14:48:17 -07:00
|
|
|
for (u32 i=0; i<vtxCnt; ++i)
|
|
|
|
addWave(Mesh->getMeshBuffer(b)->getPosition(i),
|
|
|
|
OriginalMesh->getMeshBuffer(b)->getPosition(i),
|
|
|
|
time);
|
|
|
|
}// end for all mesh buffers
|
|
|
|
|
|
|
|
SceneManager->getMeshManipulator()->recalculateNormals(Mesh);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
2009-04-28 14:48:17 -07:00
|
|
|
CMeshSceneNode::OnAnimate(timeMs);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-28 14:48:17 -07:00
|
|
|
void CWaterSurfaceSceneNode::setMesh(IMesh* mesh)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2009-04-28 14:48:17 -07:00
|
|
|
CMeshSceneNode::setMesh(mesh);
|
|
|
|
if (!mesh)
|
2007-05-20 11:03:49 -07:00
|
|
|
return;
|
2009-04-28 14:48:17 -07:00
|
|
|
if (OriginalMesh)
|
|
|
|
OriginalMesh->drop();
|
|
|
|
IMesh* clone = SceneManager->getMeshManipulator()->createMeshCopy(mesh);
|
|
|
|
OriginalMesh = mesh;
|
|
|
|
Mesh = clone;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Writes attributes of the scene node.
|
2007-09-14 15:25:59 -07:00
|
|
|
void CWaterSurfaceSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2007-09-10 02:27:37 -07:00
|
|
|
out->addFloat("WaveLength", WaveLength);
|
|
|
|
out->addFloat("WaveSpeed", WaveSpeed);
|
2007-05-20 11:03:49 -07:00
|
|
|
out->addFloat("WaveHeight", WaveHeight);
|
|
|
|
|
|
|
|
CMeshSceneNode::serializeAttributes(out, options);
|
2007-09-14 15:25:59 -07:00
|
|
|
// serialize original mesh
|
|
|
|
out->setAttribute("Mesh", SceneManager->getMeshCache()->getMeshFilename(OriginalMesh));
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Reads attributes of the scene node.
|
|
|
|
void CWaterSurfaceSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
|
|
|
|
{
|
2007-09-10 02:27:37 -07:00
|
|
|
WaveLength = in->getAttributeAsFloat("WaveLength");
|
|
|
|
WaveSpeed = in->getAttributeAsFloat("WaveSpeed");
|
|
|
|
WaveHeight = in->getAttributeAsFloat("WaveHeight");
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
if (Mesh)
|
|
|
|
{
|
|
|
|
Mesh->drop();
|
|
|
|
Mesh = OriginalMesh;
|
|
|
|
OriginalMesh = 0;
|
|
|
|
}
|
|
|
|
// deserialize original mesh
|
|
|
|
CMeshSceneNode::deserializeAttributes(in, options);
|
|
|
|
|
|
|
|
if (Mesh)
|
|
|
|
{
|
|
|
|
IMesh* clone = SceneManager->getMeshManipulator()->createMeshCopy(Mesh);
|
|
|
|
OriginalMesh = Mesh;
|
|
|
|
Mesh = clone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-28 14:48:17 -07:00
|
|
|
|
|
|
|
void CWaterSurfaceSceneNode::addWave(core::vector3df& dest, const core::vector3df &source, f32 time) const
|
|
|
|
{
|
|
|
|
dest.Y = source.Y +
|
|
|
|
(sinf(((source.X/WaveLength) + time)) * WaveHeight) +
|
|
|
|
(cosf(((source.Z/WaveLength) + time)) * WaveHeight);
|
|
|
|
}
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|
|
|
|
|