Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************/
- /* TP1: primitives 2D et transformations */
- /*********************************************************/
- /* */
- /* ESGI: Algorithmiques pour l'infographies */
- /* */
- /*********************************************************/
- /* */
- /* Objectif: afficher des formes 2D et les transformer */
- /* */
- /*********************************************************/
- #include<windows.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include<stdlib.h>
- #include<stdio.h>
- #include<math.h>
- float angle = 0.0;
- float cameraAngle = 10.0;
- float X = 0;
- float Y = 0;
- float Z = 20;
- float P = 0;
- float L = 0;
- float R = Z;
- GLUquadric* cylinder = gluNewQuadric();
- /* prototypes de fonctions */
- void initRendering(); // Initialisation du rendu
- void display(); // modélisation
- void reshape(int w,int h); // visualisation
- void update(int value); // mise à jour: appelle Timer pour l'animation de la scène
- void keyboard(unsigned char key, int x, int y); // fonction clavier
- void jambe();
- void bras();
- /* Programme principal */
- int main(int argc, // argc: nombre d'arguments, argc vaut au moins 1
- char **argv){ // argv: tableau de chaines de caractères, argv[0] contient le nom du programme lancé (plus un éventuel chemin)
- /* Initialisation de glut et creation de la fenetre */
- glutInit(&argc, argv); // Initialisation
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); // mode d'affichage RGB, et test prafondeur
- glutInitWindowSize(800, 800); // dimension fenêtre
- glutInitWindowPosition (100, 100); // position coin haut gauche
- glutCreateWindow("TP1: formes 2D et transformation"); // nom
- /* Initialisation d'OpenGL */
- initRendering();
- /* Enregistrement des fonctions de rappel
- => initialisation des fonctions callback appelées par glut */
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutTimerFunc(20,update, 0);
- /* rq: le callback de fonction (fonction de rappel) est une fonction qui est passée en argument à une
- autre fonction. Ici, le main fait usage des deux fonctions de rappel (qui fonctionnent en même temps)
- alors qu'il ne les connaît pas par avance.*/
- /* Entrée dans la boucle principale de glut, traitement des évènements */
- glutMainLoop(); // lancement de la boucle de réception des évènements
- return 0; // pour finir
- }
- /* 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);
- /* définit la taille d'un pixel*/
- glPointSize(2.0);
- }
- /* Création de la scène */
- void display(){
- /* Efface les tampons de couleur et de profondeur de l'image avec la couleur de fond.
- rq: buffer: mémoire tampon, c'est donc une surface en memoire utilisée pour stocker une image*/
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- /* la mat de visualisation-modélisation est modifiable par défaut */
- glMatrixMode(GL_MODELVIEW);
- /* on charge l'identité dans la matrice de modélisation-visualisation*/
- glLoadIdentity();
- /* Permet de créer un point de vue. Définit une matrice de modélisation-visualisation et la multiplie
- à droite de lma matrice active, ici l'identité*/
- gluLookAt(X, Y, Z, // position caméra
- 0.0, 0.0, 0.0, // point de mire
- 0.0, 1.0, 0.0); // vecteur d'orientation caméra
- /* A vous de jouer */
- glPushMatrix();
- //début du corp
- glTranslatef(0, 5, 0);
- //début du torse
- glPushMatrix();
- glColor3f(1, 1, 1);
- glScalef(2, 0.9, 1.4);
- glutSolidSphere(1.0, 800.0, 800.0);
- glPopMatrix();
- //début du milieu du corp
- glRotatef(90, 1, 0, 0);
- glPushMatrix();
- glScalef(2, 1.4, 2);
- glColor3f(0.5, 1, 1);
- gluCylinder(cylinder, 1.0, 1, 0.3, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 0.6);
- glPushMatrix();
- glScalef(2, 1.4, 2);
- glColor3f(0.5, 0.5, 1);
- gluCylinder(cylinder, 1, 0.8, 2.2, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 4.4);
- glPushMatrix();
- glScalef(2, 1.4, 2);
- glColor3f(0.5, 0.5, 0.5);
- gluCylinder(cylinder, 0.8, 0.9, 1.2, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 2.4);
- glPushMatrix();
- glScalef(2, 1.4, 2);
- glColor3f(0.5, 1, 1);
- gluCylinder(cylinder, 0.9, 0.9, 0.3, 800, 800);
- glPopMatrix();
- glRotatef(-90, 1, 0, 0);
- //fin du milieu du corp
- glTranslatef(0, -0.6, 0);
- //début de la hanche
- glPushMatrix();
- glColor3f(1, 1, 1);
- glScalef(2, 0, 1.4);
- glutSolidSphere(0.9, 800.0, 800.0);
- glPopMatrix();
- glTranslatef(-0.9, 0, 0);
- glPushMatrix();
- glColor3f(0.5, 0.5, 1);
- glScalef(1, 0.7, 1);
- glutSolidSphere(0.9, 800.0, 800.0);
- glPopMatrix();
- glTranslatef(1.8, 0, 0);
- glPushMatrix();
- glColor3f(0.5, 0.5, 1);
- glScalef(1, 1, 1);
- glutSolidSphere(0.9, 800.0, 800.0);
- glPopMatrix();
- glPushMatrix();
- glRotatef(angle, 1, 0, 0);
- jambe();
- glTranslatef(-1.8, 0, 0);
- glScalef(-1.0, 1.0, 1.0);
- jambe();
- glPopMatrix();
- glTranslatef(0, 8, 0);
- bras();
- glTranslatef(-1.8, 0, 0);
- glScalef(-1.0, 1.0, 1.0);
- bras();
- glTranslatef(-0.9, 0, 0);
- glRotatef(-90, 1, 0, 0);
- glTranslatef(0, 0, 2);
- glPushMatrix();
- glColor3f(0, 0, 1);
- glutSolidSphere(1.5, 600, 600);
- glPopMatrix();
- glPopMatrix();
- /* On swap (échange) les buffers, càd, on fait passer l'image calculée et dessinée
- dans le back buffer au buffer qui va l'afficher: le front buffer (en général), c'est le bouble buffering
- Cela évite une image animée sacadée, si elle était directement tracée dans le front buffer*/
- glutSwapBuffers();
- /* On force l'affichage */
- glFlush(); // nettoie les fenêtres précédentes
- }
- /* Mise en forme de la scène pour l'affichage */
- void reshape(int w, // w: largeur fenêtre
- int h) // h: hauteur fenêtre
- {
- /* Viewport: cadrage. Spécifie la région (position et taille en px) que l'image observée de la scène occupe
- à l'écran => réduction et agrandissement de l'image possible*/
- glViewport(0, 0,(GLsizei) w, (GLsizei) h);
- /* Spécifie que la matrice de projection va être modifiée */
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity(); // matrice courante: l'identité
- //glOrtho(-2.0, 2.0, -2.0, 2.0, 1.5, 20.0);
- //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
- /* Perspective avec point de fuite */
- gluPerspective(60.0, // angle d'ouverture vertical caméra
- (GLfloat) w / (GLfloat) h, // ratio largeur-hauteur
- 1.0, // plan proche z=1
- 200.0); // plan éloigné z=200
- }
- /* Fonction de gestion du clavier */
- void keyboard(unsigned char key, // Touche qui a été pressée
- int x, int y) { // Coordonnées courante de la souris
- switch (key){
- case 'p': /* affichage du carré 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 'l': /* affichage en mode sommets seuls*/
- glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
- glutPostRedisplay();
- break;
- case 'z': /* affichage en mode sommets seuls*/
- L += 0.1;
- if(L <= 88.5){
- X = R * cos(L) * sin(P);
- Y = R * sin(L);
- Z = R * cos(L) * cos(P);
- }
- glutPostRedisplay();
- break;
- case 's': /* affichage en mode sommets seuls*/
- L -= 0.1;
- if(L >= -88.5){
- X = R * cos(L) * sin(P);
- Y = R * sin(L);
- Z = R * cos(L) * cos(P);
- }
- glutPostRedisplay();
- break;
- case 'd': /* affichage en mode sommets seuls*/
- if(P <= 180){
- P += 0.1;
- }
- X = R * cos(L) * sin(P);
- Y = R * sin(L);
- Z = R * cos(L) * cos(P);
- glutPostRedisplay();
- break;
- case 'q': /* affichage en mode sommets seuls*/
- if(P >= -180){
- P -= 0.1;
- }
- X = R * cos(L) * sin(P);
- Y = R * sin(L);
- Z = R * cos(L) * cos(P);
- glutPostRedisplay();
- break;
- case 'g': /* affichage en mode sommets seuls*/
- R += 0.1;
- X = R * cos(L) * sin(P);
- Y = R * sin(L);
- Z = R * cos(L) * cos(P);
- glutPostRedisplay();
- break;
- case 't': /* affichage en mode sommets seuls*/
- R -= 0.1;
- X = R * cos(L) * sin(P);
- Y = R * sin(L);
- Z = R * cos(L) * cos(P);
- glutPostRedisplay();
- break;
- case 'm': /* Quitter le programme */
- exit(0);
- }
- }
- void update(int value){
- angle += 0.5;
- if(angle > 360){
- angle = 0;
- }
- /*glRotatef(-cameraAngle, 0.1, -0.1, 1.0);
- gluLookAt(0.0, 0.0, 5.0, // position caméra
- 0.0, 0.0, 0.0, // point de mire
- 0.0, 1.0, 0.0);*/
- glutPostRedisplay();
- glutTimerFunc(1,update, 0);
- }
- void jambe(){
- glPushMatrix();
- glRotatef(90, 1, 0, 0);
- //début cuisse gauche
- glTranslatef(-1.8, 0, 0);
- glPushMatrix();
- glColor3f(0.5, 1, 0.5);
- glScalef(1, 1, 1);
- gluCylinder(cylinder, 0.9, 0.75, 4, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 4);
- glPushMatrix();
- glColor3f(0.5, 1, 1);
- glScalef(1, 1, 1);
- glutSolidSphere(0.75, 800.0, 800.0);
- glPopMatrix();
- glPushMatrix();
- glColor3f(0.5, 0.5, 1);
- glScalef(1, 1, 1);
- gluCylinder(cylinder, 0.75, 0.6, 3, 800, 800);
- glPopMatrix();
- glRotatef(-90, 1, 0, 0);
- glTranslatef(0, -3, 0);
- glPushMatrix();
- glColor3f(0, 0.5, 1);
- glScalef(1, 1, 1);
- glutSolidSphere(0.6, 800.0, 800.0);
- glColor3f(0, 1, 1);
- glScalef(1, 1, 1);
- gluCylinder(cylinder, 0.6, 0.3, 2, 800, 800);
- glTranslatef(0, 0, 2);
- glColor3f(0, 0.5, 1);
- glScalef(1, 1, 1.6);
- glutSolidSphere(0.3, 800.0, 800.0);
- glPopMatrix();
- glPopMatrix();
- }
- void bras(){
- glPushMatrix();
- glRotatef(90, 0, 1, 0);
- glPushMatrix();
- glTranslatef(0, 0, 0.3);
- glColor3f(1, 0, 0.5);
- gluCylinder(cylinder, 0.7, 0.7, 1.5, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 1.3);
- glPushMatrix();
- glTranslatef(0, 0, 0.5);
- glColor3f(1, 0, 1);
- glutSolidSphere(0.7, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 0.5);
- glRotatef(90, 1, 0, 0);
- glPushMatrix();
- glColor3f(1, 1, 0.5);
- gluCylinder(cylinder, 0.7, 0.7, 4, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 4);
- glPushMatrix();
- glColor3f(1, 0, 1);
- glutSolidSphere(0.7, 800, 800);
- glPopMatrix();
- glPushMatrix();
- glColor3f(1, 0.5, 1);
- gluCylinder(cylinder, 0.7, 0.6, 3.6, 800, 800);
- glPopMatrix();
- glTranslatef(0, 0, 3.6);
- glPushMatrix();
- glColor3f(0, 1, 0.5);
- glutSolidSphere(0.6, 600, 600);
- glPopMatrix();
- glTranslatef(0, 0, 0.8);
- glPushMatrix();
- glColor3f(1, 0, 1);
- glScalef(1, 1, 1.5);
- glutSolidCube(1.2);
- glPopMatrix();
- glPopMatrix();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement