-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodes.h
561 lines (452 loc) · 18.8 KB
/
nodes.h
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#ifndef TAK_NODES_H__
#define TAK_NODES_H__
#include <list>
#include "define.h"
#include "particle.h"
class ContainerNode : public SceneNode
{
public:
ContainerNode() : SceneNode("ContainerNode") {}
protected:
virtual bool Update(float elapsedTime, SceneParams& params) { return true; }
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params) {}
};
class OrthographicProjectionNode: public SceneNode
{
public:
OrthographicProjectionNode() : SceneNode("OrthographicProjectionNode") {}
protected:
virtual bool Update(float elapsedTime, SceneParams& params)
{
// TODO do it only once, and when width or height change
m_projection.SetOrthographicProjection(0, params.GetWidth(), 0, params.GetHeight(), -100.f, 100.f);
return true;
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
}
virtual void UpdateProjectionMatrix(Matrix4f& projection, Camera* camera)
{
projection = m_projection;
}
virtual void UpdateModelviewMatrix(Matrix4f& modelview, Camera* camera)
{
modelview = Matrix4f::IDENTITY;
}
private:
Matrix4f m_projection;
};
class Entity : public SceneNode
{
public:
Entity(Model* model) : SceneNode("Entity"), m_mesh(0)
{
m_mesh = model->GetMesh();
}
protected:
virtual bool Update(float elapsedTime, SceneParams& params) { return true; }
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
m_mesh->Render();
}
private:
Mesh<VertexData3dNormal>* m_mesh;
};
class ParticleSystemNode : public SceneNode
{
public:
enum BILLBOARD_TYPE { BT_CYLINDRICAL, BT_SPHERICAL };
public:
ParticleSystemNode(const Vector3f& position, Texture* texture, BILLBOARD_TYPE type = BT_CYLINDRICAL) : SceneNode("ParticleSystemNode"), m_mesh(Mesh<VertexData3d>::MT_QUAD), m_type(type)
{
SetFlag(FLAG_NO_DEPTH_TEST);
SetTexture(texture);
SetPositionAbsolute(position);
}
protected:
typedef std::list<Particle> ParticleList;
protected:
virtual bool Update(float elapsedTime, ParticleList& pl) = 0;
private:
virtual bool Update(float elapsedTime, SceneParams& params)
{
if(Update(elapsedTime, m_particles))
{
// Rebuild mesh...
VertexData3d* vd = new VertexData3d[m_particles.size() * 4];
int idx = 0;
for(ParticleList::const_iterator it = m_particles.begin(); it != m_particles.end(); ++it)
{
const Particle& p = *it;
float x = p.position.x;
float y = p.position.y;
float z = p.position.z;
float hw = p.width / 2.f;
float hh = p.height / 2.f;
float ll = 1.f;
vd[idx++] = Mesh<VertexData3d>::VertexData(x - hw, y - hh, z, ll, ll, ll, 0, 0);
vd[idx++] = Mesh<VertexData3d>::VertexData(x + hw, y - hh, z, ll, ll, ll, 1, 0);
vd[idx++] = Mesh<VertexData3d>::VertexData(x + hw, y + hh, z, ll, ll, ll, 1, 1);
vd[idx++] = Mesh<VertexData3d>::VertexData(x - hw, y + hh, z, ll, ll, ll, 0, 1);
}
m_mesh.SetMeshData(vd, idx);
delete [] vd;
return true;
}
}
virtual void UpdateModelviewMatrix(Matrix4f& modelview, Camera* camera)
{
// Reference: http://www.lighthouse3d.com/opengl/billboarding/index.php?billCyl
const Vector3f& position = GetPosition(); // TODO should be in world space coordinate...
const Vector3f& cameraPos = camera->GetWorldPosition();
Vector3f objToCamProj = cameraPos - position;
objToCamProj.y = 0;
Vector3f lookAt(0, 0, cameraPos.z - position.z > 0.f ? 1.f : -1.f);
objToCamProj.Normalize();
Vector3f upAux = lookAt.Cross(objToCamProj);
float angleCosine = lookAt.Dot(objToCamProj);
//if ((angleCosine < 0.99990) && (angleCosine > -0.9999))
{
float angle;
//angle = 360-RADTODEG(acos(angleCosine));
angle = -RADTODEG(acos(angleCosine));
if(cameraPos.z - position.z < 0.f)
angle += 90;
//std::cout << angle << std::endl;
modelview.ApplyRotation(angle, upAux.x, upAux.y, upAux.z);
}
if(m_type == BT_SPHERICAL)
{
Vector3f objToCam = cameraPos - position;
objToCam.Normalize();
angleCosine = objToCamProj.Dot(objToCam);
if(objToCam.y < 0)
modelview.ApplyRotation(RADTODEG(acos(angleCosine)), 1, 0, 0);
else
modelview.ApplyRotation(RADTODEG(acos(angleCosine)), -1, 0, 0);
}
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
m_mesh.Render();
}
private:
ParticleList m_particles;
Mesh<VertexData3d> m_mesh;
BILLBOARD_TYPE m_type;
};
class SoundNode : public SceneNode
{
public:
SoundNode(const Vector3f& position, Sound* sound, bool playNow = true) : SceneNode("SoundNode"), m_sound(sound->GetInternalBuffer())
{
SetPositionAbsolute(position);
if(playNow)
PlaySound();
}
protected:
virtual bool Update(float elapsedTime, SceneParams& params)
{
static Vector3f lastPosition = GetPosition();
// Si le son ne doit pas jouer en loop, et qu'il est fini de jouer, faire deleter le scene node...
sf::Sound::Status status = m_sound.getStatus();
if(status == sf::Sound::Playing)
{
// Update sound position only if needed
Vector3f currentPosition = GetPosition();
if(lastPosition != currentPosition)
{
UpdatePosition();
lastPosition = currentPosition;
}
}
else if(status == sf::Sound::Stopped)
{
return false;
}
return true;
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
}
private:
void UpdatePosition()
{
Vector3f pos = GetPosition(); // TODO should be world coordinate position
m_sound.setPosition(pos.x, pos.y, pos.z);
}
void PlaySound()
{
UpdatePosition();
m_sound.play();
}
private:
sf::Sound m_sound;
};
class Sprite: public SceneNode
{
public:
Sprite(float width, float height, Texture* texture) : SceneNode("Sprite"), m_width(width), m_height(height), m_mesh(Mesh<VertexData3d>::MT_QUAD)
{
SetFlag(FLAG_HUD);
SetTexture(texture);
VertexData3d vd[4];
float hw = width / 2.f;
float hh = height / 2.f;
float ll = 1.f;
vd[0] = Mesh<VertexData3d>::VertexData(-hw, -hh, 0, ll, ll, ll, 0, 0);
vd[1] = Mesh<VertexData3d>::VertexData(hw, -hh, 0, ll, ll, ll, 1, 0);
vd[2] = Mesh<VertexData3d>::VertexData(hw, hh, 0, ll, ll, ll, 1, 1);
vd[3] = Mesh<VertexData3d>::VertexData(-hw, hh, 0, ll, ll, ll, 0, 1);
m_mesh.SetMeshData(vd, 4);
}
float GetWidth() const
{
return m_width;
}
float GetHeight() const
{
return m_height;
}
protected:
virtual bool Update(float elapsedTime, SceneParams& params)
{
return true;
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
m_mesh.Render();
}
private:
float m_width;
float m_height;
Mesh<VertexData3d> m_mesh;
};
class Floor : public SceneNode
{
public:
Floor(const float sizeX, const float sizeZ, float textureRepeat = 1.f, const std::string& name = "Floor") : SceneNode(name), m_mesh(Mesh<VertexData3dNormal>::MT_QUAD), m_sizeX(sizeX), m_sizeZ(sizeZ)
{
VertexData3dNormal vd[4];
float hx = m_sizeX * .5f;
float hz = m_sizeZ * .5f;
vd[0] = Mesh<VertexData3dNormal>::VertexData(-hx, 0, hz, 0, 1, 0, 1, 1, 1, textureRepeat, 0);
vd[1] = Mesh<VertexData3dNormal>::VertexData( hx, 0, hz, 0, 1, 0, 1, 1, 1, 0, 0);
vd[2] = Mesh<VertexData3dNormal>::VertexData( hx, 0, -hz, 0, 1, 0, 1, 1, 1, 0, textureRepeat);
vd[3] = Mesh<VertexData3dNormal>::VertexData(-hx, 0, -hz, 0, 1, 0, 1, 1, 1, textureRepeat, textureRepeat);
m_mesh.SetMeshData(vd, 4);
}
const float& GetSizeX() const
{
return m_sizeX;
}
const float& GetSizeZ() const
{
return m_sizeZ;
}
void DisableDepthTest()
{
SetFlag(SceneNode::FLAG_NO_DEPTH_TEST);
}
protected:
virtual bool Update(float elapsedTime, SceneParams& params)
{
return true;
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
m_mesh.Render();
}
private:
Mesh<VertexData3dNormal> m_mesh;
float m_sizeX, m_sizeZ;
};
class Cube : public SceneNode
{
public:
Cube(const Vector3f& size = Vector3f(1.f, 1.f, 1.f), float textureRepeat = 1.f, const std::string& name = "Cube") : SceneNode(name), m_mesh(Mesh<VertexData3dNormal>::MT_QUAD), m_size(size)
{
VertexData3dNormal* vd = new VertexData3dNormal[24];
int count = 0;
AddBlockToMesh(vd, count, textureRepeat);
m_mesh.SetMeshData(vd, count);
delete [] vd;
}
~Cube()
{
#ifdef TAK_USE_BULLET_PHYSICS
if(IsBoundToRigidBody())
{
RigidBody* rb = GetBoundRigidBody();
GPhysicEngine.RemoveRigidBody(rb);
delete rb;
}
#endif
}
const Vector3f& GetSize() const
{
return m_size;
}
#ifdef TAK_USE_BULLET_PHYSICS
virtual RigidBody* AddToPhysic(float weight, const Vector3f& position)
{
BoxRigidBody* br1 = new BoxRigidBody(weight, position, m_size);
BindToRigidBody(br1);
GPhysicEngine.AddRigidBody(br1);
return br1;
}
#endif
protected:
virtual bool Update(float elapsedTime, SceneParams& params)
{
return true;
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
m_mesh.Render();
}
private:
Mesh<VertexData3dNormal> m_mesh;
Vector3f m_size;
void AddBlockToMesh(VertexData3dNormal* vd, int& count, float textureRepeat)
{
float iu = 0;
float iv = 0;
float ts = 1.f;
float ll = 1.f;
float hx = m_size.x * .5f;
float hy = m_size.y * .5f;
float hz = m_size.z * .5f;
// front
{
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, -hy, hz, 0, 0, 1, ll, ll, ll, 0, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, -hy, hz, 0, 0, 1, ll, ll, ll, textureRepeat, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, hy, hz, 0, 0, 1, ll, ll, ll, textureRepeat, textureRepeat);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, hy, hz, 0, 0, 1, ll, ll, ll, 0, textureRepeat);
}
// back
{
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, -hy, -hz, 0, 0, -1, ll, ll, ll, 0, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, -hy, -hz, 0, 0, -1, ll, ll, ll, textureRepeat, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, hy, -hz, 0, 0, -1, ll, ll, ll, textureRepeat, textureRepeat);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, hy, -hz, 0, 0, -1, ll, ll, ll, 0, textureRepeat);
}
// left
{
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, -hy, -hz, -1, 0, 0, ll, ll, ll, 0, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, -hy, hz, -1, 0, 0, ll, ll, ll, textureRepeat, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, hy, hz, -1, 0, 0, ll, ll, ll, textureRepeat, textureRepeat);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, hy, -hz, -1, 0, 0, ll, ll, ll, 0, textureRepeat);
}
// right
{
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, -hy, hz, 1, 0, 0, ll, ll, ll, 0, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, -hy, -hz, 1, 0, 0, ll, ll, ll, textureRepeat, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, hy, -hz, 1, 0, 0, ll, ll, ll, textureRepeat, textureRepeat);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, hy, hz, 1, 0, 0, ll, ll, ll, 0, textureRepeat);
}
// top
{
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, hy, hz, 0, 1, 0, ll, ll, ll, textureRepeat, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, hy, hz, 0, 1, 0, ll, ll, ll, 0, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, hy, -hz, 0, 1, 0, ll, ll, ll, 0, textureRepeat);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, hy, -hz, 0, 1, 0, ll, ll, ll, textureRepeat, textureRepeat);
}
// bottom
{
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, -hy, -hz, 0, -1, 0, ll, ll, ll, textureRepeat, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, -hy, -hz, 0, -1, 0, ll, ll, ll, 0, 0);
vd[count++] = Mesh<VertexData3dNormal>::VertexData( hx, -hy, hz, 0, -1, 0, ll, ll, ll, 0, textureRepeat);
vd[count++] = Mesh<VertexData3dNormal>::VertexData(-hx, -hy, hz, 0, -1, 0, ll, ll, ll, textureRepeat, textureRepeat);
}
}
};
class TextNode : public SceneNode
{
public:
TextNode(Font* font, float size = 12.f, const std::string& text = "") : SceneNode("TextNode"), m_font(font), m_mesh(Mesh<VertexData3d>::MT_QUAD), m_size(size)
{
SetFlag(FLAG_HUD);
SetText(text);
SetTexture(&font->GetTexture());
}
void SetText(const std::string& text)
{
// text might be utf8 encoded, make sure it's ascii
//std::cout << "CODEPAGE: " << m_font.GetAnsiCodePage() << std::endl;
m_text = Unicode::FromUtf8ToAnsi(text, m_font->GetAnsiCodePage());
//std::cout << "SetText: " << m_text << std::endl;
if(m_text.length() == 0)
return;
VertexData3d* vd = new VertexData3d[m_text.length() * 4];
int count = 0;
float px = 0;
for(int i = 0; i < m_text.length(); ++i)
{
const Font::CharInfo& ci = m_font->GetCharInfo(m_text[i]);
//std::cout << "SetText char: " << m_text[i] << ", " << ci.charW << ", " << ci.charH << std::endl;
//std::cout << ci.texX << ", " << ci.texY << ", " << ci.texW << ", " << ci.texH << std::endl;
float ll = 1.f;
// TODO rendre counterclock-wise... pour tout ce que je render... corriger partout
vd[count++] = Mesh<VertexData3d>::VertexData(px, -ci.charB, 0, ll, ll, ll, ci.texX, ci.texY);
vd[count++] = Mesh<VertexData3d>::VertexData(px + ci.charW, -ci.charB, 0, ll, ll, ll, ci.texX + ci.texW, ci.texY);
vd[count++] = Mesh<VertexData3d>::VertexData(px + ci.charW, ci.charH - ci.charB, 0, ll, ll, ll, ci.texX + ci.texW, ci.texY + ci.texH);
vd[count++] = Mesh<VertexData3d>::VertexData(px, ci.charH - ci.charB, 0, ll, ll, ll, ci.texX, ci.texY + ci.texH);
px += ci.charW;
}
assert(count == m_text.length() * 4);
m_mesh.SetMeshData(vd, count);
delete [] vd;
}
protected:
virtual bool Update(float elapsedTime, SceneParams& params) { return true; }
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
if(m_text.length() == 0)
return;
m_mesh.Render();
}
private:
Mesh<VertexData3d> m_mesh;
const Font* m_font;
float m_size;
std::string m_text;
};
class SpriteRotationNode : public SceneNode
{
public:
SpriteRotationNode(float posX, float posY, float posZ, float width, float height) : SceneNode("SpriteRotationNode"), m_mesh(Mesh<VertexData3d>::MT_QUAD)
{
SetPositionAbsolute(posX, posY, posZ);
VertexData3d vd[4];
float hw = width / 2.f;
float hh = height / 2.f;
float ll = 1.f;
vd[0] = Mesh<VertexData3d>::VertexData(-hw, -hh, 0, ll, ll, ll, 0, 0);
vd[1] = Mesh<VertexData3d>::VertexData(hw, -hh, 0, ll, ll, ll, 1, 0);
vd[2] = Mesh<VertexData3d>::VertexData(hw, hh, 0, ll, ll, ll, 1, 1);
vd[3] = Mesh<VertexData3d>::VertexData(-hw, hh, 0, ll, ll, ll, 0, 1);
//vd[0] = Mesh<VertexData3d>::VertexData(posX - hw, posY - hh, 0, ll, ll, ll, 0, 0);
//vd[1] = Mesh<VertexData3d>::VertexData(posX + hw, posY - hh, 0, ll, ll, ll, 1, 0);
//vd[2] = Mesh<VertexData3d>::VertexData(posX + hw, posY + hh, 0, ll, ll, ll, 1, 1);
//vd[3] = Mesh<VertexData3d>::VertexData(posX - hw, posY + hh, 0, ll, ll, ll, 0, 1);
m_mesh.SetMeshData(vd, 4);
}
protected:
virtual bool Update(float elapsedTime, SceneParams& params)
{
float rotY = GetRotY() + elapsedTime * 200.f;
if(rotY >= 90.f)
rotY = -90.f;
SetRotationAbsolute(GetRotX(), rotY, GetRotZ());
//SetRotationRelative(0, 0, elapsedTime * 5.f);
return true;
}
virtual void Render(const Matrix4f& projection, const Matrix4f& modelview, SceneParams& params)
{
m_mesh.Render();
}
private:
Mesh<VertexData3d> m_mesh;
};
#endif // TAK_NODES_H__