This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
/
kishf1_ifaw8v.cpp
639 lines (504 loc) · 17.6 KB
/
kishf1_ifaw8v.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//=============================================================================================
// Szamitogepes grafika hazi feladat keret. Ervenyes 2011-tol.
// A //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sorokon beluli reszben celszeru garazdalkodni, mert a tobbit ugyis toroljuk.
// A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat.
// Tilos:
// - mast "beincludolni", illetve mas konyvtarat hasznalni
// - faljmuveleteket vegezni (printf is fajlmuvelet!)
// - new operatort hivni
// - felesleges programsorokat a beadott programban hagyni
// - tovabbi kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
// ---------------------------------------------------------------------------------------------
// A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
// es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
// a hazibeado portal ad egy osszefoglalot.
// ---------------------------------------------------------------------------------------------
// A feladatmegoldasokban csak olyan gl/glu/glut fuggvenyek hasznalhatok, amelyek
// 1. Az oran a feladatkiadasig elhangzottak ES (logikai AND muvelet)
// 2. Az alabbi listaban szerepelnek:
// Rendering pass: glBegin, glVertex[2|3]f, glColor3f, glNormal3f, glTexCoord2f, glEnd, glDrawPixels
// Transzformaciok: glViewport, glMatrixMode, glLoadIdentity, glMultMatrixf, gluOrtho2D,
// glTranslatef, glRotatef, glScalef, gluLookAt, gluPerspective, glPushMatrix, glPopMatrix,
// Illuminacio: glMaterialfv, glMaterialfv, glMaterialf, glLightfv
// Texturazas: glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glTexEnvi,
// Pipeline vezerles: glShadeModel, glEnable/Disable a kovetkezokre:
// GL_LIGHTING, GL_NORMALIZE, GL_DEPTH_TEST, GL_CULL_FACE, GL_TEXTURE_2D, GL_BLEND, GL_LIGHT[0..7]
//
// NYILATKOZAT
// ---------------------------------------------------------------------------------------------
// Nev : VARGA BALAZS
// Neptun : IFAW8V
// ---------------------------------------------------------------------------------------------
// ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
// mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
// A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
// grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
// informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
// azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
// a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
// Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
// negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
//=============================================================================================
#include <math.h>
#include <stdlib.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
// MsWindows-on ez is kell
#include <windows.h>
#endif // Win32 platform
#include <GL/gl.h>
#include <GL/glu.h>
// A GLUT-ot le kell tolteni: http://www.opengl.org/resources/libraries/glut/
#include <GL/glut.h>
#define new new_nelkul_is_meg_lehet_csinalni
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Innentol modosithatod...
//=============================================================
/*
* Irodalomjegyzek
* [Szir99] DR . SZIRMAY-KALOS LASZLO - SZAMITOGEPES GRAFIKA, 1999.
*/
#define W_HEAD_SIZE 0.03
#define W_HEAD_POINTS_NUM 4
#define W_TAIL_FULL_AMPL 0.03
#define W_TAIL_PERIODS_NUM 3
#define W_TAIL_FULL_LENGTH 0.15
#define W_TAIL_RESOLUTION 0.001
#define W_TAIL_POINTS_NUM 150 //FULL_LENGTH/TAIL_RESOLUTION
#define W_SPEED_DIV 36
//pálya (field) elemek
#define F_NUM_OF_ELEMENTS 7
#define F_LIFT_STEPPING 0.7
bool working = false;
float time = 0;
bool gameOver = false;
bool win = false;
//(c) [Szir99]
typedef float Coord;
typedef struct {
float R, G, B;
void set(float r, float g, float b) {
R = r;
G = g;
B = b;
}
} Color;
class Point2D {
Coord x, y;
public:
Point2D(float x0 = 0, float y0 = 0) {
x = x0;
y = y0;
}
float& X() {
return x;
}
float& Y() {
return y;
}
void set(float _x, float _y) {
x = _x;
y = _y;
}
};
class Lift {
Point2D beginP, endP;
int level; //0,1,2
public:
Lift(Point2D a, Point2D b) {
beginP = a;
endP = b;
level = 2;
}
bool moveUp() {
if (level < 2) {
beginP.Y() += F_LIFT_STEPPING;
endP.Y() += F_LIFT_STEPPING;
level++;
return true;
}
return false;
}
bool moveDown() {
if (level > 0) {
beginP.Y() -= F_LIFT_STEPPING;
endP.Y() -= F_LIFT_STEPPING;
level--;
return true;
}
return false;
}
Point2D getBeginP() const {
return beginP;
}
Point2D getEndP() const {
return endP;
}
};
Point2D fieldElements[F_NUM_OF_ELEMENTS][2]; //2 pontból lesz egy szakasz
Lift lift1(Point2D(-0.6, 0.4), Point2D(-0.2, 0.4));
Lift lift2(Point2D(0.6, 0.4), Point2D(0.2, 0.4));
//(c) otlet: https://lists.sch.bme.hu/wws/arc/grafika/2011-09/msg00264.html
bool fequals(float f1, float f2) {
if (fabs(f1 - f2) < 0.001) return true;
return false;
}
class Worm {
Point2D headPoints[W_HEAD_POINTS_NUM];
Point2D tailPoints[W_TAIL_POINTS_NUM];
float length;
bool shorter;
int toRight; //ha jobbra tart, akkor 1-gyel szorozzuk, ha balra, akkor -1-gyel
Color color;
void generateTailPoints() {
float const multipl = 16 * pow(W_TAIL_PERIODS_NUM, 2);
float const ampl = sqrt((pow(W_TAIL_FULL_LENGTH, 2) + multipl * pow(W_TAIL_FULL_AMPL, 2)
- pow(length, 2)) / multipl);
int c = 0;
Point2D nosePos = headPoints[0];
for (float i = 0.0; i < length; i += W_TAIL_RESOLUTION) {
float x = i;
float y = nosePos.Y() - ampl * sin(2 * W_TAIL_PERIODS_NUM * M_PI / length * x);
if (toRight > 0) { //balra megy
x = nosePos.X() - 2 * W_HEAD_SIZE - x;
} else {
x = nosePos.X() + x;
}
tailPoints[c++] = Point2D(x, y);
}
}
void fall(Lift &lift) {
float const deltaY = 0.01;
int const noseIndex = 0;
if (inLiftPipe(lift)) {
//liftre esik v. a fsz-re?
if (lift.getBeginP().Y() < headPoints[noseIndex].Y()) { //liftre
setNosePos(Point2D(headPoints[noseIndex].X(),
lift.getBeginP().Y() + W_HEAD_SIZE + deltaY));
} else { //fsz
setNosePos(Point2D(headPoints[noseIndex].X(), -1.0 + W_HEAD_SIZE + deltaY));
}
}
}
void fallDetect() {
fall(lift1);
fall(lift2);
}
bool inLiftPipe(Lift const &lift) {
int const lastPoint = length / W_TAIL_RESOLUTION;
float const headX = headPoints[0].X();
float const tailX = tailPoints[lastPoint].X();
if ((headX > lift.getBeginP().X() &&
headX < lift.getEndP().X() &&
tailX > lift.getBeginP().X() &&
tailX < lift.getEndP().X())
||
(headX < lift.getBeginP().X() &&
headX > lift.getEndP().X() &&
tailX < lift.getBeginP().X() &&
tailX > lift.getEndP().X()))
return true;
return false;
}
bool onTheLift(Lift &lift) {
int const lastPoint = length / W_TAIL_RESOLUTION;
if (((headPoints[0].X() > lift.getBeginP().X() &&
headPoints[0].X() < lift.getEndP().X()) ||
(tailPoints[lastPoint].X() > lift.getBeginP().X() &&
tailPoints[lastPoint].X() < lift.getEndP().X()))
||
((headPoints[0].X() < lift.getBeginP().X() &&
headPoints[0].X() > lift.getEndP().X()) ||
(tailPoints[lastPoint].X() < lift.getBeginP().X() &&
tailPoints[lastPoint].X() > lift.getEndP().X()))
) {
return true;
}
return false;
}
bool onDiffLevels(Lift &lift) {
float const deltaY = 0.01;
if (fequals(headPoints[0].Y() - W_HEAD_SIZE, lift.getBeginP().Y() + deltaY)) {
return false;
}
return true;
}
public:
Worm() {
toRight = 1;
shorter = true;
length = W_TAIL_FULL_LENGTH;
}
void setColor(Color color) {
this->color = color;
}
Color getColor() const {
return color;
}
void setNosePos(Point2D _nosePos) {
working = true;
headPoints[0] = _nosePos;
Point2D tmp(_nosePos.X() - W_HEAD_SIZE, _nosePos.Y() - W_HEAD_SIZE);
headPoints[1] = tmp;
tmp.set(_nosePos.X() - W_HEAD_SIZE * 2, _nosePos.Y());
headPoints[2] = tmp;
tmp.set(_nosePos.X() - W_HEAD_SIZE, _nosePos.Y() + W_HEAD_SIZE);
headPoints[3] = tmp;
generateTailPoints();
working = false;
}
float getLength() const {
return length;
}
Point2D getNosePos() const {
return headPoints[0];
}
Point2D* getHeadPoints() {
return headPoints;
}
Point2D* getTailPoints() {
return tailPoints;
}
void setDir() {
working = true;
toRight *= -1;
generateTailPoints();
working = false;
}
void checkKilled(Lift &lift) {
if (!onTheLift(lift) &&
fequals(lift.getBeginP().Y(), fieldElements[6][0].Y())) {
gameOver = true;
}
}
void moveUp(Lift &lift) {
if (onTheLift(lift) && onDiffLevels(lift))
setNosePos(Point2D(headPoints[0].X(), headPoints[0].Y() + F_LIFT_STEPPING));
}
void moveDown(Lift &lift) {
if (inLiftPipe(lift))
setNosePos(Point2D(headPoints[0].X(), headPoints[0].Y() - F_LIFT_STEPPING));
}
void control(float ts, float te) {
//step
working = true;
//mozgas animalasa
if (shorter) {
if (length - W_TAIL_FULL_LENGTH / W_SPEED_DIV
< W_TAIL_FULL_LENGTH / 2) {
shorter = false;
} else {
length -= W_TAIL_FULL_LENGTH / W_SPEED_DIV;
generateTailPoints();
}
} else {
if (length + W_TAIL_FULL_LENGTH / W_SPEED_DIV
> W_TAIL_FULL_LENGTH) {
shorter = true;
} else {
length += W_TAIL_FULL_LENGTH / W_SPEED_DIV;
Point2D newPos(headPoints[0].X() + (W_TAIL_FULL_LENGTH * toRight) / W_SPEED_DIV,
headPoints[0].Y());
setNosePos(newPos);
}
}
//kilepesdetekt
if (toRight > 0 && headPoints[0].X() > 1.0) {
setDir();
} else if (toRight < 0 && headPoints[2].X() < -1.0) {
setDir();
}
//esesdetekt
fallDetect();
working = false;
}
};
Worm greenWorm;
Worm redWorm;
//pálya elemek rajzolása
void drawFieldElements() {
glColor3f(1.0, 1.0, 1.0); // fehérrel
glBegin(GL_LINES);
for (int i = 0; i < F_NUM_OF_ELEMENTS; i++) {
glVertex2f(fieldElements[i][0].X(), fieldElements[i][0].Y());
glVertex2f(fieldElements[i][1].X(), fieldElements[i][1].Y());
}
glEnd();
}
void drawLift(Lift &lift) {
glColor3f(1.0, 1.0, 1.0); // fehérrel
glBegin(GL_LINES);
for (int i = 0; i < 2; i++) {
glVertex2f(lift.getBeginP().X(), lift.getBeginP().Y());
glVertex2f(lift.getEndP().X(), lift.getEndP().Y());
}
glEnd();
}
void initWorm(Worm &w, Point2D nosePos, Color color) {
//beállítani a színt
w.setColor(color);
//beállítani a fej pontját
w.setNosePos(nosePos);
}
void drawWorm(Worm &w) {
working = true;
//megrajzolni a fejét
glColor3f(w.getColor().R, w.getColor().G, w.getColor().B);
glBegin(GL_POLYGON);
for (int i = 0; i < W_HEAD_POINTS_NUM; i++) {
glVertex2f(w.getHeadPoints()[i].X(), w.getHeadPoints()[i].Y());
}
glEnd();
//megrajzolni a farkát
int c = w.getLength() / W_TAIL_RESOLUTION;
glBegin(GL_LINE_STRIP);
for (int i = 0; i < c; i++) {
glVertex2f(w.getTailPoints()[i].X(), w.getTailPoints()[i].Y());
}
glEnd();
working = false;
}
void simulateWorld(float tstart, float tend) {
float dt = 50;
for (float ts = tstart; ts < tend; ts += dt) {
float te;
if (tend >= ts + dt) {
te = ts + dt;
} else {
te = tend;
}
greenWorm.control(ts, te);
redWorm.control(ts, te);
//utkozesdetekt
//piros utoleri a zoldet
int const lastPoint = greenWorm.getLength() / W_TAIL_RESOLUTION;
if (fequals(redWorm.getHeadPoints()[0].X(), greenWorm.getHeadPoints()[0].X()) &&
fequals(redWorm.getHeadPoints()[0].Y(), greenWorm.getHeadPoints()[0].Y())) {
gameOver = true;
win = true;
}
}
}
// Inicializacio, a program futasanak kezdeten, az OpenGL kontextus letrehozasa utan hivodik meg (ld. main() fv.)
void onInitialization() {
//szintek letrehozasa
//felso szint
fieldElements[0][0] = Point2D(-1.0, 0.40);
fieldElements[0][1] = Point2D(-0.6, 0.40);
fieldElements[1][0] = Point2D(-0.2, 0.40);
fieldElements[1][1] = Point2D(0.2, 0.40);
fieldElements[2][0] = Point2D(0.6, 0.40);
fieldElements[2][1] = Point2D(1.0, 0.40);
//kozepso szint
fieldElements[3][0] = Point2D(-1.0, -0.30);
fieldElements[3][1] = Point2D(-0.6, -0.30);
fieldElements[4][0] = Point2D(-0.2, -0.30);
fieldElements[4][1] = Point2D(0.2, -0.30);
fieldElements[5][0] = Point2D(0.6, -0.30);
fieldElements[5][1] = Point2D(1.0, -0.30);
//also szint vegig
fieldElements[6][0] = Point2D(-1.0, -1.00);
fieldElements[6][1] = Point2D(1.0, -1.0);
Point2D nose(-0.00, 0.44);
Color c;
//zöld giliszta
c.set(0.0, 1.0, 0.0);
initWorm(greenWorm, nose, c);
//piros giliszta
nose.X() = -0.00;
nose.Y() = -0.26;
c.set(1.0, 0.0, 0.0);
initWorm(redWorm, nose, c);
redWorm.setDir();
}
void onIdle() {
if (!gameOver) {
working = true;
float old_time = time;
time = glutGet(GLUT_ELAPSED_TIME);
simulateWorld(old_time, time);
working = false;
}
glutPostRedisplay();
}
void onDisplay() {
if (gameOver) {
glClearColor(1.0, 0, 0, 0); // torlesi szin beallitasa
if (win) {
glClearColor(0, 1.0, 0, 0); // torlesi szin beallitasa
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // kepernyo torles
glutSwapBuffers();
return;
}
glClearColor(0, 0, 0, 0); // torlesi szin beallitasa
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // kepernyo torles
if (!working) {
drawFieldElements();
drawLift(lift1);
drawLift(lift2);
drawWorm(greenWorm);
drawWorm(redWorm);
}
glutSwapBuffers();
}
void onKeyboard(unsigned char key, int x, int y) {
if (gameOver) return;
if (key == 'd') glutPostRedisplay(); // d beture rajzold ujra a kepet
working = true;
//lift iranyitasanak kezelese
if (key == 'q') {
if (lift1.moveUp()) {
greenWorm.moveUp(lift1);
redWorm.moveUp(lift1);
}
}
if (key == 'a') {
if (lift1.moveDown()) {
greenWorm.checkKilled(lift1);
redWorm.checkKilled(lift1);
greenWorm.moveDown(lift1);
redWorm.moveDown(lift1);
}
}
if (key == 'o') {
if (lift2.moveUp()) {
greenWorm.moveUp(lift2);
redWorm.moveUp(lift2);
}
}
if (key == 'l') {
if (lift2.moveDown()) {
greenWorm.checkKilled(lift2);
redWorm.checkKilled(lift2);
greenWorm.moveDown(lift2);
redWorm.moveDown(lift2);
}
}
onIdle();
working = false;
glutPostRedisplay();
}
void onMouse(int button, int state, int x, int y) {
}
// ...Idaig modosithatod
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// A C++ program belepesi pontja, a main fuggvenyt mar nem szabad bantani
int main(int argc, char **argv) {
glutInit(&argc, argv); // GLUT inicializalasa
glutInitWindowSize(600, 600); // Alkalmazas ablak kezdeti merete 600x600 pixel
glutInitWindowPosition(100, 100); // Az elozo alkalmazas ablakhoz kepest hol tunik fel
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // 8 bites R,G,B,A + dupla buffer + melyseg buffer
glutCreateWindow("Grafika hazi feladat"); // Alkalmazas ablak megszuletik es megjelenik a kepernyon
glMatrixMode(GL_MODELVIEW); // A MODELVIEW transzformaciot egysegmatrixra inicializaljuk
glLoadIdentity();
glMatrixMode(GL_PROJECTION); // A PROJECTION transzformaciot egysegmatrixra inicializaljuk
glLoadIdentity();
onInitialization(); // Az altalad irt inicializalast lefuttatjuk
glutDisplayFunc(onDisplay); // Esemenykezelok regisztralasa
glutMouseFunc(onMouse);
glutIdleFunc(onIdle);
glutKeyboardFunc(onKeyboard);
glutMainLoop(); // Esemenykezelo hurok
return 0;
}