aboutsummaryrefslogtreecommitdiff
path: root/src/main.cc
blob: 92f6f2a1b88f7e228b054d0d0489ea994af0f329 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <memory>
#include <algorithm>
#include <iostream>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include "glfw/guard.h"
#include "glfw/window.h"

#include "buffer/vertex/fluid_cell_buffer.h"
#include "buffer/vertex/lattice_cell_buffer.h"

#include "shader/wrap/graphic_shader.h"
#include "shader/wrap/compute_shader.h"

#include "shader/code/geometry.glsl"
#include "shader/code/vertex.glsl"
#include "shader/code/fragment.glsl"

#include "shader/code/interact.glsl"
#include "shader/code/collide.glsl"

#include "timer.h"

GLuint maxLUPS = 100;
GLuint nX = 512;
GLuint nY = 256;
bool   open_boundaries    = false;
bool   show_fluid_quality = false;

float getWorldHeight(int window_width, int window_height, float world_width) {
	return world_width / window_width * window_height;
}

glm::mat4 getMVP(float world_width, float world_height) {
	const glm::mat4 projection = glm::ortho(
		-(world_width/2),  world_width/2,
		-(world_height/2), world_height/2,
		0.1f, 100.0f
	);

	const glm::mat4 view = glm::lookAt(
		glm::vec3(0,0,1),
		glm::vec3(0,0,0),
		glm::vec3(0,1,0)
	);

	return projection * view;
}

int setupPlainGeometry(int x, int y) {
	if ( x == 0 || y == 0 || x == nX-1 || y == nY-1 ) {
		return 0; // disable end of world
	}
	if (   ((x == 1 || x == nX-2) && (y > 0 && y < nY-1))
	    || ((y == 1 || y == nY-2) && (x > 0 && x < nX-1)) ) {
		return 2; // bounce back outer walls
	}
	return 1; // everything else shall be bulk fluid
}

int setupOpenGeometry(int x, int y) {
	if ( x == 0 || y == 0 || x == nX-1 || y == nY-1 ) {
		return 0; // disable end of world
	}
	if ( (x == 1 || x == nX-2) && (y > 0 && y < nY-1) ) {
		if ( x == 1 && y > 1 && y < nY-2 ) {
			return 5; // inflow
		}
		if ( x == nX-2 && y > 1 && y < nY-2 ) {
			return 6; // outflow
		}
		return 2; // bounce back outer walls
	}
	if ( (y == 1 || y == nY-2) && (x > 0 && x < nX-1) ) {
		return 2; // bounce back outer walls
	}
	return 1; // everything else shall be bulk fluid
}

int render() {
	Window window("compustream");

	if ( !window.isGood() ) {
		std::cerr << "Failed to open GLFW window." << std::endl;
		return -1;
	}

	float world_width  = 1.1*nX;
	float world_height = getWorldHeight(window.getWidth(), window.getHeight(), world_width);

	glm::mat4 MVP = getMVP(world_width,  world_height);

	std::unique_ptr<GraphicShader> scene_shader;

	std::unique_ptr<LatticeCellBuffer> lattice_a;
	std::unique_ptr<LatticeCellBuffer> lattice_b;
	std::unique_ptr<FluidCellBuffer>   fluid;

	std::unique_ptr<ComputeShader> interact_shader;
	std::unique_ptr<ComputeShader> collide_shader;

	window.init([&]() {
		scene_shader = std::make_unique<GraphicShader>(
			VERTEX_SHADER_CODE, GEOMETRY_SHADER_CODE, FRAGMENT_SHADER_CODE);

		lattice_a = std::make_unique<LatticeCellBuffer>(nX, nY);
		lattice_b = std::make_unique<LatticeCellBuffer>(nX, nY);
		fluid     = std::make_unique<  FluidCellBuffer>(nX, nY, open_boundaries ? setupOpenGeometry : setupPlainGeometry);

		interact_shader = std::make_unique<ComputeShader>(INTERACT_SHADER_CODE);
		collide_shader  = std::make_unique<ComputeShader>(COLLIDE_SHADER_CODE);
	});

	if ( !interact_shader->isGood() || !collide_shader->isGood() ) {
		std::cerr << "Compute shader error." << std::endl;
		return -1;
	}

	bool update_lattice = true;
	bool tick           = true;

	auto pause_key = window.getKeyWatcher(GLFW_KEY_SPACE);

	int prevMouseState = 0;
	float prevLatticeMouseX;
	float prevLatticeMouseY;

	int currMouseState = 0;
	float currLatticeMouseX;
	float currLatticeMouseY;

	auto tick_buffers = { lattice_a->getBuffer(), lattice_b->getBuffer(), fluid->getBuffer() };
	auto tock_buffers = { lattice_b->getBuffer(), lattice_a->getBuffer(), fluid->getBuffer() };

	GLuint iT = 0;
	int statLUPS = 0;

	auto last_lattice_update = timer::now();
	auto last_lups_update    = timer::now();

	window.render([&](bool window_size_changed) {
		if ( pause_key.wasClicked() ) {
			update_lattice = !update_lattice;
		}

		if ( window_size_changed ) {
			world_height = getWorldHeight(window.getWidth(), window.getHeight(), world_width);
			MVP = getMVP(world_width, world_height);
		}

		if ( update_lattice && timer::millisecondsSince(last_lattice_update) >= 1000/maxLUPS ) {
			if ( timer::secondsSince(last_lups_update) >= 1.0 ) {
				std::cout << "\rComputing about " << statLUPS << " lattice updates per second." << std::flush;
				statLUPS = 0;
				last_lups_update = timer::now();
			}

			statLUPS += 1;

			if ( tick ) {
				interact_shader->workOn(tick_buffers);
				collide_shader->workOn(tick_buffers);
				tick = false;
			} else {
				interact_shader->workOn(tock_buffers);
				collide_shader->workOn(tock_buffers);
				tick = true;
			}

			/// Perform collide & stream steps
			{
				auto guard = collide_shader->use();

				collide_shader->setUniform("fluidQuality", show_fluid_quality);
				collide_shader->setUniform("iT", iT);
				iT += 1;

				collide_shader->dispatch(nX, nY);
			}

			last_lattice_update = timer::now();
		}

		/// Update mouse projection
		{
			const auto m = window.getMouse();

			prevMouseState = currMouseState;
			prevLatticeMouseX = currLatticeMouseX;
			prevLatticeMouseY = currLatticeMouseY;

			currMouseState = std::get<0>(m);
			currLatticeMouseX = float(std::get<1>(m)) / window.getWidth()  * world_width  + nX/2;
			currLatticeMouseY = float(std::get<2>(m)) / window.getHeight() * world_height + nY/2;
		}

		/// Handle mouse-based interaction
		if ( currMouseState != 0 || prevMouseState != 0 ) {
			auto guard = interact_shader->use();

			interact_shader->setUniform("influxRequested", currMouseState == 1);
			interact_shader->setUniform("wallRequested",   currMouseState == 2);

			interact_shader->setUniform("startOfLine", prevLatticeMouseX, prevLatticeMouseY);
			interact_shader->setUniform("endOfLine", currLatticeMouseX, currLatticeMouseY);

			interact_shader->dispatch(nX, nY);
		}

		{
			auto guard = scene_shader->use();

			scene_shader->setUniform("MVP", MVP);
			scene_shader->setUniform("nX", nX);
			scene_shader->setUniform("nY", nY);
			scene_shader->setUniform("fluidQuality", show_fluid_quality);

			glClear(GL_COLOR_BUFFER_BIT);
			fluid->draw();
		}
	});

	std::cout << std::endl;

	return 0;
}

bool parseArguments(int argc, char* argv[]) {
	for ( int i = 1; i < argc; ++i ) {
		const auto& arg = std::string_view(argv[i]);

		if ( arg == "--lups" ) {
			if ( i+1 < argc ) {
				try {
					i       += 1;
					maxLUPS = std::stoi(argv[i]);
				}
				catch ( std::invalid_argument& ex ) {
					std::cerr << "Maximum lattice updates per second malformed." << std::endl;
					return false;
				}
			} else {
				std::cerr << "Maximum lattice updates per second undefined." << std::endl;
				return false;
			}
		}

		if ( arg == "--size" ) {
			if ( i+2 < argc ) {
				try {
					i += 1;
					nX = std::stoi(argv[i]);
					i += 1;
					nY = std::stoi(argv[i]);
				}
				catch ( std::invalid_argument& ex ) {
					std::cerr << "Lattice size malformed." << std::endl;
					return false;
				}
			} else {
				std::cerr << "Lattice size undefined." << std::endl;
				return false;
			}
		}

		if ( arg == "--open" ) {
			open_boundaries = true;
		}

		if ( arg == "--quality" ) {
			show_fluid_quality = true;
		}
	}
	return true;
}

int main(int argc, char* argv[]) {
	if ( parseArguments(argc, argv) ) {
		GlfwGuard glfw;

		if( !glfw.isGood() ) {
			std::cerr << "Failed to initialize GLFW." << std::endl;
			return -1;
		}

		return render();
	} else {
		return -1;
	}
}