 
  
 
  
Le source: RemplissageFacette2D.cpp
/* Auteur: Nicolas JANEY         */
  /* nicolas.janey@univ-fcomte.fr  */
  /* Avril 2020                    */
  /* Illustration du remplissage   */
  /* d'une facette 2D              */
  
  #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"
  
  static int aff = 0 ;
  static int yy = 0 ;
  
  void ligne(int xi,int yi,int xf,int yf,int *px) {
    int i,cumul ;
    int x = xi ;
    int y = yi ;
    int dx = xf - xi ;
    int dy = yf - yi ;
    int xinc = ( dx > 0 ) ? 1 : -1 ;
    int yinc = ( dy > 0 ) ? 1 : -1 ;
    dx = abs(dx) ;
    dy = abs(dy) ;
    px[y] = x ;
    if ( dx > dy ) {
      cumul = dx / 2 ;
      for ( i = 1 ; i <= dx ; i++ ) {
        x += xinc ;
        cumul += dy ;
        if (cumul >= dx) {
          cumul -= dx ;
          y += yinc ; }
        px[y] = x ; } }
      else {
      cumul = dy / 2 ;
      for ( i = 1 ; i <= dy ; i++ ) {
        y += yinc ;
        cumul += dx ;
        if ( cumul >= dy ) {
          cumul -= dy ;
          x += xinc ; }
        px[y] = x ; } }
  }
  
  void ligneCote(int xi,int yi,int xf,int yf,int *px) {
    if ( xi > xf )
      ligne(xf,yf,xi,yi,px) ;
      else
      ligne(xi,yi,xf,yf,px) ;
  }
  
  void axes() {
    glColor4fv(couleurCyan()) ;
    glBegin(GL_LINES) ;
    glVertex3f(0.0F,0.0F,0.0F) ;
    glVertex3f(20.0F,0.0F,0.0F) ;
    glEnd() ;
    glColor4fv(couleurVert()) ;
    glBegin(GL_LINES) ;
    glVertex3f(0.0F,0.0F,0.0F) ;
    glVertex3f(0.0F,20.0F,0.0F) ;
    glEnd() ;
    glColor4fv(couleurBleu()) ;
    glBegin(GL_LINES) ;
    glVertex3f(0.0F,0.0F,0.0F) ;
    glVertex3f(0.0F,0.0F,20.0F) ;
    glEnd() ;
  }
  
  void myinit() {
    glDepthFunc(GL_LESS);
    glShadeModel(GL_SMOOTH);
    glClearColor(0.8F,0.8F,0.8F,1.0F);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);
  }
  
  void Facette(float *coul,float *p1,float *p2,float *p3) {
    glPushMatrix() ;
    glEnable(GL_DEPTH_TEST);
    glColor4fv(coul) ;
    glBegin(GL_TRIANGLES) ;
    glVertex3fv(p1) ;
    glVertex3fv(p2) ;
    glVertex3fv(p3) ;
    glEnd() ;
    glDisable(GL_DEPTH_TEST);
    glColor4fv(couleurNoir()) ;
    glLineWidth(2.0) ;
    glBegin(GL_LINE_LOOP) ;
    glVertex3fv(p1) ;
    glVertex3fv(p2) ;
    glVertex3fv(p3) ;
    glEnd() ;
    glLineWidth(1.0) ;
    glEnable(GL_DEPTH_TEST);
    glPopMatrix() ;
  }
  
  void Pixel(int x, int y) {
    glBegin(GL_QUADS) ;
    glVertex2f(2*x-30.0F,2*y-30.0F) ;
    glVertex2f(2*x-28.0F,2*y-30.0F) ;
    glVertex2f(2*x-28.0F,2*y-28.0F) ;
    glVertex2f(2*x-30.0F,2*y-28.0F) ;
    glEnd() ;
  }
  
  void LigneDiscrete(int y,int x1,int x2) {
    if ( x2 != -100 ) {
      int i ;
      int x = x1 ;
      int dx = x2 - x1 ;
      int xinc = ( dx > 0 ) ? 1 : -1 ;
      dx = abs(dx) ;
      Pixel(x,y) ;
      for ( i = 1 ; i <= dx ; i++ ) {
        x += xinc ;
        Pixel(x,y) ; } }
  }
  
  void echange(float *p1,float *p2) {
    float aux = *p1 ;
    *p1 = *p2 ;
    *p2 = aux ;
  }
  
  void echangeSommet(float *p1,float *p2) {
    if ( p1[1] > p2[1] ) {
      echange(&p1[0],&p2[0]) ;
      echange(&p1[1],&p2[1]) ; }
  }
  
  void FacetteDiscrete(float *coul,float *p1,float *p2,float *p3) {
    glColor4fv(coul) ;
    glPushMatrix() ;
    int px1[30] ;
    int px2[30] ;
    int px3[30] ;
    int i;
    for ( i = 0 ; i < 30 ; i++ )
      px1[i] = px2[i] = px3[i] = -100 ;
    ligneCote((int) (p1[0]+30)/2,(int) (p1[1]+30)/2,(int) (p2[0]+30)/2,(int) (p2[1]+30)/2,px1) ;
    ligneCote((int) (p1[0]+30)/2,(int) (p1[1]+30)/2,(int) (p3[0]+30)/2,(int) (p3[1]+30)/2,px2) ;
    ligneCote((int) (p2[0]+30)/2,(int) (p2[1]+30)/2,(int) (p3[0]+30)/2,(int) (p3[1]+30)/2,px3) ;
    int x1[30] ;
    int x2[30] ;
    for ( i = 0 ; i < 30 ; i++ )
      x1[i] = x2[i] = -100 ;
    for ( i = 0 ; i < 30 ; i++ ) {
      if ( px1[i] > x1[i] )
        x1[i] = px1[i] ;
      if ( px2[i] > x1[i] )
        x1[i] = px2[i] ;
      if ( px3[i] > x1[i] )
        x1[i] = px3[i] ; }
    for ( i = 0 ; i < 30 ; i++ )
      x2[i] = x1[i] ;
    for ( i = 0 ; i < 30 ; i++ ) {
      if ( ( px1[i] <= x2[i] ) && ( px1[i] != -100 ) )
        x2[i] = px1[i] ;
      if ( ( px2[i] <= x2[i] ) && ( px2[i] != -100 ) )
        x2[i] = px2[i] ;
      if ( ( px3[i] <= x2[i] ) && ( px3[i] != -100 ) )
        x2[i] = px3[i] ; }
    for ( int y = 0 ; y < 30 ; y++ )
      LigneDiscrete(y,x2[y],x1[y]) ;
    glPopMatrix() ;
  }
  
  void lignePixels(int xi,int yi,int xf,int yf) {
    int i,cumul ;
    int x = xi ;
    int y = yi ;
    int dx = xf - xi ;
    int dy = yf - yi ;
    int xinc = ( dx > 0 ) ? 1 : -1 ;
    int yinc = ( dy > 0 ) ? 1 : -1 ;
    dx = abs(dx) ;
    dy = abs(dy) ;
    Pixel(x,y) ;
    if ( dx > dy ) {
      cumul = dx / 2 ;
      for ( i = 1 ; i <= dx ; i++ ) {
        x += xinc ;
        cumul += dy ;
        if (cumul >= dx) {
          cumul -= dx ;
          y += yinc ; }
        Pixel(x,y) ; } }
      else {
      cumul = dy / 2 ;
      for ( i = 1 ; i <= dy ; i++ ) {
        y += yinc ;
        cumul += dx ;
        if ( cumul >= dy ) {
          cumul -= dy ;
          x += xinc ; }
        Pixel(x,y) ; } }
  }
  
  void CoteFacette(float *coul,float *p1,float *p2) {
    glColor4fv(coul) ;
    glPushMatrix() ;
    if ( p2[1] > p1[1] )
      lignePixels((int) (p1[0]+30)/2,(int) (p1[1]+30)/2,(int) (p2[0]+30)/2,(int) (p2[1]+30)/2) ;
      else
      lignePixels((int) (p2[0]+30)/2,(int) (p2[1]+30)/2,(int) (p1[0]+30)/2,(int) (p1[1]+30)/2) ;
    glPopMatrix() ;
  }
  
  void FacetteDiscreteTrame(float *coul,float *c,float *cc,float *p1,float *p2,float *p3,int yy) {
    glColor4fv(coul) ;
    glPushMatrix() ;
    int px1[30] ;
    int px2[30] ;
    int px3[30] ;
    int i;
    for ( i = 0 ; i < 30 ; i++ )
      px1[i] = px2[i] = px3[i] = -100 ;
    ligneCote((int) (p1[0]+30)/2,(int) (p1[1]+30)/2,(int) (p2[0]+30)/2,(int) (p2[1]+30)/2,px1) ;
    ligneCote((int) (p1[0]+30)/2,(int) (p1[1]+30)/2,(int) (p3[0]+30)/2,(int) (p3[1]+30)/2,px2) ;
    ligneCote((int) (p2[0]+30)/2,(int) (p2[1]+30)/2,(int) (p3[0]+30)/2,(int) (p3[1]+30)/2,px3) ;
    int x1[30] ;
    int x2[30] ;
    for ( i = 0 ; i < 30 ; i++ )
      x1[i] = x2[i] = -100 ;
    for ( i = 0 ; i < 30 ; i++ ) {
      if ( px1[i] > x1[i] )
        x1[i] = px1[i] ;
      if ( px2[i] > x1[i] )
        x1[i] = px2[i] ;
      if ( px3[i] > x1[i] )
        x1[i] = px3[i] ; }
    for ( i = 0 ; i < 30 ; i++ )
      x2[i] = x1[i] ;
    for ( i = 0 ; i < 30 ; i++ ) {
      if ( ( px1[i] <= x2[i] ) && ( px1[i] != -100 ) )
        x2[i] = px1[i] ;
      if ( ( px2[i] <= x2[i] ) && ( px2[i] != -100 ) )
        x2[i] = px2[i] ;
      if ( ( px3[i] <= x2[i] ) && ( px3[i] != -100 ) )
        x2[i] = px3[i] ; }
    int y;
    for ( y = 0 ; y <= yy-1 ; y++ )
      LigneDiscrete(y,x2[y],x1[y]) ;
    glColor4fv(c) ;
    LigneDiscrete(yy,x2[y],x1[y]) ;
    CoteFacette(cc,p1,p2) ;
    CoteFacette(cc,p1,p3) ;
    CoteFacette(cc,p2,p3) ;
    glPopMatrix() ;
  }
  
  void CotesFacette(float *coul1,float *p1,float *coul2,float *p2,float *coul3,float *p3) {
    CoteFacette(coul1,p1,p2) ;
    CoteFacette(coul2,p1,p3) ;
    CoteFacette(coul3,p2,p3) ;
  }
  
  void display() {
    float f11[] = { -25.0F,-25.0F,0.0F } ;
    float f12[] = { -20.0F,20.0F,0.0F } ;
    float f13[] = { 25.0F,5.0F,0.0F } ;
    echangeSommet(f11,f12) ;
    echangeSommet(f12,f13) ;
    echangeSommet(f11,f12) ;
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    manipulateurSouris();
    manipulateurClavier();
    glPushMatrix();
    switch (aff) {
      case 0 : Facette(couleurRouge(),f11,f12,f13) ;
               break ;
      case 1 : CoteFacette(couleurRouge(),f11,f12) ;
               break ;
      case 2 : CoteFacette(couleurVert(),f11,f13) ;
               break ;
      case 3 : CoteFacette(couleurBleu(),f12,f13) ;
               break ;
      case 4 : CotesFacette(couleurRouge(),f11,couleurVert(),f12,couleurBleu(),f13) ;
               break ;
      case 5 : FacetteDiscreteTrame(couleurRouge(),couleurBleu(),couleurVert(),f11,f12,f13,yy) ;
               break ;
      case 6 : FacetteDiscrete(couleurRouge(),f11,f12,f13) ;
               break ; }
    glPopMatrix();
    glPushMatrix();
    glDisable(GL_DEPTH_TEST);
    axes() ;
    glPopMatrix();
    glPopMatrix();
    glFlush();
    glutSwapBuffers() ;
  }
  
  void key(unsigned char key,int x,int y) {
    if ( keyManipulateur(key,x,y) )
      glutPostRedisplay();
      else
      switch ( key ) {
        case 0x0D : yy = 0 ;
                    aff = (aff+1)%7;
                    glutPostRedisplay();
                    break;
        case 32   : yy = (yy+1)%30;
                    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("Remplissage d'une facette 2D"); 
    myinit(); 
    creationMenuBasique();
    setParametresOrthoBasique(-30.0,30.0,-30.0,30.0,-500.0,500.0);
    setManipulateurDistance(1.0F);
    glutReshapeFunc(reshapeOrthoBasique);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialBasique);
    glutMotionFunc(motionBasique);
    glutMouseFunc(sourisBasique);
    glutDisplayFunc(display);
    glutMainLoop();
    return(0);
  }