Skip to content

Commit

Permalink
#5584: Particle Quads are transformed to world space now, they are re…
Browse files Browse the repository at this point in the history
…calculated each frame anyway
  • Loading branch information
codereader committed Dec 16, 2021
1 parent 4456273 commit 40f6537
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 54 deletions.
3 changes: 2 additions & 1 deletion include/iparticles.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ class IRenderableParticle :
* the current render time.
*
* @viewRotation: the matrix to orient themselves to the viewer.
* @localToWorld: the combined transform of local to world coordinates
*/
virtual void update(const Matrix4& viewRotation) = 0;
virtual void update(const Matrix4& viewRotation, const Matrix4& localToWorld) = 0;

/**
* Get the particle definition used by this renderable.
Expand Down
7 changes: 1 addition & 6 deletions radiantcore/particles/ParticleNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ Matrix4 ParticleNode::localToParent() const
return _local2Parent;
}

bool ParticleNode::isOriented() const
{
return true;
}

void ParticleNode::onPreRender(const VolumeTest& volume)
{
if (!_renderableParticle) return;
Expand Down Expand Up @@ -131,7 +126,7 @@ void ParticleNode::update(const VolumeTest& viewVolume) const
_renderableParticle->setEntityColour(Vector3(
_renderEntity->getShaderParm(0), _renderEntity->getShaderParm(1), _renderEntity->getShaderParm(2)));

_renderableParticle->update(viewRotation);
_renderableParticle->update(viewRotation, localToWorld());
}

} // namespace
4 changes: 2 additions & 2 deletions radiantcore/particles/RenderableParticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RenderableParticle::~RenderableParticle()
}

// Time is in msecs
void RenderableParticle::update(const Matrix4& viewRotation)
void RenderableParticle::update(const Matrix4& viewRotation, const Matrix4& localToWorld)
{
auto renderSystem = _renderSystem.lock();

Expand All @@ -47,7 +47,7 @@ void RenderableParticle::update(const Matrix4& viewRotation)
stage->update(time, invViewRotation);

// Attach the geometry to the shader
stage->submitGeometry(pair.second.shader);
stage->submitGeometry(pair.second.shader, localToWorld);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion radiantcore/particles/RenderableParticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RenderableParticle : public IRenderableParticle,
~RenderableParticle();

// Time is in msecs
void update(const Matrix4& viewRotation) override;
void update(const Matrix4& viewRotation, const Matrix4& localToWorld) override;

// Front-end render methods
void renderSolid(IRenderableCollector& collector, const VolumeTest& volume) const override;
Expand Down
24 changes: 4 additions & 20 deletions radiantcore/particles/RenderableParticleBunch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void RenderableParticleBunch::update(std::size_t time)
}

void RenderableParticleBunch::addVertexData(std::vector<ArbitraryMeshVertex>& vertices,
std::vector<unsigned int>& indices)
std::vector<unsigned int>& indices, const Matrix4& localToWorld)
{
if (_quads.empty()) return;

Expand All @@ -165,8 +165,10 @@ void RenderableParticleBunch::addVertexData(std::vector<ArbitraryMeshVertex>& ve
{
for (auto i = 0; i < 4; ++i)
{
auto worldVertex = localToWorld * quad.verts[i].vertex;

vertices.push_back(ArbitraryMeshVertex(
quad.verts[i].vertex,
worldVertex,
quad.verts[i].normal,
quad.verts[i].texcoord,
quad.verts[i].colour)
Expand All @@ -180,24 +182,6 @@ void RenderableParticleBunch::addVertexData(std::vector<ArbitraryMeshVertex>& ve
}
}

void RenderableParticleBunch::render(const RenderInfo& info) const
{
if (_quads.empty()) return;

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_DOUBLE, sizeof(ParticleQuad::Vertex), &(_quads.front().verts[0].vertex));
glTexCoordPointer(2, GL_DOUBLE, sizeof(ParticleQuad::Vertex), &(_quads.front().verts[0].texcoord));
glNormalPointer(GL_DOUBLE, sizeof(ParticleQuad::Vertex), &(_quads.front().verts[0].normal));
glColorPointer(4, GL_DOUBLE, sizeof(ParticleQuad::Vertex), &(_quads.front().verts[0].colour));

glDrawArrays(GL_QUADS, 0, static_cast<GLsizei>(_quads.size())*4);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

const AABB& RenderableParticleBunch::getBounds()
{
if (!_bounds.isValid())
Expand Down
7 changes: 3 additions & 4 deletions radiantcore/particles/RenderableParticleBunch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace particles
#define MS2SEC(x) ((x)*0.001f)

/// A single bunch of particles, consisting of a renderable set of quads
class RenderableParticleBunch : public OpenGLRenderable
class RenderableParticleBunch
{
// The bunch index
std::size_t _index;
Expand Down Expand Up @@ -74,9 +74,8 @@ class RenderableParticleBunch : public OpenGLRenderable
void update(std::size_t time);

// Add the renderable geometry to the given arrays
void addVertexData(std::vector<ArbitraryMeshVertex>& vertices, std::vector<unsigned int>& indices);

void render(const RenderInfo& info) const;
void addVertexData(std::vector<ArbitraryMeshVertex>& vertices,
std::vector<unsigned int>& indices, const Matrix4& localToWorld);

const AABB& getBounds();

Expand Down
23 changes: 6 additions & 17 deletions radiantcore/particles/RenderableParticleStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RenderableParticleStage::RenderableParticleStage(
_seeds(_numSeeds),
_bunches(2), // two bunches
_viewRotation(Matrix4::getIdentity()), // is re-calculated each update anyway
_localToWorld(Matrix4::getIdentity()),
_direction(direction),
_entityColour(entityColour)
{
Expand All @@ -24,20 +25,6 @@ RenderableParticleStage::RenderableParticleStage(
}
}

void RenderableParticleStage::render(const RenderInfo& info) const
{
// Draw up to two active bunches
if (_bunches[0])
{
_bunches[0]->render(info);
}

if (_bunches[1])
{
_bunches[1]->render(info);
}
}

// Generate particle geometry, time is absolute in msecs
void RenderableParticleStage::update(std::size_t time, const Matrix4& viewRotation)
{
Expand Down Expand Up @@ -81,8 +68,10 @@ void RenderableParticleStage::update(std::size_t time, const Matrix4& viewRotati
}
}

void RenderableParticleStage::submitGeometry(const ShaderPtr& shader)
void RenderableParticleStage::submitGeometry(const ShaderPtr& shader, const Matrix4& localToWorld)
{
_localToWorld = localToWorld;

RenderableGeometry::update(shader);
}

Expand All @@ -93,12 +82,12 @@ void RenderableParticleStage::updateGeometry()

if (_bunches[0])
{
_bunches[0]->addVertexData(vertices, indices);
_bunches[0]->addVertexData(vertices, indices, _localToWorld);
}

if (_bunches[1])
{
_bunches[1]->addVertexData(vertices, indices);
_bunches[1]->addVertexData(vertices, indices, _localToWorld);
}

RenderableGeometry::updateGeometry(render::GeometryType::Quads, vertices, indices);
Expand Down
6 changes: 3 additions & 3 deletions radiantcore/particles/RenderableParticleStage.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class RenderableParticleStage :

// The rotation matrix to orient particles
Matrix4 _viewRotation;
// Matrix to produce world coordinates
Matrix4 _localToWorld;

// The particle direction (instance owned by RenderableParticle)
const Vector3& _direction;
Expand All @@ -48,12 +50,10 @@ class RenderableParticleStage :
const Vector3& direction,
const Vector3& entityColour);

void render(const RenderInfo& info) const;

// Generate particle geometry, time is absolute in msecs
void update(std::size_t time, const Matrix4& viewRotation);

void submitGeometry(const ShaderPtr& shader);
void submitGeometry(const ShaderPtr& shader, const Matrix4& localToWorld);

const AABB& getBounds();

Expand Down

0 comments on commit 40f6537

Please sign in to comment.