-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
412 lines (326 loc) · 10.9 KB
/
main.cpp
File metadata and controls
412 lines (326 loc) · 10.9 KB
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
/*
* Dobó László
* DOLSAAT.SZE
*
* Kötelező program
* Fejlett Grafikai algoritmusok
* 2014
*
* Labirintus generálás: vertextömb
*
* Fejlesztőkörnyezet: Ubuntu 13.10, Qt Creator (Fordítás Qt-val)
* Függvénykönyvtárak: GLEW GL glut GLU SOIL
*
* A labirintus szerkezetének generálása külön történik, c alkalmazás segítségével.
* Forrás: https://github.com/ccouzens/maze
* Az alkalmazást úgy módosítottam, hogy a konzol helyett fájlba írja a kimenetet a megadott formátumban.
* Első sor: width height\n
* Ezután width * height mátrix, mely 0 (folyosó) és 1 (fal) elemekből áll, ez reprezentálja a labirintust.
*
* Kijárat és bejárat szerepel a labirintusban, mindig el lehet érni a célt.
*
* A labirintust a maze.txt fájlba várja a program, majd innen beolvassa és memóriában eltárolja.
*
* A labirintus rajzolása vertextömb segítségével történik, így ezeket legenerálom: Vertextömb, indextömb, textúratömb.
* Textúra alkalmazva az objektumokon: Soartex Fanver resource pack for Minecraft
* Forrás: http://www.minecraftforum.net/topic/824150-17x-soartex-fanver-smooth-and-clean-released/
*
* Világítás alkalmazva.
* Irányítás: nyilakkal, fps-like. (kamera és irány mozgatás)
* Jobb, bal: forgás
* Előre, hátra: mozgás
* "F1" gomb lenyomásával felülnézeti kameraállás
*
* További források: openGL wiki, stackoverflow, számítógépes grafika kötelező program
*
* 2. teljesítés:
*
* Input fájl módosítása: ahol nincs fal (0), ott el lehet helyezni 2-es írásával egy labdát
* Tetszőleges mennyiségű labda
*
* Ütközés detektálás megvalósítása
* Ütközés helyének kiíratása
* Ütközés esetén a mozgás megáll
* Labirintus szélének figyelése
* Objektumok betöltése: labdák és a játékos avatarja
* Kameramozgatás
*
* az erőforrásoknak a futtatható állomány mellett mappákban kell lenniük!
*
*/
#include <main.h>
GameData maze;
GLfloat glTime;
GLfloat mouseX = -1.0, mouseY = -1.0;
GLfloat centerX = 0.0;
GLfloat centerY = 0.0;
// lights
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat lightPos[] = { 0.0f, -5.0f, 10.0f, 1.0f };
bool* keyStates = new bool[256]();
bool running;
int ballTexture;
/**
* @brief loadTextures Loading textures and saving the id for binding
*/
void loadTextures() {
int numberOfTextures = 4;
int currentTexture = 0;
// asking gl
maze.genTextures(numberOfTextures);
maze.loadTexture("texture/stone.png", currentTexture);
maze.wall.setTextureId(currentTexture++);
maze.loadTexture("texture/grass.png", currentTexture);
maze.floor.setTextureId(currentTexture++);
maze.loadTexture("texture/char.png", currentTexture);
maze.player.setTextureId(currentTexture++);
maze.loadTexture("texture/ball.png", currentTexture);
ballTexture = currentTexture;
}
/**
* @brief ChangeSizePerspective Move the camera
* @param w window dimension
* @param h window dimension
*/
void ChangeSizePerspective(GLsizei w, GLsizei h) {
GLfloat fAspect;
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
fAspect = (GLfloat)w/(GLfloat)h;
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Produce the perspective projection
gluPerspective( 90.0f, // fovy
fAspect, // aspect
0.1f, // zNear
1000.0 // zFar
);
gluLookAt( maze.camera[0], maze.camera[1], maze.camera[2], // eye / camera
maze.center[0], maze.center[1], maze.center[2], // center
0.0, 0.0, 1.0 // up
);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/**
* @brief RenderScene Called to draw scene
*/
void RenderScene(void)
{
keyOperations();
if (!maze.isTextureActive) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } else { glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); }
// Move the camera every scene
ChangeSizePerspective(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
// draw the elements, calling render
maze.wall.render(maze.textures); // wall
maze.floor.render(maze.textures); // floor
maze.player.render(maze.textures); // player
for (unsigned int i=0; i<maze.balls.size(); i++)
{
maze.balls[i].render(maze.textures); // balls
}
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
// cleanup the texture
glBindTexture(GL_TEXTURE_2D, 0);
// Display the results
glutSwapBuffers();
}
/**
* @brief SetupRC This function does any needed initialization on the rendering context.
*/
void SetupRC()
{
//lights
glEnable(GL_LIGHTING);
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, ambientLight );
// Setup and enable light 0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_NORMALIZE);
// All materials hereafter have full specular reflectivity
// with a high shine
glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
glMateriali(GL_FRONT,GL_SHININESS, 20);
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glShadeModel(GL_SMOOTH);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_CULL_FACE);
loadTextures();
maze.generateCubes();
maze.wall.calculate(); // generating the bounding shape
// boundaries of the wall
vector< vector<boundary> > b;
b.push_back(maze.wall.getBoundaryBox());
// loading models
maze.player.load("model/MinecraftPlayer.obj",true); // player
for (unsigned int i=0; i<maze.ballPositions.size(); i++)
{
Element ball;
ball.load("model/Football.obj", true);
maze.balls.push_back(ball);
maze.balls[i].calculate();
maze.balls[i].move(maze.ballPositions[i].x, maze.ballPositions[i].y, maze.sizeZ/3, b, true); // moving to the start point
maze.balls[i].setTextureId(ballTexture);
}
maze.player.move(maze.camera[0], 0.4f, 0, b, true); // moving player
b.clear();
glewInit();
glClearColor( 0.878f, 0.878f, 0.878f, 1.0f ); //background
glEnable(GL_TEXTURE_2D);
}
/**
* @brief SpecialKeys Move the camera
* @param key
* @param x
* @param y
*/
void SpecialKeys(int key, int x, int y)
{
GLfloat dirX = 0;
GLfloat dirZ = 0;
// arrow keys
/*if(key == GLUT_KEY_UP) { // move
dirZ = maze.sizeZ/maze.speed;
}
if(key == GLUT_KEY_DOWN) { // move
dirZ = -maze.sizeZ/maze.speed;
}*/
if(key == GLUT_KEY_LEFT) { // move
dirX = -maze.sizeX/20;
maze.center[1] += sin(dirX * M_PI / 180) * cos(dirX * M_PI / 180);
maze.center[0] += -cos(dirX * M_PI / 180) * cos(dirX * M_PI / 180);
}
if(key == GLUT_KEY_RIGHT) { // move
dirX = maze.sizeX/20;
maze.center[1] -= sin(dirX * M_PI / 180) * cos(dirX * M_PI / 180);
maze.center[0] -= -cos(dirX * M_PI / 180) * cos(dirX * M_PI / 180);
}
// F1: switch from fps to upper camera (and backwards)
if(key == GLUT_KEY_F1) {
if(maze.isUpperView) {
maze.backupCamera = maze.camera; // save the current camera
maze.backupCenter = maze.center;
maze.camera[2] = maze.upperViewHeight; // set the upper angle
} else {
maze.camera[2] = maze.backupCamera[2]; // load the backup camera angle
}
maze.isUpperView = !maze.isUpperView;
}
if(key == GLUT_KEY_F2) {
maze.isTextureActive = !maze.isTextureActive;
}
glutPostRedisplay();
}
/**
* @brief ChangeSize Orthographic projection
* @param w width
* @param h height
*/
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0,0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Keyboard(unsigned char key, int x, int y)
{
keyStates[key] = true;
}
void keyUp (unsigned char key, int x, int y)
{
keyStates[key] = false;
}
void keyOperations(){
GLfloat dirX = 0;
GLfloat dirZ = 0;
int speed = maze.speed;
// arrow keys
if(keyStates['w']) { // move
dirZ = maze.sizeZ/speed;
}
if(keyStates['s']) { // move
dirZ = -maze.sizeZ/speed;
}
if(keyStates['a']) { // move
dirX = -maze.sizeX/speed;
}
if(keyStates['d']) { // move
dirX = maze.sizeX/speed;
}
if (dirZ!=0 || dirX != 0)
{
// selecting the vectors for the player object
vector< vector<boundary> > b;
b.push_back(maze.wall.getBoundaryBox());
for (unsigned int i=0; i<maze.balls.size(); i++)
{
b.push_back(maze.balls[i].getBoundaryBox());
}
bool moved = maze.player.move(dirX, dirZ, 0, b, false); // move the player if possible
b.clear();
if (moved)
{
maze.camera[0] += dirX;
maze.camera[1] += dirZ;
maze.center[0] += dirX;
maze.center[1] += dirZ;
}
}
}
void Timer(int value)
{
glTime += 1;
glutPostRedisplay();
glutTimerFunc(100, Timer, value);
}
int main(int argc, char* argv[])
{
// load the maze
maze.readMaze();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(maze.windowWidth, maze.windowHeight);
glutCreateWindow("Dobo Laszlo DOLSAAT.SZE | Fejlett Grafika 2014");
glutReshapeFunc(ChangeSizePerspective); // Perspektiv vetites
glutSpecialFunc(SpecialKeys);
glutKeyboardFunc(Keyboard);
glutKeyboardUpFunc(keyUp);
glutTimerFunc(100, Timer, 1);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}