-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
445 lines (364 loc) · 12.1 KB
/
main.cpp
File metadata and controls
445 lines (364 loc) · 12.1 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "main.h"
#include "kernel.h"
/*****************************************************************
*
* Main
*
****************************************************************/
__host__
int main(int argc, char* argv[]) {
// Parse command line parameters
for (int i = 1; i < argc; i++) {
#define check_index(i,str) \
if ((i) >= argc) \
{ fprintf(stderr,"Missing 2nd argument for %s\n", str); return 1; }
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
help();
return 1;
}
else if (strcmp(argv[i], "--nboids") == 0 || strcmp(argv[i], "-n") == 0) {
check_index(i + 1, "--nboids|-n");
i++;
if (isdigit(*argv[i]))
nBoids = atoi(argv[i]);
}
else if (strcmp(argv[i], "--mouse") == 0 || strcmp(argv[i], "-m") == 0) {
followMouse = true;
} else if (strcmp(argv[i], "--naive") == 0) {
naive = true;
} else {
fprintf(stderr, "Unknown option %s\n", argv[i]);
help();
return 1;
}
}
//nBoids = 512;
//followMouse = false;
//naive = false;
// OpenGL / GLUT Initialization
Init(argc, argv);
cudaGLSetGLDevice(0);
// Register VBO
cudaGLRegisterBufferObject(positionVBO);
cudaGLRegisterBufferObject(velocityVBO);
cudaGLRegisterBufferObject(accelerationVBO);
// CUDA Init
initCuda(nBoids);
// Perspective
projection = glm::perspective(
fovy, // Field of View
float(window_width) / float(window_height), // Aspect Ratio
zNear, // Near Plane
zFar // Far Plane
);
view = glm::lookAt(cameraPosition, glm::vec3(0.0, 0.0, 0), glm::vec3(0, 1, 0));
projection = projection * view;
initShaders(program);
printControls();
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(windowResize);
glutDisplayFunc(Render);
glutKeyboardFunc(Keyboard);
glutPassiveMotionFunc(mouseMotion);
// Start GLUT Loop
glutMainLoop();
return 0;
}
/*****************************************************************
*
* OpenGL Init
*
****************************************************************/
__host__
void printDeviceProps() {
{
cudaDeviceProp props;
cudaGetDeviceProperties(&props, 0);
fprintf(stderr, " name: %s\n", props.name);
fprintf(stderr, " major.minor: %d.%d\n", props.major, props.minor);
fprintf(stderr, " totalGlobalMem: %lu (MB)\n", props.totalGlobalMem / (1024 * 1024));
fprintf(stderr, " sharedMemPerBlock: %lu (KB)\n", props.sharedMemPerBlock / 1024);
fprintf(stderr, " sharedMemPerMultiprocessor: %lu (KB)\n", props.sharedMemPerMultiprocessor / 1024);
fprintf(stderr, " regsPerBlock: %d\n", props.regsPerBlock);
fprintf(stderr, " warpSize: %d\n", props.warpSize);
fprintf(stderr, " multiProcessorCount: %d\n", props.multiProcessorCount);
fprintf(stderr, " maxThreadsPerBlock: %d\n\n", props.maxThreadsPerBlock);
}
}
__host__
void printControls() {
fprintf(stderr, " **************** Controls ****************\n");
fprintf(stderr, " W: Increase Separation Radius\n");
fprintf(stderr, " T: Increase Alignment Radius\n");
fprintf(stderr, " I: Increase Cohesion Radius\n\n");
fprintf(stderr, " S: Decrease Separation Radius\n");
fprintf(stderr, " G: Decrease Alignment Radius\n");
fprintf(stderr, " K: Decrease Cohesion Radius\n\n");
fprintf(stderr, " A: Increase Separation Weight\n");
fprintf(stderr, " F: Increase Alignment Weight\n");
fprintf(stderr, " J: Increase Cohesion Weight\n\n");
fprintf(stderr, " D: Decrease Separation Weight\n");
fprintf(stderr, " J: Decrease Alignment Weight\n");
fprintf(stderr, " L: Decrease Cohesion Weight\n\n");
fprintf(stderr, " R: Reset Parameter Values to Default\n");
fprintf(stderr, " *****************************************\n\n");
}
__host__
void Init(int argc, char* argv[]) {
fprintf(stdout, " Initalizing application.\n\n");
// Print out GPU Details
printDeviceProps();
// Create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(0, 30);
glutCreateWindow("cuda-boids");
// Init GLFW
if (!glfwInit()) {
std::cout << "glfw init failed" << std::endl;
}
// Init GLEW
glewInit();
GLenum err = glewInit();
if (GLEW_OK != err) {
// Problem: glewInit failed, something is seriously wrong.
std::cout << "glewInit failed, aborting." << std::endl;
exit(1);
}
fprintf(stdout, " Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
timeSinceLastFrame = glutGet(GLUT_ELAPSED_TIME);
// For mouse location
seekTarget = make_float3(0.0, 0.0, 0.0);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
initVAO();
}
void initShaders(GLuint* program) {
GLint location;
program[1] = glslUtility::createProgram(
"shaders/particleVS.glsl",
"shaders/particleGS.glsl",
"shaders/particleFS.glsl",
attributeLocations,
1
);
glUseProgram(program[1]);
if ((location = glGetUniformLocation(program[1], "u_projMatrix")) != -1)
{
glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
}
if ((location = glGetUniformLocation(program[1], "u_cameraPos")) != -1)
{
glUniform3fv(location, 1, &cameraPosition[0]);
}
}
__host__
void initVAO(void) {
fprintf(stdout, " Creating Vertex Array Objects.\n");
GLfloat* bodies = new GLfloat[4 * (nBoids)];
GLfloat* velocities = new GLfloat[3 * (nBoids)];
GLfloat* accelerations = new GLfloat[3 * (nBoids)];
GLuint* bindices = new GLuint[nBoids];
// Initializing all positions and vel's at 0
for (int i = 0; i < nBoids; i++) {
bodies[4 * i + 0] = 0.0f;
bodies[4 * i + 1] = 0.0f;
bodies[4 * i + 2] = 0.0f;
bodies[4 * i + 3] = 1.0f;
velocities[3 * i + 0] = 0.0f;
velocities[3 * i + 1] = 0.0f;
velocities[3 * i + 2] = 0.0f;
accelerations[3 * i + 0] = 0.0f;
accelerations[3 * i + 1] = 0.0f;
accelerations[3 * i + 2] = 0.0f;
bindices[i] = i;
}
// Generate buffers for VBO
glGenBuffers(1, &positionVBO);
glGenBuffers(1, &velocityVBO);
glGenBuffers(1, &accelerationVBO);
glGenBuffers(1, &IBO);
fprintf(stdout, " Binding VBO to GL buffers.\n");
// Assign VBO to buffer
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
// Add bodies and size to buffer
glBufferData(GL_ARRAY_BUFFER, 4 * (nBoids) * sizeof(GLfloat), bodies, GL_DYNAMIC_DRAW);
// Same for vel
glBindBuffer(GL_ARRAY_BUFFER, velocityVBO);
glBufferData(GL_ARRAY_BUFFER, 3 * (nBoids) * sizeof(GLfloat), velocities, GL_DYNAMIC_DRAW);
// Same for acc
glBindBuffer(GL_ARRAY_BUFFER, accelerationVBO);
glBufferData(GL_ARRAY_BUFFER, 3 * (nBoids) * sizeof(GLfloat), accelerations, GL_DYNAMIC_DRAW);
// Same for IBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (nBoids) * sizeof(GLuint), bindices, GL_STATIC_DRAW);
// Unbind buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// No need for these anymore since in buffers
delete[] bodies;
delete[] bindices;
delete[] velocities;
delete[] accelerations;
}
__host__
void help() {
fprintf(stderr, "./boids --help|-h --nboids|-n --mouse|-m --naive \n");
}
/*****************************************************************
*
* CUDA Wrapper
*
****************************************************************/
// Runs all CUDA related functions
__host__
void runCUDA(bool followMouse, float sep_dist, float sep_weight, float ali_dist, float ali_weight, float coh_dist, float coh_weight) {
float* dptrvert = NULL;
float* velptr = NULL;
float* accptr = NULL;
// Map BO to array
cudaGLMapBufferObject((void**)&dptrvert, positionVBO);
cudaGLMapBufferObject((void**)&velptr, velocityVBO);
cudaGLMapBufferObject((void**)&accptr, accelerationVBO);
// Call wrappers for kernels
flock(nBoids, window_width, window_height, seekTarget, followMouse, naive, sep_dist, sep_weight, ali_dist, ali_weight, coh_dist, coh_weight);
cudaUpdateVBO(nBoids, dptrvert, velptr, accptr);
// Unmap buffer object
cudaGLUnmapBufferObject(positionVBO);
cudaGLUnmapBufferObject(velocityVBO);
cudaGLUnmapBufferObject(accelerationVBO);
}
/*****************************************************************
*
* GLUT Functions
*
****************************************************************/
__host__
void mouseMotion(int x, int y) {
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
viewPhi += 0.005f*dx;
viewTheta += 0.005f*dy;
seekTarget.x = 400.0f*sin(viewTheta)*sin(viewPhi);
seekTarget.y = 400.0f*cos(viewTheta);
seekTarget.z = 400.0f*sin(viewTheta)*cos(viewPhi);
mouse_old_x = x;
mouse_old_y = y;
}
__host__
void windowResize(int height, int width) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
__host__
void Keyboard(unsigned char key, int x, int y) {
if (key == 27) { // Escape
fprintf(stdout, " Exiting application.\n");
exit(1);
}
else if (key == 'w') { // sep up
if (sep_dist <= 0) sep_dist = 0;
else sep_dist += 10;
}
else if (key == 'a') { // sep weight down
if (sep_weight <= 0) sep_weight = 0;
else sep_weight -= 0.1;
}
else if (key == 's') { // sep down
if (sep_dist <= 0) sep_dist = 0;
else sep_dist -= 10;
}
else if (key == 'd') { // sep weight up
if (sep_weight <= 0) sep_weight = 0;
else sep_weight += 0.1;
}
else if (key == 't') { // ali up
if (ali_dist <= 0) ali_dist = 0;
else ali_dist += 10;
}
else if (key == 'f') { // ali weight down
if (ali_weight <= 0) ali_weight = 0;
else ali_weight -= 0.1;
}
else if (key == 'g') { // ali down
if (ali_dist <= 0) ali_dist = 0;
else ali_dist -= 10;
}
else if (key == 'h') { // ali weight up
if (ali_weight <= 0) ali_weight = 0;
else ali_weight += 0.1;
}
else if (key == 'i') { // coh up
if (coh_dist <= 0) coh_dist = 0;
else coh_dist += 10;
}
else if (key == 'j') { // coh weight down
if (coh_weight <= 0) coh_weight = 0;
else coh_weight -= 0.1;
}
else if (key == 'k') { // coh down
if (coh_dist <= 0) coh_dist = 0;
else coh_dist -= 10;
}
else if (key == 'l') { // coh weight up
if (coh_weight <= 0) coh_weight = 0;
else coh_weight += 0.1;
}
else if (key == 'r') { // reset values
sep_dist = 100;
ali_dist = 400;
coh_dist = 300;
sep_weight = 1.0f;
ali_weight = 1.5f;
coh_weight = 1.0f;
}
// Request display update
glutPostRedisplay();
}
__host__
void Render() {
static float fps = 0;
frame++;
int time = glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 1000) {
fps = frame*1000.0f / (time - timebase);
timebase = time;
frame = 0;
}
float executionTime = glutGet(GLUT_ELAPSED_TIME) - timeSinceLastFrame;
timeSinceLastFrame = glutGet(GLUT_ELAPSED_TIME);
// Launch Kernel
runCUDA(followMouse, sep_dist, sep_weight, ali_dist, ali_weight, coh_dist, coh_weight);
char title[200];
if (followMouse) {
// sep_dist not taken into consideration when mouse is target
sprintf(title, "[%d boids] [%0.2f fps] [%0.2f, %0.2f, %0.2f mouse] dist: (%0.2f ali, %0.2f coh) weight: (%0.2f sep, %0.2f ali, %0.2f coh)",
nBoids, fps, seekTarget.x, seekTarget.y, seekTarget.z, ali_dist, coh_dist, sep_weight, ali_weight, coh_weight);
} else {
sprintf(title, "[%d boids] [%0.2f fps] dist: (%0.2f sep, %0.2f ali, %0.2f coh) weight: (%0.2f sep, %0.2f ali, %0.2f coh)",
nBoids, fps, sep_dist, ali_dist, coh_dist, sep_weight, ali_weight, coh_weight);
}
glutSetWindowTitle(title);
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program[PASS_THROUGH]);
// Render from VBO
glEnableVertexAttribArray(positionLocation);
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(velocityLocation);
glBindBuffer(GL_ARRAY_BUFFER, velocityVBO);
glVertexAttribPointer((GLuint)velocityLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(accelerationLocation);
glBindBuffer(GL_ARRAY_BUFFER, accelerationVBO);
glVertexAttribPointer((GLuint)accelerationLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glPointSize(4.0f);
glDrawElements(GL_POINTS, nBoids, GL_UNSIGNED_INT, 0); // where draw happens
glDisableVertexAttribArray(positionLocation);
// Switch Buffers to show rendered
glutPostRedisplay();
glutSwapBuffers();
}