-
Notifications
You must be signed in to change notification settings - Fork 0
/
boids.cpp
233 lines (215 loc) · 5.74 KB
/
boids.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
// boids.cpp : Devon McKee
#define _USE_MATH_DEFINES
#include <glad.h>
#include <GLFW/glfw3.h>
#include "GLXtras.h"
#include <time.h>
#include <vector>
#include "VecMat.h"
GLuint vBuffer = 0;
GLuint program = 0;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 800;
const float BOID_SIZE = 40.0f;
const float BOID_SPEED = 2.0f;
const float BOID_PERCEPTION = 75.0f;
const float ALIGNMENT_WEIGHT = 1.0f;
const float COHESION_WEIGHT = 1.0f;
const float SEPARATION_WEIGHT = 1.5f;
int num_boids = 10;
float rand_float(float min = 0, float max = 1) { return min + (float)rand() / (RAND_MAX/(max-min)); }
struct Boid {
vec2 p, r;
vec3 col;
Boid(vec2 xy, vec2 nr) {
p = xy, r = nr;
col = vec3(rand_float(0.25f, 1.0f), rand_float(0.25f, 1.0f), rand_float(0.25f, 1.0f));
}
float dist(vec2 p1, vec2 p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}
vec2 Alignment(std::vector<Boid>* flock) {
vec2 cv = vec2(0.0f, 0.0f);
int nc = 0;
for (int i = 0; i < flock->size(); i++) {
if (&(*flock)[i] != this && dist(p, (*flock)[i].p) < BOID_PERCEPTION) {
cv += (*flock)[i].r;
nc++;
}
}
if (nc > 0) {
cv /= (float)nc;
cv = normalize(cv);
return cv;
} else {
return vec2(0.0f, 0.0f);
}
}
vec2 Cohesion(std::vector<Boid>* flock) {
vec2 cv = vec2(0.0f, 0.0f);
int nc = 0;
for (int i = 0; i < flock->size(); i++) {
if (&(*flock)[i] != this && dist(p, (*flock)[i].p) < BOID_PERCEPTION) {
cv += (*flock)[i].p;
nc++;
}
}
if (nc > 0) {
cv /= (float)nc;
cv -= p;
cv = normalize(cv);
return cv;
} else {
return vec2(0.0f, 0.0f);
}
}
vec2 Separation(std::vector<Boid>* flock) {
vec2 cv = vec2(0.0f, 0.0f);
int nc = 0;
for (int i = 0; i < flock->size(); i++) {
if (&(*flock)[i] != this &&
dist(p, (*flock)[i].p) < BOID_PERCEPTION &&
dist(p, (*flock)[i].p) > 0) {
vec2 iv = p - (*flock)[i].p;
iv = normalize(iv);
iv /= dist(p, (*flock)[i].p);
cv += iv;
nc++;
}
}
if (nc > 0) {
cv /= nc;
cv = normalize(cv);
return cv;
} else {
return vec2(0.0f, 0.0f);
}
}
void Move() {
// Get movement vector pointing upward
vec4 m_vec = vec4(0.0f, BOID_SPEED, 0, 1);
// Rotate movement vector
mat4 rot = RotateZ(atan2(r.y, r.x) * (180 / M_PI));
m_vec = rot * m_vec;
// Add movement vector to position vector
p += vec2(m_vec.x, m_vec.y);
// Wrap around screen
if (p.x > 400)
p.x = -400;
if (p.x < -400)
p.x = 400;
if (p.y > 400)
p.y = -400;
if (p.y < -400)
p.y = 400;
}
void Run(std::vector<Boid>* flock) {
vec2 a_vec = Alignment(flock) * ALIGNMENT_WEIGHT;
vec2 c_vec = Cohesion(flock) * COHESION_WEIGHT;
vec2 s_vec = Separation(flock) * SEPARATION_WEIGHT;
r += a_vec + c_vec + s_vec;
r = normalize(r);
r *= BOID_SPEED;
Move();
}
};
std::vector<Boid> flock = std::vector<Boid>();
float vertices[][2] = {
{0.0f, (BOID_SIZE / 2) / SCREEN_HEIGHT},
{(BOID_SIZE / 2) / SCREEN_WIDTH, (BOID_SIZE / 2) / SCREEN_HEIGHT * -1},
{(BOID_SIZE / 2) / SCREEN_WIDTH * -1, (BOID_SIZE / 2) / SCREEN_HEIGHT * -1}
};
int triangles[] = { 0, 1, 2 };
const char* vertexShader = R"(
#version 130
in vec2 point;
uniform mat4 m;
void main() {
gl_Position = m * vec4(point, 0, 1);
}
)";
const char* fragmentShader = R"(
#version 130
out vec4 pColor;
uniform vec3 col;
void main() {
pColor = vec4(col, 1);
}
)";
void InitVertexBuffer() {
glGenBuffers(1, &vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*6, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*6, vertices);
}
void MouseMove(GLFWwindow* w, double x, double y) {
if (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
vec2 m = vec2((float)x, (float)y);
m.x -= (SCREEN_WIDTH / 2), m.y = (m.y - (SCREEN_HEIGHT / 2)) * -1;
float r_ang = rand_float() * 2 * M_PI;
vec2 r = vec2(cos(r_ang), sin(r_ang));
Boid b = Boid(m, r);
flock.push_back(b);
num_boids++;
}
}
void Display() {
glClearColor(0.3, 0.3, 0.3, 1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
VertexAttribPointer(program, "point", 2, 0, (void*)0);
for (int i = 0; i < num_boids; i++) {
flock[i].Run(&flock);
mat4 rot = RotateZ(atan2(flock[i].r.y, flock[i].r.x) * (180 / M_PI));
mat4 trans = Translate(flock[i].p.x / (SCREEN_WIDTH / 2), flock[i].p.y / (SCREEN_HEIGHT / 2), 0.0f);
mat4 m = trans * rot;
SetUniform(program, "m", m);
SetUniform(program, "col", flock[i].col);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, triangles);
}
glFlush();
}
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);
}
}
int main() {
srand(time(NULL));
if (!glfwInit())
return 1;
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Boids", NULL, NULL);
if (!window) {
glfwTerminate();
return 1;
}
glfwSetWindowPos(window, 100, 100);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
PrintGLErrors();
if (!(program = LinkProgramViaCode(&vertexShader, &fragmentShader)))
return 0;
// Init boids
for (int i = 0; i < num_boids; i++) {
float x = (rand() % SCREEN_WIDTH), y = (rand() % SCREEN_HEIGHT);
float r_ang = rand_float() * 2 * M_PI;
vec2 p = vec2(x, y);
vec2 r = vec2(cos(r_ang), sin(r_ang));
Boid b = Boid(p, r);
flock.push_back(b);
}
InitVertexBuffer();
glfwSetCursorPosCallback(window, MouseMove);
glfwSetKeyCallback(window, Keyboard);
glfwSwapInterval(1); // vsync
while (!glfwWindowShouldClose(window)) {
Display();
glfwPollEvents();
glfwSwapBuffers(window);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vBuffer);
glfwDestroyWindow(window);
glfwTerminate();
}