Des objets créés par application de transformations géométriques sur l'objet canonique:
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Decembre 2001 */
/* Creation d'un objet dans une scene */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleManipulateur.h"
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"
#include "ModuleReshape.h"
static int type = 0 ;
static int affi = 1 ;
static int face = 18 ;
static float t[3][4];
static int tt[3] = { 0,0,0 };
static int f1;
static int f2;
static int param = 2;
static int pparam = 0;
static int nbparam[3] = { 1,1,1 };
void axes(void) {
setFont(GLUT_BITMAP_8_BY_13,CENTER);
glColor4fv(couleurRouge());
flecheEnVolume(1.0F,0.0F,0.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurVert());
flecheEnVolume(0.0F,1.0F,0.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurBleu());
flecheEnVolume(0.0F,0.0F,1.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurRougeFonce());
bitmapStringOutput(1.3F,0.0F,0.0F,"x");
glColor4fv(couleurVertFonce());
bitmapStringOutput(0.0F,1.3F,0.0F,"y");
glColor4fv(couleurBleuFonce());
bitmapStringOutput(0.0F,0.0F,1.3F,"z");
setFont(GLUT_BITMAP_8_BY_13,LEFT);
}
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 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 cone(void) {
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()) ;
if ( affi == 0 )
glutSolidCone(1.0,1.0,3*face,3*face) ;
else
glutWireCone(1.0,1.0,face,face) ;
glPopMatrix();
glDisable(GL_LIGHTING);
}
void sphere(void) {
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurVert()) ;
if ( affi == 0 )
glutSolidSphere(1.0,3*face,3*face) ;
else
glutWireSphere(1.0,face,face) ;
glPopMatrix();
glDisable(GL_LIGHTING);
}
void cylindre(void) {
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()) ;
if ( affi == 0 )
solidCylindre(1.0,1.0,3*face) ;
else
wireCylindre(1.0,1.0,face) ;
glPopMatrix();
glDisable(GL_LIGHTING);
}
void cube(void) {
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune()) ;
if ( affi == 0 )
glutSolidCube(1.0) ;
else
glutWireCube(1.0) ;
glPopMatrix();
glDisable(GL_LIGHTING);
}
void objetsIllumines(int type) {
switch (type) {
case 0 : cube();
break;
case 1 : cylindre();
break;
case 2 : sphere();
break;
case 3 : cone(); }
}
void display2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor4fv(couleurJaune());
glBegin(GL_LINE_LOOP);
glVertex2f(2.0F,5.0F+param*20.0F);
glVertex2f(418.0F,5.0F+param*20.0F);
glVertex2f(418.0F,25.0F+param*20.0F);
glVertex2f(2.0F,25.0F+param*20.0F);
glEnd();
glColor4fv(couleurRouge());
glBegin(GL_LINE_LOOP);
glVertex2f(138.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(206.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(206.0F+70.0*pparam,24.0F+param*20.0F);
glVertex2f(138.0F+70.0*pparam,24.0F+param*20.0F);
glEnd();
glPopMatrix();
glPushMatrix();
for ( int pos = 0 ; pos < 3 ; pos++ ) {
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
if ( tt[pos] == 0 ) {
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13,"IDENTITE"); }
else {
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F);
switch (tt[pos]) {
case 1 : simpleBitmapOutput(REGULAR8x13,"TRANSLATION : ");
break;
case 2 : simpleBitmapOutput(REGULAR8x13,"ROTATION : %6.2f",t[pos][3]);
break;
case 3 : simpleBitmapOutput(REGULAR8x13,"SCALE : ");
break; }
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13," %6.1f %6.2f %6.2f",t[pos][0],t[pos][1],t[pos][2]); } }
glPopMatrix();
glutSwapBuffers();
}
void display() {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glShadeModel(GL_SMOOTH);
axes();
for ( int i = 2 ; i >= 0 ; i-- )
switch ( tt[i] ) {
case 1 : glTranslatef(t[i][0],t[i][1],t[i][2]);
break;
case 2 : glRotatef(t[i][0],t[i][1],t[i][2],t[i][3]);
break;
case 3 : glScalef(t[i][0],t[i][1],t[i][2]);
break; }
objetsIllumines(type);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void special(int k,int x,int y) {
switch (k) {
case GLUT_KEY_UP : param = (param+1)%3;
pparam = pparam%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_DOWN : param = (param+2)%3;
pparam = pparam%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_RIGHT : pparam = (pparam+1)%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_LEFT : pparam = (pparam+nbparam[param]-1)%nbparam[param];
glutPostWindowRedisplay(f2);
break; }
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case '8' : special(GLUT_KEY_UP,x,y);
break;
case '2' : special(GLUT_KEY_DOWN,x,y);
break;
case '4' : special(GLUT_KEY_LEFT,x,y);
break;
case '6' : special(GLUT_KEY_RIGHT,x,y);
break;
case 'i' :
case 'I' : if ( tt[param] != 0 ) {
tt[param] = 0 ; }
nbparam[param] = 1;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 't' :
case 'T' : if ( tt[param] != 1 ) {
tt[param] = 1 ;
t[param][0] = t[param][1] = t[param][2] = 0.0F ; }
nbparam[param] = 3;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 'r' :
case 'R' : if ( tt[param] != 2 ) {
tt[param] = 2 ;
t[param][3] = 1.0F ;
t[param][0] = t[param][1] = t[param][2] = 0.0F ; }
nbparam[param] = 4;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 's' :
case 'S' : if ( tt[param] != 3 ) {
tt[param] = 3 ;
t[param][0] = t[param][1] = t[param][2] = 1.0F ; }
nbparam[param] = 3;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case ' ' : affi = 1 - affi;
glutPostWindowRedisplay(f1);
break;
case 'd' : face++;
glutPostWindowRedisplay(f1);
break;
case 'D' : face--;
if ( face < 3 )
face = 3;
glutPostWindowRedisplay(f1);
break;
case 43 : if ( ( tt[param] == 2 ) && ( pparam == 0 ) )
t[param][pparam] += 1.0F;
else
t[param][pparam] += 0.1F;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 45 : if ( ( tt[param] == 2 ) && ( pparam == 0 ) )
t[param][pparam] -= 1.0F;
else {
t[param][pparam] -= 0.1F;
if ( tt[param] == 3 )
if ( t[param][pparam] < 0.0F )
t[param][pparam] = 0.0F; }
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case '#' : if ( glutGetWindow() == f1 ) {
static int fullScreen = 0;
static int tx;
static int ty;
static int px;
static int py;
fullScreen = !fullScreen;
if ( fullScreen ) {
px = glutGet(GLUT_WINDOW_X);
py = glutGet(GLUT_WINDOW_Y);
tx = glutGet(GLUT_WINDOW_WIDTH);
ty = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen(); }
else {
glutPositionWindow(px,py);
glutReshapeWindow(tx,ty); } }
break;
case 0x0D : type = (type+1)%4;
glutPostWindowRedisplay(f1);
break;
case 0x1B : exit(0);
break; }
}
void reshape2(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,250);
glutInitWindowPosition(50,50);
f1 = glutCreateWindow("Creation d'objets");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-2.5F,2.5F,-2.5F,2.5F,-500.0,500.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(20.0F,-45.0F,0.0F) ;
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutInitWindowSize(420,70);
glutInitWindowPosition(400,150);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
myinit();
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutMainLoop();
return(0);
}