-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
371 lines (294 loc) · 11 KB
/
main.cpp
File metadata and controls
371 lines (294 loc) · 11 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <iostream>
#include <fstream>
#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/constants.hpp>
#include "tga.h"
#include "ShaderProgram.h"
#include <vector>
#include "BufferObject.h"
void initPoints(int);
void drawPoints(int);
bool init();
void display();
void resize(int, int);
void idle();
void keyboard(unsigned char, int, int);
void specialKeyboard(int, int, int);
void mouse(int, int, int, int);
void mouseMotion(int, int);
bool fullscreen = false;
bool mouseDown = false;
bool animation = false;
int lighting = 0;
enum {PBF_FIRST, PBF_UPDATEGRID, PBF_UPDATENEIGHBORS, PBF_CALCULATELAMBDA, PBF_CALCULATEDELTA, PBF_UPDATE, PBF_APPLYVISOSITY, PBF_APPLYVORTICITY, PBF_DEBUG, COMPUTE_SHADERS};
char* computeFilename[] = {"shaders/compute/pbf_first.glsl",
"shaders/compute/pbf_updateGrid.glsl",
"shaders/compute/pbf_updateNeighbors.glsl",
"shaders/compute/pbf_calculateLambda.glsl",
"shaders/compute/pbf_calculateDelta.glsl",
"shaders/compute/pbf_update.glsl",
"shaders/compute/pbf_applyViscosity.glsl",
"shaders/compute/pbf_applyVorticity.glsl",
"shaders/compute/pbf_debug.glsl",
"shaders/compute/pbf_debug.glsl"};
enum {
POS_SSBO, VEL_SSBO, COL_SSBO, POSPRED_SSBO, VORTICITY_SSBO, LAMBDA_SSBO, CELL_SSBO, GRIDCELLS_SSBO, NEIGHBORS_SSBO, SSBO_NUMBER
};
float xrot = 0.0f;
float yrot = 0.0f;
float xdiff = 0.0f;
float ydiff = 0.0f;
int g_Width = 1280; // Ancho inicial de la ventana
int g_Height = 720; // Altura incial de la ventana
GLuint cubeVAOHandle, pointsVAOHandle;
ShaderProgram* graphicProgram;
ShaderProgram* computeProgram[COMPUTE_SHADERS];
BufferObject<GL_SHADER_STORAGE_BUFFER>* ssbo[SSBO_NUMBER];
GLuint locUniformSpriteTex;
const unsigned ITERATIONS = 4;
float dt = 0.01;
const float density_rest = 998.29f;
const int ppedge = 30;
const float kernel_part = 20;
const float UMBRAL = 1e-5f;
const float CRF = 0;
const float side = 4;
const float volume = side*side*side;
const float inc = side / ppedge;
const int numPart = ppedge*ppedge*ppedge;
const float mass = density_rest * volume / numPart;
// Radio kernel
const int WORK_GROUP_SIZE = 100;
const int NUM_PARTICLES = ppedge*ppedge*ppedge;
const float h = pow( (3*volume*kernel_part) / ( 4 * glm::pi<float>() * NUM_PARTICLES ) , 1.0f/3);
const float h2 = h * h;
const float h3 = h2 * h;
const float h6 = h2 * h2 * h2;
const float h9 = h3 * h3 * h3;
// Recipiente
const float LIM_X_INF = 0;
const float LIM_X_SUP = 6;
const float LIM_Y_INF = 0;
const float LIM_Y_SUP = 6;
const float LIM_Z_INF = 0;
const float LIM_Z_SUP = 6;
// Cantidad de celdas
const unsigned CELL_WIDTH = (unsigned)( (LIM_X_SUP - LIM_X_INF) / h ) + 1;
const unsigned CELL_HEIGHT = (unsigned)( (LIM_Y_SUP - LIM_Y_INF) / h ) + 1;
const unsigned CELL_LARGE = (unsigned)( (LIM_Z_SUP - LIM_Z_INF) / h ) + 1;
const unsigned CELL_NUMBER = CELL_WIDTH * CELL_HEIGHT * CELL_LARGE;
const int MAX_PART_PER_CELL = 20; // Máximo número de partículas por celda
const int CELL_NEIGHBORS = 27; // Celdas vecinas (incluyendo propia)
const int MAX_PART_NEIGHBORS = CELL_NEIGHBORS*MAX_PART_PER_CELL; // Máximo número de vecinos por celda
// BEGIN: Inicializa primitivas ////////////////////////////////////////////////////////////////////////////////////
// TODO: no generar los buffers cada vez que se llama a la función
void initPoints() {
std::vector<glm::vec4> position(NUM_PARTICLES);
std::vector<glm::vec4> colors(NUM_PARTICLES);
std::vector<glm::vec4> velocs(NUM_PARTICLES);
float ox = (LIM_X_SUP - LIM_X_INF - side) / 2.0f;
float oy = (LIM_Y_SUP - LIM_Y_INF - side) / 2.0f;
float oz = (LIM_Z_SUP - LIM_Z_INF - side) / 2.0f;
int n = 0;
for(int i = 0; i < ppedge; i++)
for(int j = 0; j < ppedge; j++)
for(int k = 0; k < ppedge; k++) {
position[n].x = ox + i*inc;
position[n].y = oy + j*inc;
position[n].z = oz + k*inc;
colors[n].r = i / (GLfloat)ppedge;
colors[n].g = j / (GLfloat)ppedge;
colors[n].b = k / (GLfloat)ppedge;
colors[n].a = 1.0f;
velocs[n].x = 0;
velocs[n].y = 0;
velocs[n].z = 0;
n++;
}
// Transferir/reservar memoria
ssbo[POS_SSBO] ->transfer(position, GL_STATIC_DRAW);
ssbo[VEL_SSBO] ->transfer(velocs, GL_STATIC_DRAW);
ssbo[COL_SSBO] ->transfer(colors, GL_STATIC_DRAW);
ssbo[POSPRED_SSBO] ->allocate(NUM_PARTICLES * sizeof(GLfloat)*4, GL_STATIC_DRAW);
ssbo[VORTICITY_SSBO]->allocate(NUM_PARTICLES * sizeof(GLfloat)*4, GL_STATIC_DRAW);
ssbo[LAMBDA_SSBO] ->allocate(NUM_PARTICLES * sizeof(GLfloat)*4, GL_STATIC_DRAW);
ssbo[CELL_SSBO] ->allocate(NUM_PARTICLES * sizeof(unsigned), GL_STATIC_DRAW);
ssbo[NEIGHBORS_SSBO]->allocate((MAX_PART_NEIGHBORS+1) * NUM_PARTICLES * sizeof(unsigned), GL_STATIC_DRAW);
ssbo[GRIDCELLS_SSBO]->allocate(CELL_NUMBER * (MAX_PART_PER_CELL + 1) * sizeof(unsigned), GL_STATIC_DRAW );
glGenVertexArrays (1, &pointsVAOHandle);
glBindVertexArray (pointsVAOHandle);
graphicProgram->vertexAttribPointer(ssbo[POS_SSBO], "aPosition", 4, GL_FLOAT, GL_FALSE, 0, (char *)NULL + 0);
graphicProgram->vertexAttribPointer(ssbo[COL_SSBO], "aColor", 4, GL_FLOAT, GL_FALSE, 0, (char *)NULL + 0);
glBindVertexArray (0);
}
// END: Inicializa primitivas ////////////////////////////////////////////////////////////////////////////////////
// BEGIN: Funciones de dibujo ////////////////////////////////////////////////////////////////////////////////////
void drawPoints(int numPoints) {
glBindVertexArray(pointsVAOHandle);
glDrawArrays(GL_POINTS, 0, numPoints);
glBindVertexArray(0);
}
// END: Funciones de dibujo ////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowPosition(240, 480);
glutInitWindowSize(g_Width, g_Height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Position Based Fluids");
GLenum err = glewInit();
if (GLEW_OK != err) {
/* Problem: glewInit failed, something is seriously wrong. */
std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
system("pause");
exit(-1);
}
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
glutMouseFunc(mouse);
glutMotionFunc(mouseMotion);
glutReshapeFunc(resize);
glutIdleFunc(idle);
glutMainLoop();
return EXIT_SUCCESS;
}
bool init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Compute shader programs
for(int i = 0; i < COMPUTE_SHADERS; i++) {
computeProgram[i] = new ShaderProgram();
computeProgram[i]->addShader(GL_COMPUTE_SHADER, computeFilename[i]);
computeProgram[i]->compile();
computeProgram[i]->setDispatch(NUM_PARTICLES / WORK_GROUP_SIZE, 1, 1);
}
// Graphic shaders program
graphicProgram = new ShaderProgram();
graphicProgram->addShader(GL_VERTEX_SHADER, "shaders/pbf.vert");
graphicProgram->addShader(GL_FRAGMENT_SHADER, "shaders/pbf.frag");
graphicProgram->addShader(GL_GEOMETRY_SHADER, "shaders/oriented_sprite.geom");
graphicProgram->compile();
// Sprite points
TGAFILE tgaImage;
GLuint textId;
glGenTextures (1, &textId);
if ( LoadTGAFile("textures/white_sphere.tga", &tgaImage) ) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tgaImage.imageWidth, tgaImage.imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, tgaImage.imageData);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
// Crear buffers
for(int i = 0; i < SSBO_NUMBER; i++) {
ssbo[i] = new BufferObject<GL_SHADER_STORAGE_BUFFER>();
}
initPoints();
locUniformSpriteTex = glGetUniformLocation(graphicProgram->id(), "uSpriteTex");
return true;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 Projection = glm::perspective(45.0f, 1.0f * g_Width / g_Height, 1.0f, 100.0f);
glm::vec3 cameraPos = glm::vec3(15) * glm::vec3( cos( yrot / 100 ), sin(xrot / 100), sin( yrot / 100 ) * cos(xrot /100)) + glm::vec3(6.0f);
glm::mat4 View = glm::lookAt(cameraPos, glm::vec3(6.0f, 6.0f, 6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 ModelCube = glm::translate(glm::scale(glm::mat4(1.0f), glm::vec3(2.0f, 2.0f, 2.0f)), glm::vec3(0.0f, 0.0f, 0.0f));
glm::mat4 mvp; // Model-view-projection matrix
glm::mat4 mv; // Model-view matrix
if (animation) {
ShaderProgram::useAtomic(computeProgram[PBF_FIRST]);
ShaderProgram::useAtomic(computeProgram[PBF_UPDATEGRID]);
ShaderProgram::useAtomic(computeProgram[PBF_UPDATENEIGHBORS]);
for(int i = 0; i < ITERATIONS; i++) {
ShaderProgram::useAtomic(computeProgram[PBF_CALCULATELAMBDA]);
ShaderProgram::useAtomic(computeProgram[PBF_CALCULATEDELTA]);
}
ShaderProgram::useAtomic(computeProgram[PBF_UPDATE]);
ShaderProgram::useAtomic(computeProgram[PBF_APPLYVISOSITY]);
ShaderProgram::useAtomic(computeProgram[PBF_APPLYVORTICITY]);
ShaderProgram::useAtomic(computeProgram[PBF_DEBUG]);
}
ShaderProgram::use(graphicProgram);
// Dibuja Puntos
mvp = Projection * View * ModelCube;
mv = View * ModelCube;
graphicProgram->uniformMatrix4fv( "uModelViewProjMatrix", 1, GL_FALSE, &mvp[0][0] );
graphicProgram->uniformMatrix4fv( "uModelViewMatrix", 1, GL_FALSE, &mv[0][0] );
graphicProgram->uniformMatrix4fv( "uProjectionMatrix", 1, GL_FALSE, &Projection[0][0] );
glUniform1i(locUniformSpriteTex, 0);
drawPoints(NUM_PARTICLES);
glUseProgram(0);
glutSwapBuffers();
}
void resize(int w, int h)
{
g_Width = w;
g_Height = h;
glViewport(0, 0, g_Width, g_Height);
}
void idle()
{
/*if (!mouseDown && animation)
{
xrot += 0.3f;
yrot += 0.4f;
}*/
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
static bool wireframe = false;
switch(key)
{
case 27 : case 'q': case 'Q':
exit(1);
break;
case 'a': case 'A':
animation = !animation;
std::cout << (animation ? "Animación activada" : "Animación desactivada");
break;
case ' ':
initPoints();
}
}
void specialKeyboard(int key, int x, int y) {
if (key == GLUT_KEY_F4) {
fullscreen = !fullscreen;
if (fullscreen)
glutFullScreen();
else {
glutReshapeWindow(g_Width, g_Height);
glutPositionWindow(50, 50);
}
}
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseDown = true;
xdiff = x - yrot;
ydiff = -y + xrot;
}
else
mouseDown = false;
}
void mouseMotion(int x, int y)
{
if (mouseDown)
{
yrot = x - xdiff;
xrot = y + ydiff;
glutPostRedisplay();
}
}