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
|
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <iostream>
#include "Wuerfel.h"
#define ball_radius 0.22
#define goal_width 7.32
#define goal_height 2.44
GLfloat green[]={0.0,1.0,0.0,1.0};
GLfloat white[]={1.0,1.0,1.0,1.0};
GLfloat mat_ball[]={1.0,1.0,1.0,1.0};
GLfloat mat_lines[]={1.0,1.0,1.0,1.0};
GLfloat mat_goal[]={1.0,1.0,1.0,1.0};
GLfloat mat_board[]={1.0,0.0,0.0,1.0};
GLfloat mat_field[]={0.0,1.0,0.0,1.0};
GLfloat light0_pos[]={1.0,30.0,25.0,1.0};
GLfloat light1_pos[]={1.0,30.0,50.0,1.0};
GLfloat light_col[]={1.0,1.0,1.0,1.0};
GLfloat field_topleft[]={-39.5, 0.0, 52.f};
GLfloat field_topright[]={39.5, 0.0, 52.f};
GLfloat field_bottomleft[]={-39.5, 0.0, -52.f};
GLfloat field_bottomright[]={39.5, 0.0, -52.f};
GLfloat board_topleft[]={-1.0, 1.0, 0.5};
GLfloat board_topright[]={1.0, 1.0, 0.5};
GLfloat board_bottomleft[]={-1.0, 0.0, 0.0};
GLfloat board_bottomright[]={1.0, 0.0, 0.0};
GLfloat ball_x = 3.9;
GLfloat ball_y = ball_radius;
GLfloat ball_z = 19.9;
GLfloat t = 37.5;
GLfloat pl1 = -30;
GLfloat pl2 = 0;
GLfloat pl3 = 0;
GLfloat cam_x = 30.;
GLfloat cam_y = 5.;
GLfloat cam_z = 25.;
GLuint texture_id[1];
int dir = 0;
int z=0;
int LoadGLTextures();
void animate_player1(int);
void animate_player2(int);
void animate_player3(int);
void do_nothing(int n);
// wrapper function for vector form
void glTranslatefv(GLfloat *);
void glTranslatefv(GLfloat *p)
{
glTranslatef(p[0],p[1],p[2]);
}
//void animate_ball2(int n);
//void animate_ball3(int n);
void init(void)
{
LoadGLTextures(); // Jump To Texture Loading Routine ( NEW )
glEnable(GL_LINE_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable (GL_LIGHT0);
glLightfv( GL_LIGHT0, GL_POSITION, light0_pos );
glLightfv( GL_LIGHT0, GL_DIFFUSE, light_col );
glEnable (GL_LIGHT1);
glLightfv( GL_LIGHT1, GL_POSITION, light1_pos );
glLightfv( GL_LIGHT1, GL_DIFFUSE, light_col );
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
}
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("field.bmp"))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture_id[0]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture_id[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
return Status; // Return The Status
}
void field(void)
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glLoadIdentity();
glPushMatrix();
glBindTexture ( GL_TEXTURE_2D, texture_id[0] );
//glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_field );
glBegin(GL_POLYGON);
//glColor4fv(green);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(field_topleft);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(field_topright);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(field_bottomright);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(field_bottomleft);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void boards()
{
glLoadIdentity();
glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_board );
glPushMatrix();
glTranslatef(0.0,0.0,52.0);
glScalef(39.5,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3fv(board_topleft);
glVertex3fv(board_topright);
glVertex3fv(board_bottomright);
glVertex3fv(board_bottomleft);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-39.5, 0.0 ,0.0);
glRotatef(-90, 0.0, 1.0, 0.0);
glScalef(52.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3fv(board_topleft);
glVertex3fv(board_topright);
glVertex3fv(board_bottomright);
glVertex3fv(board_bottomleft);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(39.5, 0.0, 0.0);
glRotatef(90, 0.0, 1.0, 0.0);
glScalef(52.0 , 1.0,1.0);
glBegin(GL_POLYGON);
glVertex3fv(board_topleft);
glVertex3fv(board_topright);
glVertex3fv(board_bottomright);
glVertex3fv(board_bottomleft);
glEnd();
glPopMatrix();
}
void ball(void)
{
glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_ball );
glColor4fv(white);
glLoadIdentity();
glPushMatrix();
glTranslatef(ball_x,ball_y,ball_z);
glutSolidSphere(ball_radius,20,20);
glPopMatrix();
}
void goal()
{
glLoadIdentity();
glPushMatrix();
glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_goal );
glTranslatef(0.0, 0.0, 50.0);
glPushMatrix();
glTranslatef(goal_width / 2.0, 0.0, 0.0);
glRotatef(90,1.0,0.0,0.0);
glScalef(0.12, 0.12, goal_height);
// post
Wuerfel(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(- (goal_width / 2.0), 0.0, 0.0);
glRotatef(90,1.0,0.0,0.0);
glScalef(0.12, 0.12, goal_height);
// post
Wuerfel(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, goal_height, 0.0);
glScalef(goal_width/2.0,0.12,0.12 );
// crossbar
Wuerfel(1.0);
glPopMatrix();
glPopMatrix();
}
void player(int playerNr, GLfloat *pos)
{
GLUquadricObj* left_lower_leg;
GLUquadricObj* right_lower_leg;
GLUquadricObj* left_upper_leg;
GLUquadricObj* right_upper_leg;
GLUquadricObj* torso;
GLUquadricObj* left_lower_arm;
GLUquadricObj* right_lower_arm;
GLUquadricObj* left_upper_arm;
GLUquadricObj* right_upper_arm;
left_lower_leg = gluNewQuadric();
right_lower_leg = gluNewQuadric();
left_upper_leg = gluNewQuadric();
right_upper_leg = gluNewQuadric();
torso = gluNewQuadric();
left_lower_arm = gluNewQuadric();
right_lower_arm = gluNewQuadric();
left_upper_arm = gluNewQuadric();
right_upper_arm = gluNewQuadric();
glLoadIdentity();
glPushMatrix();
glColor4fv(white);
//
//glTranslatef(0.0, 5.8, 0.0);
glTranslatef(0.0, 1.6, 0.0);
glTranslatefv(pos);
switch (playerNr) {
case 1:
glRotatef(-90, 0.0, 1.0, 0.0);
break;
case 2:
glRotatef(90, 0.0, 1.0, 0.0);
break;
case 3:
break;
}
glRotatef(90, 1.0, 0.0, 0.0);
//glScalef(3,3,3);
// torso
gluCylinder(torso, 0.25, 0.25, 0.8, 20, 20);
glPushMatrix();
glTranslatef(0.2, 0.0, 0.8);
switch (playerNr) {
case 1:
glRotatef(-pl1, 1.0, 0.0, 0.0);
break;
case 2:
glRotatef(-pl2, 1.0, 0.0, 0.0);
break;
case 3:
glRotatef(-pl3, 1.0, 0.0, 0.0);
break;
}
gluCylinder(left_upper_leg, 0.10, 0.10, 0.45, 20, 20);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.45);
switch (playerNr) {
case 1:
glRotatef(-pl1*2, 1.0, 0.0, 0.0);
break;
case 2:
glRotatef(-pl2*2, 1.0, 0.0, 0.0);
break;
case 3:
glRotatef(-pl3*2, 1.0, 0.0, 0.0);
break;
}
gluCylinder(left_lower_leg, 0.05, 0.05, 0.35, 20, 20);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0.0, 0.8);
gluCylinder(right_upper_leg, 0.10, 0.10, 0.45, 20, 20);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.45);
gluCylinder(right_lower_leg, 0.05, 0.05, 0.35, 20, 20);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.35, 0.0, 0.0);
glRotatef(45, 1.0, 0.0, 0.0);
gluCylinder(left_upper_arm, 0.10, 0.10, 0.45, 20, 20);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.45);
glRotatef(-90,1.0, 0.0, 0.0);
gluCylinder(left_lower_arm, 0.05, 0.05, 0.35, 20, 20);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.35, 0.0, 0.0);
glRotatef(45, 1.0, 0.0, 0.0);
gluCylinder(right_upper_arm, 0.10, 0.10, 0.45, 20, 20);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.45);
glRotatef(-90,1.0, 0.0, 0.0);
gluCylinder(right_lower_arm, 0.05, 0.05, 0.35, 20, 20);
glPopMatrix();
glPopMatrix();
// head
glTranslatef(0.0, 0.0, -0.2);
glutSolidSphere(0.2,20,20);
glPopMatrix();
}
//void animate()
void animate_player1(int n)
//void animate_player1()
{
//cam_x+=0.1;
//cam_z-=0.1;
//int dir = 0;
int move=30;
if (ball_x >= -4)
ball_x-=.2;
if ( (pl1 < move) && dir == 0)
pl1++;
else if ((pl1 > -move) && dir == 1)
pl1--;
else if (pl1 == move)
dir = 1;
else if (pl1 == -move)
//dir = 0;
{
z=2;
glutTimerFunc(50,animate_player2,z);
}
if (z==1)
glutTimerFunc(50,animate_player1,z);
//glutIdleFunc(animate_player2);
//z++;
//if (z>51)
// glutTimerFunc(10,animate_player2,2);
//else
// exit(0);
glutPostRedisplay();
//std::cout << "here " << n << std::endl;
}
void animate_player2(int n)
//void animate_player2()
{
//int dir = 0;
int move=30;
if (ball_x <= -3)
ball_x+=.2;
if ( (pl2 < move) && dir == 0)
pl2++;
else if ((pl2 > -move) && dir == 1)
pl2--;
else if (pl2 == move)
dir = 1;
else if (pl2 == -move)
//dir = 0;
{
z=3;
glutTimerFunc(50,animate_player3,z);
}
if (z==2)
glutTimerFunc(50,animate_player2,z);
//glutIdleFunc(animate_player3);
//z++;
//if (z>51)
// glutTimerFunc((int) z*10,animate_player3,3);
//else
// exit(0);
glutPostRedisplay();
//std::cout << "here " << n << std::endl;
}
void animate_player3(int n)
//void animate_player3()
{
//int dir = 0;
int move=30;
if (ball_z <= 51)
ball_z+=.4;
if ( (pl3 < move) && dir == 0)
pl3++;
else if ((pl3 > -move) && dir == 1)
pl3--;
else if (pl3 == move)
dir = 1;
else if (pl3 == -move)
//dir = 0;
glutTimerFunc(50,do_nothing,0);
//z++;
//if (z<51)
//glutTimerFunc((int) z*10,animate_player3,0);
if (z==3)
glutTimerFunc(50,animate_player3,z);
//glutIdleFunc(animate_player3);
//else
// exit(0);
glutPostRedisplay();
//std::cout << "here " << n << std::endl;
}
void do_nothing(int n)
{
}
void RenderScene(void)
{
GLfloat pos_pl1[]={4.0, .0, 20.0};
GLfloat pos_pl2[]={-4.0, .0, 20.0};
GLfloat pos_pl3[]={0.0, .0, 19.0};
//glClearColor(0.f, 0.f, 0.f, 0.f);
glClearColor(0.32f, 0.85f, 1.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT)); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(90.0f,glutGet(GLUT_WINDOW_WIDTH)/glutGet(GLUT_WINDOW_HEIGHT),1.0f,100.0f); // View Depth of 100
gluLookAt(cam_x, cam_y, cam_z,
0., 1., 25.,
0., 1., 0.);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
//glLoadIdentity(); // Reset The Modelview Matrix
ball();
goal();
field();
boards();
glLoadIdentity();
//glTranslatef(0.0, 4.0, 0.0);
glPushMatrix();
// glTranslatef(4.0, 1.6, 20.0);
player(1,pos_pl1);
glPopMatrix();
glPushMatrix();
// glTranslatef(-4.0, 1.6, 20.0);
player(2,pos_pl2);
glPopMatrix();
glPushMatrix();
// glTranslatef(0.0, 1.6, 16.0);
player(3,pos_pl3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit( &argc, argv );
glutInitWindowSize(800,600);
glutInitDisplayMode( GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH );
glutCreateWindow("Fussball Animation");
glutDisplayFunc( RenderScene );
//glutIdleFunc(animate_player1);
//for (int i=0;i<100;i++)
z=1;
glutTimerFunc(1,animate_player1,z);
init();
glutMainLoop();
return 0;
}
|