L'exécutable

Exam-TD1-2004-2005-Exo101.gif (13960 octets) Exam-TD1-2004-2005-Exo102.gif (14434 octets)

Exam-TD1-2004-2005-Exo103.gif (14480 octets)

Le source : Exam-TD1-2004-2005-Exo1.cpp

/* Auteur: Nicolas JANEY              */
/* nicolas.janey@univ-fcomte.fr       */
/* Novembre 2004                      */
/* Fonctions de dessin de sphere      */

#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 "ModuleReshape.h"
#include "ModuleAxes.h"

#ifndef M_PI
#define M_PI 3.14159F
#endif

static float anglex = 0.0F;
static float angley = 0.0F;
static float anglez = 0.0F;
static int n = 10;

GLubyte *makeImage(void) {
  GLubyte *img =(GLubyte *) calloc(8*8*3,sizeof(GLubyte));
  for ( int i = 0 ; i < 8 ; i++ )
    for ( int j = 0 ; j < 8 ; j++ )
      if ( ((i+j)%2) == 1 ) {
        int ind = 3*(i*8+j);
        img[ind] = img[ind+1] = img[ind+2] = 0xFF; }
  return(img);
}

void solidSphere(int n) {
  for ( int i = 0 ; i < n ; i++ ) {
    float a1 = -M_PI/2.0F + i*M_PI/n ;
    float a2 = a1 + M_PI/n ;
    float cs1 = cos(a1);
    float cs2 = cos(a2);
    float sn1 = sin(a1);
    float sn2 = sin(a2);
    float s1 =(float) i/n; 
    float s2 =(float) (i+1)/n; 
    glBegin(GL_QUAD_STRIP);
    for ( int j = 0 ; j <= n ; j++ ) {
      float a = j*2*M_PI/n;
      float x1 = cs1*cos(a);
      float y1 = cs1*sin(a);
      float x2 = cs2*cos(a);
      float y2 = cs2*sin(a);
      float t =(float) j/n; 
      glTexCoord2f(s1,t);
      glNormal3f(x1,y1,sn1);
      glVertex3f(x1,y1,sn1);
      glTexCoord2f(s2,t);
      glNormal3f(x2,y2,sn2);
      glVertex3f(x2,y2,sn2); }
    glEnd(); }
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  manipulateurSouris();
  manipulateurClavier();
  axes();
  glEnable(GL_TEXTURE_2D);
  glPushMatrix();
  glTranslatef(1.5F,1.5F,0.0F) ;
  glRotatef(anglex,1.0F,0.0F,0.0F);
  glRotatef(angley,0.0F,1.0F,0.0F);
  glRotatef(anglez,0.0F,0.0F,1.0F);
  solidSphere(n) ;
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-1.5F,-1.5F,0.0F) ;
  glRotatef(angley,0.0F,1.0F,0.0F);
  glRotatef(anglez,0.0F,0.0F,1.0F);
  glRotatef(anglex,1.0F,0.0F,0.0F);
  solidSphere(n) ;
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-1.5F,1.5F,0.0F) ;
  glRotatef(anglez,0.0F,0.0F,1.0F);
  glRotatef(anglex,1.0F,0.0F,0.0F);
  glRotatef(angley,0.0F,1.0F,0.0F);
  solidSphere(n) ;
  glPopMatrix();
  glPushMatrix();
  glTranslatef(1.5F,-1.5F,0.0F) ;
  glRotatef(angley,0.0F,1.0F,0.0F);
  glRotatef(anglex,1.0F,0.0F,0.0F);
  glRotatef(anglez,0.0F,0.0F,1.0F);
  solidSphere(n) ;
  glPopMatrix();
  glDisable(GL_TEXTURE_2D);
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

void myinit (void) {
  glClearColor(0.75F,0.75F,0.75F,0.0F);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_NORMALIZE);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_LIGHT2);
  glPixelStorei(GL_UNPACK_ALIGNMENT,1); 
  glTexImage2D(GL_TEXTURE_2D,0,3,8,8,0,GL_RGB,GL_UNSIGNED_BYTE,makeImage());
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
  float dir0[4] = { 1.0F,1.0F,1.0F,0.0F };
  glLightfv(GL_LIGHT0,GL_POSITION,dir0) ;
  glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurVert()) ;
  float dir1[4] = { -1.0F,1.0F,1.0F,0.0F };
  glLightfv(GL_LIGHT1,GL_POSITION,dir1) ;
  glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBleu()) ;
  float dir2[4] = { 0.0F,-1.0F,1.0F,0.0F };
  glLightfv(GL_LIGHT2,GL_POSITION,dir2) ;
  glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurRouge()) ;
}

void key(unsigned char key,int x,int y) {
  static int aff = 1;
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 43     : n++ ;
                    glutPostRedisplay();
                    break ;
      case 45     : n-- ;
                    if ( n < 3 )
                      n = 3 ;
                    glutPostRedisplay();
                    break ;
      case ' '    : switchAffichagePlan();
                    glutPostRedisplay();
                    break;
      case 0x0D   : aff = !aff;
                    glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
                    glutPostRedisplay(); }
}

void idle(void) {
  anglex += 0.33F;
  angley += 0.44F;
  anglez += 0.55F;
  glutPostRedisplay();
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(250,250); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Spheres"); 
  myinit(); 
  creationMenuBasique();
  setParametresOrthoBasique(-3.0F,3.0F,-3.0F,3.0F,-500.0,500.0);
  setManipulateurDistance(1.0F);
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutIdleFunc(idle);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

Les modules utilitaires : Modules.zip

RETOUR