#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glaux.h> #include <stdio.h> #include <stdlib.h> #include <math.h> static float anglex = 0.0F ; static float angley = 0.0F ; static int type = 0 ; void cylindre(double h,double r,int n) { double *cs =(double *) calloc(n+1,sizeof(double)) ; double *sn =(double *) calloc(n+1,sizeof(double)) ; double *ncs =(double *) calloc(n+1,sizeof(double)) ; double *nsn =(double *) calloc(n+1,sizeof(double)) ; cs[0] = cs[n] = r ; sn[0] = sn[n] = 0.0 ; ncs[0] = ncs[n] = 1.0 ; nsn[0] = nsn[n] = 0.0 ; for ( int i = 1 ; i < n ; i++ ) { double a = 3.14159/n*2*i ; ncs[i] = cos(a) ; nsn[i] = sin(a) ; cs[i] = r*ncs[i] ; sn[i] = r*nsn[i] ; } glBegin(GL_POLYGON) ; glNormal3f(0.0,1.0,0.0) ; for ( i = 0 ; i < n ; i++ ) { glVertex3d(cs[i],h/2,sn[i]); } glEnd() ; glBegin(GL_POLYGON) ; glNormal3f(0.0,-1.0,0.0) ; for ( i = 0 ; i < n ; i++ ) { glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; glBegin(GL_QUADS) ; for ( i = 0 ; i < n ; i++ ) { glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],h/2,sn[i]); glNormal3d(ncs[i+1],0.0,nsn[i+1]) ; glVertex3d(cs[i+1],h/2,sn[i+1]); glVertex3d(cs[i+1],-h/2,sn[i+1]); glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; free(sn) ; free(cs) ; free(nsn) ; free(ncs) ; } void cone(double h,double r,int n) { double *cs =(double *) calloc(n+1,sizeof(double)) ; double *sn =(double *) calloc(n+1,sizeof(double)) ; double *ncs =(double *) calloc(n+1,sizeof(double)) ; double *nsn =(double *) calloc(n+1,sizeof(double)) ; cs[0] = cs[n] = r ; sn[0] = sn[n] = 0.0 ; ncs[0] = ncs[n] = 1.0 ; nsn[0] = nsn[n] = 0.0 ; for ( int i = 1 ; i < n ; i++ ) { double a = 3.14159/n*2*i ; ncs[i] = cos(a) ; nsn[i] = sin(a) ; cs[i] = r*ncs[i] ; sn[i] = r*nsn[i] ; } glBegin(GL_POLYGON) ; glNormal3f(0.0,-1.0,0.0) ; for ( i = 0 ; i < n ; i++ ) { glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; glBegin(GL_QUADS) ; for ( i = 0 ; i < n ; i++ ) { glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(0,h/2,0); glNormal3d(ncs[i+1],0.0,nsn[i+1]) ; glVertex3d(0,h/2,0); glVertex3d(cs[i+1],-h/2,sn[i+1]); glNormal3d(ncs[i],0.0,nsn[i]) ; glVertex3d(cs[i],-h/2,sn[i]); } glEnd() ; glEnd() ; free(sn) ; free(cs) ; free(nsn) ; free(ncs) ; } void CALLBACK display(void) { glClearColor(0.5F,0.5F,0.5F,0.0F); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angley,0.0F,1.0F,0.0F) ; glRotatef(anglex,1.0F,0.0F,0.0F) ; if ( type ) { float jaune[] = { 1.0F,1.0F,0.5F,1.0F }; float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,bleu) ; glTranslatef(0.5F,0.7F,0.0F) ; cylindre(0.8,0.3,40) ; float rouge[] = { 1.0F,0.2F,0.2F,1.0F }; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,rouge) ; glTranslatef(0.0F,-1.1F,0.0F) ; cone(0.8,0.4,40) ; float vert[] = { 0.2F,1.0F,0.2F,1.0F }; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,vert) ; glTranslatef(-1.0F,0.0F,0.0F) ; auxSolidSphere(0.4) ; float cyan[] = { 1.0F,0.2F,1.0F,1.0F }; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,cyan) ; glTranslatef(0.0F,1.1F,0.0F) ; auxSolidCube(0.7) ; } else { glScalef(0.15F,0.15F,0.15F) ; float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; glNormal3f(0.0F,0.0F,1.0F) ; glBegin(GL_QUADS) ; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,bleu) ; glVertex3f(-7.0F,2.0F,1.0F) ; glVertex3f(-1.0F,7.0F,-1.0F) ; glVertex3f(2.0F,6.0F,1.0F) ; glVertex3f(-6.0F,-3.0F,3.0F) ; glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,rouge) ; glVertex3f(3.0F,-8.0F,-1.0F) ; glVertex3f(7.0F,-5.0F,-2.0F) ; glVertex3f(9.0F,2.0F,3.0F) ; glVertex3f(1.0F,-3.0F,-2.0F) ; glEnd() ; } glPopMatrix(); glFlush(); auxSwapBuffers(); } void myinit (void) { glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); float jaune[] = { 1.0F,1.0F,1.0F,1.0F }; glLightfv(GL_LIGHT0,GL_DIFFUSE,jaune) ; } void CALLBACK myReshape(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.3,1.3,-1.3/w*h,1.3/w*h,-3.0,3.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void CALLBACK up(void) { anglex++ ; } void CALLBACK down(void) { anglex-- ; } void CALLBACK left(void) { angley++ ; } void CALLBACK right(void) { angley-- ; } void CALLBACK enter(void) { type = 1 - type ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); auxInitPosition (0,0,300,300); auxInitWindow("Objets") ; myinit(); auxKeyFunc(AUX_UP,up) ; auxKeyFunc(AUX_DOWN,down) ; auxKeyFunc(AUX_LEFT,left) ; auxKeyFunc(AUX_RIGHT,right) ; auxKeyFunc(AUX_RETURN,enter) ; auxReshapeFunc(myReshape); auxMainLoop(display); }
RETOUR