Skip to content

Commit c8f52fd

Browse files
author
beryll1um
committed
impl(fluid), mod(shader), fix(bounce): a lot of changes and new features
1 parent 5d567f4 commit c8f52fd

File tree

28 files changed

+348
-37
lines changed

28 files changed

+348
-37
lines changed

examples/bounce/bounce.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using namespace Unified;
99
using namespace Unified::Graphics;
1010

11-
class CubeLayer;
11+
class CircleLayer;
1212
class ImGuiLayer;
1313

1414
class ExampleBounce : public Application
@@ -57,7 +57,7 @@ class ExampleBounce : public Application
5757
public:
5858

5959
ExampleBounce() : Application("ExampleBounce", VideoMode(600, 600), !Window::Resizable) {
60-
push_layer<CubeLayer>(this);
60+
push_layer<CircleLayer>(this);
6161
push_layer<ImGuiLayer>(this);
6262
set_frame_limit(60);
6363
}
@@ -87,15 +87,15 @@ class ExampleBounce : public Application
8787

8888
};
8989

90-
class CubeLayer : public Layer
90+
class CircleLayer : public Layer
9191
{
9292
public:
9393

9494
ExampleBounce *application;
9595

9696
Graphics2D::VertexArray vertex_array;
9797

98-
CubeLayer(ExampleBounce *application) : application(application), vertex_array(PrimitiveType::Polygon, 32) {
98+
CircleLayer(ExampleBounce *application) : application(application), vertex_array(PrimitiveType::Polygon, 32) {
9999
std::fill(application->circle, application->circle + 32, Vertex2d({ 1.f, 0.f, 1.f, 1.f }));
100100
}
101101

examples/fluid/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
project(fluid)
2+
3+
add_executable(${PROJECT_NAME} "${PROJECT_NAME}.cpp")
4+
target_link_libraries(${PROJECT_NAME} PUBLIC ${UNIFIED_PROJECT})

examples/fluid/fluid.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <unified.hpp>
2+
3+
#include <unified/core/math/point4.hpp>
4+
5+
#include <unified/graphics/2d/drawable/vertex_array.hpp>
6+
#include <unified/graphics/shader.hpp>
7+
8+
#include <unified/core/system/sleep.hpp>
9+
10+
#include <imgui_layer/imgui_layer.hpp>
11+
12+
using namespace Unified;
13+
using namespace Unified::Graphics;
14+
15+
class FluidLayer;
16+
class ImGuiLayer;
17+
18+
class ExampleInterfaces : public Application
19+
{
20+
public:
21+
22+
Time start_time;
23+
24+
float wave_matrix[4] = { 0.9, 0.9, 0.9, -0.9 };
25+
26+
Vertex2d quad[4] =
27+
{ { { -1.0, -1.0 }, { 1.0, 0.0, 0.0, 1.0 } },
28+
{ { -1.0, 1.0 }, { 0.0, 1.0, 0.0, 1.0 } },
29+
{ { 1.0, 1.0 }, { 0.0, 0.0, 1.0, 1.0 } },
30+
{ { 1.0, -1.0 }, { 1.0, 1.0, 0.0, 1.0 } } };
31+
32+
void window_resize_event(const WindowResizeEvent &event) {
33+
set_viewport(event.size);
34+
}
35+
36+
public:
37+
38+
ExampleInterfaces() : Application("ExampleFluid"), start_time(get_current_time()) {
39+
push_layer<FluidLayer>(this);
40+
push_layer<ImGuiLayer>(this);
41+
set_frame_limit(60);
42+
}
43+
44+
virtual bool OnUpdate(Time) override {
45+
clear();
46+
process_layers();
47+
swap_buffers();
48+
return poll_events();
49+
}
50+
51+
virtual void OnEvent(EventDispatcher &dispatcher) override {
52+
dispatcher.dispatch<WindowResizeEvent>(BIND_EVENT_FN(&ExampleInterfaces::window_resize_event, this));
53+
}
54+
55+
};
56+
57+
class FluidLayer : public Layer
58+
{
59+
public:
60+
61+
ExampleInterfaces *application;
62+
63+
Graphics2D::VertexArray vertex_array;
64+
65+
Shader shader;
66+
67+
FluidLayer(ExampleInterfaces *application)
68+
: application(application), vertex_array(PrimitiveType::Polygon, 4), shader(
69+
#include "fluid.vert"
70+
,
71+
#include "fluid.frag"
72+
) { }
73+
74+
virtual void OnUpdate(Time) override {
75+
shader.set_float("start_time", static_cast<float>(get_current_time().asSeconds() - application->start_time.asSeconds()));
76+
77+
auto resolution = application->get_size();
78+
shader.set_float2("resolution", { static_cast<float>(resolution.x), static_cast<float>(resolution.y) });
79+
shader.set_float4("wave_matrix", application->wave_matrix);
80+
81+
vertex_array.write(application->quad, sizeof(application->quad));
82+
application->draw(vertex_array, &shader);
83+
}
84+
85+
};
86+
87+
class ImGuiLayer : public Modules::ImGuiLayer
88+
{
89+
public:
90+
91+
ExampleInterfaces *application;
92+
93+
ImGuiLayer(ExampleInterfaces *application) : application(application) {
94+
Create(application);
95+
}
96+
97+
virtual void OnUpdate(Time) override {
98+
ImGui::Begin("ExampleFluid");
99+
if (ImGui::CollapsingHeader("Waves")) {
100+
ImGui::InputFloat4("Wave Matrix", application->wave_matrix);
101+
}
102+
if (ImGui::CollapsingHeader("Background")) {
103+
ImGui::ColorEdit3("Down Left", (float*)&application->quad[0].color);
104+
ImGui::ColorEdit3("Down Right", (float*)&application->quad[3].color);
105+
ImGui::ColorEdit3("Up Left", (float*)&application->quad[1].color);
106+
ImGui::ColorEdit3("Up Right", (float*)&application->quad[2].color);
107+
}
108+
ImGui::End();
109+
}
110+
111+
~ImGuiLayer() {
112+
Destroy();
113+
}
114+
115+
};
116+
117+
Application *UNIFIED_NAMESPACE::CreateApplication() {
118+
return new ExampleInterfaces();
119+
}

examples/fluid/fluid.frag

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
R"glsl(
2+
3+
#version 330 core
4+
5+
out vec4 fragment_color;
6+
7+
in vec4 out_wave_matrix;
8+
in float out_start_time;
9+
in vec2 out_resolution;
10+
in vec4 out_color;
11+
12+
// @note: much of magick numbers
13+
float position_random(in vec2 position) {
14+
return fract(sin(dot(position.xy, vec2(12.9898, 78.233))) * 43758.5453123);
15+
}
16+
17+
// @link: https://www.shadertoy.com/view/4dS3Wd
18+
float position_noise(in vec2 position) {
19+
vec2
20+
i = floor(position),
21+
f = fract(position);
22+
23+
float
24+
a = position_random(i),
25+
b = position_random(i + vec2(1.0, 0.0)),
26+
c = position_random(i + vec2(0.0, 1.0)),
27+
d = position_random(i + vec2(1.0, 1.0));
28+
29+
vec2 u = f * f * (3.0 - 2.0 * f);
30+
31+
return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
32+
}
33+
34+
const int octaves_count = 4;
35+
36+
float position_fbm(in vec2 position) {
37+
float value = 0.0;
38+
39+
mat2 rot = mat2(out_wave_matrix.x, out_wave_matrix.y, out_wave_matrix.z, out_wave_matrix.w);
40+
for (int i = 0; i < octaves_count; ++i) {
41+
value += 0.25 * position_noise(position);
42+
position = rot * position * 2.0 + vec2(-0.2, -0.7);
43+
}
44+
45+
return value;
46+
}
47+
48+
void main() {
49+
vec2 position = gl_FragCoord.xy / out_resolution.xy;
50+
51+
vec2 q = vec2(position_fbm(position), position_fbm(position + vec2(1.0)));
52+
53+
vec2 r = vec2(0.0);
54+
r.x = position_fbm(position + 1.0 * q + vec2(1.0, 1.0) + 0.085 * out_start_time);
55+
r.y = position_fbm(position + 1.0 * q + vec2(1.0, 1.0) + 0.085 * out_start_time);
56+
57+
fragment_color = vec4(position_fbm(position + r) * vec3(out_color), 1.0);
58+
}
59+
60+
)glsl"

examples/fluid/fluid.vert

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
R"glsl(
2+
3+
#version 330 core
4+
5+
layout (location = 0) in vec2 position;
6+
layout (location = 1) in vec4 color;
7+
8+
uniform vec4 wave_matrix;
9+
uniform float start_time;
10+
uniform vec2 resolution;
11+
12+
out vec4 out_wave_matrix;
13+
out float out_start_time;
14+
out vec2 out_resolution;
15+
out vec4 out_color;
16+
17+
void main()
18+
{
19+
gl_Position = vec4(position, 0.0, 1.0);
20+
out_wave_matrix = wave_matrix;
21+
out_start_time = start_time;
22+
out_resolution = resolution;
23+
out_color = color;
24+
}
25+
26+
27+
)glsl"

examples/layers/layers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <unified/graphics/2d/drawable/vertex_array.hpp>
44
#include <unified/graphics/2d/vertex.hpp>
55

6+
#include <unified/core/string.hpp>
7+
68
using namespace Unified;
79
using namespace Unified::Graphics;
810

include/unified/application/window/icons/icons.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class Icons
1717
public:
1818

1919
Icons();
20-
Icons(const string &icon);
20+
Icons(const Unified::string &icons);
2121
Icons(std::initializer_list<string> icons);
2222

2323
virtual ~Icons();
2424

25-
void add_icon(const string &icon);
25+
void add_icon(const Unified::string &icon);
2626

2727
u32 count() const;
2828

include/unified/core/math/point2.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Point<_type, 2>
1414
UNIFIED_CONSTEXPR Point() : x(_type()), y(_type()) { }
1515
UNIFIED_CONSTEXPR Point(_type xy) : x(xy), y(xy) { }
1616
UNIFIED_CONSTEXPR Point(_type x, _type y) : x(x), y(y) { }
17+
UNIFIED_CONSTEXPR Point(_type *xy) : x(xy[0]), y(xy[1]) { }
1718

1819
UNIFIED_CONSTEXPR _type operator[](u32 i) const {
1920
UNIFIED_CONSTEXPR _type Point::*accessors[] = {

include/unified/core/math/point3.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Point<_type, 3>
1414
UNIFIED_CONSTEXPR Point() : x(_type()), y(_type()), z(_type()) { }
1515
UNIFIED_CONSTEXPR Point(_type xyz) : x(xyz), y(xyz), z(xyz) { }
1616
UNIFIED_CONSTEXPR Point(_type x, _type y, _type z) : x(x), y(y), z(z) { }
17+
UNIFIED_CONSTEXPR Point(_type *xyz) : x(xyz[0]), y(xyz[1]), z(xyz[2]) { }
1718

1819
UNIFIED_CONSTEXPR _type operator[](u32 i) const {
1920
UNIFIED_CONSTEXPR _type Point::*accessors[] = {

include/unified/core/math/point4.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Point<_type, 4>
1414
UNIFIED_CONSTEXPR Point() : x(_type()), y(_type()), z(_type()), w(_type()) { }
1515
UNIFIED_CONSTEXPR Point(_type xyzw) : x(xyzw), y(xyzw), z(xyzw), w(xyzw) { }
1616
UNIFIED_CONSTEXPR Point(_type x, _type y, _type z, _type w) : x(x), y(y), z(z), w(w) { }
17+
UNIFIED_CONSTEXPR Point(_type *xyzw) : x(xyzw[0]), y(xyzw[1]), z(xyzw[2]), w(xyzw[3]) { }
1718

1819
UNIFIED_CONSTEXPR _type operator[](u32 i) const {
1920
UNIFIED_CONSTEXPR _type Point::*accessors[] = {

0 commit comments

Comments
 (0)