forked from aman-chauhan/3dmazebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
192 lines (166 loc) · 5.58 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
#include <GL/glut.h>
#include <math.h>
#include<iostream>
#include<time.h>
using namespace std;
const int map[17][17]= {
{1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1},
{1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1},
{1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1},
{1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1},
{1,0,1,1,1,0,1,0,2,0,1,0,1,1,1,1,1},
{1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1},
{1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1},
{1,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1},
{1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1},
{1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1},
{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
int mapi,mapj,flag=0;
float gx=7.5;
float gz=-0.5;
float angle=0;
int treasurex=0;
int treasurez=0;
time_t start;
const GLfloat light_ambient[]={0.0f,0.0f,0.0f,1.0f};
const GLfloat light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat light_specular[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat light_position[]={8.0f,8.0f,-8.0f,1.0f};
const GLfloat mat_ambient[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f};
const GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
const GLfloat high_shininess[]={50.0f};
void generatemap()
{
for(mapi=0;mapi<17;++mapi)
{
for(mapj=0;mapj<17;++mapj)
{
if((mapi+mapj)%2==0)
glColor3ub(0,102,0);
else
glColor3ub(0,153,0);
if(map[mapi][mapj]==1)
{
glPushMatrix();
glTranslatef((mapj+0.5),0,-1*(mapi+0.5));
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef((mapj+0.5),1,-1*(mapi+0.5));
glutSolidCube(1);
glPopMatrix();
}
if(map[mapi][mapj]==2)
{
glColor3ub(255,255,0);
glPushMatrix();
glTranslatef((mapj+0.5),-0.35,-1*(mapi+0.5));
glutSolidCube(0.3);
glPopMatrix();
}
}
}
glColor3ub(255,180,0);
glPushMatrix();
glTranslatef(8.5,-9,-8.5);
glutSolidCube(17);
glPopMatrix();
}
void init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glClearColor(1,1,1,1);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
double aspect = (double)viewport[2] / (double)viewport[3];
gluPerspective(90, aspect, 0.1, 100);
gluLookAt( gx,5,gz, gx + sin(angle),2,gz-cos(angle),0,1,0 );
glMatrixMode(GL_MODELVIEW);
generatemap();
if(gx>0.0 && gz<0.0)
{
treasurex=floor(gx);
treasurez=floor(-1*gz);
if(map[treasurex][treasurez]==2)
{
cout<<endl<<"Time Taken - "<<difftime(time(0),start)<<" seconds";
char ch;
cout<<endl<<"Press Y to exit... ";
glutHideWindow();
cin>>ch;
if(ch=='Y'||ch=='y')
exit(0);
}
}
glutSwapBuffers();
}
void specialkeys(int key, int x, int y)
{
float fraction = 0.05;
switch(key)
{
case GLUT_KEY_UP: gx= gx + fraction*sin(angle);
gz= gz - fraction*cos(angle);
//cout<<gx<<" "<<gz<<endl;
break;
case GLUT_KEY_DOWN: gx= gx - fraction*sin(angle);
gz= gz + fraction*cos(angle);
//cout<<gx<<" "<<gz<<endl;
break;
case GLUT_KEY_LEFT: angle -= M_PI/180;
//cout<<gx<<" "<<gz<<" angle"<<angle<<endl;
break;
case GLUT_KEY_RIGHT:angle += M_PI/180;
//cout<<gx<<" "<<gz<<" angle"<<angle<<endl;
break;
glutPostRedisplay();
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(1280,720);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("3D MAZE");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(specialkeys);
init();
start=time(0);
glutMainLoop();
return 0;
}