#include #include #include #include #include #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; }