Vivid
Loading...
Searching...
No Matches
marching_squares.cpp
1#include "Vivid.h"
2
3#include <thread>
4
5#include <math.h>
6
7int g_Dim;
8float g_DimPerlin;
9bool g_RenderQuad;
10double Time;
11
12float g_Thickness = 0.8f;
13float* g_Color = new float[3] { 0.0f, 0.0f, 0.0f };
14
15Vector<Vector<double>> val1, val2, val3, val4;
16
17int a[256] = { 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
18 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
19 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,
20 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
21 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122,
22 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54,
23 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169,
24 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
25 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212,
26 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213,
27 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
28 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104,
29 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241,
30 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157,
31 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
32 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180 };
33
34int p[512];
35
36double fade(double t)
37{
38 return t * t * t * (t * (t * 6 - 15) + 10);
39}
40
41double lerp(double t, double a, double b)
42{
43 return a + t * (b - a);
44}
45
46double grad(int hash, double x, double y, double z)
47{
48 int h = hash & 15;
49 double u = h < 8 ? x : y,
50 v = h < 4 ? y : h == 12 || h == 14 ? x
51 : z;
52 return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
53}
54
55void noise(double x, double y, double z, double& value)
56{
57 int X = (int)floor(x) & 255,
58 Y = (int)floor(y) & 255,
59 Z = (int)floor(z) & 255;
60 x -= floor(x);
61 y -= floor(y);
62 z -= floor(z);
63 double u = fade(x),
64 v = fade(y),
65 w = fade(z);
66 int A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z,
67 B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z;
68
69 double val = lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z)), lerp(u, grad(p[AB], x, y - 1, z), grad(p[BB], x - 1, y - 1, z))),
70 lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1, y, z - 1)),
71 lerp(u, grad(p[AB + 1], x, y - 1, z - 1),
72 grad(p[BB + 1], x - 1, y - 1, z - 1))));
73 value = val * 0.5 + 0.5;
74}
75
76void GetNoiseVec(int x, int y, double time, Vector<Vector<double>>& vec)
77{
78 for (int i = -1000 / g_Dim; i <= 1000 / g_Dim; i++)
79 {
80 Vector<double> temp;
81 temp.reserve(1000 / g_Dim * 2 + 1);
82 for (int j = -1000 / g_Dim; j <= 1000 / g_Dim; j++)
83 {
84 double v;
85 noise((i + x) * g_DimPerlin, (j + y) * g_DimPerlin, time, std::ref(v));
86 // temp.push_back(std::move(v));
87 temp.push_back(std::move(v));
88 }
89 // vec.emplace_back(std::move(temp));
90 vec.emplace_back(std::move(temp));
91 }
92}
93
94int GetState(const double& a, const double& b, const double& c, const double& d)
95{
96 int a1 = std::round(a);
97 int b1 = std::round(b);
98 int c1 = std::round(c);
99 int d1 = std::round(d);
100
101 return a1 * 8 + b1 * 4 + c1 * 2 + d1 * 1;
102}
103
105{
106private:
107public:
108 void Setup() override
109 {
110 OPENGL_2D_CONFS
111
112 for (int i = 0; i < 256; i++)
113 {
114 p[i] = a[i];
115 p[256 + i] = a[i];
116 }
117
118 g_Dim = 2;
119 g_DimPerlin = g_Dim * 0.020735;
120
121 Time = 0;
122
123 val1.reserve((1000 / g_Dim * 1000 / g_Dim) + 1000);
124 val2.reserve((1000 / g_Dim * 1000 / g_Dim) + 1000);
125 val3.reserve((1000 / g_Dim * 1000 / g_Dim) + 1000);
126 val4.reserve((1000 / g_Dim * 1000 / g_Dim) + 1000);
127
129 }
130
131 void Draw() override
132 {
134
135 // GetNoiseVec(0, 0, Time, std::ref(val1));
136 // GetNoiseVec(1, 0, Time, std::ref(val2));
137 // GetNoiseVec(1, 1, Time, std::ref(val3));
138 // GetNoiseVec(0, 1, Time, std::ref(val4));
139
140 std::thread t1(GetNoiseVec, 0, 0, Time, std::ref(val1));
141 std::thread t2(GetNoiseVec, 1, 0, Time, std::ref(val2));
142 std::thread t3(GetNoiseVec, 1, 1, Time, std::ref(val3));
143 std::thread t4(GetNoiseVec, 0, 1, Time, std::ref(val4));
144
145 t1.join();
146 t2.join();
147 t3.join();
148 t4.join();
149
150 for (int i = -1000 / g_Dim; i <= 1000 / g_Dim; i++)
151 {
152 for (int j = -1000 / g_Dim; j <= 1000 / g_Dim; j++)
153 {
154 double v1, v2, v3, v4;
155 v1 = val1[i + 1000 / g_Dim][j + 1000 / g_Dim];
156 v2 = val2[i + 1000 / g_Dim][j + 1000 / g_Dim];
157 v3 = val3[i + 1000 / g_Dim][j + 1000 / g_Dim];
158 v4 = val4[i + 1000 / g_Dim][j + 1000 / g_Dim];
159
160 int x = i * g_Dim;
161 int y = j * g_Dim;
162
164 Vivid::Maths::Vec2 x2 = Vivid::Maths::Vec2(x + g_Dim, y);
165 Vivid::Maths::Vec2 x3 = Vivid::Maths::Vec2(x + g_Dim, y + g_Dim);
166 Vivid::Maths::Vec2 x4 = Vivid::Maths::Vec2(x, y + g_Dim);
167
168 if (g_RenderQuad)
169 {
170 Vivid::Renderer2D::DrawQuad(x1.x, x1.y, g_Dim, g_Dim, Vivid::Maths::Vec3(v1, v1, v1));
171 }
172
173 Vivid::Maths::Vec2 a = x1 + (x2 - x1) * (float)(v1 / (v1 + v2));
174 Vivid::Maths::Vec2 b = x2 + (x3 - x2) * (float)(v2 / (v2 + v3));
175 Vivid::Maths::Vec2 c = x3 + (x4 - x3) * (float)(v3 / (v3 + v4));
176 Vivid::Maths::Vec2 d = x4 + (x1 - x4) * (float)(v4 / (v4 + v1));
177
178 Vivid::Maths::Vec3 col = Vivid::Maths::Vec3(g_Color[0], g_Color[1], g_Color[2]);
179 float thickness = g_Thickness;
180 switch (GetState(v1, v2, v3, v4))
181 {
182 case 1:
183 Vivid::Renderer2D::DrawLine(c, d, thickness, col);
184 break;
185 case 2:
186 Vivid::Renderer2D::DrawLine(b, c, thickness, col);
187 break;
188 case 3:
189 Vivid::Renderer2D::DrawLine(b, d, thickness, col);
190 break;
191 case 4:
192 Vivid::Renderer2D::DrawLine(a, b, thickness, col);
193 break;
194 case 5:
195 Vivid::Renderer2D::DrawLine(a, d, thickness, col);
196 Vivid::Renderer2D::DrawLine(b, c, thickness, col);
197 break;
198 case 6:
199 Vivid::Renderer2D::DrawLine(a, c, thickness, col);
200 break;
201 case 7:
202 Vivid::Renderer2D::DrawLine(a, d, thickness, col);
203 break;
204 case 8:
205 Vivid::Renderer2D::DrawLine(d, a, thickness, col);
206 break;
207 case 9:
208 Vivid::Renderer2D::DrawLine(c, a, thickness, col);
209 break;
210 case 10:
211 Vivid::Renderer2D::DrawLine(a, b, thickness, col);
212 Vivid::Renderer2D::DrawLine(c, d, thickness, col);
213 break;
214 case 11:
215 Vivid::Renderer2D::DrawLine(a, b, thickness, col);
216 break;
217 case 12:
218 Vivid::Renderer2D::DrawLine(b, d, thickness, col);
219 break;
220 case 13:
221 Vivid::Renderer2D::DrawLine(b, c, thickness, col);
222 break;
223 case 14:
224 Vivid::Renderer2D::DrawLine(d, c, thickness, col);
225 break;
226 }
227 }
228 }
229
230 Time += 0.025;
231
232 val1.clear();
233 val2.clear();
234 val3.clear();
235 val4.clear();
237 }
238
239 void ImGuiRender() override
240 {
241 ImGui::Begin("Settings");
242 // ImGui::SliderFloat3("Translation Model 1", &suzannePosition.x, -500.0f, 500.0f);
243 // // ImGui::SliderFloat3("Translation Model 2", &lightPosition.x, -300.0f, 300.0f);
244 // ImGui::SliderFloat3("Light Position", &lightPos.x, -500.0f, 500.0f);
245 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
246 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
247
248 ImGui::SliderFloat("Thickness", &g_Thickness, 0.0f, 10.0f);
249 ImGui::SliderInt("Dimension", &g_Dim, 2.0f, 10.0f);
250 ImGui::SliderFloat("Perlin Dimension", &g_DimPerlin, 0.0f, 1.0f);
251 ImGui::Checkbox("RenderQuad", &g_RenderQuad);
252 ImGui::ColorPicker3("Color", g_Color);
253 ImGui::End();
254 }
255};
256
257Application* Vivid::CreateApplication()
258{
259 Application* app = Application::GetInstance(1920, 1080, "Marching Squares");
260 Maths::Vec3 pos = Maths::Vec3(0, 0, 0);
261 float rot = 0.0f;
262 float zoom = 0.10f;
263
264 Camera* camera = new OrthoCamera(pos, rot, zoom, -1000.0f, 1000.0f);
265 app->SetCamera(camera);
266 app->SetRenderingInterface(new ExampleInterface);
267 return app;
268}
269
270int main()
271{
272 return Vivid::main(0, nullptr);
273}
Application class.
Definition: Application.h:19
A class that represents the camera.
Definition: Camera.h:25
void Setup() override
Setup function.
void ImGuiRender() override
ImGuiRender function.
void Draw() override
Draw function.
A class for the OrthoCamera's.
Definition: OrthoCamera.h:12
RenderingInterface class.
static void BeginScene()
Begins the scene.
Definition: Renderer2D.cpp:67
static void Init(int reserveVertices=10000)
Initializes the Renderer2D.
Definition: Renderer2D.cpp:9
static void EndScene()
Ends the scene.
Definition: Renderer2D.cpp:119
static void DrawLine(Maths::Vec2 start, Maths::Vec2 end, float thickness, Maths::Vec3 color)
Draws a line.
Definition: Renderer2D.cpp:98
static void DrawQuad(float x, float y, float width, float height, const Maths::Vec3 &color)
Draws a quad.
Definition: Renderer2D.cpp:87
Contains a 2D vector.
Definition: Vec.h:108
Contains a 3D vector.
Definition: Vec.h:51