/* Examen de TD n°1 2012-2023 */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2012 */ #include #include #include #include #include #include #include "Modules/ModuleAxes.h" /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static int question = 0; static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ static void init(void) { const GLfloat shininess[] = { 50.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,blanc); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune); glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); } ////////////////////////////////////////////////// /* Variables et constantes globales question 2 */ #ifndef M_PI #define M_PI 3.14159 #endif /* Question 2a */ static void cercle(void) { int n = 10000; glPushMatrix(); glBegin(GL_LINE_LOOP); for ( int i = 0 ; i < n ; i++ ) { double a = i*2.0*M_PI/n; float x = 10.0*cos(a); float y = 10.0*sin(a); glVertex2f(x,y); } glEnd(); glPopMatrix(); } /* Question 2b */ static void enroulementToroidal(void) { int n = 10000; int m = 50; glPushMatrix(); glBegin(GL_LINE_LOOP); for ( int i = 0 ; i < n ; i++ ) { double a1 = i*2.0*M_PI/n; float x = (10.0+sin(m*a1))*cos(a1); float y = (10.0+sin(m*a1))*sin(a1); float z = cos(m*a1); glVertex3f(x,y,z); } glEnd(); glPopMatrix(); } /* Scene question 3a */ static void sceneQuestion3a(void) { glPushMatrix(); glutSolidTorus(0.3,10.0,18,72); glPushMatrix(); glTranslatef(10.0F,0.0F,0.0F); glutSolidSphere(1.0,36,36); glPopMatrix(); glPushMatrix(); glTranslatef(-10.0F,0.0F,0.0F); glutSolidSphere(1.0,36,36); glPopMatrix(); glPopMatrix(); } /* Variables et constantes globales question 3 */ static float r1 = 0.0F; static float r2 = 180.0F; /* Scene questions 3b et 3c */ static void sceneQuestion3bEt3c(void) { glPushMatrix(); glutSolidTorus(0.3,10.0,18,72); glPushMatrix(); glRotatef(r1,0.0F,0.0F,1.0F); glTranslatef(10.0F,0.0F,0.0F); glutSolidSphere(1.0,36,36); glPopMatrix(); glPushMatrix(); glRotatef(r2,0.0F,0.0F,1.0F); glTranslatef(10.0F,0.0F,0.0F); glutSolidSphere(1.0,36,36); glPopMatrix(); glPopMatrix(); } /* Question 3b */ /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ static void idle3b(void) { r1 += 1.0F; r2 += -3.0F; glutPostRedisplay(); } /* Variables et constantes globales question 3 */ static float v1 = 1.0F; static float v2 = -3.0F; /* Question 3c */ /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ static void idle3c(void) { static const float deltaMin = acos(0.98)*180.0/3.14159; float delta = (r2+v2)-(r1+v1); if ( ( delta < deltaMin ) || ( delta > 360.0-deltaMin ) ) { float dt; if ( delta < deltaMin ) dt = fabs((r2-r1-deltaMin)/(v1-v2)); else dt = fabs((r1-r2+360.0F-deltaMin)/(v1-v2)); r1 += (2.0F*v1*dt-v1); r2 += (2.0F*v2*dt-v2); v1 = -v1; v2 = -v2; } else { r1 += v1; r2 += v2; } glutPostRedisplay(); } /* Question 4 */ /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ static void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if ( tx >= ty ) glOrtho(-12.0*(double) tx/ty,12.0*(double) tx/ty, -12.0,12.0, -12.0,12.0); else glOrtho(-12.0,12.0, -12.0*(double) ty/tx,12.0*(double) ty/tx, -12.0,12.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } ////////////////////////////////////////////////// /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClearColor(0.75F,0.75F,0.75F,1.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const GLfloat light0_position[] = { 0.0,0.0,0.0,1.0 }; const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 }; const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); glLineWidth(2.0); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glRotatef(rz,0.0F,0.0F,1.0F); dessinAxes(); switch (question) { case 0 : cercle(); break; case 1 : enroulementToroidal(); break; case 2 : sceneQuestion3a(); break; case 3 : case 4 : sceneQuestion3bEt3c(); break; } glPopMatrix(); glLineWidth(1.0); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention erreur %d\n",error); } /* Fonction executee lors de la frappe */ /* d'une touche du special clavier */ static void special(int code,int x,int y) { switch ( code ) { case GLUT_KEY_UP : rx += 1.0F; glutPostRedisplay(); break; case GLUT_KEY_DOWN : rx -= 1.0F; glutPostRedisplay(); break; case GLUT_KEY_LEFT : ry += 1.0F; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : ry -= 1.0F; glutPostRedisplay(); break; case GLUT_KEY_PAGE_UP : rz += 1.0F; glutPostRedisplay(); break; case GLUT_KEY_PAGE_DOWN : rz -= 1.0F; glutPostRedisplay(); break; } } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ static void keyboard(unsigned char key,int x,int y) { switch (key) { case 0x0D : question = (question+1)%5; switch (question) { case 0 : case 1 : case 2 : glutIdleFunc(NULL); break; case 3 : r1 = 0.0F; r2 = 180.0F; v1 = 1.0F; v2 = -3.0F; glutIdleFunc(idle3b); break; case 4 : r1 = 0.0F; r2 = 180.0F; v1 = 1.0F; v2 = -3.0F; glutIdleFunc(idle3c); break; } glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Examen de TD n°1 2012-2013"); init(); glutSpecialFunc(special); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }