-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab6.cpp
324 lines (244 loc) · 5.99 KB
/
lab6.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
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <vector>
#include "glut.h"
#include "ground.h"
#include "camera.h"
#include "MazeGen.h"
#define WIN_WIDTH 800
#define WIN_HEIGHT 800
#define MAZE_LINES 40
#define MAZE_COLS 40
#define WALL_SIZE 60
#define WALL_MARGIN 0.001
#define MOUSE_SPEED 0.001
#define MOVE_SPEED 2
#define RADIUS 23 //3rd Person distance from target
enum Direction{
forward,
backward,
left,
right,
none
};
enum{
MOUSE_LEFT_BUTTON = 1,
MOUSE_RIGHT_BUTTON = 2,
};
GameMode crntMode = first_person;
Direction toMove = none;
int **mazeMap;
Maze *maze;
int newWidth, newHeight;
float radiusNew = RADIUS;
//cam
Camera camera;
//cuburi
std::vector<cubeCoords> cube;
void getCubeCoords(){
for (int i = 0; i < MAZE_LINES; ++i)
for (int j = 0; j < MAZE_COLS; ++j)
if (mazeMap[i][j] != ROAD)
cube.push_back(cubeCoords(i * (WALL_SIZE + WALL_MARGIN),
j * (WALL_SIZE + WALL_MARGIN),
WALL_SIZE));
}
//functie afisare
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
//set proiectie #1
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,newWidth,newHeight);
gluPerspective(45,(float)newWidth/(float)newHeight,0.2,500);
//setup view
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
camera.render(crntMode, radiusNew);
//rest scena
GLfloat white[] = {0.8f, 0.8f, 0.8f, 1.0f};
GLfloat cyan[] = {0.f, .8f, .8f, 1.f};
glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
GLfloat shininess[] = {50};
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
for (int i = 0; i < MAZE_LINES; ++i)
for (int j = 0; j < MAZE_COLS; ++j){
switch(mazeMap[i][j]){
case ROAD:
break;
case WALL:
case MARGIN_WALL:
glPushMatrix();
glColor3f(1, 0, 1);
glTranslatef(0, WALL_SIZE / 2, 0);
glTranslatef(i * (WALL_SIZE + WALL_MARGIN),
0,
j * (WALL_SIZE + WALL_MARGIN));
glScalef(WALL_SIZE, 60, WALL_SIZE);
glutSolidCube(1);
glPopMatrix();
break;
}
}
glPushMatrix();
glTranslatef(maze->finishX * (WALL_SIZE + WALL_MARGIN), 20, maze->finishY * (WALL_SIZE + WALL_MARGIN));
gluSphere(gluNewQuadric(), 10, 50,50);
glPopMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, (float)newWidth / 4 , newHeight / 4);
glClear(GL_DEPTH_BUFFER_BIT);
//set proiectie #2
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(20, 20, newWidth / 4, newHeight / 4);
gluPerspective(45,(float)newWidth/(float)newHeight,0.2,500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
camera.render(mini_map, radiusNew);
glColor3f(0.5,0.5,0.5);
//rest scena
for (int i = 0; i < MAZE_LINES; ++i)
for (int j = 0; j < MAZE_COLS; ++j){
switch(mazeMap[i][j]){
case ROAD:
break;
case WALL:
case MARGIN_WALL:
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(0, -WALL_SIZE / 2, 0);
glTranslatef(i * (WALL_SIZE + WALL_MARGIN),
0,
j * (WALL_SIZE + WALL_MARGIN));
glutSolidCube(WALL_SIZE);
glPopMatrix();
break;
}
}
//swap buffers
glDisable(GL_SCISSOR_TEST);
glFlush();
glutSwapBuffers();
}
void reshape(int width, int height){
newWidth = width;
newHeight = height;
}
void idle();
void keyboardRelease(unsigned char ch, int x, int y){
toMove = none;
}
void keyboard(unsigned char ch, int x, int y){
switch(ch){
case 27: //esc
exit(0);
break;
case 'w':
toMove = forward;
break;
case 's':
toMove = backward;
break;
case 'a':
toMove = left;
break;
case 'd':
toMove = right;
break;
case '1':
crntMode = first_person;
break;
case '2':
crntMode = third_person;
break;
case '3':
crntMode = map;
break;
case '+':
radiusNew++;
if (radiusNew > 50)
radiusNew = 50;
break;
case '-':
radiusNew--;
if (radiusNew < 4)
radiusNew = 4;
break;
default:
break;
}
}
//idle
void idle(){
switch(toMove){
case none:
break;
case forward:
camera.translateForward(1, MOVE_SPEED);
break;
case backward:
camera.translateForward(-1, MOVE_SPEED);
break;
case left:
camera.translateRight(-1, MOVE_SPEED);
break;
case right:
camera.translateRight(1, MOVE_SPEED);
break;
}
glutPostRedisplay();
}
void onMouseMove(int x, int y){
switch (crntMode){
case map:
case third_person:
case first_person:
camera.onMouseMove(x, y, MOUSE_SPEED, crntMode);
break;
}
glutPostRedisplay();
}
void onMouseClick(int button, int state, int x, int y){
onMouseMove(x, y);
}
int main(int argc, char *argv[]){
Maze m = Maze(MAZE_LINES, MAZE_COLS);
maze = &m;
mazeMap = m.getMap();
getCubeCoords();
//init glut
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
//init window
glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
glutInitWindowPosition(200,400);
glutCreateWindow("Tema 3");
glutSetCursor(GLUT_CURSOR_NONE);
//callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardRelease);
glutPassiveMotionFunc(onMouseMove);
glutMouseFunc(onMouseClick);
glutIdleFunc(idle);
//z test on
glEnable(GL_DEPTH_TEST);
//set background
glClearColor(0.2,0.2,0.2,1.0);
//init camera
cubeCoords endCoords = cubeCoords(maze->finishX * (WALL_SIZE + WALL_MARGIN), maze->finishY * (WALL_SIZE + WALL_MARGIN), WALL_SIZE);
camera.init(Vector3D(m.startX * (WALL_SIZE + WALL_MARGIN), 0, m.startY * (WALL_SIZE + WALL_MARGIN)),
cube, &endCoords);
//loop
glutMainLoop();
return 0;
}