Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************/
- /* TP2: Primitives 3D et couleur */
- /************************************************************/
- /* */
- /* ESGI: algorithmique pour l'infographie */
- /* */
- /************************************************************/
- /* */
- /* Objectif: afficher des formes 3D et illuminer la scène */
- /* */
- /************************************************************/
- #include<windows.h>
- #include <math.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <glut.h>
- #endif
- #include<stdlib.h>
- #include<stdio.h>
- float epaule = 0.0;
- float coude = 0.0;
- float counter = 0.0;
- float bounceman = 0.0;
- float counterb = 0.0;
- // camera centree
- /*int x = 0;
- int y = 0;
- int z = 0;
- int u=0;
- int v=1;
- int w=0;
- float R= 10.0;
- float alpha=0.1;
- float b = 0.1;
- float x0;
- float yy0;
- float z0;*/
- int xm = 0;
- int ym = 0;
- int zm = 0;
- int u=0;
- int v=1;
- int w=0;
- int R= 10;
- float alpha=0.1;
- float b = 1;
- /* prototypes de fonctions */
- void initRendering();
- void display(void);
- void reshape(int w,int h);
- void update(int value);
- void keyboard(unsigned char key, int x, int y);
- /* Programme principal */
- int main(int argc, char **argv){
- /* Initialisation de glut et creation de la fenetre */
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition (100, 100);
- glutCreateWindow("Robot Articulé");
- /* Initialisation d'OpenGL */
- initRendering();
- /* Enregistrement des fonctions de rappel */
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutTimerFunc (20,update, 0);
- glutKeyboardFunc(keyboard);
- /* Entrée dans la boucle principale de glut, traitement des évènements */
- glutMainLoop();
- return 0;
- }
- /* Initialisation d'OpenGL pour le rendu de la scène */
- void initRendering() {
- /* Active le z-buffer */
- glEnable(GL_DEPTH_TEST);
- /* Activation des couleurs */
- glEnable(GL_COLOR_MATERIAL);
- /* définit la couleur d'effacement et la couleur de fond */
- glClearColor(0.0, 0.0, 0.0, 0.0);
- /* Activation des lumières */
- glEnable(GL_LIGHTING); // Activation du mode
- glEnable(GL_LIGHT0); // Activation lumière n°0
- glEnable(GL_LIGHT1); // Activation lumière n°1
- /* Les normales (crées par glNormal(*)) sont automatiquement unitaires */
- glEnable(GL_NORMALIZE);
- /* Activation du mode ombrage (shading) et lissage (smooth) des couleur */
- glShadeModel(GL_SMOOTH);
- /* définit la taille d'un pixel*/
- glPointSize(2.0);
- }
- /* Création de la scène avec lampes */
- void display(void){
- /* Déclaration des couleurs et positions des lampes */
- GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; // Color (0.2, 0.2, 0.2)
- GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; // Color (0.5, 0.5, 0.5)
- GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; // Positioned at (4, 0, 8)
- GLfloat lightColor1[] = {0.8f, 0.2f, 0.2f, 1.0f}; // Color (0.5, 0.2, 0.2)
- GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f}; // Coming from the direction (-1, 0.5, 0.5)
- /* Déclaration des différents types de matière des sphères */
- GLfloat no_mat[] = {0.0, 0.0, 0.0, 1.0};
- GLfloat mat_ambient_color[] = {0.8, 0.8, 0.2, 1.0};
- GLfloat mat_diffuse[] = {0.1, 0.5, 0.8, 1.0};
- GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat no_shininess[] = {0.0};
- GLfloat low_shininess[] = {5.0};
- GLfloat high_shininess[] = {100.0};
- GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- // Ajout lumière ambiante
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
- // Ajout lumière positionnelle L0
- glLightfv(GL_LIGHT0 , GL_DIFFUSE , lightColor0); // lumière diffuse
- glLightfv(GL_LIGHT0 , GL_POSITION , lightPos0); // position
- // Ajout lumière positionnelle L1
- glLightfv(GL_LIGHT1, GL_DIFFUSE , lightColor1);
- glLightfv(GL_LIGHT1, GL_SPECULAR , lightColor1); // lumiére spéculaire
- glLightfv(GL_LIGHT1, GL_POSITION , lightPos1);
- float x0 = R*cos(b)*sin(alpha);
- float yy0 = R*sin(b);
- float z0 = R*cos(alpha)*cos(b);
- gluLookAt(x0,yy0,z0,xm,ym,zm,u,v,w);
- /*----------------ENSEMBLE HUMAIN-----------------------*/
- glPushMatrix();
- glPushMatrix();
- /*-------------------- CORPS ---------------------*/
- glScalef(1,1,1);
- glTranslatef(-1, 0, 0); // au milieu ecrire variable bounceman pour le balancement
- /* TETE */
- glTranslatef(-1, 2, 0);
- glutSolidSphere(1, 10.0, 10.0);
- /* SQUELETTE ET PIED DE BICHE */
- glPushMatrix();
- glScalef(1,7,1);
- glTranslatef(-0.1, -1, 0);
- glutSolidCube(1.0);
- glPopMatrix();
- /* VENTRE*/
- glPushMatrix();
- glScalef(2,2,2);
- glTranslatef(-0.1, -2.1, 0);
- glutSolidSphere(1.1, 35.0, 10.0);
- glPopMatrix();
- /*-------------------- CORPS FIN ---------------------*/
- /*-------------------- BRAS ---------------------*/
- /* BRAS DROIT */
- glPushMatrix();
- /* PREMIERE EPAULE HAUTE */
- glTranslatef(0, -3, 1.5);
- glRotatef(epaule, 0.0, 0.0, 1);
- glutSolidSphere(0.6, 10.0, 10.0);
- glTranslatef(1,0,0);
- /* PREMIER COUDE HAUT */
- glPushMatrix();
- glScalef(2,1,1);
- glRotatef(250,1,1,1);
- glutSolidCube(1.0);
- glPopMatrix();
- /* PREMIERE ROTULE MILIEU */
- glTranslatef(1,0,0);
- glRotatef(coude, 0.0, 0.0, 1);
- glutSolidSphere(0.6, 10.0, 10.0);
- glTranslatef(1,0,0);
- /* AVANT BRAS */
- glPushMatrix();
- glScalef(2,1,1);
- glutSolidCube(1.0);
- glPopMatrix();
- /* MAINS */
- glTranslatef(1,0,0);
- glScalef(1.5,0.2,0.5);
- glutSolidCube(1.0);
- /* DOIGTS*/
- glPushMatrix();
- glTranslatef(0.6,0,0.5);
- glPushMatrix();
- glScalef(0.3,0.2,0.2);
- glRotatef(50, 0, 5, 1);
- glutSolidCube(1.0);
- glPopMatrix();
- glTranslatef(0,0,-0.9);
- glPushMatrix();
- glScalef(0.3,0.2,0.2);
- glRotatef(50, 0, 1, 5);
- glutSolidCube(1.0);
- glPopMatrix();
- glTranslatef(0,0,0.7);
- glPushMatrix();
- glScalef(0.3,0.2,0.2);
- glRotatef(50, 0, 2, 3);
- glutSolidCube(1.0);
- glPopMatrix();
- glTranslatef(0,0,-0.4);
- glPushMatrix();
- glScalef(0.3,0.2,0.2);
- glRotatef(50, 0, 2, 3);
- glutSolidCube(1.0);
- glPopMatrix();
- glTranslatef(0,0,-0.4);
- glPushMatrix();
- glScalef(0.3,0.2,0.2);
- glRotatef(180, 0, 3, 5);
- glutSolidCube(1.0);
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- /* FIN BRAS DROIT */
- /* BRAS GAUCHE*/
- glPushMatrix();
- /* PREMIERE EPAULE HAUTE */
- glTranslatef(0, -3, -1.5);
- glRotatef(epaule, 0.0, 0.0, 1);
- glutSolidSphere(0.6, 10.0, 10.0);
- glTranslatef(1,0,0);
- /* PREMIER COUDE HAUT */
- glPushMatrix();
- glScalef(2,1,1);
- glRotatef(250,1,1,1);
- glutSolidCube(1.0);
- glPopMatrix();
- /* PREMIERE ROTULE MILIEU */
- glTranslatef(1,0,0);
- glRotatef(coude, 0.0, 0.0, 1);
- glutSolidSphere(0.6, 10.0, 10.0);
- glTranslatef(1,0,0);
- /* AVANT BRAS */
- glPushMatrix();
- glScalef(2,1,1);
- glutSolidCube(1.0);
- glPopMatrix();
- /* MAINS */
- glTranslatef(1,0,0);
- glutSolidSphere(0.6, 10.0, 10.0);
- glPopMatrix();
- /*-------------------- BRAS FIN---------------------*/
- glPopMatrix();
- glPopMatrix();
- /*----------------ENSEMBLE HUMAIN FIN-----------------------*/
- glutSwapBuffers();
- /* On force l'affichage */
- glFlush();
- }
- /* Fonction de mise à jour: mouvements des objets*/
- void update(int value){
- /*epaule += 0.2;
- if (epaule > 360){
- epaule -= 360;
- }
- */
- if( counterb > 1.5 ){
- counterb = 0.0;
- }
- counterb += 0.1;
- if (counterb > 1 ){
- bounceman -= 0.15;
- }else{
- bounceman += 0.1;
- }
- if( counter > 180 ){
- counter = 0.0;
- }
- counter += 0.2;
- if (counter > 90 && counter < 180 ){
- coude -= 0.2;
- }else{
- coude += 0.2;
- }
- glutPostRedisplay();
- glutTimerFunc(10,update, 0);
- }
- /* Mise en forme de la scène pour l'affichage */
- void reshape(int w, int h){
- glViewport(0, 0,(GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 1.0, 200.0);
- }
- /* Fonction de gestion au clavier des activations des lumières */
- void keyboard(unsigned char key, int x, int y) {
- switch (key){
- case 'a': /* activation lumière n°0 */
- alpha += 0.2;
- // printf(" alpha : %f ",alpha);
- if(alpha > (2*3.14-0.02)){
- alpha = 0.0;
- }
- glutPostRedisplay();
- break;
- case 'b': /* activation lumière n°1*/
- alpha -= 0.2;
- if(alpha > (2*3.14-0.02)){
- alpha = 0.0;
- }
- glutPostRedisplay();
- break;
- case 'c': /* activation lumière n°2*/
- R += 2;
- glutPostRedisplay();
- break;
- case 'e': /* activation des lumières */
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
- glutPostRedisplay();
- break;
- case 'f': /* désactivation des lumières */
- glDisable(GL_LIGHTING);
- glutPostRedisplay();
- break;
- case 't':
- alpha += 0.2;
- if(alpha > (2*M_PI-0.02)) alpha = 0.0;
- glutPostRedisplay();
- break;
- case 'g':
- alpha -= 0.2;
- if(alpha > (2*M_PI-0.02)) alpha = 0.0;
- glutPostRedisplay();
- break;
- case 'z':
- if(R > 2)
- R -= 1;
- glutPostRedisplay();
- break;
- case 's':
- R += 1;
- glutPostRedisplay();
- break;
- case 'p':
- b += 0.1;
- glutPostRedisplay();
- break;
- case 'n':
- zm += 1;
- glutPostRedisplay();
- break;
- case 'j':
- zm -= 1;
- glutPostRedisplay();
- break;
- case 'q': /* Quitter le programme */
- exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement