forked from rahuls98/OpenGl-Portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
609 lines (544 loc) · 14.4 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//Main version : Final versions per iteration
/*
-------------------Headers-----------------------
*/
#include<iostream>
#include<unistd.h>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include "imageloader.h"
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
/*
-------------------Global variables-----------------------
*/
#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 1000
using namespace std;
GLdouble cp_eqn[4] = {0.0, 0.0, 1.0, 0.0};
int choice;
float xmin = -8.0;
float xmax = 8.0;
float posChasm = 1.0f;
float negChasm = -3.0f;
//camera variables
float angle = 0.0, yAngle = 0.0;
int cam = 1;
float roll = 0.0f;
//camera and keyboard
float lx = 0.0f, ly = 0.0f, lz = -1.0f;
float lxs = -1.0f, lys = 0.0f, lzs = 0.0f;
//cam1
float x = -5.0f, y = 2.5f, z = 18.0f;
//cam2
//float x2 = 9.0f, yside = 2.5f,z2 = -7.5f;
float xside = -9.0f, yside = 2.5f, zside = -7.5f;
//mouse movements
float halfWidth= (float)(WINDOW_WIDTH/2.0);
float halfHeight = (float)(WINDOW_HEIGHT/2.0);
float mouseX = 0.0f, mouseY = 0.0f;
//ball variables
float b1 = 8.0;
float b2 = 18.0;
float b3 = 35.0;
float ang = 0.0;
/*
-------------------Set the cameras-----------------------
*/
//Portal Room 1 front camera
void cam1(){
gluLookAt(x, y, z,
x + lx, 2.5f + ly, z + lz,
roll + 0.0f, 2.5f, 0.0f);
glFlush();
}
//Portal Room 1 side camera
void cam2(){
gluLookAt(xside, 2.5f, zside,
4.5,0.8,-7.0,
//-4.5f, 0.3f, -2.0f,
roll + 0.0f, 2.5f, 0.0f);
glFlush();
}
//Portal Room 2 front camera
void cam3(){
gluLookAt(x, 3.0f, z,
0, 3.0, -10.0,
roll + 0.0f, 2.5f, 0.0f);
}
/*
-------------------Textures-----------------------
*/
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}
GLuint _textureId; //The id of the texture
//GLuint _textureId2;
void texPortal(){
Image* image = loadBMP("portal.bmp");
_textureId = loadTexture(image);
delete image;
}
void texWall(){
Image* image = loadBMP("greymarble_brick.bmp");
_textureId = loadTexture(image);
delete image;
}
void texLava(){
Image* image = loadBMP("lava.bmp");
_textureId = loadTexture(image);
delete image;
}
/*
-------------------Doors-----------------------
*/
//draw the portals in room 1 (User teleportation room)
void drawRoom1Doors(){
//door1
texPortal();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-6.0f, 0.0f, 3.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(-6.0f, 5.0f, 3.0f);
glTexCoord2f(2.0f, 1.0f);
glVertex3f(-3.0f, 5.0f, 3.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-3.0f, 0.0f, 3.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//door2
texPortal();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
//glColor3f(0.4f, 0.2f, 0.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-9.9f, 0.0f, -6.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(-9.9f, 5.0f, -6.0f);
glTexCoord2f(2.0f, 1.0f);
glVertex3f(-9.9f, 5.0f, -9.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-9.9f, 0.0f, -9.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//chasm top layer
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-9.0f, 0.1f, 1.0f);
glVertex3f(-9.0f, 0.1f, -3.0f);
glVertex3f(9.0f, 0.1f, -3.0f);
glVertex3f(9.0f, 0.1f, 1.0f);
glEnd();
//chasm bottom layer
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-9.0f, -0.1f, 1.0f);
glVertex3f(-9.0f, -0.1f, -3.0f);
glVertex3f(9.0f, -0.1f, -3.0f);
glVertex3f(9.0f, -0.1f, 1.0f);
glEnd();
}
//draw the portals in room 2 (Object teleportation room)
void drawRoom2Doors(){
//door1
texPortal();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-6.0f, 0.0f, 0.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(-6.0f, 5.0f, 0.0f);
glTexCoord2f(2.0f, 1.0f);
glVertex3f(-3.0f, 5.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-3.0f, 0.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//door2
texPortal();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(9.9f, 0.0f, -9.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(9.9f, 5.0f, -9.0f);
glTexCoord2f(2.0f, 1.0f);
glVertex3f(9.9f, 5.0f, -6.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(9.9f, 0.0f, -6.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//door3
texPortal();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-9.9f, 0.0f, -9.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(-9.9f, 5.0f, -9.0f);
glTexCoord2f(2.0f, 1.0f);
glVertex3f(-9.9f, 5.0f, -6.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-9.9f, 0.0f, -6.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//door4
texPortal();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(7.5f, 6.9f, 1.5f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(2.5f, 6.9f, 1.5f);
glTexCoord2f(2.0f, 1.0f);
glVertex3f(2.5f, 6.9f, -1.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(7.5f, 6.9f, -1.5f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
/*
-------------------Objects-----------------------
*/
//draw ball
void drawBall(float ballx, float bally, float ballz){
glColor3f(0.556863, 0.137255, 0.137255);
glLineWidth(1);
glTranslatef(ballx,bally,ballz);
glutWireSphere(1.0,40,40);
usleep(5000);
}
//draw teatpot
void drawTeaPot(){
glPushMatrix();
glColor3f(0.556863, 0.137255, 0.137255);
//glTranslated(-4.5,0.3,-7.6);
//glTranslated(-4.5,0.3,-2.0);
glTranslated(4.5,0.5,-7.0);
glutSolidTeapot(0.6);
glPopMatrix();
}
/*
-------------------Render scene-----------------------
*/
void fall(){
y -= 0.05;
}
//main animation function
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, _textureId);
if(choice==1 || choice==2){
glLoadIdentity();
//toggle camera
if(choice==1){
if(cam==1)
cam1();
else if(cam==2)
cam2();
}
else if(choice==2){
cam = 3;
cam3();
}
//Draw Floor
glColor3f(0.7f, 0.7f, 0.7f);
glBegin(GL_QUADS);
glVertex3f(-10.0f, 0.0f, -10.0f);
glVertex3f(-10.0f, 0.0f, 10.0f);
glVertex3f(10.0f, 0.0f, 10.0f);
glVertex3f(10.0f, 0.0f, -10.0f);
glEnd();
//back wall
texWall();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-10.0f, 0.0f, -10.0f);
glTexCoord2f(0.0f, 2.0f);
glVertex3f(-10.0f, 7.0f, -10.0f);
glTexCoord2f(5.0f, 2.0f);
glVertex3f(10.0f, 7.0f, -10.0f);
glTexCoord2f(5.0f, 0.0f);
glVertex3f(10.0f, 0.0f, -10.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//left wall
texWall();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-10.0f, 0.0f, -10.0f);
glTexCoord2f(0.0f, 2.0f);
glVertex3f(-10.0f, 7.0f, -10.0f);
glTexCoord2f(5.0f, 2.0f);
glVertex3f(-10.0f, 7.0f, 10.0f);
glTexCoord2f(5.0f, 0.0f);
glVertex3f(-10.0f, 0.0f, 10.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//right wall
texWall();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(10.0f, 0.0f, -10.0f);
glTexCoord2f(0.0f, 2.0f);
glVertex3f(10.0f, 7.0f, -10.0f);
glTexCoord2f(5.0f, 2.0f);
glVertex3f(10.0f, 7.0f, 10.0f);
glTexCoord2f(5.0f, 0.0f);
glVertex3f(10.0f, 0.0f, 10.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
//ceiling
glColor3f(0.95f, 0.95f, 0.95f);
glBegin(GL_QUADS);
glVertex3f(-10.0f, 7.0f, -10.0f);
glVertex3f(10.0f, 7.0f, -10.0f);
glVertex3f(10.0f, 7.0f, 10.0f);
glVertex3f(-10.0f, 7.0f, 10.0f);
glEnd();
//User teleportation room
if(choice==1){
drawRoom1Doors();
drawTeaPot();
if(z<posChasm){
if(y>-30.0)
fall();
}
}
//object teleportation room
else if(choice==2){
drawRoom2Doors();
//ball : portal2 -> portal3
glPushMatrix();
drawBall(b2,0.95,-7.5);
b2 -= 0.1;
if(ang>=360.0)
ang = 0.0;
ang+=30;
glPopMatrix();
//ball : portal4 -> ground
glPushMatrix();
drawBall(5.0,b3,0.0);
if(b3>1.0){
b3 -=0.1;
if(ang>=360.0)
ang = 0.0;
ang+=30;
}
else
b3 = 1.0;
glPopMatrix();
//ball : user -> portal1
glClipPlane(GL_CLIP_PLANE0, cp_eqn);
glEnable(GL_CLIP_PLANE0);
drawBall(-4.5,0.95,b1);
b1 -= 0.1;
if(ang>=360.0)
ang = 0.0;
ang+=30;
glDisable(GL_CLIP_PLANE0);
}
glFlush();
glutSwapBuffers();
}
}
/*
-------------------special key functions-----------------------
*/
//keyboard keys
void processSpecialKeys(int key, int xx, int yy){
float fraction = 0.3f;
switch(key){
//left arrow key event
case GLUT_KEY_LEFT:
if(cam==1){
x += sin(angle - M_PI/2.0) * fraction;
z += -cos(angle - M_PI/2.0) * fraction;
}
else if(cam==3){
if(x>xmin){
//printf("\n%f",x);
x += sin(angle - M_PI/2.0) * fraction;
z += -cos(angle - M_PI/2.0) * fraction;
}
}
else if(cam==2){
xside -= sin(angle - M_PI/2.0) * fraction;
zside -= -cos(angle - M_PI/2.0) * fraction;
}
break;
//right arrow key event
case GLUT_KEY_RIGHT:
if(cam==1){
x += sin(M_PI/2.0 + angle) * fraction;
z += -cos(M_PI/2.0 + angle) * fraction;
}
else if(cam==3){
if(x<xmax){
x += sin(M_PI/2.0 + angle) * fraction;
z += -cos(M_PI/2.0 + angle) * fraction;
}
}
else if(cam==2){
xside -= sin(M_PI/2.0 + angle) * fraction;
zside -= -cos(M_PI/2.0 + angle) * fraction;
}
break;
//top arrow key event
case GLUT_KEY_UP:
if(choice==1){
if(x<-3 && x>-6 && z<3.5){
cam = 2;
}
if(cam==1){
x += lx * fraction;
z += lz * fraction;
}
if(cam==2){
xside -= lxs * fraction;
zside -= lzs * fraction;
}
}
break;
//bottom arrow key event
case GLUT_KEY_DOWN:
if(choice==1){
if(cam==1){
x -= lx * fraction;
z -= lz * fraction;
}
if(cam==2){
xside += lxs * fraction;
zside += lzs * fraction;
}
}
break;
//Exit
case 27:
exit(0);
}
}
//mouse movement
void processMouseMovement(int xx, int yy){
mouseX = (float)(halfWidth - xx)/halfWidth;
mouseY = (float)(halfHeight - yy)/halfHeight;
angle -= (0.003f * mouseX);
lx = sin(angle);
lz = -cos(angle);
if(abs(yAngle) < (M_PI/2)){
yAngle += (0.003f * mouseY);
}
ly = sin(yAngle);
}
//menu selection function
void demo_menu(int id){
switch(id){
case 0:exit(0);
break;
case 1:choice=1;
break;
case 2:choice=2;
break;
}
glutPostRedisplay();
}
//window size and viewport
void changeSize(int w, int h){
if(h==0)
h=1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
halfWidth = (float)(w/2.0);
halfHeight = (float)(h/2.0);
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//refresh screen
void animate () {
glutPostRedisplay();
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Portal");
initRendering();
int config = glutCreateMenu(demo_menu);
glutAddMenuEntry("Enter Portal Room 1",1);
glutAddMenuEntry("Enter Portal Room 2",2);
glutAddMenuEntry("Exit",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(animate);
glutSpecialFunc(processSpecialKeys);
glutPassiveMotionFunc(processMouseMovement);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 1;
}