-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
409 lines (375 loc) · 11.9 KB
/
main.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
#include <GL/glut.h>
#include <Box2D/Box2D.h>
#include <string.h>
#include <FTGL/ftgl.h>
#include "./components/headers/circle.h"
#include "./components/headers/rectangle.h"
#include "./components/headers/triangle.h"
#include "./components/headers/configs.h"
#include "./components/headers/textures.h"
#include "./components/headers/main.h"
b2World* world;
b2Body* selectedBody;
FTGLPixmapFont* font;
int introScreenFlag=1;
char rotateflag = 'r';
b2Body* duster;
int WIDTH,HEIGHT;
extern GLuint RectTexture;
extern GLuint TriTexture;
extern GLuint BackTexture;
extern GLuint CircTexture;
extern GLuint DusterTexture;
void resetWorld(){
delete world;
float gravity = loadConfig("configs","world","gravity");
world = new b2World(b2Vec2(0.0,gravity));
addRect(WIDTH/2,HEIGHT-50,WIDTH,30, world, false);//ground
duster = addRect(WIDTH/10,HEIGHT/10,30,70, world, false);
}
void menu(int value){
if(value == 1){
resetWorld();
}
else if(value == 0){
exit(0);
}
else if(value == 2){
readFiles();
}
}
void createMenu(){
glutCreateMenu(menu);
glutAddMenuEntry("Reset",1);
glutAddMenuEntry("Reload Configs",2);
glutAddMenuEntry("Exit",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void renderWorld(){
font->FaceSize(25);
font->Render("Press i to view Instructions",-1,FTPoint(WIDTH/1.25,HEIGHT/1.2,0));
b2Body* tmp=world->GetBodyList();
b2Vec2 points[4];
while(tmp){
if(tmp->GetUserData()){
if((*(char*)tmp->GetUserData()) == rotateflag){
if(tmp->GetType() == b2_dynamicBody)
tmp->SetAngularVelocity(0.5f);
else
tmp->SetTransform(tmp->GetPosition(),(tmp->GetAngle()+0.01f));
//increase angle by 0.01 radians at every step
}
}
if(tmp->GetFixtureList()->GetType() == b2Shape::e_circle){
drawCircle(tmp->GetWorldCenter(), tmp->GetAngle(),((b2CircleShape*)(tmp->GetFixtureList()->GetShape()))->m_radius,CircTexture);
tmp=tmp->GetNext();
}
else if(((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertexCount() == 3){
for(int i = 0; i < 3; i++)
points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);
drawTriangle(points, tmp->GetWorldCenter(), tmp->GetAngle(),TriTexture);
tmp=tmp->GetNext();
}
else{
for(int i = 0; i < 4; i++)
points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);
GLuint texture;
texture = (tmp == duster? DusterTexture : RectTexture);
drawRect(points,tmp->GetWorldCenter(),tmp->GetAngle(),texture);
tmp=tmp->GetNext();
}
}
}
void renderBackground(){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, BackTexture);
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glColor3ub(255,255,255);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0, 0.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex2f(WIDTH, 0.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex2f(WIDTH, HEIGHT); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, HEIGHT);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void loadInstructions(){
glPushMatrix();
//glTranslatef(100,0,0);
font->FaceSize(50);
font->Render("2D WORLD SIMULATOR",-1,FTPoint(WIDTH/3, HEIGHT/1.25, 0));
font->Render("By",-1,FTPoint(WIDTH/2,HEIGHT/1.4,0));
font->Render("Ranadip Saha",-1,FTPoint(WIDTH/4,HEIGHT/1.6,0));
font->Render("And",-1,FTPoint(WIDTH/2.05,HEIGHT/1.6,0));
font->Render("Ravi Kumar L",-1,FTPoint(WIDTH/1.75,HEIGHT/1.6,0));
font->Render("INSTRUCTIONS",-1,FTPoint(WIDTH/2.5,HEIGHT/1.8,0));
font->FaceSize(25);
float start_height = HEIGHT/2.0;
font->Render("A or a - Add Static or Dynamic Circle",-1,FTPoint(WIDTH/15,start_height,0));
font->Render("S or s - Add Static or Dynamic Square",-1,FTPoint(WIDTH/15,start_height-30.0,0));
font->Render("D or d - Add Static or Dynamic Triangle",-1,FTPoint(WIDTH/15,start_height-60.0,0));
font->Render("p - To save screenshot of the simulation",-1,FTPoint(WIDTH/15,start_height-90.0,0));
font->Render("Enter - To Begin the simulator",-1,FTPoint(WIDTH/15,start_height-120.0,0.0));
font->Render("Left-Click + Drag - Draw a static line or pickup object if click is on an object",-1,FTPoint(WIDTH/15,start_height-150.0,0.0));
font->Render("+ - Increase size of the body that is currently being held with the mouse",-1,FTPoint(WIDTH/15,start_height-180.0,0.0));
font->Render("- - Decrease size of the body that is currently being held with the mouse",-1,FTPoint(WIDTH/15,start_height-210.0,0.0));
font->Render("r - Rotate body being held by the mouse",-1,FTPoint(WIDTH/15,start_height-240.0,0.0));
font->Render("t - Toggle the type of the body being held by the mouse between static and dynamic",-1,FTPoint(WIDTH/15,start_height-270.0,0.0));
font->Render("R - Clear all bodies in the world.",-1,FTPoint(WIDTH/15,start_height-300.0,0));
font->Render("Q - Quit",-1,FTPoint(WIDTH/15,start_height-330.0,0));
font->Render("Right-click - Menu to Reset World, Reload Configurations or Exit.",-1,FTPoint(WIDTH/15,start_height-360.0,0));
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
renderBackground();
if(introScreenFlag){
loadInstructions();
}
else{
renderWorld();
}
glutSwapBuffers();
}
void windowToWorld(int* x,int* y){
GLint viewport[4]; //var to hold the viewport info
GLdouble modelview[16]; //var to hold the modelview info
GLdouble projection[16]; //var to hold the projection matrix info
GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates
glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info
winX = (float)*x;
winY = (float)viewport[3] - (float)*y;
winZ = 0;
//get the world coordinates from the screen coordinates
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
*x = worldX;
*y = worldY;
}
void keyboard(unsigned char key, int x, int y){
windowToWorld(&x,&y);
if(key == ' '){
float width = loadConfig("configs","rectangle","width");
float height = loadConfig("configs","rectangle","height");
addRect(x, y, width, height, world, false);
}
else if(key == 's'){
float edge = loadConfig("configs","square","edge");
addRect(x, y, edge, edge, world, true);
}
else if(key == 'a'){
float radius = loadConfig("configs","circle","radius");
addCircle(x, y, radius, world, true);
}
else if(key == 'd'){
float scale = loadConfig("configs","triangle","scale");
addTriangle(x, y, scale, world, true);
}
else if(key == 'S'){
float edge = loadConfig("configs","square","edge");
addRect(x, y, edge, edge, world, false);
}
else if(key == 'A'){
float radius = loadConfig("configs","circle","static-radius");
addCircle(x, y, radius, world, false);
}
else if(key == 'D'){
float scale = loadConfig("configs","triangle","static-scale");
addTriangle(x, y, scale, world, false);
}
else if(key == 'R'){
resetWorld();
}
else if(key == 'Q'){
exit(0);
}
else if(key == 'p'){
screenshot();
}
else if(key == 'i'){
switchToIntro();
}
else if(key == '+'){
scaleSelectedBody(1.1);
}
else if(key == '-'){
scaleSelectedBody(1.0/1.1);
}
else if(key == 't'){
if(selectedBody){
if(selectedBody->GetType() == b2_dynamicBody)
selectedBody->SetType(b2_staticBody);
else
selectedBody->SetType(b2_dynamicBody);
}
}
else if(key == 'r'){
if(selectedBody){
if(selectedBody->GetUserData()){
selectedBody->SetUserData(NULL);
selectedBody->SetAngularVelocity(0.5f);
}
else{
selectedBody->SetUserData(&rotateflag);
}
}
}
else if(key == 'x'){
if(selectedBody && selectedBody != duster){
selectedBody->GetWorld()->DestroyBody(selectedBody);
selectedBody = NULL;
}
}
}
void scaleSelectedBody(float scalingFactor){
if(!selectedBody){
return;
}
if( selectedBody->GetFixtureList()->GetType() == b2Shape::e_circle){
selectedBody->GetFixtureList()->GetShape()->m_radius*=scalingFactor;
}
else{
b2PolygonShape* shape = (b2PolygonShape*)selectedBody->GetFixtureList()->GetShape();
int vertexCount = shape->GetVertexCount();
b2Vec2 vertices[vertexCount];
for(int i=0; i<vertexCount; i++){
vertices[i] = shape->GetVertex(i);
vertices[i]*=scalingFactor;
}
b2PolygonShape newShape;
newShape.Set(vertices, vertexCount);
b2Fixture* fixture = selectedBody->GetFixtureList();
b2FixtureDef newFixture;
newFixture.shape = &newShape;
newFixture.density = fixture->GetDensity();
newFixture.friction = fixture->GetFriction();
newFixture.restitution = fixture->GetRestitution();
selectedBody->DestroyFixture(fixture);
selectedBody->CreateFixture(&newFixture);
}
}
class WorldQueryCallback : public b2QueryCallback {
public:
bool ReportFixture(b2Fixture* fixture) {
selectedBody=fixture->GetBody();
return false;//Stop after finding the first fixture
}
};
void mouse(int button, int state, int x, int y){
windowToWorld(&x,&y);
if (button == GLUT_LEFT_BUTTON){
if(selectedBody && state == GLUT_UP){
selectedBody->SetAwake(true);
selectedBody = NULL;
}
if (state == GLUT_DOWN){
b2Vec2 p;
b2Vec2 d;
p.Set((float)x*P2M,(float)y*P2M);
d.Set(0.001f, 0.001f);
b2AABB aabb;
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
// Query the world for overlapping fixtures on aabb.
WorldQueryCallback callback;
world->QueryAABB(&callback, aabb);
}
}
}
class DusterQueryCallback : public b2QueryCallback {
public:
bool ReportFixture(b2Fixture* fixture) {
if(fixture->GetBody() == duster){
return true;
}
fixture->GetBody()->GetWorld()->DestroyBody(fixture->GetBody());
return true;
}
};
void motion(int x, int y){
windowToWorld(&x,&y);
if(selectedBody){
selectedBody->SetTransform(b2Vec2(x*P2M,y*P2M),selectedBody->GetAngle());
selectedBody->SetAwake(false);
selectedBody->SetTransform(b2Vec2(x*P2M,y*P2M),selectedBody->GetAngle());
selectedBody->SetAwake(false);
if(selectedBody == duster){
b2Vec2 p;
b2Vec2 d;
p.Set((float)(x-15)*P2M,(float)(y-35)*P2M);
d.Set((float)(x+15)*P2M,(float)(y+35)*P2M);
b2AABB aabb;
aabb.lowerBound = p;
aabb.upperBound = d;
// Query the world for overlapping fixtures on aabb.
DusterQueryCallback callback;
world->QueryAABB(&callback, aabb);
}
}
else{
addRect(x,y,2,2, world, false);
}
}
void step(){
float m=glutGet(GLUT_ELAPSED_TIME)/(1.0/30.0);
if(floor(m)==m){//If the time is a multiple of 1/30 th of a second, redisplay
world->Step(1.0/30.0,8,3);
glutPostRedisplay();
}
}
void init(){
glMatrixMode(GL_PROJECTION);
glOrtho(0,WIDTH,HEIGHT,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0,0,0,1);
LoadAllTextures();
computeCircleVertices();
font = new FTGLPixmapFont("../fonts/Chalk3.ttf");
if(font->Error()){
printf("\nError loading font!\n");
exit(0);
}
float gravity = loadConfig("configs","world","gravity");
world=new b2World(b2Vec2(0.0,gravity));
addRect(WIDTH/2,HEIGHT-50,WIDTH,30, world, false);//ground
duster = addRect(WIDTH/10,HEIGHT/10,30,70, world, false);
glutPostRedisplay();
}
void switchToSimulation(){
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutIdleFunc(step);
introScreenFlag = 0;
}
void switchToIntro(){
glutKeyboardFunc(introKeyboard);
glutMouseFunc(NULL);
glutMotionFunc(NULL);
glutIdleFunc(NULL);
introScreenFlag = 1;
}
void introKeyboard(unsigned char key, int x, int y){
if(key==13){
switchToSimulation();
}
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0,0);
readFiles();
WIDTH = (int)loadConfig("configs","window","width");
HEIGHT = (int)loadConfig("configs","window","height");
glutInitWindowSize(WIDTH,HEIGHT);
glutCreateWindow("Roll It");
init();
switchToIntro();
createMenu();
glutDisplayFunc(display);
glutKeyboardFunc(introKeyboard);
glutMainLoop();
}