faster particle sfx crap.

git-svn-id: https://pioneer.svn.sourceforge.net/svnroot/pioneer/trunk@450 e632f14b-6550-0410-b89e-a82653faca30
master
tompox 2009-10-22 10:35:34 +00:00
parent ab630a1e31
commit c60dd53e0c
8 changed files with 128 additions and 41 deletions

View File

@ -8,7 +8,6 @@
#include "SpaceStation.h"
#include "Ship.h"
#include "Player.h"
#include "Sfx.h"
#include "Projectile.h"
Body::Body()
@ -56,7 +55,6 @@ void Body::Serialize()
case Object::SPACESTATION:
case Object::SHIP:
case Object::PLAYER:
case Object::SFX:
case Object::CARGOBODY:
case Object::PROJECTILE:
Save();
@ -86,8 +84,6 @@ Body *Body::Unserialize()
b = new Ship(); break;
case Object::PLAYER:
b = new Player(); break;
case Object::SFX:
b = new Sfx(); break;
case Object::PROJECTILE:
b = new Projectile(); break;
case Object::CARGOBODY:

View File

@ -2,6 +2,7 @@
#include "Space.h"
#include "Serializer.h"
#include "collider/collider.h"
#include "Sfx.h"
Frame::Frame()
{
@ -34,6 +35,7 @@ void Frame::Serialize(Frame *f)
i != f->m_children.end(); ++i) {
Serialize(*i);
}
Sfx::Serialize(f);
}
Frame *Frame::Unserialize(Frame *parent)
@ -53,6 +55,7 @@ Frame *Frame::Unserialize(Frame *parent)
for (int i=rd_int(); i>0; --i) {
f->m_children.push_back(Unserialize(f));
}
Sfx::Unserialize(f);
return f;
}
@ -73,6 +76,7 @@ void Frame::RemoveChild(Frame *f)
void Frame::Init(Frame *parent, const char *label, unsigned int flags)
{
m_sfx = 0;
m_sbody = 0;
m_astroBody = 0;
m_parent = parent;
@ -91,6 +95,7 @@ void Frame::Init(Frame *parent, const char *label, unsigned int flags)
Frame::~Frame()
{
if (m_sfx) delete [] m_sfx;
delete m_collisionSpace;
for (std::list<Frame*>::iterator i = m_children.begin(); i != m_children.end(); ++i) delete *i;
}

View File

@ -9,6 +9,7 @@ class Body;
class CollisionSpace;
class Geom;
class SBody;
class Sfx;
/*
* Frame of reference.
@ -59,6 +60,7 @@ public:
std::list<Frame*> m_children;
SBody *m_sbody; // points to SBodies in Pi::current_system
Body *m_astroBody; // if frame contains a star or planet or something
Sfx *m_sfx;
enum { TEMP_VIEWING=1 };
private:

View File

@ -3,7 +3,7 @@
class Object {
public:
enum Type { OBJECT, BODY, MODELBODY, DYNAMICBODY, SHIP, PLAYER, SPACESTATION, PLANET, STAR, SFX, CARGOBODY, CITYONPLANET, PROJECTILE };
enum Type { OBJECT, BODY, MODELBODY, DYNAMICBODY, SHIP, PLAYER, SPACESTATION, PLANET, STAR, CARGOBODY, CITYONPLANET, PROJECTILE };
virtual Type GetType() { return OBJECT; }
virtual bool IsType(Type c) { return GetType() == c; }
};

View File

@ -5,7 +5,7 @@
#include "Frame.h"
#include "Space.h"
#define SAVEFILE_VERSION 6
#define SAVEFILE_VERSION 7
namespace Serializer {

View File

@ -8,17 +8,16 @@
#include "Shader.h"
#include "Render.h"
Sfx::Sfx(): Body()
#define MAX_SFX_PER_FRAME 1024
Sfx::Sfx()
{
m_pos = vector3d(0,0,0);
m_type = TYPE_EXPLOSION;
m_age = 0;
m_type = TYPE_NONE;
}
void Sfx::Save()
{
using namespace Serializer::Write;
Body::Save();
wr_vector3d(m_pos);
wr_vector3d(m_vel);
wr_float(m_age);
@ -28,16 +27,45 @@ void Sfx::Save()
void Sfx::Load()
{
using namespace Serializer::Read;
Body::Load();
m_pos = rd_vector3d();
if (IsOlderThan(2)) m_vel = vector3d(0,0,0);
else m_vel = rd_vector3d();
m_vel = rd_vector3d();
m_age = rd_float();
m_type = static_cast<Sfx::TYPE>(rd_int());
}
void Sfx::Serialize(const Frame *f)
{
using namespace Serializer::Write;
// 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_int(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();
}
}
}
void Sfx::Unserialize(Frame *f)
{
using namespace Serializer::Read;
if (IsOlderThan(7)) return;
int numActive = rd_int();
if (numActive) {
f->m_sfx = new Sfx[MAX_SFX_PER_FRAME];
for (int i=0; i<numActive; i++) {
f->m_sfx[i].Load();
}
}
}
void Sfx::SetPosition(vector3d p)
{
m_pos = p;
@ -50,27 +78,25 @@ void Sfx::TimeStepUpdate(const float timeStep)
switch (m_type) {
case TYPE_EXPLOSION:
if (m_age > 0.2) Space::KillBody(this);
if (m_age > 0.2) m_type = TYPE_NONE;
break;
case TYPE_DAMAGE:
if (m_age > 2.0) Space::KillBody(this);
if (m_age > 2.0) m_type = TYPE_NONE;
break;
case TYPE_NONE: break;
}
}
void Sfx::Render(const Frame *camFrame)
void Sfx::Render(const matrix4x4d &ftransform)
{
static GLuint tex;
float col[4];
if (!tex) tex = util_load_tex_rgba("data/textures/smoke.png");
matrix4x4d ftran;
Frame::GetFrameTransform(GetFrame(), camFrame, ftran);
vector3d fpos = ftran * GetPosition();
vector3d fpos = ftransform * GetPosition();
Shader::EnableVertexProgram(Shader::VPROG_POINTSPRITE);
switch (m_type) {
case TYPE_NONE: break;
case TYPE_EXPLOSION:
glPushMatrix();
glTranslatef(fpos.x, fpos.y, fpos.z);
@ -95,18 +121,70 @@ void Sfx::Render(const Frame *camFrame)
Render::PutPointSprites(1, &pos, 20.0f, col, tex);
break;
}
Shader::DisableVertexProgram();
}
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 = new Sfx();
Sfx *sfx = AllocSfxInFrame(b->GetFrame());
if (!sfx) return;
sfx->m_type = t;
sfx->SetFrame(b->GetFrame());
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);
Space::AddBody(sfx);
}
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 (std::list<Frame*>::iterator i = f->m_children.begin();
i != f->m_children.end(); ++i) {
TimeStepAll(timeStep, *i);
}
}
void Sfx::RenderAll(const Frame *f, const Frame *camFrame)
{
if (f->m_sfx) {
Shader::EnableVertexProgram(Shader::VPROG_POINTSPRITE);
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(ftran);
}
}
Shader::DisableVertexProgram();
}
for (std::list<Frame*>::const_iterator i = f->m_children.begin();
i != f->m_children.end(); ++i) {
RenderAll(*i, camFrame);
}
}

View File

@ -5,25 +5,27 @@
class Frame;
class Sfx: public Body {
class Sfx {
public:
OBJDEF(Sfx, Body, SFX);
enum TYPE { TYPE_EXPLOSION, TYPE_DAMAGE };
enum TYPE { TYPE_NONE, TYPE_EXPLOSION, TYPE_DAMAGE };
static void Add(const Body *, TYPE);
static void TimeStepAll(const float timeStep, Frame *f);
static void RenderAll(const Frame *f, const Frame *camFrame);
static void Serialize(const Frame *f);
static void Unserialize(Frame *f);
Sfx();
//virtual ~Sfx();
virtual void SetPosition(vector3d p);
virtual vector3d GetPosition() const { return m_pos; }
virtual double GetRadius() const { return 10; }
virtual void Render(const Frame *camFrame);
void TimeStepUpdate(const float timeStep);
protected:
virtual void Save();
virtual void Load();
void SetPosition(vector3d p);
vector3d GetPosition() const { return m_pos; }
private:
static Sfx *AllocSfxInFrame(Frame *f);
void Render(const matrix4x4d &transform);
void TimeStepUpdate(const float timeStep);
void Save();
void Load();
vector3d m_pos;
vector3d m_vel;
float m_age;

View File

@ -14,6 +14,7 @@
#include "Serializer.h"
#include "collider/collider.h"
#include "pirates.h"
#include "Sfx.h"
namespace Space {
@ -553,6 +554,7 @@ void TimeStep(float step)
for (bodiesIter_t i = bodies.begin(); i != bodies.end(); ++i) {
(*i)->StaticUpdate(step);
}
Sfx::TimeStepAll(step, rootFrame);
// see if anyone has been shot
PruneCorpses();
@ -657,6 +659,8 @@ void Render(const Frame *cam_frame)
for (unsigned int i=0; i<bodies.size(); i++) {
bz[i].b->Render(cam_frame);
}
Sfx::RenderAll(rootFrame, cam_frame);
delete [] bz;
}