forked from pioneerspacesim/pioneer
/
Sfx.cpp
212 lines (186 loc) · 5.3 KB
/
Sfx.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
// Copyright © 2008-2013 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include "Sfx.h"
#include "Frame.h"
#include "galaxy/StarSystem.h"
#include "libs.h"
#include "Pi.h"
#include "Pi.h"
#include "Space.h"
#include "graphics/Drawables.h"
#include "graphics/Graphics.h"
#include "graphics/Material.h"
#include "graphics/Renderer.h"
#include "graphics/TextureBuilder.h"
using namespace Graphics;
#define MAX_SFX_PER_FRAME 1024
Graphics::Drawables::Sphere3D *Sfx::shieldEffect = 0;
Graphics::Drawables::Sphere3D *Sfx::explosionEffect = 0;
Graphics::Material *Sfx::damageParticle = 0;
Graphics::Material *Sfx::ecmParticle = 0;
Sfx::Sfx()
{
m_type = TYPE_NONE;
}
void Sfx::Save(Serializer::Writer &wr)
{
wr.Vector3d(m_pos);
wr.Vector3d(m_vel);
wr.Float(m_age);
wr.Int32(m_type);
}
void Sfx::Load(Serializer::Reader &rd)
{
m_pos = rd.Vector3d();
m_vel = rd.Vector3d();
m_age = rd.Float();
m_type = static_cast<Sfx::TYPE>(rd.Int32());
}
void Sfx::Serialize(Serializer::Writer &wr, const Frame *f)
{
// how many sfx turds are active in frame?
int numActive = 0;
if (f->m_sfx) {
for (int i=0; i<MAX_SFX_PER_FRAME; i++) {
if (f->m_sfx[i].m_type != TYPE_NONE) numActive++;
}
}
wr.Int32(numActive);
if (numActive) for (int i=0; i<MAX_SFX_PER_FRAME; i++) {
if (f->m_sfx[i].m_type != TYPE_NONE) {
f->m_sfx[i].Save(wr);
}
}
}
void Sfx::Unserialize(Serializer::Reader &rd, Frame *f)
{
int numActive = rd.Int32();
if (numActive) {
f->m_sfx = new Sfx[MAX_SFX_PER_FRAME];
for (int i=0; i<numActive; i++) {
f->m_sfx[i].Load(rd);
}
}
}
void Sfx::SetPosition(const vector3d &p)
{
m_pos = p;
}
void Sfx::TimeStepUpdate(const float timeStep)
{
m_age += timeStep;
m_pos += m_vel * double(timeStep);
switch (m_type) {
case TYPE_EXPLOSION:
if (m_age > 0.5) m_type = TYPE_NONE;
break;
case TYPE_DAMAGE:
if (m_age > 2.0) m_type = TYPE_NONE;
break;
case TYPE_NONE: break;
}
}
void Sfx::Render(Renderer *renderer, const matrix4x4d &ftransform)
{
vector3d fpos = ftransform * GetPosition();
switch (m_type) {
case TYPE_NONE: break;
case TYPE_EXPLOSION: {
//Explosion effect: A quick flash of three concentric coloured spheres. A bit retro.
glPushMatrix();
matrix4x4f trans = trans.Identity();
trans.Translate(fpos.x, fpos.y, fpos.z);
RefCountedPtr<Material> exmat = Sfx::explosionEffect->GetMaterial();
exmat->diffuse = Color(1.f, 1.f, 0.5f, 1.f);
renderer->SetTransform(trans * matrix4x4f::ScaleMatrix(500*m_age));
Sfx::explosionEffect->Draw(renderer);
renderer->SetBlendMode(BLEND_ALPHA);
exmat->diffuse = Color(1.f, 0.5f, 0.f, 0.66f);
renderer->SetTransform(trans * matrix4x4f::ScaleMatrix(750*m_age));
Sfx::explosionEffect->Draw(renderer);
exmat->diffuse = Color(1.f, 0.f, 0.f, 0.33f);
renderer->SetTransform(trans * matrix4x4f::ScaleMatrix(1000*m_age));
Sfx::explosionEffect->Draw(renderer);
glPopMatrix();
break;
}
case TYPE_DAMAGE:
vector3f pos(&fpos.x);
damageParticle->diffuse = Color(1.f, 1.f, 0.f, 1.0f-(m_age/2.0f));
renderer->SetBlendMode(BLEND_ALPHA_ONE);
renderer->DrawPointSprites(1, &pos, damageParticle, 20.f);
break;
}
}
Sfx *Sfx::AllocSfxInFrame(Frame *f)
{
if (!f->m_sfx) {
f->m_sfx = new Sfx[MAX_SFX_PER_FRAME];
}
for (int i=0; i<MAX_SFX_PER_FRAME; i++) {
if (f->m_sfx[i].m_type == TYPE_NONE) {
return &f->m_sfx[i];
}
}
return 0;
}
void Sfx::Add(const Body *b, TYPE t)
{
Sfx *sfx = AllocSfxInFrame(b->GetFrame());
if (!sfx) return;
sfx->m_type = t;
sfx->m_age = 0;
sfx->SetPosition(b->GetPosition());
sfx->m_vel = b->GetVelocity() + 200.0*vector3d(
Pi::rng.Double()-0.5,
Pi::rng.Double()-0.5,
Pi::rng.Double()-0.5);
}
void Sfx::TimeStepAll(const float timeStep, Frame *f)
{
if (f->m_sfx) {
for (int i=0; i<MAX_SFX_PER_FRAME; i++) {
if (f->m_sfx[i].m_type != TYPE_NONE) {
f->m_sfx[i].TimeStepUpdate(timeStep);
}
}
}
for (Frame::ChildIterator it = f->BeginChildren(); it != f->EndChildren(); ++it) {
TimeStepAll(timeStep, *it);
}
}
void Sfx::RenderAll(Renderer *renderer, Frame *f, const Frame *camFrame)
{
if (f->m_sfx) {
matrix4x4d ftran;
Frame::GetFrameTransform(f, camFrame, ftran);
for (int i=0; i<MAX_SFX_PER_FRAME; i++) {
if (f->m_sfx[i].m_type != TYPE_NONE) {
f->m_sfx[i].Render(renderer, ftran);
}
}
}
for (Frame::ChildIterator it = f->BeginChildren(); it != f->EndChildren(); ++it) {
RenderAll(renderer, *it, camFrame);
}
}
void Sfx::Init(Graphics::Renderer *r)
{
Graphics::MaterialDescriptor desc;
RefCountedPtr<Graphics::Material> shieldMat(r->CreateMaterial(desc));
RefCountedPtr<Graphics::Material> explosionMat(r->CreateMaterial(desc));
shieldEffect = new Graphics::Drawables::Sphere3D(shieldMat, 2);
explosionEffect = new Graphics::Drawables::Sphere3D(explosionMat, 2);
desc.textures = 1;
damageParticle = r->CreateMaterial(desc);
damageParticle->texture0 = Graphics::TextureBuilder::Billboard("textures/smoke.png").GetOrCreateTexture(r, "billboard");
ecmParticle = r->CreateMaterial(desc);
ecmParticle->texture0 = Graphics::TextureBuilder::Billboard("textures/ecm.png").GetOrCreateTexture(r, "billboard");
}
void Sfx::Uninit()
{
delete shieldEffect; shieldEffect = 0;
delete explosionEffect; explosionEffect = 0;
delete damageParticle; damageParticle = 0;
delete ecmParticle; ecmParticle = 0;
}