Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************/
- /* cube.cpp */
- /********************************************************/
- /* Affiche a l'ecran un cube en 3D */
- /********************************************************/
- /* inclusion des fichiers d'en-tete freeglut */
- #ifdef __APPLE__
- #include <GLUT/glut.h> /* Pour Mac OS X */
- #else
- #include <GL/freeglut.h> /* Pour les autres systemes */
- #endif
- #include <cstdlib>
- #include <windows.h>
- #include <string>
- #include <iostream>
- #include <math.h>
- using namespace std;
- /*class Point*/
- class Point{
- public :
- //coordonnées x, y et z du point
- double x;
- double y;
- double z;
- // couleur r, v et b du point
- float r;
- float g;
- float b;
- };
- char presse;
- int anglex,angley,x,y,xold,yold;
- //Variable de l'animation des jambes
- GLfloat angleJambeGPos = 0;
- GLfloat angleJambeGNeg = -20;
- GLfloat angleJambeDPos = 0;
- GLfloat angleJambeDNeg = 20;
- GLfloat anglePos = 0;
- GLfloat angleNeg = -40;
- //variables de l'animation du bras droit
- GLfloat angleBHaut = 0;
- GLfloat angleBBas = 0;
- //camera
- GLfloat g_angle_y = 32.0f;
- GLfloat g_inc = 0.0f;
- GLfloat g_eye_y = 0;
- /* Prototype des fonctions */
- void affichage();
- void clavier(unsigned char touche,int x,int y);
- void reshape(int x,int y);
- void idle();
- void mouse(int bouton,int etat,int x,int y);
- void mousemotion(int x,int y);
- void marche(int i);
- void dessinMain();
- GLUquadricObj *pObj;
- int main(int argc,char **argv)
- {
- /* initialisation de glut et creation
- de la fenetre */
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(500,500);
- glutCreateWindow("cube");
- /* Initialisation d'OpenGL */
- glClearColor(0.0,0.0,0.0,0.0);
- glColor3f(1.0,1.0,1.0);
- glPointSize(2.0);
- glEnable(GL_DEPTH_TEST);
- GLfloat diffuse_0[]={1.0,1.0,1.0,1.0};
- GLfloat ambient_0[]={0.2,0.2,0.2,1.0};
- GLfloat specular_0[]={1.0,1.0,1.0,1.0};
- GLfloat spot_direction[]={-1.0,-1.0,0.0};
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING);
- GLfloat position_source0[]={-2.0,5.0,1.0,0.5};
- glLightfv(GL_LIGHT0,GL_POSITION,position_source0);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse_0);
- glLightfv(GL_LIGHT0,GL_SPECULAR,specular_0);
- glLightfv(GL_LIGHT0,GL_AMBIENT,ambient_0);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse_0);
- /*glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,1.5);
- glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION,0.5);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION,0.2);
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
- glLightf(GL_LIGHT0, GL_SPOT_EXPONENT,2.0);*/
- glEnable(GL_LIGHT0);
- pObj = gluNewQuadric();
- gluQuadricNormals(pObj, GLU_SMOOTH);
- /* enregistrement des fonctions de rappel */
- glutDisplayFunc(affichage);
- glutKeyboardFunc(clavier);
- glutReshapeFunc(reshape);
- glutMouseFunc(mouse);
- glutMotionFunc(mousemotion);
- glutTimerFunc(200,marche,1);
- /* Entree dans la boucle principale glut */
- glutMainLoop();
- return 0;
- }
- void torusMain(int nParallele, int nMeridien)
- {
- double s, t, x, y, z;
- GLfloat phi, theta;
- GLfloat R = 1.0;
- GLfloat r = 0.2;
- GLfloat deux_pi = 2 * M_PI;
- for (int i = 0; i < nParallele; i++) {
- glBegin(GL_QUAD_STRIP);
- for (int j = 0; j <= 3*nMeridien/4; j++) {
- for (int k = 1; k >= 0; k--) {
- s = (i + k) % nParallele + 0.5;
- t = j % nMeridien;
- theta = s * deux_pi / nParallele;
- phi =t*deux_pi/nMeridien;
- x = (R+r*cos(theta))*cos(phi);
- y = (R+r*cos(theta))*sin(phi);
- z = r * sin(theta);
- glVertex3f(x, y, z);
- }
- }
- glEnd();
- }
- }
- void dessinMain()
- {
- glPushMatrix();
- glColor3d(1.0,1.0,0.0);
- glPushMatrix();
- glTranslatef(0.0,0.4,-0.9);
- glutSolidCylinder(0.5,0.5,50,50);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0.0,0.8,0.7);
- glRotatef(-90,1.0,0.0,0.0);
- glRotatef(-45,0.0,0.0,1.0);
- glScalef(1.0,1.0,4.0);
- torusMain(50,50);
- glPopMatrix();
- glPopMatrix();
- }
- void marche(int i)
- {
- for(int k=0; k<7; k++)
- {
- if(anglePos>-40)
- {
- angleJambeGPos -=1;
- angleJambeDPos +=1;
- anglePos -=1;
- }
- else if (angleNeg<40)
- {
- angleJambeGNeg +=1;
- angleJambeDNeg -=1;
- angleNeg +=1;
- }
- else if (anglePos == -40&&angleNeg==40)
- {
- anglePos =40;
- angleNeg =-40;
- }
- }
- glutPostRedisplay();
- glutTimerFunc(40,marche,i);
- }
- void dessinCorps()
- {
- //dimension corps lego : larg : 4.4, prof = 2 haut : 4
- //Tableau pour stocker les sommets du cube et leur couleur
- Point pCorps[8]={
- //
- {-0.5,-0.5, 0.25,1.0,1.0,1.0},
- { 0.5, -0.5, 0.25,1.0,1.0,1.0},
- { 0.5, -0.5, -0.25,1.0,1.0,1.0},
- { -0.5, -0.5, -0.25,1.0,1.0,1.0},
- { -0.4,0.5, 0.25,1.0,1.0,1.0},
- { 0.4, 0.5, 0.25,1.0,1.0,1.0},
- { 0.4, 0.5, -0.25,1.0,1.0,1.0},
- { -0.4, 0.5, -0.25,1.0,1.0,1.0}};
- //Tableau pour stocker les indices des sommets par face pour le cube
- int fCorps[6][4]={
- {0,3,2,1},
- {0,1,5,4},
- {1,2,6,5},
- {2,3,7,6},
- {0,4,7,3},
- {4,5,6,7}};
- glPushMatrix();
- glTranslatef(0.0,5.1,0.0);
- glTranslatef(0.0,2.0,0.0);
- glScalef(4.4,4.0,4.0);
- glPushMatrix();
- // Dessin du Corps
- for (int i=0;i<6;i++)
- {
- glBegin(GL_POLYGON);
- for (int j=0;j<4;j++){
- glColor3f(pCorps[fCorps[i][j]].r,pCorps[fCorps[i][j]].g,pCorps[fCorps[i][j]].b);
- glVertex3f(pCorps[fCorps[i][j]].x,pCorps[fCorps[i][j]].y,pCorps[fCorps[i][j]].z);
- }
- glEnd();
- }
- glPopMatrix();
- glPopMatrix();
- }
- void dessinCeinture()
- {
- //dessin haut ceinture
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(0.0,4.9,0.0);
- glScalef(4.4,0.5,2);
- glutSolidCube(1);
- glPopMatrix();
- //dessin cylindre entre jambe
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(-0.2,4.0,0.0);
- glRotatef(90,0.0,1.0,0.0);
- glutSolidCylinder(1.0,0.4,20,20);
- glPopMatrix();
- }
- void tete()
- {
- glPushMatrix();
- glColor3f(1.0,1.0,0.0);
- glTranslatef(0.0,9.1,0.0);
- glScalef(0.5,0.5,0.5);
- //cou
- glPushMatrix();
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidCylinder(2,0.3,20,1);
- glPopMatrix();
- //tete
- glPushMatrix();
- glTranslatef(0.0,0.3,0.0);
- //glRotatef();
- //glScalef();
- glPushMatrix();
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidCylinder(2.0,1,20,20);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0.0,1.0,0.0);
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidTorus(1.0,2.0,20,20);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0.0,1.0,0.0);
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidCylinder(3,4,20,20);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0.0,5.0,0.0);
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidTorus(1.0,2.0,20,20);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0.0,5.0,0.0);
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidCylinder(2.0,1,20,20);
- glPopMatrix();
- glPopMatrix();
- //bitoniau
- glPushMatrix();
- glTranslatef(0.0,6.3,0.0);
- glRotatef(-90,1.0,0.0,0.0);
- glutSolidCylinder(1.5,0.6,20,20);
- glPopMatrix();
- glPopMatrix();
- }
- void dessinBrasGauche()
- {
- glPushMatrix();
- glTranslated(2.2,8.2,0.0);
- glRotated(-angleBBas,1.0,0.0,0.0);
- glRotated(-angleBHaut,1.0,0.0,0.0);
- glTranslated(-2.2,-8.2,0.0);
- glPushMatrix();
- glColor3f(0.0,0.0,1.0);
- glTranslatef(2.2,8.2,0.0);
- glRotatef(18,0,0,1.0);
- //epaule
- glPushMatrix();
- glutSolidSphere(0.75,20,20);
- glPopMatrix();
- //apres bras
- glPushMatrix();
- glRotatef(90,1.0,0.0,0.0);
- glutSolidCylinder(0.75,1.8,20,20);
- glPopMatrix();
- //avant bras et main
- glPushMatrix();
- glTranslatef(0.0,-1.4,-0.12);
- glRotatef(60,1.0,0.0,0.0);
- glutSolidCylinder(0.75,1.3,20,20);
- glPushMatrix();
- glTranslatef(0.0,-0.4,2.2);
- dessinMain();
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- }
- void dessinBrasDroit()
- {
- glPushMatrix();
- glColor3f(0.0,0.0,1.0);
- glTranslatef(-2.2,8.2,0.0);
- glRotatef(-18,0,0,1.0);
- glPushMatrix();
- glutSolidSphere(0.75,20,20);
- glPopMatrix();
- glPushMatrix();
- glRotatef(90,1.0,0.0,0.0);
- glutSolidCylinder(0.75,1.8,20,20);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0.0,-1.4,-0.12);
- glRotatef(60,1.0,0.0,0.0);
- glutSolidCylinder(0.75,1.3,20,20);
- glPushMatrix();
- glTranslatef(0.0,-0.4,2.2);
- dessinMain();
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- }
- void dessinJambeDroite()
- {
- glPushMatrix();
- glTranslatef(0.0,4.0,0.0);
- glRotatef(angleJambeDPos, 1.0,0.0,0.0);
- glRotatef(angleJambeDNeg, 1.0,0.0,0.0);
- glTranslatef(0.0,-4.0,0.0);
- glPushMatrix();
- glTranslatef(0.0,4.0,0.0);
- glRotatef(-20, 1.0,0.0,0.0);
- glTranslatef(0.0,-4.0,0.0);
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(-0.2,4.0,0.0);
- glRotatef(-90,0.0,1.0,0.0);
- glutSolidCylinder(1.0,2.0,20,1);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(-1.2,0.5,0.0);
- glScalef(1.0,0.5,1.0);
- glutSolidCube(2);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(-1.2,2.5,-0.3);
- glScalef(1.0,1.5,0.7);
- glutSolidCube(2);
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- }
- void dessinJambeGauche()
- {
- glPushMatrix();
- glTranslatef(0.0,4.0,0.0);
- glRotatef(angleJambeGPos, 1.0,0.0,0.0);
- glRotatef(angleJambeGNeg, 1.0,0.0,0.0);
- glTranslatef(0.0,-4.0,0.0);
- glPushMatrix();
- glTranslatef(0.0,4.0,0.0);
- glRotatef(20, 1.0,0.0,0.0);
- glTranslatef(0.0,-4.0,0.0);
- //glScalef();
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(1.2,0.5,0.0);
- glScalef(1.0,0.5,1.0);
- glutSolidCube(2);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(0.2,4.0,0.0);
- glRotatef(90,0.0,1.0,0.0);
- glutSolidCylinder(1.0,2.0,20,1);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1.0,0.0,0.0);
- glTranslatef(1.2,2.5,-0.3);
- glScalef(1.0,1.5,0.7);
- glutSolidCube(2);
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- }
- void affichage()
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glOrtho(-5,5,5,15,-5,5); // vue tete
- //glOrtho(-3,3,-3,3,-3,3); //vue proche en 0
- glOrtho(-10,10,-3,17,-5,5); // vue tout corps
- glMatrixMode(GL_MODELVIEW);
- int i,j;
- /* effacement de l'image avec la couleur de fond */
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glShadeModel(GL_SMOOTH);
- glLoadIdentity();
- glRotatef(angley,1.0,0.0,0.0);
- glRotatef(anglex,0.0,1.0,0.0);
- dessinCorps();
- dessinCeinture();
- dessinJambeDroite();
- dessinJambeGauche();
- dessinBrasDroit();
- dessinBrasGauche();
- tete();
- //Repère
- //axe x en rouge
- glBegin(GL_LINES);
- glColor3f(1.0,0.0,0.0);
- glVertex3f(0, 0,0.0);
- glVertex3f(1, 0,0.0);
- glEnd();
- //axe des y en vert
- glBegin(GL_LINES);
- glColor3f(0.0,1.0,0.0);
- glVertex3f(0, 0,0.0);
- glVertex3f(0, 1,0.0);
- glEnd();
- //axe des z en bleu
- glBegin(GL_LINES);
- glColor3f(0.0,0.0,1.0);
- glVertex3f(0, 0,0.0);
- glVertex3f(0, 0,1.0);
- glEnd();
- glFlush();
- //On echange les buffers
- glutSwapBuffers();
- }
- void clavier(unsigned char touche,int x,int y)
- {
- switch (touche)
- {
- case 'p': /* affichage du carre plein */
- glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
- glutPostRedisplay();
- break;
- case 'f': /* affichage en mode fil de fer */
- glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
- glutPostRedisplay();
- break;
- case 's' : /* Affichage en mode sommets seuls */
- glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
- glutPostRedisplay();
- break;
- case 'd':
- glEnable(GL_DEPTH_TEST);
- glutPostRedisplay();
- break;
- case 'D':
- glDisable(GL_DEPTH_TEST);
- glutPostRedisplay();
- break;
- case 'b':
- angleBHaut+=5;
- glutPostRedisplay();
- break;
- case 'B':
- angleBBas -=5;
- glutPostRedisplay();
- break;
- case 'q' : /*la touche 'q' permet de quitter le programme */
- exit(0);
- }
- }
- void reshape(int x,int y)
- {
- if (x<y)
- glViewport(0,(y-x)/2,x,x);
- else{
- glViewport((x-y)/2,0,y,y);}
- /*gluLookAt( 0.0f, 3.5f * sin( g_eye_y ), 3.5f * cos( g_eye_y ),
- 0.0f, 0.0f, 0.0f,
- 0.0f, ( cos( g_eye_y ) < 0 ? -1.0f : 1.0f ), 0.0f );*/
- }
- void mouse(int button, int state,int x,int y)
- {
- /* si on appuie sur le bouton gauche */
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- presse = 1; /* le booleen presse passe a 1 (vrai) */
- xold = x; /* on sauvegarde la position de la souris */
- yold=y;
- }
- /* si on relache le bouton gauche */
- if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
- presse=0; /* le booleen presse passe a 0 (faux) */
- }
- void mousemotion(int x,int y)
- {
- if (presse) /* si le bouton gauche est presse */
- {
- /* on modifie les angles de rotation de l'objet
- en fonction de la position actuelle de la souris et de la derniere
- position sauvegardee */
- anglex=anglex+(x-xold);
- angley=angley+(y-yold);
- glutPostRedisplay(); /* on demande un rafraichissement de l'affichage */
- }
- xold=x; /* sauvegarde des valeurs courante de le position de la souris */
- yold=y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement