aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CWaterSurfaceSceneNode.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CWaterSurfaceSceneNode.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CWaterSurfaceSceneNode.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CWaterSurfaceSceneNode.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CWaterSurfaceSceneNode.cpp
new file mode 100644
index 0000000..12d20c2
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CWaterSurfaceSceneNode.cpp
@@ -0,0 +1,137 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "CWaterSurfaceSceneNode.h"
6#include "ISceneManager.h"
7#include "IMeshManipulator.h"
8#include "IMeshCache.h"
9#include "S3DVertex.h"
10#include "SMesh.h"
11#include "os.h"
12
13namespace irr
14{
15namespace scene
16{
17
18//! constructor
19CWaterSurfaceSceneNode::CWaterSurfaceSceneNode(f32 waveHeight, f32 waveSpeed, f32 waveLength,
20 IMesh* mesh, ISceneNode* parent, ISceneManager* mgr, s32 id,
21 const core::vector3df& position, const core::vector3df& rotation,
22 const core::vector3df& scale)
23 : CMeshSceneNode(mesh, parent, mgr, id, position, rotation, scale),
24 WaveLength(waveLength), WaveSpeed(waveSpeed), WaveHeight(waveHeight),
25 OriginalMesh(0)
26{
27 #ifdef _DEBUG
28 setDebugName("CWaterSurfaceSceneNode");
29 #endif
30
31 setMesh(mesh);
32}
33
34
35//! destructor
36CWaterSurfaceSceneNode::~CWaterSurfaceSceneNode()
37{
38 // Mesh is dropped in CMeshSceneNode destructor
39 if (OriginalMesh)
40 OriginalMesh->drop();
41}
42
43
44//! frame
45void CWaterSurfaceSceneNode::OnRegisterSceneNode()
46{
47 CMeshSceneNode::OnRegisterSceneNode();
48}
49
50
51void CWaterSurfaceSceneNode::OnAnimate(u32 timeMs)
52{
53 if (Mesh && IsVisible)
54 {
55 const u32 meshBufferCount = Mesh->getMeshBufferCount();
56 const f32 time = timeMs / WaveSpeed;
57
58 for (u32 b=0; b<meshBufferCount; ++b)
59 {
60 const u32 vtxCnt = Mesh->getMeshBuffer(b)->getVertexCount();
61
62 for (u32 i=0; i<vtxCnt; ++i)
63 Mesh->getMeshBuffer(b)->getPosition(i).Y = addWave(
64 OriginalMesh->getMeshBuffer(b)->getPosition(i),
65 time);
66 }// end for all mesh buffers
67 Mesh->setDirty(scene::EBT_VERTEX);
68
69 SceneManager->getMeshManipulator()->recalculateNormals(Mesh);
70 }
71 CMeshSceneNode::OnAnimate(timeMs);
72}
73
74
75void CWaterSurfaceSceneNode::setMesh(IMesh* mesh)
76{
77 CMeshSceneNode::setMesh(mesh);
78 if (!mesh)
79 return;
80 if (OriginalMesh)
81 OriginalMesh->drop();
82 IMesh* clone = SceneManager->getMeshManipulator()->createMeshCopy(mesh);
83 OriginalMesh = mesh;
84 Mesh = clone;
85 Mesh->setHardwareMappingHint(scene::EHM_STATIC, scene::EBT_INDEX);
86// Mesh->setHardwareMappingHint(scene::EHM_STREAM, scene::EBT_VERTEX);
87}
88
89
90//! Writes attributes of the scene node.
91void CWaterSurfaceSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
92{
93 out->addFloat("WaveLength", WaveLength);
94 out->addFloat("WaveSpeed", WaveSpeed);
95 out->addFloat("WaveHeight", WaveHeight);
96
97 CMeshSceneNode::serializeAttributes(out, options);
98 // serialize original mesh
99 out->setAttribute("Mesh", SceneManager->getMeshCache()->getMeshName(OriginalMesh).getPath().c_str());
100}
101
102
103//! Reads attributes of the scene node.
104void CWaterSurfaceSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
105{
106 WaveLength = in->getAttributeAsFloat("WaveLength");
107 WaveSpeed = in->getAttributeAsFloat("WaveSpeed");
108 WaveHeight = in->getAttributeAsFloat("WaveHeight");
109
110 if (Mesh)
111 {
112 Mesh->drop();
113 Mesh = OriginalMesh;
114 OriginalMesh = 0;
115 }
116 // deserialize original mesh
117 CMeshSceneNode::deserializeAttributes(in, options);
118
119 if (Mesh)
120 {
121 IMesh* clone = SceneManager->getMeshManipulator()->createMeshCopy(Mesh);
122 OriginalMesh = Mesh;
123 Mesh = clone;
124 }
125}
126
127
128f32 CWaterSurfaceSceneNode::addWave(const core::vector3df &source, f32 time) const
129{
130 return source.Y +
131 (sinf(((source.X/WaveLength) + time)) * WaveHeight) +
132 (cosf(((source.Z/WaveLength) + time)) * WaveHeight);
133}
134
135} // end namespace scene
136} // end namespace irr
137