-
Notifications
You must be signed in to change notification settings - Fork 0
/
boids-3d.cpp
462 lines (431 loc) · 14.3 KB
/
boids-3d.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// boids-3d.cpp : Devon McKee
#define _USE_MATH_DEFINES
#include <glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <time.h>
#include <vector>
#include "VecMat.h"
#include "Camera.h"
#include "Mesh.h"
#include "Draw.h"
#include "Misc.h"
#include "GLXtras.h"
#include "dMesh.h"
#include "GeomUtils.h"
using std::vector;
GLuint program = 0;
GLuint texUnit = 0;
GLuint planeTexUnit, planeTexName;
GLuint cubeBuffer, planeBuffer;
GLuint cubeIBuffer, planeIBuffer;
GLuint cubeVAO, planeVAO;
vector<const char*> boidObjFilenames = { "./objects/fish/fish1.obj", "./objects/fish/fish2.obj", "./objects/fish/fish3.obj", "./objects/fish/fish4.obj" };
vector<const char*> boidTexFilenames = { "./textures/fish/fish1.png", "./textures/fish/fish2.png", "./textures/fish/fish3.png", "./textures/fish/fish4.png" };
vector<mat4> boidObjTransforms = { mat4(), mat4(), mat4(), mat4()};
vector<const char*> rockObjFilenames = { "./objects/rock/rock1.obj", "./objects/rock/rock2.obj", "./objects/rock/rock3.obj" };
vector<const char*> rockTexFilenames = { "./textures/rock/rock1.png", "./textures/rock/rock2.png", "./textures/rock/rock3.png" };
vector<const char*> rockNormFilenames = { "./textures/rock/rock1_normal.png", "./textures/rock/rock2_normal.png", "./textures/rock/rock3_normal.png" };
const char* sandTexFilename = "./textures/sand.png";
int win_width = 800, win_height = 800;
Camera camera((float) win_width / win_height, vec3(0, 0, 0), vec3(0, 0, -5));
vector<dMesh> boid_meshes;
vector<dMesh> rock_meshes;
vector<vec3> cube_points = { {-1, -1, 1}, {1, -1, 1}, {1, -1, -1}, {-1, -1, -1}, {-1, 1, -1}, {1, 1, -1}, {1, 1, 1}, {-1, 1, 1} }; vector<int4> cube_faces = { {0, 1, 2, 3}, {2, 3, 4, 5}, {4, 5, 6, 7}, {6, 7, 0, 1}, {0, 3, 4, 7}, {1, 2, 5, 6} };
vector<vec3> plane_points = { {-1, -1, -1}, {1, -1, -1}, {1, -1, 1}, {-1, -1, 1} }; vector<vec2> plane_uvs = { {0, 0}, {1, 0}, {1, 1}, {0, 1} }; vector<int3> plane_tris = { {0, 1, 2}, {2, 3, 0} };
vec3 lightSource = vec3(1, 1, 0);
vec3 XVec(vec3 v, mat4 m) { vec4 x = m * vec4(v); return .1f * vec3(x.x, x.y, x.z); }
const float BOID_SPEED = 0.005f;
const float BOID_SIZE = 0.06f;
const float BOID_SIZE_VARIANCE = 0.02f;
const float ROCK_SIZE = 0.15f;
const float ROCK_SIZE_VARIANCE = 0.075f;
const float BOID_PERCEPTION = 0.3f;
const float WALL_RANGE = 0.2f;
const float ALIGNMENT_WEIGHT = 1.0f;
const float COHESION_WEIGHT = 1.0f;
const float SEPARATION_WEIGHT = 1.0f;
const int STARTING_BOIDS = 150;
const int STARTING_ROCKS = 200;
const bool DRAW_REF_VECTORS = false;
bool running = true;
struct Boid;
struct Rock;
vector<Boid> flock;
vector<Rock> rocks;
struct Boid {
vec3 p, v; // position and velocity vector
vector<int> nb, _nb; // neighbors, bucket for different meshed neighbors
int mesh;
float size = BOID_SIZE + (rand_float(-1, 1) * BOID_SIZE_VARIANCE);
Boid(vec3 xyz, vec3 nv) {
p = xyz; v = nv; mesh = rand() % boidObjFilenames.size();
}
void FindNeighbors() {
for (size_t i = 0; i < flock.size(); i++) {
float d = dist(p, flock[i].p);
if (&flock[i] != this && d < BOID_PERCEPTION && d > 0) {
if (mesh == flock[i].mesh)
nb.push_back(i);
else
_nb.push_back(i);
}
}
}
vec3 Alignment() {
vec3 cv = vec3(0.0f);
int nc = 0;
for (size_t i = 0; i < nb.size(); i++) {
size_t n = nb[i];
cv += flock[n].v;
nc++;
}
if (nc > 0) {
cv /= (float)nc;
cv = normalize(cv);
return cv;
} else {
return vec3(0.0f);
}
}
vec3 Cohesion() {
vec3 cv = vec3(0.0f);
int nc = 0;
for (size_t i = 0; i < nb.size(); i++) {
size_t n = nb[i];
cv += flock[n].p;
nc++;
}
if (nc > 0) {
cv /= (float)nc;
cv -= p;
cv = normalize(cv);
return cv;
} else {
return vec3(0.0f);
}
}
vec3 Separation() {
// Include neighbors without same mesh
for (size_t i = 0; i < _nb.size(); i++)
nb.push_back(_nb[i]);
_nb.clear();
vec3 cv = vec3(0.0f);
float nc = 0;
for (size_t i = 0; i < nb.size(); i++) {
size_t n = nb[i];
vec3 iv = p - flock[n].p;
iv = normalize(iv);
iv /= dist(p, flock[n].p);
cv += iv;
nc++;
}
if (nc > 0) {
cv /= nc;
cv = normalize(cv);
return cv;
} else {
return vec3(0.0f);
}
}
vec3 Avoidance() {
p += v;
// Wrap boids around edges of screen
if (p.x > 1.0f)
p.x = -1.0f;
if (p.x < -1.0f)
p.x = 1.0f;
if (p.y > 1.0f)
p.y = -1.0f;
if (p.y < -1.0f)
p.y = 1.0f;
if (p.z > 1.0f)
p.z = -1.0f;
if (p.z < -1.0f)
p.z = 1.0f;
// Steer boids away from walls
/*
if (p.x < -1 + WALL_RANGE)
return vec3(1 / (1 - p.x), 0.0f, 0.0f); // left wall
if (p.x > 1 - WALL_RANGE)
return vec3(1 / (-1 - p.x), 0.0f, 0.0f); // right wall
*/
if (p.y > 1 - WALL_RANGE)
return vec3(0.0f, 1 / (-1 - p.y), 0.0f); // top wall
if (p.y < -1 + WALL_RANGE)
return vec3(0.0f, 1 / (1 - p.y), 0.0f); // bottom wall
/*
if (p.z < -1 + WALL_RANGE)
return vec3(0.0f, 0.0f, 1 / (1 - p.z)); // front wall
if (p.z > 1 - WALL_RANGE)
return vec3(0.0f, 0.0f, 1 / (-1 - p.z)); // back wall
*/
return vec3(0.0f);
}
void Run() {
FindNeighbors();
vec3 a_vec = Alignment() * ALIGNMENT_WEIGHT;
vec3 c_vec = Cohesion() * COHESION_WEIGHT;
vec3 s_vec = Separation() * SEPARATION_WEIGHT;
vec3 w_vec = Avoidance();
v += a_vec + c_vec + s_vec + w_vec;
v = normalize(v);
v *= BOID_SPEED;
// Clear neighbors
nb.clear();
}
void Draw() {
boid_meshes[mesh].PreDisplay();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
mat4 trans = Translate(p);
mat4 scale = Scale(size);
mat4 orient = Orientation(v, vec3(0.0f, 1.0f, 0.0f));
mat4 modelview = trans * scale * orient;
boid_meshes[mesh].Display(camera, &modelview);
}
};
struct Rock {
vec3 p, r; // pos, rotation
int mesh;
float size;
Rock(vec3 pos) {
p = pos;
mesh = rand() % rockObjFilenames.size();
size = ROCK_SIZE + (rand_float(-1, 1) * ROCK_SIZE_VARIANCE);
r = rand_vec3(0, 360);
}
void Draw() {
rock_meshes[mesh].PreDisplay();
mat4 trans = Translate(p);
mat4 scale = Scale(size);
mat4 rot = RotateX(r.x) * RotateY(r.y) * RotateZ(r.z);
mat4 modelview = trans * scale * rot;
rock_meshes[mesh].Display(camera, &modelview);
}
};
const char* vertShader = R"(
#version 410 core
in vec3 point;
in vec2 uv;
out vec2 vUv;
uniform mat4 modelview;
uniform mat4 persp;
void main() {
gl_Position = persp * modelview * vec4(point, 1);
vUv = uv;
}
)";
const char* fragShader = R"(
#version 410 core
in vec2 vUv;
out vec4 pColor;
uniform sampler2D textureUnit;
uniform int useTexture = 0;
void main() {
pColor = (useTexture == 1) ? texture(textureUnit, vUv) : vec4(1);
}
)";
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);
}
if (key == GLFW_KEY_J && action == GLFW_PRESS)
running = !running;
}
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 InitBuffers() {
// Set up cube VAO/buffer
glGenVertexArrays(1, &cubeVAO);
glBindVertexArray(cubeVAO);
glGenBuffers(1, &cubeBuffer);
glBindBuffer(GL_ARRAY_BUFFER, cubeBuffer);
glBufferData(GL_ARRAY_BUFFER, cube_points.size() * sizeof(vec3), cube_points.data(), GL_STATIC_DRAW);
glGenBuffers(1, &cubeIBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_faces.size() * sizeof(int4), cube_faces.data(), GL_STATIC_DRAW);
VertexAttribPointer(program, "point", 3, 0, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Set up plane VAO/buffer
glGenVertexArrays(1, &planeVAO);
glBindVertexArray(planeVAO);
glGenBuffers(1, &planeBuffer);
glBindBuffer(GL_ARRAY_BUFFER, planeBuffer);
size_t vBufSize = (plane_points.size() * sizeof(vec3)) + (plane_uvs.size() * sizeof(vec2));
glBufferData(GL_ARRAY_BUFFER, vBufSize, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, plane_points.size()*sizeof(vec3), plane_points.data());
glBufferSubData(GL_ARRAY_BUFFER, plane_points.size()*sizeof(vec3), plane_uvs.size()*sizeof(vec2), plane_uvs.data());
glGenBuffers(1, &planeIBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planeIBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane_tris.size()*sizeof(int3), plane_tris.data(), GL_STATIC_DRAW);
glActiveTexture(GL_TEXTURE0 + planeTexUnit);
glBindTexture(GL_TEXTURE_2D, planeTexName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
VertexAttribPointer(program, "point", 3, 0, 0);
VertexAttribPointer(program, "uv", 2, 0, (GLvoid*)(plane_points.size()*sizeof(vec3)));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void InitSceneObjects() {
for (int i = 0; i < STARTING_BOIDS; i++) {
vec3 p = rand_vec3();
vec3 v = rand_vec3();
v *= BOID_SPEED;
Boid b = Boid(p, v);
flock.push_back(b);
}
for (int i = 0; i < STARTING_ROCKS; i++) {
vec3 p = vec3(rand_float(-1, 1), -1.0f, rand_float(-1, 1));
Rock r = Rock(p);
rocks.push_back(r);
}
}
void DrawVectors() {
UseDrawShader(camera.modelview);
for (size_t i = 0; i < flock.size(); i++) {
mat4 rot = Orientation(flock[i].v, vec3(0.0f, 1.0f, 0.0f));
vec3 xaxis = XVec(vec3(1, 0, 0), rot), yaxis = XVec(vec3(0, 1, 0), rot), zaxis = XVec(vec3(0, 0, 1), rot);
ArrowV(flock[i].p, xaxis, camera.modelview, camera.persp, vec3(1, 0, 0));
ArrowV(flock[i].p, yaxis, camera.modelview, camera.persp, vec3(0, 1, 0));
ArrowV(flock[i].p, zaxis, camera.modelview, camera.persp, vec3(0, 0, 1));
}
}
void Display() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glClearColor(0.3f, 0.3f, 0.4f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// Draw cube
glBindVertexArray(cubeVAO);
glUseProgram(program);
SetUniform(program, "useTexture", 0);
SetUniform(program, "persp", camera.persp);
SetUniform(program, "modelview", camera.modelview);
for (int i = 0; i < 6; i++)
glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_INT, (GLvoid*)(i * sizeof(int4)));
glBindVertexArray(0);
PrintGLErrors("Cube");
// Draw floor
glUseProgram(program);
glBindVertexArray(planeVAO);
SetUniform(program, "useTexture", 1);
SetUniform(program, "persp", camera.persp);
SetUniform(program, "modelview", camera.modelview);
SetUniform(program, "textureUnit", (int)planeTexUnit);
glDrawElements(GL_TRIANGLES, plane_points.size() * sizeof(vec3), GL_UNSIGNED_INT, 0);
// Draw other side of floor
mat4 modelview = camera.modelview * Translate(0, -1, 0) * RotateZ(180.0f) * Translate(0, 1, 0);
SetUniform(program, "modelview", modelview);
glDrawElements(GL_TRIANGLES, plane_points.size() * sizeof(vec3), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
PrintGLErrors("Floor");
// Draw boids
for (size_t i = 0; i < flock.size(); i++) {
if (running)
flock[i].Run();
flock[i].Draw();
}
PrintGLErrors("Boids");
// Draw rocks
for (size_t i = 0; i < rocks.size(); i++) {
rocks[i].Draw();
}
PrintGLErrors("Rocks");
// Draw reference vectors
if (DRAW_REF_VECTORS) DrawVectors();
glFlush();
}
int main() {
srand((int)time(NULL));
if (!glfwInit())
return 1;
glfwWindowHint(GLFW_SAMPLES, 4);
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(win_width, win_height, "Boids 3D", NULL, NULL);
if (!window) {
printf("Failed to create GLFW window!\n");
glfwTerminate();
return 1;
}
glfwSetWindowPos(window, 100, 100);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
PrintGLErrors();
if (!(program = LinkProgramViaCode(&vertShader, &fragShader)))
return 1;
planeTexUnit = ++texUnit;
planeTexName = LoadTexture(sandTexFilename, planeTexUnit);
for (size_t i = 0; i < boidObjFilenames.size(); i++) {
boid_meshes.push_back(dMesh());
texUnit++;
boid_meshes[i].Read((char*)boidObjFilenames[i], (char*)boidTexFilenames[i], texUnit, &boidObjTransforms[i]);
printf("'%s' : %i vertices, %i normals, %i uvs, %i triangles\n", boidObjFilenames[i], (int)boid_meshes[i].points.size(), (int)boid_meshes[i].normals.size(), (int)boid_meshes[i].uvs.size(), (int)boid_meshes[i].triangles.size());
}
for (size_t i = 0; i < rockObjFilenames.size(); i++) {
rock_meshes.push_back(dMesh());
texUnit++;
printf(" Creating and pushing back dMesh...\n");
rock_meshes[i].Read((char*)rockObjFilenames[i], (char*)rockTexFilenames[i], texUnit);
printf("'%s' : %i vertices, %i normals, %i uvs, %i triangles\n", rockObjFilenames[i], (int)rock_meshes[i].points.size(), (int)rock_meshes[i].normals.size(), (int)rock_meshes[i].uvs.size(), (int)rock_meshes[i].triangles.size());
}
InitSceneObjects();
InitBuffers();
glfwSetCursorPosCallback(window, MouseMove);
glfwSetMouseButtonCallback(window, MouseButton);
glfwSetScrollCallback(window, MouseWheel);
glfwSetKeyCallback(window, Keyboard);
glfwSetWindowSizeCallback(window, Resize);
glfwSwapInterval(1);
printf("GL vendor: %s\n", glGetString(GL_VENDOR));
printf("GL renderer: %s\n", glGetString(GL_RENDERER));
printf("GL version: %s\n", glGetString(GL_VERSION));
printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
while (!glfwWindowShouldClose(window)) {
Display();
glfwPollEvents();
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &cubeVAO);
glDeleteBuffers(1, &cubeBuffer);
glDeleteBuffers(1, &cubeIBuffer);
glDeleteVertexArrays(1, &planeVAO);
glDeleteBuffers(1, &planeBuffer);
glDeleteBuffers(1, &planeIBuffer);
glfwDestroyWindow(window);
glfwTerminate();
}