-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl.cu
308 lines (261 loc) · 8.69 KB
/
gl.cu
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <fluid.cuh>
#include <glad/glad.h>
// GLFW
#include <GLFW/glfw3.h>
#include <iostream>
#include "params.h"
// 着色器源码
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"out vec2 TexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
" TexCoord = aTexCoord;\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D ourTexture;\n"
"void main()\n"
"{\n"
" FragColor = texture(ourTexture, TexCoord);\n"
"}\0";
int i = 0;
void add_force(FluidCUDA &fluid, int i) {
// if (i==0) {
// srand(123124);
// // random
// for (int x = 0; x < CELLS_X; x++) {
// for (int y = 0; y < CELLS_Y; y++) {
// for (int z = 0; z < CELLS_Z; z++) {
// fluid.add_source_at(z, y, x, 0, 1.F * ((rand() % RAND_MAX) / RAND_MAX - 0.5F));
// }
// }
// }
// }
// 0.1 < x < 0.3, 0.4 < y < 0.6, 0.4 < z < 0.6
// for (int x = 0.2F * CELLS_X; x <= 0.3F * CELLS_X; x++) {
// for (int y = 0.45F * CELLS_Y; y <= 0.55F * CELLS_Y; y++) {
// for (int z = 0.45F * CELLS_Z; z <= 0.55F * CELLS_Z; z++) {
// fluid.add_U_x_force_at(z - 5, y, x, FORCE_SCALE * (sinf(i / 80.0F) + 1));
// fluid.add_source_at(z - 5, y, x, 0, 0.1F);
// }
// }
// }
//
// // // 0.7 < x < 0.9, 0.4 < y < 0.6, 0.4 < z < 0.6
// for (int x = 0.7F * CELLS_X; x <= 0.8F * CELLS_X; x++) {
// for (int y = 0.45F * CELLS_Y; y <= 0.55F * CELLS_Y; y++) {
// for (int z = 0.45F * CELLS_Z; z <= 0.55F * CELLS_Z; z++) {
// fluid.add_U_x_force_at(z + 5, y, x, -FORCE_SCALE * (sinf(i / 80.0F) + 1));
// fluid.add_source_at(z + 5, y, x, 1, 0.1F);
// }
// }
// }
const int radius = 5;
const int duration = 1000;
// if (i == 0) {
// float x = 0;
// float y = 0;
// float z = 0;
//
// for (int a = -radius; a <= radius; a++) {
// for (int b = -radius; b <= radius; b++) {
// for (int c = -radius; c <= radius; c++) {
// if (a * a + b * b + c * c <= radius * radius) {
// fluid.add_source_at(
// z + c + CELLS_Z / 2,
// y + b + CELLS_Y / 10,
// x + a + CELLS_X / 2,
// 4,
// 10.f);
// }
// }
// }
// }
// }
if (i < duration) {
float x = 0;
float y = 0;
float z = 0;
for (int a = -radius; a <= radius; a++) {
for (int b = -radius; b <= radius; b++) {
for (int c = -radius; c <= radius; c++) {
if (a * a + b * b + c * c <= radius * radius) {
fluid.add_source_at(
z + c + CELLS_Z / 2,
y + b + CELLS_Y / 10,
x + a + CELLS_X / 2,
4,
60.f - fluid.S_at(z + c + CELLS_Z / 2, y + b + CELLS_Y / 10, x + a + CELLS_X / 2, 4));
}
}
}
}
}
}
FluidCUDA fluid;
bool simulateFlag = true;
void processInput(GLFWwindow *window);
int main() {
// 初始化和配置 GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 创建窗口
GLFWwindow *window = glfwCreateWindow(600, 600, "LearnOpenGL", NULL, NULL);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// 加载所有OpenGL函数指针
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// 编译着色器
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// 检查编译错误(省略)
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// 检查编译错误(省略)
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// 检查链接错误(省略)
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// 设置顶点数据和缓冲区,并配置顶点属性
float vertices[] = {
// 位置 // 纹理坐标
1.0f,
1.0f,
0.0f,
1.0f,
1.0f,
// 右上角
1.0f,
-1.0f,
0.0f,
1.0f,
0.0f,
// 右下角
-1.0f,
-1.0f,
0.0f,
0.0f,
0.0f,
// 左下角
-1.0f,
1.0f,
0.0f,
0.0f,
1.0f // 左上角
};
unsigned int indices[] = {
0,
1,
3,
// 第一个三角形
1,
2,
3 // 第二个三角形
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// 位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// 纹理坐标属性
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// 创建纹理
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// 设置纹理环绕和过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 假设图像大小和格式已知
int width, height;
width = 600;
height = 600;
fluid.init();
// 渲染循环
while (!glfwWindowShouldClose(window)) {
// 输入
processInput(window);
if (simulateFlag) {
add_force(fluid, i++);
fluid.step();
}
fluid.render();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, fluid.get_render_buffer());
// 渲染
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// 绑定纹理
glBindTexture(GL_TEXTURE_2D, texture);
// 绘制物体
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// 交换缓冲区和轮询IO事件
glfwSwapBuffers(window);
glfwPollEvents();
}
// 释放资源
glfwTerminate();
return 0;
}
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
fluid.rot_left(0.1);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
fluid.rot_left(-0.1);
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
fluid.rot_up(0.1);
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
fluid.rot_up(-0.1);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
fluid.zoom_in(0.1);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
fluid.zoom_in(-0.1);
}
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) {
fluid.cleanup();
fluid.init();
}
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
simulateFlag = false;
}
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
simulateFlag = true;
}
}