Skip to content

Commit 9dffbe2

Browse files
author
Cheng Xie
committed
Added wind
1 parent ecbcb7c commit 9dffbe2

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed

src/final.pro

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ SOURCES += main.cpp \
5151
cloud.cpp \
5252
rain.cpp \
5353
rainsplash.cpp \
54-
PhysicsObject.cpp
54+
PhysicsObject.cpp \
55+
wind.cpp
5556

5657
HEADERS += mainwindow.h \
5758
view.h \
@@ -91,6 +92,7 @@ HEADERS += mainwindow.h \
9192
cloud.h \
9293
rain.h \
9394
rainsplash.h \
94-
PhysicsObject.h
95+
PhysicsObject.h \
96+
wind.h
9597

9698
FORMS += mainwindow.ui

src/rain.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void Rain::spawnParticle(Particle *particle)
4242
particle->size = 0.08;
4343

4444
float scale = randf(0.6f, 0.8f);
45-
particle->color = {scale, scale * 0.9, scale * 0.85};
45+
particle->color = {scale, scale * 0.9, scale * 0.80};
4646

4747
particle->opacity = randf(0.4, 0.7);
4848
}

src/scene.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "rain.h"
1717
#include "rainsplash.h"
1818
#include "camera.h"
19+
#include "wind.h"
1920

2021
Scene::Scene()
2122
{
@@ -104,7 +105,7 @@ void Scene::initialize()
104105
tornado_->setDestination(dest);
105106
TornadoParticleSystem* tPart = new TornadoParticleSystem(tornado_);
106107
tPart->init();
107-
Texture* tornadoMap = TextureCache::getInstance()->acquire("tornado", TextureType::Texture2D);
108+
Texture* tornadoMap = TextureCache::getInstance()->acquire("tornado2", TextureType::Texture2D);
108109
tPart->setParticleTexture(tornadoMap);
109110
sceneObjects_.push_back(tPart);
110111

@@ -130,6 +131,8 @@ void Scene::initialize()
130131
phys_->objects.push_back(p);
131132
}
132133

134+
Wind* wind = new Wind(50);
135+
sceneObjects_.push_back(wind);
133136
}
134137

135138
void Scene::render(DrawContext &context)

src/textures/tornado2.png

136 KB
Loading

src/textures/wind.png

8.66 KB
Loading

src/wind.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "wind.h"
2+
#include "texture.h"
3+
#include "texturecache.h"
4+
#include "particlematerial.h"
5+
#include "drawcontext.h"
6+
#include "camera.h"
7+
8+
Wind::Wind(float radius)
9+
{
10+
radius_ = radius;
11+
12+
Texture* texture = TextureCache::getInstance()->acquire("wind", TextureType::Texture2D);
13+
setParticleTexture(texture);
14+
15+
setEmissionRate(5);
16+
17+
setMaxParticleCount(40);
18+
}
19+
20+
void Wind::spawnParticle(Particle *particle)
21+
{
22+
float speed = 10;
23+
vec3 velocity = {randf(0, 1), 0, randf(0, 1)};
24+
velocity *= speed;
25+
26+
particle->velocity = velocity;
27+
28+
float phi = randf(0, M_PI * 2);
29+
float r = radius_ * randf(0.3, 1.0);
30+
31+
float x = cosf(phi) * r;
32+
float y = 0;
33+
float z = sinf(phi) * r;
34+
35+
particle->position = {x, y, z};
36+
37+
particle->maxLife = 10;
38+
39+
particle->size = randf(25, 50);
40+
41+
float scale = randf(0.6f, 0.8f);
42+
particle->color = {scale, scale * 0.9, scale * 0.85};
43+
44+
particle->opacity = randf(0.05, 0.1);
45+
}
46+
47+
void Wind::updateParticle(Particle &particle, float dt)
48+
{
49+
particle.position += particle.velocity * dt;
50+
51+
particle.opacity = randf(0.1, 0.11);
52+
}
53+
54+
void Wind::renderGeometry(DrawContext &context)
55+
{
56+
ParticleSystem::renderGeometry(context);
57+
transform().setToIdentity();
58+
vec3 camPos = context.camera->position();
59+
transform().translate(camPos.x(), camPos.y(), camPos.z());
60+
}
61+

src/wind.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef WIND_H
2+
#define WIND_H
3+
4+
#include "particlesystem.h"
5+
6+
class Wind : public ParticleSystem
7+
{
8+
public:
9+
Wind(float radius);
10+
11+
virtual void spawnParticle(Particle *particle) override;
12+
13+
virtual void updateParticle(Particle &particle, float dt) override;
14+
15+
virtual void renderGeometry(DrawContext &context) override;
16+
17+
private:
18+
float radius_;
19+
20+
};
21+
22+
#endif // WIND_H

0 commit comments

Comments
 (0)