-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluid.cpp
198 lines (173 loc) · 4.85 KB
/
fluid.cpp
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
// fluid.cpp - Devon McKee
#include <glad.h>
#include <GLFW/glfw3.h>
#include "GLXtras.h"
#include <time.h>
#include <vector>
#include "VecMat.h"
#include "Camera.h"
GLuint vBuffer = 0;
GLuint program = 0;
int win_width = 800;
int win_height = 800;
Camera camera((float)win_width / win_height, vec3(0, 0, 0), vec3(0, 0, -5));
float cube_points[][3] = { {-1, -1, 1}, {1, -1, 1}, {1, -1, -1}, {-1, -1, -1}, {-1, 1, -1}, {1, 1, -1}, {1, 1, 1}, {-1, 1, 1} };
int cube_faces[][4] = { {0, 1, 2, 3}, {2, 3, 4, 5}, {4, 5, 6, 7}, {6, 7, 0, 1}, {0, 3, 4, 7}, {1, 2, 5, 6} };
const int GRID_NUM = 4;
enum CELL { AIR, BLOCK, WATER };
struct Cell {
CELL type = AIR;
float level = 0;
};
struct Grid {
std::vector<std::vector<std::vector<Cell>>> grid;
Grid() {
// Initialize grid
for (int i = 0; i < GRID_NUM; i++) {
grid.push_back(std::vector<std::vector<Cell>>());
for (int j = 0; j < GRID_NUM; j++) {
grid[i].push_back(std::vector<Cell>());
for (int k = 0; k < GRID_NUM; k++) {
grid[i][j].push_back(Cell());
grid[i][j][k].type = BLOCK;
}
}
}
}
void DownwardFlow() {
}
void SidewaysFlow() {
}
void UpwardsFlow() {
}
void Simulate() {
}
void Render() {
SetUniform(program, "persp", camera.persp);
for (int i = 0; i < GRID_NUM; i++) {
for (int j = 0; j < GRID_NUM; j++) {
for (int k = 0; k < GRID_NUM; k++) {
float interval = 2.0 / GRID_NUM;
mat4 modelview = camera.modelview * Translate(interval * i - 1.0, interval * j - 1.0, interval * k - 1.0) * Scale(vec3(1 / GRID_NUM));
SetUniform(program, "modelview", modelview);
switch (grid[i][j][k].type) {
case BLOCK:
SetUniform(program, "color", vec4(0, 0, 0, 1));
break;
case WATER:
SetUniform(program, "color", vec4(0, 0, 1, 1));
break;
}
if (grid[i][j][k].type != AIR) {
for (int f = 0; f < 6; f++) {
glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, cube_faces[f]);
}
}
}
}
}
}
};
Grid grid;
const char* vertexShader = R"(
#version 130
in vec3 point;
uniform mat4 persp;
uniform mat4 modelview;
void main() {
gl_Position = persp * modelview * vec4(point, 1);
}
)";
const char* fragmentShader = R"(
#version 130
uniform vec4 color;
out vec4 pColor;
void main() {
pColor = color;
}
)";
void InitVertexBuffer() {
glGenBuffers(1, &vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_points), cube_points, GL_STATIC_DRAW);
}
void Resize(GLFWwindow* window, int width, int height) {
camera.Resize(win_width = width, win_height = height);
glViewport(0, 0, win_width, win_height);
}
void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
bool Shift(GLFWwindow* w) {
return glfwGetKey(w, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS ||
glfwGetKey(w, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS;
}
void MouseButton(GLFWwindow* w, int butn, int action, int mods) {
double x, y;
glfwGetCursorPos(w, &x, &y);
y = win_height - y;
if (action == GLFW_PRESS)
camera.MouseDown((int)x, (int)y);
if (action == GLFW_RELEASE)
camera.MouseUp();
}
void MouseMove(GLFWwindow* w, double x, double y) {
if (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { // drag
y = win_height - y;
camera.MouseDrag((int)x, (int)y, Shift(w));
}
}
void MouseWheel(GLFWwindow* w, double ignore, double spin) {
camera.MouseWheel(spin > 0, Shift(w));
}
void Display() {
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glClearColor(0.6, 0.6, 0.6, 1);
glClear(GL_COLOR_BUFFER_BIT);
VertexAttribPointer(program, "point", 3, 0, (void*)0);
// Draw enclosing cube
SetUniform(program, "persp", camera.persp);
SetUniform(program, "modelview", camera.modelview);
SetUniform(program, "color", vec4(1, 1, 1, 1));
for (int f = 0; f < 6; f++) {
glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, cube_faces[f]);
}
grid.Simulate();
grid.Render();
glFlush();
}
int main() {
srand(time(NULL));
if (!glfwInit())
return 1;
GLFWwindow* window = glfwCreateWindow(win_width, win_height, "FluidSim", NULL, NULL);
if (!window) {
glfwTerminate();
return 1;
}
glfwSetWindowPos(window, 100, 100);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
PrintGLErrors();
if (!(program = LinkProgramViaCode(&vertexShader, &fragmentShader)))
return 0;
InitVertexBuffer();
glfwSetCursorPosCallback(window, MouseMove);
glfwSetMouseButtonCallback(window, MouseButton);
glfwSetScrollCallback(window, MouseWheel);
glfwSetKeyCallback(window, Keyboard);
glfwSetWindowSizeCallback(window, Resize);
glfwSwapInterval(1);
while (!glfwWindowShouldClose(window)) {
Display();
glfwPollEvents();
glfwSwapBuffers(window);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vBuffer);
glfwDestroyWindow(window);
glfwTerminate();
}