-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCore.cpp
321 lines (251 loc) · 10 KB
/
Core.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
#include <cstdlib>
#include <vector>
#include <cmath>
#include <cstring>
#include <gl/glut.h>
#include "SocialForce.h"
using namespace std;
// Global Constant Variables
const float PI = 3.14159265359F;
// Global Variables
GLsizei winWidth = 992; // Window width (16:9 ratio)
GLsizei winHeight = 558; // Window height (16:9 ratio)
SocialForce *socialForce;
float fps = 0; // Frames per second
bool animate = false; // Animate scene flag
// Function Prototypes
void init();
void createWalls();
void createAgents();
void display();
void drawAgents();
void drawCylinder(float x, float y, float radius = 0.2, int slices = 15, float height = 0.5);
void drawWalls();
void showInformation();
void drawText(float x, float y, const char text[]);
void reshape(int width, int height);
void normalKey(unsigned char key, int xMousePos, int yMousePos);
float randomFloat(float lowerBound, float upperBound);
void update();
void computeFPS();
int main(int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); // Set display mode Default mode used
glutInitWindowSize(winWidth, winHeight); // Set window width and height
glutInitWindowPosition(90, 90); // Set window position
glutCreateWindow("Crowd Simulation using Social Force"); // Set window title and create display window
init(); // Initialization
glutDisplayFunc(display); // Send graphics to display window
glutReshapeFunc(reshape); // Maintain aspect ratio when window first created, resized and moved
glutKeyboardFunc(normalKey);
glutIdleFunc(update); // Continuously execute 'update()'
glutMainLoop(); // Enter GLUT's main loop
return 0;
}
void init() {
// General Light Intensity
GLfloat gnrlAmbient[] = { 0.8F, 0.8F, 0.8F, 1.0 }; // Ambient (R, G, B, A) light intensity of entire scene
// Object Light Intensity
GLfloat lghtDiffuse[] = { 0.7F, 0.7F, 0.7F, 1.0 }; // Diffuse (R, G, B, A) light intensity
// Light Position
GLfloat lghtPosition[] = { 4.0, -4.0, 4.0, 0.0 };
glClearColor(1.0, 1.0, 1.0, 0.0); // Set colour used when colour buffer cleared
glShadeModel(GL_SMOOTH); // Set shading option
// General Lighting
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, gnrlAmbient);
// Object Lighting
glLightfv(GL_LIGHT0, GL_DIFFUSE, lghtDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, lghtPosition);
glEnable(GL_DEPTH_TEST); // Enable hidden surface removal
glEnable(GL_NORMALIZE); // Normalize normal vector
glEnable(GL_LIGHTING); // Prepare OpenGL to perform lighting calculations
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHT0);
glCullFace(GL_BACK); // Specify face to be culled
glEnable(GL_CULL_FACE); // Enable culling of specified face
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
srand(1604010629); // Seed to generate random numbers
socialForce = new SocialForce;
createWalls();
createAgents();
}
void createWalls() {
Wall *wall;
// Upper Wall
wall = new Wall(-25.0, 6.0, 25.0, 6.0); // Step 1: Create wall and define its coordinates (param: x1, y1, x2, y2)
socialForce->addWall(wall); // Step 2: Add wall to SFM
// Lower Wall
wall = new Wall(-25.0, -6.0, 25.0, -6.0);
socialForce->addWall(wall);
}
void createAgents() {
Agent *agent;
bool opposite = false;
for (int idx = 0; idx < 400; idx++) {
agent = new Agent; // Step 1: Create agent
if (!opposite) {
agent->setPosition(randomFloat(-20.3F, -5.0), randomFloat(-5.0, 5.0)); // Step 2: Set initial position (param: x, y)
agent->setPath(randomFloat(25.0, 30.0), randomFloat(-5.0, 5.0), 5.0); // Step 3: Set target position(s) (param: x, y, waypt_radius) Can set multiple targets by repeating step 3
opposite = true;
}
else {
agent->setPosition(randomFloat(5.0, 20.3F), randomFloat(-5.0, 5.0));
agent->setPath(randomFloat(-30.0, -25.0), randomFloat(-5.0, 5.0), 5.0);
opposite = false;
}
socialForce->addAgent(agent); // Step 4: Add agent to SFM
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the colour and depth buffer
glLoadIdentity(); // Initialize modelview matrix to identity matrix
// Camera
gluLookAt(0.0, 0.0, 18.0, // Position
0.0, 0.0, 0.0, // Look-at point
0.0, 1.0, 0.0); // Up-vector
glPushMatrix();
glScalef(1.0, 1.0, 1.0);
drawAgents();
drawWalls();
glPopMatrix();
showInformation();
glutSwapBuffers();
}
void drawAgents() {
vector<Agent *> agents = socialForce->getCrowd();
for (Agent *agent : agents) {
// Draw Agents
glColor3f(agent->getColour().x, agent->getColour().y, agent->getColour().z);
drawCylinder(agent->getPosition().x, agent->getPosition().y, agent->getRadius(), 15, 0.0);
}
}
void drawCylinder(float x, float y, float radius, int slices, float height) {
float sliceAngle;
Point3f current, next;
glPushMatrix();
glTranslatef(x, y, 0.0);
sliceAngle = static_cast<float>(360.0 / slices);
current.x = radius; // Set initial point
current.y = 0; // Set initial point
for (float angle = sliceAngle; angle <= 360; angle += sliceAngle) {
next.x = radius * cos(angle * PI / 180); // Compute next point
next.y = radius * sin(angle * PI / 180); // Compute next point
// Top Cover
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, height); // Centre of triangle
glVertex3f(current.x, current.y, height); // Left of triangle
glVertex3f(next.x, next.y, height); // Right of triangle
glEnd();
// Body
glBegin(GL_QUADS);
glVertex3f(current.x, current.y, height); // Top-left of quadrilateral
glVertex3f(current.x, current.y, 0.0); // Bottom-left of quadrilateral
glVertex3f(next.x, next.y, 0.0); // Bottom-right of quadrilateral
glVertex3f(next.x, next.y, height); // Top-right of quadrilateral
glEnd();
// Bottom Cover
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0); // Centre of triangle
glVertex3f(current.x, current.y, 0.0); // Left of triangle
glVertex3f(next.x, next.y, 0.0); // Right of triangle
glEnd();
current = next; // New point becomes initial point
}
glPopMatrix();
}
void drawWalls() {
vector<Wall *> walls = socialForce->getWalls();
glColor3f(0.2F, 0.2F, 0.2F);
glPushMatrix();
for (Wall *wall : walls) {
glBegin(GL_LINES);
glVertex2f(wall->getStartPoint().x, wall->getStartPoint().y);
glVertex2f(wall->getEndPoint().x, wall->getEndPoint().y);
glEnd();
}
glPopMatrix();
}
void showInformation() {
Point3f margin;
char totalAgentsStr[5] = "\0", fpsStr[8] = "\0", frctnStr[6] = "\0";
margin.x = static_cast<float>(-winWidth) / 50;
margin.y = static_cast<float>(winHeight) / 50 - 0.75F;
glColor3f(0.0, 0.0, 0.0);
// Total Agents
drawText(margin.x, margin.y, "Total agents:");
_itoa_s(socialForce->getCrowdSize(), totalAgentsStr, 10);
drawText(margin.x + 4.0F, margin.y, totalAgentsStr);
// FPS
drawText(margin.x, margin.y - 0.9F, "FPS:");
_itoa_s(static_cast<int>(fps), fpsStr, 10); // Convert integer portion into char
strcat_s(fpsStr, "."); // Append decimal mark
_itoa_s((fps - static_cast<int>(fps)) * 100000, frctnStr, 10); // Convert fractional portion into char
strncat_s(fpsStr, frctnStr, sizeof(fpsStr) - (strlen(fpsStr) + 1)); // Append fractional portion
fpsStr[7] = '\0';
drawText(margin.x + 1.7F, margin.y - 0.9F, fpsStr);
}
void drawText(float x, float y, const char text[]) {
glDisable(GL_LIGHTING); // Disable lighting for proper display of 'drawText()'
glDisable(GL_DEPTH_TEST); // Disable depth test for proper display of 'drawText()'
glPushMatrix();
glTranslatef(x, y, 0.0);
glScalef(0.0045F, 0.0045F, 0.0);
glLineWidth(1.4F);
int idx = 0;
while (text[idx] != '\0')
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[idx++]);
glPopMatrix();
glEnable(GL_DEPTH_TEST); // Enable hidden surface removal
glEnable(GL_LIGHTING); // Prepare OpenGL to perform lighting calculations
}
void reshape(int width, int height) {
glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Initialize projection matrix to identity matrix
gluPerspective(65.0, static_cast<GLfloat>(width) / height, 1.0, 100.0); // Create matrix for symmetric perspective-view frustum
glMatrixMode(GL_MODELVIEW);
winWidth = width;
winHeight = height;
}
void normalKey(unsigned char key, int xMousePos, int yMousePos) {
switch (key) {
case 'a': // Animate or inanimate scene
animate = (!animate) ? true : false;
break;
case 27: // ASCII character for Esc key
delete socialForce;
socialForce = 0;
exit(0); // Terminate program
break;
}
}
float randomFloat(float lowerBound, float upperBound) {
return (lowerBound + (static_cast<float>(rand()) / RAND_MAX) * (upperBound - lowerBound));
}
void update() {
int currTime, frameTime; // Store time in milliseconds
static int prevTime; // Stores time in milliseconds
currTime = glutGet(GLUT_ELAPSED_TIME); // Get time in milliseconds since 'glutInit()' called
frameTime = currTime - prevTime;
prevTime = currTime;
if (animate)
socialForce->moveCrowd(static_cast<float>(frameTime) / 1000); // Perform calculations and move agents
computeFPS();
glutPostRedisplay();
glutIdleFunc(update); // Continuously execute 'update()'
}
void computeFPS() {
static int frameCount = 0; // Stores number of frames
int currTime, frameTime; // Store time in milliseconds
static int prevTime; // Stores time in milliseconds
frameCount++;
currTime = glutGet(GLUT_ELAPSED_TIME); // Get time in milliseconds since 'glutInit()' called
frameTime = currTime - prevTime;
if (frameTime > 1000) {
fps = frameCount / (static_cast<float>(frameTime) / 1000); // Compute the number of FPS
prevTime = currTime;
frameCount = 0; // Reset number of frames
}
}