/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Illustration de la modelisation */
/* de scenes */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleReshape.h"
static int type = 0 ;
static int lumi = 0 ;
static int mode = 0 ;
static int affi = 0 ;
static int face = 20 ;
static int maille = 0 ;
void wireCylindre(double r,double h,int n) {
int i;
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 ( 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_LINE_LOOP) ;
glNormal3f(0.0,1.0,0.0) ;
for ( i = 0 ; i < n ; i++ ) {
glVertex3d(cs[i],h/2,sn[i]); }
glEnd() ;
glBegin(GL_LINE_LOOP) ;
glNormal3f(0.0,-1.0,0.0) ;
for ( i = 0 ; i < n ; i++ ) {
glVertex3d(cs[i],-h/2,sn[i]); }
glEnd() ;
for ( i = 0 ; i < n ; i++ ) {
glBegin(GL_LINE_LOOP) ;
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 solidCylindre(double r,double h,int n) {
int i;
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 ( 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 r,double h,int n) {
int i;
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 ( 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 objetsIllumines() {
glPushMatrix();
glScalef(3.0F,3.0F,3.0F) ;
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()) ;
glTranslatef(0.5F,0.1F,0.0F) ;
glRotatef(-90.0f,1.0f,0.0f,0.0f);
if ( affi == 0 )
glutSolidCone(0.4,0.8,face,face) ;
else
glutWireCone(0.4,0.8,face,face) ;
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurVert()) ;
glTranslatef(-0.5F,0.5F,0.0F) ;
if ( affi == 0 )
glutSolidSphere(0.4,face,face) ;
else
glutWireSphere(0.4,face,face) ;
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurCyan()) ;
glTranslatef(0.5F,-0.5F,0.0F) ;
if ( affi == 0 )
glutSolidCube(0.7) ;
else
glutWireCube(0.7) ;
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()) ;
glTranslatef(-0.5F,-0.5F,0.0F) ;
if ( affi == 0 )
solidCylindre(0.3,0.8,face) ;
else
wireCylindre(0.3,0.8,face) ;
glPopMatrix();
glPopMatrix();
glDisable(GL_LIGHTING);
}
void facettesIlluminees() {
glEnable(GL_LIGHTING);
glPushMatrix();
glScalef(0.4F,0.4F,0.4F) ;
glBegin(GL_QUADS) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()) ;
glNormal3f(0.0F,0.0F,1.0F) ;
glVertex3f(-7.0F,2.0F,1.0F) ;
glNormal3f(0.0F,1.0F,1.0F) ;
glVertex3f(-1.0F,7.0F,-1.0F) ;
glNormal3f(1.0F,0.0F,1.0F) ;
glVertex3f(2.0F,6.0F,1.0F) ;
glNormal3f(1.0F,1.0F,1.0F) ;
glVertex3f(-6.0F,-3.0F,3.0F) ;
glEnd() ;
glBegin(GL_QUADS) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()) ;
glNormal3f(1.0F,0.0F,1.0F) ;
glVertex3f(3.0F,-8.0F,-1.0F) ;
glNormal3f(0.0F,0.0F,1.0F) ;
glVertex3f(7.0F,-5.0F,-2.0F) ;
glNormal3f(1.0F,1.0F,1.0F) ;
glVertex3f(9.0F,2.0F,3.0F) ;
glNormal3f(0.0F,1.0F,1.0F) ;
glVertex3f(1.0F,-3.0F,-2.0F) ;
glEnd() ;
glPopMatrix();
glDisable(GL_LIGHTING);
}
void objetsNonIllumines() {
glPushMatrix();
glScalef(3.0F,3.0F,3.0F) ;
glPushMatrix();
glColor4fv(couleurRouge()) ;
glTranslatef(0.5F,0.1F,0.0F) ;
glRotatef(-90.0f,1.0f,0.0f,0.0f);
if ( affi == 0 )
glutSolidCone(0.4,0.8,face,face) ;
else
glutWireCone(0.4,0.8,face,face) ;
glPopMatrix();
glPushMatrix();
glColor4fv(couleurVert()) ;
glTranslatef(-0.5F,0.5F,0.0F) ;
if ( affi == 0 )
glutSolidSphere(0.4,face,face) ;
else
glutWireSphere(0.4,face,face) ;
glPopMatrix();
glPushMatrix();
glColor4fv(couleurCyan()) ;
glTranslatef(0.5F,-0.5F,0.0F) ;
if ( affi == 0 )
glutSolidCube(0.7) ;
else
glutWireCube(0.7) ;
glPopMatrix();
glPushMatrix();
glColor4fv(couleurBleu()) ;
glTranslatef(-0.5F,-0.5F,0.0F) ;
if ( affi == 0 )
solidCylindre(0.3,0.8,face) ;
else
wireCylindre(0.3,0.8,face) ;
glPopMatrix();
glPopMatrix();
}
void facettesNonIlluminees() {
glPushMatrix();
glScalef(0.4F,0.4F,0.4F) ;
glBegin(GL_QUADS) ;
glColor4fv(couleurBleu()) ;
glVertex3f(-7.0F,2.0F,1.0F) ;
glColor4fv(couleurRouge()) ;
glVertex3f(-1.0F,7.0F,-1.0F) ;
glColor4fv(couleurVert()) ;
glVertex3f(2.0F,6.0F,1.0F) ;
glColor4fv(couleurNoir()) ;
glVertex3f(-6.0F,-3.0F,3.0F) ;
glEnd() ;
glBegin(GL_QUADS) ;
glColor4fv(couleurJaune()) ;
glVertex3f(3.0F,-8.0F,-1.0F) ;
glColor4fv(couleurCyan()) ;
glVertex3f(7.0F,-5.0F,-2.0F) ;
glColor4fv(couleurMagenta()) ;
glVertex3f(9.0F,2.0F,3.0F) ;
glColor4fv(couleurBlanc()) ;
glVertex3f(1.0F,-3.0F,-2.0F) ;
glEnd() ;
glPopMatrix();
}
void axes() {
glPushMatrix() ;
glColor4fv(couleurJaune()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(1.0F,0.0F,0.0F) ;
glEnd() ;
placeFontCursor(1.05F,0.05F,0.05F);
simpleBitmapOutput("x");
glColor4fv(couleurCyan()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,1.0F,0.0F) ;
glEnd() ;
placeFontCursor(0.05F,1.05F,0.05F);
simpleBitmapOutput("y");
glColor4fv(couleurMagenta()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,0.0F,1.0F) ;
glEnd() ;
placeFontCursor(0.05F,0.05F,1.05F);
simpleBitmapOutput("z");
if ( maille ) {
glColor4fv(couleurGrisMoyen()) ;
for ( int i = -100 ; i < 100 ; i++ ) {
glBegin(GL_LINES) ;
glVertex3f((float) i,200.0F,0.0F) ;
glVertex3f((float) i,-200.0F,0.0F) ;
glEnd() ;
glBegin(GL_LINES) ;
glVertex3f(200.0F,(float) i,0.0F) ;
glVertex3f(-200.0F,(float) i,0.0F) ;
glEnd() ; } }
glPopMatrix() ;
}
void display() {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
axes();
if ( mode == 1 )
glShadeModel(GL_FLAT);
else
glShadeModel(GL_SMOOTH);
switch ( type + 2*lumi ) {
case 3 : objetsNonIllumines();
break;
case 2 : facettesNonIlluminees() ;
break;
case 1 : objetsIllumines();
break;
case 0 : facettesIlluminees() ; }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc()) ;
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 'r' :
case 'R' : maille = 1 - maille;
glutPostRedisplay();
break;
case 'm' :
case 'M' : mode = 1 - mode;
glutPostRedisplay();
break;
case 'a' :
case 'A' : affi = 1 - affi;
glutPostRedisplay();
break;
case ' ' : lumi = 1 - lumi;
glutPostRedisplay();
break;
case 43 : face++;
glutPostRedisplay();
break;
case 45 : face--;
if ( face < 3 )
face = 3;
glutPostRedisplay();
break;
case 0x0D : type = 1 - type;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Objets");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-3.8F,3.8F,-3.8F,3.8F,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}