-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGhostTrailSceneNode.cpp
306 lines (237 loc) · 9.89 KB
/
GhostTrailSceneNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "ghostTrailSceneNode.h"
ghostTrailSceneNode::ghostTrailSceneNode(scene::ISceneNode* parent, scene::ISceneManager* smgr, s32 id, f32 TWidth, s32 numberSegments)
:scene::ISceneNode(parent, smgr, id)
{
trailMesh = new scene::SMesh();
trailMeshBuffer = new scene::SMeshBuffer();
trailMesh->addMeshBuffer(trailMeshBuffer);
trailMeshBuffer->drop();
//number of vertices: as many segments plus 2, multiplied by 2; one initial, one final, n between
//number of indices: as many segments plus 1, multiplied by 6; 2 triangles each segment.
numberOfSegments = (numberSegments + 2) * 2 < 65536 ? numberSegments : 32766; //max 65536 vertices! we use 16 bit indices.
numberOfSegments < 0 ? numberOfSegments = 2 : 0; //faster than an IF :)
video::S3DVertex* vertexData = new video::S3DVertex[2 * (numberOfSegments + 2)];
u16* indexData = new u16[6 * (numberOfSegments + 1)];
//We have to startup the mapping coordinates...
for (u32 i = 0; i < numberOfSegments + 2; i++)
{
f32 uData = i / (f32)(numberOfSegments + 1);
vertexData[2 * i + 0].TCoords = core::vector2df(uData, 0.0f);
vertexData[2 * i + 0].Color = video::SColor(255, 255, 255, 255);
vertexData[2 * i + 1].TCoords = core::vector2df(uData, 1.0f);
vertexData[2 * i + 1].Color = video::SColor(255, 255, 255, 255);
}
/*
Now, we have to sew the indices...
0-2
|/| if 0 segments. indices: 0,1,2,2,1,3
1-3
0-2-4
|/|/| if 1 segment; indices 0,1,2,2,1,3 ,2,3,4,4,3,5
1-3-5
0-2-4-6
|/|/|/| if 2 segments; indices 0,1,2,2,1,3 ,2,3,4,4,3,5 ,4,5,6,6,5,7
1-3-5-7
if N segments... indices ... 2i, 2i+1, 2i+2, 2i+2, 2i+1, 2i+3 ...
*/
for (u32 i = 0; i < numberOfSegments + 1; i++)
{
indexData[6 * i + 0] = 2 * i + 0;
indexData[6 * i + 1] = 2 * i + 1;
indexData[6 * i + 2] = 2 * i + 2;
indexData[6 * i + 3] = 2 * i + 2;
indexData[6 * i + 4] = 2 * i + 1;
indexData[6 * i + 5] = 2 * i + 3;
}
//Finally, we add this soup of triangles to the mesh buffer
trailMeshBuffer->append(vertexData, 2 * (numberOfSegments + 2), indexData, 6 * (numberOfSegments + 1));
startingPoint = 0;//It must be an in range index!
pointBuffer = new core::vector3df[2 * (numberOfSegments + 2)];
//Having a single strip of triangles creates somewhat odd effects when this isn't set, so we help the
//user by setting this for him. Anyway, he can unset it again later if he wishes...
for (u32 i = 0; i < _IRR_MATERIAL_MAX_TEXTURES_; i++)
{
material.TextureLayer[i].TextureWrapU = material.TextureLayer[i].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
}
material.BackfaceCulling = false;
trailWidth = TWidth;
updateNormals = false;//Just in case, this trail won't update its normals unless specified otherwise.
autoAnimate = true;//If we have a multiple pass setup, maybe we DON'T want to let this happen...
secondaryNode = 0;//for the moment, only the parent
wind = core::vector3df(0, 0, 0);//no wind.
mode = EALM_GLOBAL_Y_AXIS;//default, aligned with the Y world axis.
trailMesh->setHardwareMappingHint(scene::EHM_DYNAMIC);//It may change, or it may not, still, the user may alter it...
}
ghostTrailSceneNode::~ghostTrailSceneNode()
{
trailMesh->drop();
delete pointBuffer;
}
video::SMaterial& ghostTrailSceneNode::getMaterial(u32 index)
{
return material;
}
u32 ghostTrailSceneNode::getMaterialCount() const
{
return 1;
}
const core::aabbox3df& ghostTrailSceneNode::getBoundingBox() const
{
return boundingBox;
}
void ghostTrailSceneNode::OnRegisterSceneNode()
{
if (getParent()) { //If there is no parent node, there is no need for rendering
if (isVisible()) {
getSceneManager()->registerNodeForRendering(this);
}
scene::ISceneNode::OnRegisterSceneNode();
}
}
void ghostTrailSceneNode::OnAnimate(u32 timeMs)
{
lastTime = currentTime;
currentTime = timeMs;
if (autoAnimate)
{
animate();
}
scene::ISceneNode::OnAnimate(timeMs);
}
void ghostTrailSceneNode::render()
{
scene::ISceneManager* smgr = getSceneManager();
video::IVideoDriver* drv = smgr->getVideoDriver();
scene::ICameraSceneNode* cam = smgr->getActiveCamera();
drv->setMaterial(material);
drv->setTransform(video::ETS_WORLD, core::matrix4().makeIdentity());
drv->setTransform(video::ETS_VIEW, cam->getViewMatrix());
drv->setTransform(video::ETS_PROJECTION, cam->getProjectionMatrix());
drv->drawMeshBuffer(trailMeshBuffer);
}
void ghostTrailSceneNode::setTrailWidth(f32 width)
{
trailWidth = width;
//variable trail size, but only for the last pair of points,
//this allows, for instance, for the matrix bullet trails effect :)
}
void ghostTrailSceneNode::updateTrailData()
{
core::matrix4 parentMatrix = getParent()->getAbsoluteTransformation();
core::vector3df alignmentVector, viewVector, upVector;
core::vector3df parentPosition;
parentMatrix.transformVect(parentPosition, getPosition()); //Posicion local
core::vector3df secondaryPosition;
s32 p;
startingPoint < 0 ? startingPoint = numberOfSegments + 1 : 0;
if (!secondaryNode)
{
switch (mode) {
case EALM_CAMERA_X_AXIS:
upVector = getSceneManager()->getActiveCamera()->getUpVector();
upVector.normalize();
viewVector = getSceneManager()->getActiveCamera()->getTarget() - getSceneManager()->getActiveCamera()->getAbsolutePosition();
viewVector.normalize();
alignmentVector = viewVector.crossProduct(upVector);
alignmentVector.normalize();
break;
case EALM_CAMERA_Y_AXIS:
alignmentVector = getSceneManager()->getActiveCamera()->getUpVector();
alignmentVector.normalize();
break;
case EALM_GLOBAL_X_AXIS:
alignmentVector = core::vector3df(1.0f, 0, 0);
break;
case EALM_GLOBAL_Y_AXIS:
alignmentVector = core::vector3df(0, 1.0f, 0);
break;
case EALM_GLOBAL_Z_AXIS:
alignmentVector = core::vector3df(0, 0, 1.0f);
break;
case EALM_PARENT_X_AXIS:
parentMatrix.setTranslation(core::vector3df());//absolute transforms include a translation, hence we need this
parentMatrix.transformVect(alignmentVector, core::vector3df(1, 0, 0));
alignmentVector.normalize();
break;
case EALM_PARENT_Y_AXIS:
parentMatrix.setTranslation(core::vector3df());
parentMatrix.transformVect(alignmentVector, core::vector3df(0, 1, 0));
alignmentVector.normalize();
break;
case EALM_PARENT_Z_AXIS:
parentMatrix.setTranslation(core::vector3df());
parentMatrix.transformVect(alignmentVector, core::vector3df(0, 0, 1));
alignmentVector.normalize();
break;
}
pointBuffer[2 * startingPoint + 0] = parentPosition - alignmentVector * trailWidth / 2.0f;
pointBuffer[2 * startingPoint + 1] = parentPosition + alignmentVector * trailWidth / 2.0f;
}
else
{//There is no need for alignment options with a secondary node
parentPosition = getParent()->getAbsolutePosition();
secondaryNode->updateAbsolutePosition();
secondaryPosition = secondaryNode->getAbsolutePosition();
pointBuffer[2 * startingPoint + 0] = parentPosition;
pointBuffer[2 * startingPoint + 1] = secondaryPosition;
}
//Wind update... It is okay, in the end all the data is transferred from the app to the video card.
//The good thing is that this affects all the points at the same time.
if (wind.getLengthSQ() > 0.001f)
{
u32 timeIncrement = currentTime - lastTime;
irr::core::vector3df out;
for (u32 i = 0; i < numberOfSegments + 2; i++)
{
s32 windStrength = i < startingPoint ? numberOfSegments + 2 - startingPoint + i : i - startingPoint;
// Problem here
if (fixedSegSize > 0.001f)
out = fixedSegSize * wind;
else
out = wind * (f32)windStrength / (f32)(numberOfSegments + 2);
pointBuffer[2 * i + 0] += out;
pointBuffer[2 * i + 1] += out;
}
}
transferTrailData();
startingPoint--;
}
void ghostTrailSceneNode::animate()
{
if (getParent())
{
updateTrailData();
}
}
void ghostTrailSceneNode::transferTrailData()
{
video::S3DVertex* vertices = (video::S3DVertex*)trailMeshBuffer->getVertices();
core::vector3df normal, vectorU, vectorV;
boundingBox.reset(0, 0, 0);
trailMeshBuffer->BoundingBox.reset(0, 0, 0);
trailMesh->BoundingBox.reset(0, 0, 0);
for (u32 i = 0; i < numberOfSegments + 2; i++)
{
s32 vertexIndex = i < startingPoint ? numberOfSegments + 2 - startingPoint + i : i - startingPoint;
vertices[2 * vertexIndex + 0].Pos = pointBuffer[2 * i + 0];
vertices[2 * vertexIndex + 1].Pos = pointBuffer[2 * i + 1];
boundingBox.addInternalBox(core::aabbox3df(vertices[2 * vertexIndex + 0].Pos, vertices[2 * vertexIndex + 1].Pos));
}
trailMeshBuffer->BoundingBox.addInternalBox(boundingBox);
trailMesh->BoundingBox.addInternalBox(boundingBox);
if (updateNormals)
{
for (u32 i = 0; i < numberOfSegments + 1; i++)
{
vectorU = vertices[2 * i + 2].Pos - vertices[2 * i].Pos;
vectorV = vertices[2 * i + 1].Pos - vertices[2 * i + 2].Pos;
normal = vectorU.crossProduct(vectorV);
normal.normalize();
vertices[2 * i].Normal = normal;
vertices[2 * i + 1].Normal = normal;
}
vertices[2 * (numberOfSegments + 1) + 0].Normal = normal;
vertices[2 * (numberOfSegments + 1) + 1].Normal = normal;
}
trailMeshBuffer->setDirty(scene::EBT_VERTEX);
}