Le source : CubesEtCylindres.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Septembre 2007 */
/* Un programme OpenGL */
/* de dessin de cubes et de cylindres */
/* au moyen de primitives graphiques */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleAxes.h"
#ifndef M_PI
#define M_PI 3.14159
#endif
/* Variables globales */
static int l = 1;
static int ff = 0;
static int sc = 0;
static int n = 18;
static float zoom = 1.0F;
static float px = 0.0F;
static float py = 0.0F;
static float pz = 0.0F;
static float rx = 0.0F;
static float ry = 0.0F;
static int clic = 0;
static int mx;
static int my;
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
const GLfloat l_pos[] = { 1.0F,1.0F,1.0F,0.0F };
const GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,c);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glEnable(GL_LIGHT0);
glColor4fv(c) ;
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
/* Dessin d'un cube sans specification */
/* des normales */
void mySolidCubeSansNormales(float c){
c /= 2.0F;
glPushMatrix();
glBegin(GL_QUADS);
{ glVertex3f(c,c,-c);
glVertex3f(c,-c,-c);
glVertex3f(-c,-c,-c);
glVertex3f(-c,c,-c); }
{ glVertex3f(c,c,c);
glVertex3f(-c,c,c);
glVertex3f(-c,-c,c);
glVertex3f(c,-c,c); }
{ glVertex3f(-c,c,-c);
glVertex3f(-c,-c,-c);
glVertex3f(-c,-c,c);
glVertex3f(-c,c,c); }
{ glVertex3f(c,c,c);
glVertex3f(c,-c,c);
glVertex3f(c,-c,-c);
glVertex3f(c,c,-c); }
{ glVertex3f(-c,-c,c);
glVertex3f(-c,-c,-c);
glVertex3f(c,-c,-c);
glVertex3f(c,-c,c); }
{ glVertex3f(c,c,c);
glVertex3f(c,c,-c);
glVertex3f(-c,c,-c);
glVertex3f(-c,c,c); }
glEnd();
glPopMatrix();
}
/* Dessin d'un cube avec specification */
/* des normales */
void mySolidCubeAvecNormales(float c) {
c /= 2.0F;
glPushMatrix();
glBegin(GL_QUADS);
{ glNormal3f(0.0F,0.0F,-1.0F);
glVertex3f(c,c,-c);
glVertex3f(c,-c,-c);
glVertex3f(-c,-c,-c);
glVertex3f(-c,c,-c); }
{ glNormal3f(0.0F,0.0F,1.0F);
glVertex3f(c,c,c);
glVertex3f(-c,c,c);
glVertex3f(-c,-c,c);
glVertex3f(c,-c,c); }
{ glNormal3f(-1.0F,0.0F,0.0F);
glVertex3f(-c,c,-c);
glVertex3f(-c,-c,-c);
glVertex3f(-c,-c,c);
glVertex3f(-c,c,c); }
{ glNormal3f(1.0F,0.0F,0.0F);
glVertex3f(c,c,c);
glVertex3f(c,-c,c);
glVertex3f(c,-c,-c);
glVertex3f(c,c,-c); }
{ glNormal3f(0.0F,-1.0F,0.0F);
glVertex3f(-c,-c,c);
glVertex3f(-c,-c,-c);
glVertex3f(c,-c,-c);
glVertex3f(c,-c,c); }
{ glNormal3f(0.0F,1.0F,0.0F);
glVertex3f(c,c,c);
glVertex3f(c,c,-c);
glVertex3f(-c,c,-c);
glVertex3f(-c,c,c); }
glEnd();
glPopMatrix();
}
/* Dessin d'un cylindre sans specification */
/* des normales */
void mySolidCylinderSansNormales(float r,float h,int n){
float *cs =(float *) calloc(n,sizeof(float));
float *sn =(float *) calloc(n,sizeof(float));
int i;
for( i = 0 ; i < n ; i++ ){
float a = (2*M_PI*i)/n;
sn[i] = sin(a);
cs[i] = cos(a); }
glPushMatrix();
glBegin(GL_QUAD_STRIP);
for( i = 0 ; i <= n ; i++ ){
float x = r*cs[i%n];
float z = -r*sn[i%n];
glVertex3f(x,h/2,z);
glVertex3f(x,-h/2,z); }
glEnd();
glBegin(GL_POLYGON);
for( i = 0 ; i < n ; i++ ){
float si = -sn[i];
float ci = cs[i];
glVertex3f(r*ci,h/2,r*si); }
glEnd();
glBegin(GL_POLYGON);
for( i = 0 ; i < n ; i++ ){
float si = sn[i];
float ci = cs[i];
glVertex3f(r*ci,-h/2,r*si); }
glEnd();
free(cs);
free(sn);
glPopMatrix();
}
/* Dessin d'un cylindre avec specification */
/* des normales */
void mySolidCylinderAvecNormales(float r,float h,int n){
float *cs =(float *) calloc(n,sizeof(float));
float *sn =(float *) calloc(n,sizeof(float));
int i;
for( i = 0 ; i < n ; i++ ){
float a = (2*M_PI*i)/n;
sn[i] = sin(a);
cs[i] = cos(a); }
glPushMatrix();
glBegin(GL_QUAD_STRIP);
for( i = 0 ; i <= n ; i++ ){
float x = r*cs[i%n];
float z = -r*sn[i%n];
glNormal3f(cs[i%n],0.0F,-sn[i%n]);
glVertex3f(x,h/2,z);
glVertex3f(x,-h/2,z); }
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0F,1.0F,0.0F);
for( i = 0 ; i < n ; i++ ){
float si = -sn[i];
float ci = cs[i];
glVertex3f(r*ci,h/2,r*si); }
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0F,-1.0F,0.0F);
for( i = 0 ; i < n ; i++ ){
float si = sn[i];
float ci = cs[i];
glVertex3f(r*ci,-h/2,r*si); }
glEnd();
free(cs);
free(sn);
glPopMatrix();
}
/* Scene dessinee */
void scene(void) {
glPushMatrix();
glPushMatrix();
glTranslatef(1.0F,1.0F,0.0F);
mySolidCubeAvecNormales(1.0F);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0F,-1.0F,0.0F);
mySolidCubeSansNormales(1.0F);
glPopMatrix();
glPushMatrix();
glTranslatef(1.0F,-1.0F,0.0F);
mySolidCylinderAvecNormales(0.5F,1.0F,n);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0F,1.0F,0.0F);
mySolidCylinderSansNormales(0.5F,1.0F,n);
glPopMatrix();
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClearColor(0.6F,0.6F,0.6F,1.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
if ( l )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
switch (ff) {
case 0 : glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
break;
case 1 : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
break;
case 2 : glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK,GL_FILL);
break; }
glPushMatrix();
{ glTranslatef(px,py,pz);
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
glScalef(zoom,zoom,zoom); }
glScalef(0.45F,0.45F,0.45F);
axes();
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
/* Fonction executee lors de la frappe */
/* d'une touche alphanumerique du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case 'x' : px -= zoom/50.0F;
glutPostRedisplay();
break;
case 'X' : px += zoom/50.0F;
glutPostRedisplay();
break;
case 'y' : py -= zoom/50.0F;
glutPostRedisplay();
break;
case 'Y' : py += zoom/50.0F;
glutPostRedisplay();
break;
case 'z' : pz -= 0.1F;
glutPostRedisplay();
break;
case 'Z' : pz += 0.1F;
glutPostRedisplay();
break;
case 43 : n++;
glutPostRedisplay();
break;
case 45 : n--;
if ( n < 3 )
n = 3;
glutPostRedisplay();
break;
case 's' :
case 'S' : sc = (sc+1)%2 ;
glutPostRedisplay();
break;
case 0x0D : l = !l ;
glutPostRedisplay();
break;
case 0x20 : ff = (ff+1)%3 ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
/* Fonction executee lors de la frappe */
/* d'une touche non alphanumerique du clavier */
void special(int key,int x,int y) {
switch ( key ) {
case GLUT_KEY_PAGE_UP : zoom *= 1.01F;
glutPostRedisplay();
break;
case GLUT_KEY_PAGE_DOWN : zoom /= 1.01F;
glutPostRedisplay();
break;
case GLUT_KEY_UP : rx -= 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : rx += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT : ry += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : ry -= 1.0F;
glutPostRedisplay();
break; }
}
/* Fonction executee lors d'un clic de souris */
/* dans la fenetre */
void mouse(int bouton,int etat,int x,int y) {
if ( bouton == GLUT_LEFT_BUTTON ) {
if ( etat == GLUT_DOWN ) {
clic = 1;
mx = x;
my = y; }
if ( etat == GLUT_UP ) {
clic = 0; } }
}
/* Fonction executee lors d'un deplacement */
/* de la souris sur la fenetre */
/* avec un bouton appuye */
void motion(int x,int y) {
if ( clic ) {
ry += (x-mx);
rx += (y-my);
mx = x;
my = y;
glutPostRedisplay(); }
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Configuration d'une camera de visualisation */
/* en projection orthographique */
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((double) -tx/ty,(double) tx/ty,-1.0,1.0,-5.0,5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(350,350);
glutInitWindowPosition(50,50);
glutCreateWindow("Cubes et cylindres");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}