-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFluidSolver2D.cpp
More file actions
90 lines (81 loc) · 2.52 KB
/
Copy pathFluidSolver2D.cpp
File metadata and controls
90 lines (81 loc) · 2.52 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
#include <iostream>
#include <fstream>
#include <CL/cl.hpp>
#include <SFML/Graphics.hpp>
#include <math.h>
#include "FluidSolver2D.h"
#include "FluidSolver.h"
#include "Config.h"
using namespace std;
/** Entry point of the application*/
int main() {
// constants:
constexpr float initial_radius = 10.0f;
constexpr float velocity_add = 0.01f;
constexpr float mouse_wheel_increment = 1.0f;
constexpr float mouse_pressure_increment = 10.0f;
// sfml init
constexpr auto window_style = (!FULLSCREEN) ? sf::Style::Default : sf::Style::Fullscreen;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT, 32), "Fluid Solver", window_style);
window.setFramerateLimit(60); // framerate limit 60Hz
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
image.create(WIDTH, HEIGHT);
texture.loadFromImage(image);
sprite.setTexture(texture);
sprite.setPosition(0, 0);
// fluid simulation solver init
FluidSolver fluid;
fluid.initialization();
cl_uint8* pixelData = (cl_uint8*)image.getPixelsPtr();
fluid.set_data_image(pixelData);
// other variables
sf::Clock deltaClock;
sf::Vector2f pos0; // for the mouse
float radius = initial_radius; // mouse radius
// main loop
while (window.isOpen()){
sf::Time deltaTime = deltaClock.restart();
const float dt = deltaTime.asSeconds();
// Manage input
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Escape) {
window.close();
}
if (event.key.code == sf::Keyboard::Space) {
fluid.reset();
}
}
if (event.type == sf::Event::MouseWheelMoved) {
radius += mouse_wheel_increment*event.mouseWheel.delta;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
sf::Vector2f pos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
sf::Vector2f delta = pos - pos0;
if(delta.x < 100.f && delta.y < 100.f) {
fluid.add_velocity((int)pos.x, (int)pos.y, delta.x, delta.y, velocity_add, (int)radius);
}
pos0 = window.mapPixelToCoords(sf::Mouse::getPosition(window));
}
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
sf::Vector2f pos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
fluid.add_pressure((int)pos.x, (int)pos.y, (int)radius, mouse_pressure_increment*dt);
}
// update the simulation
fluid.update(dt);
fluid.update_image();
// display
texture.update(image);
window.clear(sf::Color::Black);
window.draw(sprite);
window.display();
}
return 0;
}