-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (144 loc) · 5.72 KB
/
main.cpp
File metadata and controls
167 lines (144 loc) · 5.72 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
164
165
166
167
/***************************************************************************
*
* Copyright (C) Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Description:
* Application description for Fluid Simulation demo.
*
**************************************************************************/
#include "fluid.h"
#include <Corrade/Containers/StringStlView.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Texture.h>
#include <Magnum/GL/TextureFormat.h>
#include <Magnum/ImageView.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/PixelFormat.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Primitives/Square.h>
#include <Magnum/Shaders/FlatGL.h>
#include <Magnum/Trade/MeshData.h>
#include <sycl/sycl.hpp>
constexpr Magnum::PixelFormat PIXELFORMAT{Magnum::PixelFormat::RGBA8Unorm};
// Title bar text
constexpr std::string_view WINDOWTITLE{
"Codeplay Fluid Simulation"
" - Move mouse to add fluid"
" - Press space to clear fluid"};
// Size of the fluid container edge (always square shaped).
constexpr int SIZE{300};
// Default scale at which fluid container is rendered.
constexpr int SCALE{3};
class FluidSimulationApp : public Magnum::Platform::Application {
public:
FluidSimulationApp(const Arguments& arguments)
: Magnum::Platform::Application{arguments,
Configuration{}.setTitle(WINDOWTITLE),
GLConfiguration{}.setFlags(
GLConfiguration::Flag::QuietLog)},
size_{SIZE},
fluid_{SIZE, 0.2f, 0.0f, 0.0000001f},
mesh_{Magnum::MeshTools::compile(Magnum::Primitives::squareSolid(
Magnum::Primitives::SquareFlag::TextureCoordinates))},
shader_{Magnum::Shaders::FlatGL2D::Configuration{}.setFlags(
Magnum::Shaders::FlatGL2D::Flag::Textured |
Magnum::Shaders::FlatGL2D::Flag::TextureTransformation)} {
// Set window size.
setWindowSize({SIZE * SCALE, SIZE * SCALE});
Magnum::GL::defaultFramebuffer.setViewport(
{{0, 0}, {SIZE * SCALE, SIZE * SCALE}});
texture_.setWrapping(Magnum::GL::SamplerWrapping::ClampToEdge)
.setMagnificationFilter(Magnum::GL::SamplerFilter::Linear)
.setMinificationFilter(Magnum::GL::SamplerFilter::Linear)
.setStorage(1, Magnum::GL::textureFormat(PIXELFORMAT), {size_, size_});
shader_.bindTexture(texture_);
}
// Called once per frame to update the fluid.
void tickEvent() override final {
// Check that mouse has moved.
if (prev_x != 0 && prev_x != 0) {
// Add density at mouse cursor location.
auto x{static_cast<std::size_t>(prev_x * size_)};
auto y{static_cast<std::size_t>(prev_y * size_)};
fluid_.AddDensity(x, y, 400, 2);
}
// Fade overall dye levels slowly over time.
fluid_.DecreaseDensity(0.99f);
// Update fluid physics.
fluid_.Update();
}
// Draws fluid to the screen.
void drawEvent() override final {
// Clear screen.
Magnum::GL::defaultFramebuffer.clear(Magnum::GL::FramebufferClear::Color |
Magnum::GL::FramebufferClear::Depth);
// Update texture with pixel data array.
fluid_.WithData([&](sycl::uchar4 const* data) {
Magnum::ImageView2D img{
PIXELFORMAT,
{size_, size_},
Corrade::Containers::ArrayView{
reinterpret_cast<const char*>(data),
size_ * size_ * Magnum::pixelFormatSize(PIXELFORMAT)}};
texture_.setSubImage(0, {0, 0}, img);
});
// Draw texture to screen.
shader_.draw(mesh_);
redraw();
swapBuffers();
}
private:
void keyPressEvent(KeyEvent& event) override final {
// Reset fluid container to empty if SPACE key is pressed.
if (event.key() == Sdl2Application::Key::Space) {
fluid_.Reset();
}
}
void pointerMoveEvent(PointerMoveEvent& event) override final {
// Get mouse position as fraction of window size.
auto x{event.position().x() / float(windowSize().x())};
auto y{1.0f - event.position().y() / float(windowSize().y())};
// Check that previous mouse position was not only zero (for initial value
// to be set).
if (prev_x != 0.0 || prev_y != 0.0) {
// Get amount by which mouse has moved.
auto amount_x{(x - prev_x) * size_};
auto amount_y{(y - prev_y) * size_};
// Add velocity and density in direction of mouse travel.
// Multiplied by size because x and y are in 0.0 to 1.0 relative window
// coordinates.
auto current_x{static_cast<std::size_t>(x * size_)};
auto current_y{static_cast<std::size_t>(y * size_)};
fluid_.AddVelocity(current_x, current_y, amount_x, amount_y);
}
// Update previous mouse position.
prev_x = x;
prev_y = y;
}
// Square size of fluid container.
int size_{0};
// Store previous mouse positions.
float prev_x{0};
float prev_y{0};
// Fluid container object.
SYCLFluidContainer fluid_;
// Fluid texture.
Magnum::GL::Texture2D texture_;
// Mesh and shader to draw the texture.
Magnum::GL::Mesh mesh_;
Magnum::Shaders::FlatGL2D shader_;
};
// Magnum app initialization.
MAGNUM_APPLICATION_MAIN(FluidSimulationApp)