Skip to content

Commit 12fc072

Browse files
author
Cheng Xie
committed
Drawing order, rain adjustments
1 parent 4cfbb08 commit 12fc072

8 files changed

+72
-12
lines changed

src/cloud.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Cloud::Cloud(float radius)
1414

1515
setMaxParticleCount(100);
1616

17+
setRenderingOrder(RenderingOrder::YoungestFirst);
18+
1719
ParticleMaterial* material = static_cast<ParticleMaterial *>(material_);
1820
material->setHorizontal(true);
1921
}

src/common.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using std::string;
88
#include <vector>
99
using std::vector;
10+
#include <list>
11+
using std::list;
1012
#include <map>
1113
using std::map;
1214
#include <assert.h>

src/particlesystem.cpp

+30-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ParticleSystem::~ParticleSystem()
1717
delete mesh_;
1818
}
1919

20-
// To Ken: This function can be ignored.
2120
void ParticleSystem::renderGeometry(DrawContext &context)
2221
{
2322
update(context.deltaTime);
@@ -26,9 +25,25 @@ void ParticleSystem::renderGeometry(DrawContext &context)
2625
return;
2726
}
2827

28+
Particles particles = particles_;
29+
30+
if (renderingOrder_ == RenderingOrder::Reversed) {
31+
std::reverse(begin(particles), end(particles));
32+
}
33+
else if (renderingOrder_ == RenderingOrder::OldestFirst) {
34+
std::sort(begin(particles), end(particles), [](const Particle& left, const Particle& right) {
35+
return left.life > right.life;
36+
});
37+
}
38+
else if (renderingOrder_ == RenderingOrder::YoungestFirst) {
39+
std::sort(begin(particles), end(particles), [](const Particle& left, const Particle& right) {
40+
return left.life < right.life;
41+
});
42+
}
43+
2944
VertexBufferDesc desc;
30-
desc.bufferData = particles_.data();
31-
desc.bufferSize = particles_.size() * sizeof(Particle);
45+
desc.bufferData = particles.data();
46+
desc.bufferSize = particles.size() * sizeof(Particle);
3247
desc.vertexElementSizes = {3, 1, 1, 3, 1};
3348
desc.stride = sizeof(Particle);
3449

@@ -79,7 +94,7 @@ void ParticleSystem::update(float dt)
7994

8095
void ParticleSystem::spawnParticle(Particle *particle)
8196
{
82-
97+
8398
}
8499

85100
void ParticleSystem::updateParticle(Particle &particle, float dt)
@@ -107,5 +122,16 @@ void ParticleSystem::setMaxParticleCount(int maxParticleCount)
107122
maxParticleCount_ = maxParticleCount;
108123
}
109124

125+
RenderingOrder ParticleSystem::renderingOrder() const
126+
{
127+
return renderingOrder_;
128+
}
129+
130+
void ParticleSystem::setRenderingOrder(RenderingOrder renderingOrder)
131+
{
132+
renderingOrder_ = renderingOrder;
133+
}
134+
135+
110136

111137

src/particlesystem.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ struct Particle
2323
};
2424

2525
typedef vector<Particle> Particles;
26+
27+
enum class RenderingOrder {
28+
Default,
29+
Reversed,
30+
OldestFirst,
31+
YoungestFirst,
32+
};
2633

2734
class ParticleSystem : public SceneObject
2835
{
@@ -48,9 +55,12 @@ class ParticleSystem : public SceneObject
4855
uint32_t maxParticleCount() const;
4956
void setMaxParticleCount(int maxParticleCount);
5057

58+
RenderingOrder renderingOrder() const;
59+
void setRenderingOrder(RenderingOrder renderingOrder);
60+
5161
protected:
5262
vector<Particle> particles_;
53-
63+
5464
private:
5565
// time elapsed since the particle system was created
5666
float timeElapsed_ = 0;
@@ -62,6 +72,8 @@ class ParticleSystem : public SceneObject
6272
uint32_t emissionRate_ = 0;
6373

6474
float numParticlesToSpawn_ = 0;
75+
76+
RenderingOrder renderingOrder_ = RenderingOrder::Reversed;
6577
};
6678

6779
#endif // PARTICLESYSTEM_H

src/rain.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "texture.h"
33
#include "texturecache.h"
44
#include "particlematerial.h"
5+
#include "drawcontext.h"
6+
#include "camera.h"
57

68
Rain::Rain(float radius)
79
{
@@ -20,14 +22,14 @@ Rain::Rain(float radius)
2022

2123
void Rain::spawnParticle(Particle *particle)
2224
{
23-
float speed = randf(60, 80);
25+
float speed = randf(60, 100);
2426
vec3 velocity = {randf(0, 0.05), -1, randf(0, 0.1)};
2527
velocity *= speed;
2628

2729
particle->velocity = velocity;
2830

2931
float phi = randf(0, M_PI * 2);
30-
float r = radius_ * randf();
32+
float r = radius_ * randf(0.3, 1.0);
3133

3234
float x = cosf(phi) * r;
3335
float y = 0;
@@ -39,7 +41,7 @@ void Rain::spawnParticle(Particle *particle)
3941

4042
particle->size = 0.07;
4143

42-
float scale = randf(0.6f, 0.7f);
44+
float scale = randf(0.6f, 0.8f);
4345
particle->color = {scale, scale, scale};
4446

4547
particle->opacity = randf(0.4, 0.7);
@@ -48,6 +50,15 @@ void Rain::spawnParticle(Particle *particle)
4850
void Rain::updateParticle(Particle &particle, float dt)
4951
{
5052
particle.position += particle.velocity * dt;
51-
5253
particle.opacity = randf(0.4, 0.7);
5354
}
55+
56+
void Rain::renderGeometry(DrawContext &context)
57+
{
58+
ParticleSystem::renderGeometry(context);
59+
transform().setToIdentity();
60+
vec3 camPos = context.camera->position();
61+
transform().translate(camPos.x(), camPos.y() + 30, camPos.z());
62+
}
63+
64+

src/rain.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Rain : public ParticleSystem
1212

1313
virtual void updateParticle(Particle &particle, float dt) override;
1414

15+
virtual void renderGeometry(DrawContext &context) override;
16+
1517
private:
1618
float radius_;
1719
};

src/scene.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Scene::~Scene()
3030
}
3131

3232
delete skybox_;
33-
delete tornado_;
33+
//delete tornado_;
3434
}
3535

3636
void Scene::initialize()
@@ -92,9 +92,7 @@ void Scene::initialize()
9292

9393
// Rain
9494
Rain* rain = new Rain(30);
95-
rain->transform().translate(0, 50, 0);
9695
sceneObjects_.push_back(rain);
97-
9896
}
9997

10098
void Scene::render(DrawContext &context)
@@ -130,6 +128,11 @@ void Scene::setFogDensity(float fogDensity)
130128
fogDensity_ = fogDensity;
131129
}
132130

131+
void Scene::update(float dt)
132+
{
133+
134+
}
135+
133136

134137

135138

src/scene.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Scene
2525
float fogDensity() const;
2626
void setFogDensity(float fogDensity);
2727

28+
void update(float dt);
29+
2830
private:
2931
SceneObjects sceneObjects_;
3032
Lights lights_;

0 commit comments

Comments
 (0)