Skip to content

Commit 41f0e25

Browse files
committed
added velocity color mode
1 parent c7a754a commit 41f0e25

File tree

11 files changed

+45
-18
lines changed

11 files changed

+45
-18
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Euler fluid simulation
22
===
3-
![Demo Gif](demo/euler-fluid-simulation-resized.gif)
3+
![Demo Gif](demo/euler-fluid-simulation-default.gif)
4+
![Demo Gif](demo/euler-fluid-simulation-hsb.gif)
5+
![Demo Gif](demo/euler-fluid-simulation-velocity.gif)
46

57
## Inspiration
68

@@ -25,7 +27,7 @@ Pressing left mouse will add `density`.
2527
Dragging the mouse will apply `force` towards the moving direction.
2628

2729
Keyboard options:
28-
- `c` = Color Mode
30+
- `c` = switch color mode (default, hsb, velocity)
2931

3032
## Building the project
3133

File renamed without changes.
3.5 MB
Loading
2.86 MB
Loading

header/color.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
enum class Color { Default, Hsb, Velocity };

header/container.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "./physics.h"
55
#include "./const.h"
6+
#include "./options.h"
67

78
class Container {
89
private:
@@ -24,6 +25,7 @@ class Container {
2425
float density[SIZE*SIZE];
2526

2627
void InitArr(float arr[], int size);
28+
float MapToRange(float value, float minIn, float maxIn, float minOut, float maxOut);
2729
public:
2830
Container();
2931
Container(float dt, float diff, float visc);
@@ -32,7 +34,7 @@ class Container {
3234
void AddDensity(float x, float y, float amount);
3335
void AddVelocity(float x, float y, float px, float py);
3436
void Step();
35-
void Render(sf::RenderWindow& win, bool color);
37+
void Render(sf::RenderWindow& win, Color color);
3638
void FadeDensity(int size);
3739

3840
sf::Color Hsv(int hue, float sat, float val, float d);

header/options.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#include "./color.h"
2+
13
class Options {
24
private:
3-
bool color;
5+
Color color;
46
public:
5-
Options(bool color=false);
7+
Options(Color color=Color::Default);
68
~Options();
79

8-
bool GetColor();
9-
void SetColor(bool value);
10+
Color GetColor();
11+
void SetColor(Color c);
1012
};

header/sim.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include<vector>
22

33
#include "./container.h"
4-
#include "./options.h"
54

65
class Sim {
76
private:

src/container.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,36 @@ sf::Color Container::Hsv(int hue, float sat, float val, float d) {
8181
}
8282
}
8383

84-
void Container::Render(sf::RenderWindow& win, bool color) {
84+
float Container::MapToRange(float val, float minIn, float maxIn, float minOut, float maxOut) {
85+
float x = (val - minIn) / (maxIn - minIn);
86+
float result = minOut + (maxOut - minOut) * x;
87+
return (result < minOut) ? minOut : (result > maxOut) ? maxOut : result;
88+
}
89+
90+
void Container::Render(sf::RenderWindow& win, Color color) {
8591
win.clear();
8692
for (int i = 0; i < this->size; i++) {
8793
for(int j = 0; j < this->size; j++) {
8894
sf::RectangleShape rect;
8995
rect.setSize(sf::Vector2f(SCALE, SCALE));
9096
rect.setPosition(j * SCALE, i * SCALE);
9197

92-
if (color) {
93-
rect.setFillColor(this->Hsv((this->density[IX(i,j,this->size)]), 1, 1, 255));
94-
} else {
95-
rect.setFillColor(sf::Color(255, 255, 255, this->density[IX(i,j,this->size)]));
96-
}
98+
switch (color) {
99+
case Color::Default:
100+
rect.setFillColor(sf::Color(255, 255, 255, this->density[IX(i,j,this->size)]));
101+
break;
102+
case Color::Hsb:
103+
rect.setFillColor(this->Hsv((this->density[IX(i,j,this->size)]), 1, 1, 255));
104+
break;
105+
case Color::Velocity: {
106+
int r = (int)this->MapToRange(this->x[IX(i,j,this->size)], -0.05f, 0.05f, 0, 255);
107+
int g = (int)this->MapToRange(this->y[IX(i,j,this->size)], -0.05f, 0.05f, 0, 255);
108+
rect.setFillColor(sf::Color(r, g, 255));
109+
break;
110+
}
111+
default:
112+
break;
113+
};
97114

98115
win.draw(rect);
99116
}

src/options.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "../header/options.h"
22

3-
Options::Options(bool color) {
3+
Options::Options(Color color) {
44
this->color = color;
55
}
66

77
Options::~Options() {}
88

9-
bool Options::GetColor() { return this->color; }
9+
Color Options::GetColor() { return this->color; }
1010

11-
void Options::SetColor(bool value) { this->color = value; }
11+
void Options::SetColor(Color c) { this->color = c; }

0 commit comments

Comments
 (0)