Le source : MoleculeBenzene7.cpp
/* Une molecule de benzene eclairee */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2011 */
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleAxes.h"
/* Variables et constantes globales */
static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;
static int objet = 0;
static int aff = 1;
static int all = 0;
/* Fonction executee lors de la frappe */
/* d'une touche du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case 0x20 :
aff = (aff+1)%2;
glutPostRedisplay();
break;
case 'o' :
objet = (objet+1)%2;
glutPostRedisplay();
break;
case 0x1B :
exit(0);
break; }
}
/* Fonction executee lors de l'appui */
/* d'une touche de curseur ou d'une touche */
/* page up ou page down */
void special(int key,int x,int y) {
switch(key) {
case GLUT_KEY_UP :
rx++;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN :
rx--;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT :
ry++;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT :
ry--;
glutPostRedisplay();
break;
case GLUT_KEY_PAGE_UP :
rz++;
glutPostRedisplay();
break;
case GLUT_KEY_PAGE_DOWN :
rz--;
glutPostRedisplay();
break; }
}
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
float noir[] = { 0.0F, 0.0F, 0.0F, 1.0F };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,noir);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_CULL_FACE);
}
/* Scene dessinee */
void mySolidCylinder(double h,double r,int n,int m) {
glPushMatrix();
glRotatef(90.0F,1.0F,0.0F,0.0F);
glTranslatef(0.0F,0.0F,-h/2);
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj,r,r,h,n,m);
gluDeleteQuadric(qobj);
glPopMatrix();
}
void motif(int objet) {
glPushMatrix();
glTranslatef(2.0F,0.0F,0.0F);
{ float rouge[] = { 1.0F, 0.0F, 0.0F, 1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge); }
glutSolidSphere(0.5,36,36);
glPushMatrix();
glRotatef(30.0,0.0F,0.0F,1.0F);
glTranslatef(0.0F,1.0F,0.0F);
{ float grisFonce[] = { 0.3F, 0.3F, 0.3F, 1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,grisFonce); }
switch (objet) {
case 2 :
mySolidCylinder(2.0,0.15,20,20);
break;
case 1 :
glTranslatef(0.1F,0.0F,0.0F);
mySolidCylinder(2.0,0.05,20,20);
glTranslatef(-0.2F,0.0F,0.0F);
mySolidCylinder(2.0,0.05,20,20);
break;
case 0 :
mySolidCylinder(2.0,0.05,20,20);
break; }
glPopMatrix();
glTranslatef(0.6F,0.0F,0.0F);
glPushMatrix();
glRotatef(90.0,0.0F,0.0F,1.0F);
{ float grisFonce[] = { 0.3F, 0.3F, 0.3F, 1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,grisFonce); }
mySolidCylinder(1.2,0.05,20,20);
glPopMatrix();
glTranslatef(0.6F,0.0F,0.0F);
{ float blanc[] = { 1.0F, 1.0F, 1.0F, 1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc); }
glutSolidSphere(0.25,36,36);
glPopMatrix();
}
void scene(void) {
float blanc[] = { 1.0F, 1.0F, 1.0F, 1.0F };
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialf(GL_FRONT,GL_SHININESS,50.0F);
glPushMatrix();
for ( int i = 0 ; i < 6 ; i++ ) {
float angle = i*60.0F;
glPushMatrix();
glRotatef(angle,0.0F,0.0F,1.0F);
motif((objet) ? i%2 : 2);
glPopMatrix(); }
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClearColor(0.0F,0.0F,0.0F,1.0F);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
glPushMatrix();
dessinAxes();
{ float lum0Pos[] = { 1.0F,-1.0F, 1.0F, 1.0F };
glLightfv(GL_LIGHT0,GL_POSITION,lum0Pos);
float blanc[] = { 1.0F, 1.0F, 1.0F, 1.0F };
glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,0.0F);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0F);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,1.0F); }
{ float lum1Dir[] = { 1.0F, 1.0F, 1.0F, 0.0F };
glLightfv(GL_LIGHT1,GL_POSITION,lum1Dir);
float blanc[] = { 1.0F, 1.0F, 1.0F, 1.0F };
glLightfv(GL_LIGHT1,GL_DIFFUSE,blanc);
glLightfv(GL_LIGHT1,GL_SPECULAR,blanc); }
{ float lum2Pos[] = { 4.0F, 3.0F, 5.0F, 1.0F };
glLightfv(GL_LIGHT2,GL_POSITION,lum2Pos);
float lum2Dir[] = { -3.0F,-2.0F,-5.0F };
glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,lum2Dir);
glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,15.0F);
float blanc[] = { 1.0F, 1.0F, 1.0F, 1.0F };
glLightfv(GL_LIGHT2,GL_DIFFUSE,blanc);
glLightfv(GL_LIGHT2,GL_SPECULAR,blanc); }
{ float noir[] = { 0.0F, 0.0F, 0.0F, 1.0F };
float grisMoyen[] = { 0.5F, 0.5F, 0.5F, 1.0F };
glLightfv(GL_LIGHT3,GL_DIFFUSE,noir);
glLightfv(GL_LIGHT3,GL_SPECULAR,noir);
glLightfv(GL_LIGHT3,GL_AMBIENT,grisMoyen); }
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
glEnable(GL_LIGHTING);
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
glRotatef(rz,0.0F,0.0F,1.0F);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Erreur OpenGL: %d\n",error);
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* -> Ajustement de la camera de visualisation */
void reshape(int x,int y) {
glViewport(0,0,x,y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(23.0F,(float) x/y,1.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5.0,10.0,15.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
/* 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("Une molecule de benzene eclairee");
init();
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}