-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluid.cpp
More file actions
163 lines (124 loc) · 3.9 KB
/
Fluid.cpp
File metadata and controls
163 lines (124 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "Fluid.h"
#include <array>
#include "Constants.h"
#include "Physics.h"
#include <iostream>
int IX(int x, int y);
sf::Color Hsv(int hue, float sat, float val, float d);
float MapToRange(float val, float minIn, float maxIn, float minOut, float maxOut);
Fluid::Fluid(float diffusion, float viscosity, float dt)
{
this->dt = dt;
this->diff = diffusion;
this->visc = viscosity;
// create vectors with size N*N filled with 0
//this->cells.resize(N * N, sf::RectangleShape(sf::Vector2f(SCALE, SCALE)));
this->s.resize(N * N, 0);
this->density.resize(N * N, 5);
this->Vx.resize(N * N, 0);
this->Vy.resize(N * N, 0);
this->Vx0.resize(N * N, 0);
this->Vy0.resize(N * N, 0);
sf::VertexArray cellVertexArray(sf::Quads);
// create particles
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int x = i * SCALE;
int y = j * SCALE;
float s = SCALE / 2.0f;
sf::Vertex v1(sf::Vector2f(x - s, y - s));
sf::Vertex v2(sf::Vector2f(x - s, y + s));
sf::Vertex v3(sf::Vector2f(x + s, y + s));
sf::Vertex v4(sf::Vector2f(x + s, y - s));
// random colors
v1.color = v2.color = v3.color = v4.color = sf::Color::White;
// add to vertex array
cellVertexArray.append(v1);
cellVertexArray.append(v2);
cellVertexArray.append(v3);
cellVertexArray.append(v4);
int index = IX(i, j);
}
}
// set vertex array
this->vertexArray = cellVertexArray;
}
void Fluid::AddDensity(int x, int y, float amount)
{
this->density[IX(x, y)] += amount;
}
void Fluid::AddVelocity(int x, int y, float amountX, float amountY)
{
this->Vx[IX(x, y)] += amountX;
this->Vy[IX(x, y)] += amountY;
}
void Fluid::Step()
{
Physics::Diffuse(1, this->Vx0, this->Vx, this->visc, this->dt);
Physics::Diffuse(2, this->Vy0, this->Vy, this->visc, this->dt);
Physics::Project(this->Vx0, this->Vy0, this->Vx, this->Vy);
Physics::Advection(1, this->Vx, this->Vx0, this->Vx0, this->Vy0, this->dt);
Physics::Advection(2, this->Vy, this->Vy0, this->Vx0, this->Vy0, dt);
Physics::Project(this->Vx, this->Vy, this->Vx0, this->Vy0);
Physics::Diffuse(0, this->s, this->density, this->diff, this->dt);
Physics::Advection(0, this->density, this->s, this->Vx, this->Vy, this->dt);
}
void Fluid::RenderDensity(sf::RenderWindow& window)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int index = IX(i, j);
float d = this->density[index];
sf::Color newColor = Hsv(d, 1, 1, 255);
// update vertex colors
vertexArray[index * 4].color = newColor;
vertexArray[index * 4 + 1].color = newColor;
vertexArray[index * 4 + 2].color = newColor;
vertexArray[index * 4 + 3].color = newColor;
}
}
window.draw(this->vertexArray);
}
void Fluid::RenderVelocity(sf::RenderWindow& window)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int index = IX(i, j);
float vx = this->Vx[index];
float vy = this->Vy[index];
int r = MapToRange(vx, -0.05f, 0.05f, 0, 255);
int g = MapToRange(vy, -0.05f, 0.05f, 0, 255);
sf::Color newColor = sf::Color(r, g, 255);
// update vertex colors
vertexArray[index * 4].color = newColor;
vertexArray[index * 4 + 1].color = newColor;
vertexArray[index * 4 + 2].color = newColor;
vertexArray[index * 4 + 3].color = newColor;
}
}
window.draw(this->vertexArray);
}
void Fluid::RenderDefault(sf::RenderWindow& window)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int index = IX(i, j);
float d = std::min(255.f, this->density[index]);
sf::Color newColor = sf::Color(255, 255, 255, d);
// update vertex colors
vertexArray[index * 4].color = newColor;
vertexArray[index * 4 + 1].color = newColor;
vertexArray[index * 4 + 2].color = newColor;
vertexArray[index * 4 + 3].color = newColor;
}
}
window.draw(this->vertexArray);
}