L'exécutable

ZB12.gif (6293 octets)  ZB13.gif (8265 octets)

ZB14.gif (7231 octets)  ZB15.gif (6320 octets)

Le source: RemplissageFacette2D.cpp

#include <windows.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float anglex = 0.0F ;
float angley = 0.0F ;
float anglez = 0.0F ;
int aff = 0 ;
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() {
  float rouge[] = { 1.0F,1.0F,0.0F,1.0F };
  glColor4fv(rouge) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(20.0F,0.0F,0.0F) ;
  glEnd() ;
  float vert[] = { 0.0F,1.0F,1.0F,1.0F };
  glColor4fv(vert) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(0.0F,20.0F,0.0F) ;
  glEnd() ;
  float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
  glColor4fv(bleu) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(0.0F,0.0F,20.0F) ;
  glEnd() ;
}

void CALLBACK init() {
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  glClearColor(0.8F,0.8F,0.8F,1.0F);
}

void Facette(float *coul,float *p1,float *p2,float *p3) {
  glPushMatrix() ;
  glColor4fv(coul) ;
  glBegin(GL_TRIANGLES) ;
  glVertex3fv(p1) ;
  glVertex3fv(p2) ;
  glVertex3fv(p3) ;
  glEnd() ;
  float noir[] = { 0.0F,0.0F,0.0F } ;
  glColor3fv(noir) ;
  glLineWidth(1.0) ;
  glBegin(GL_LINE_LOOP) ;
  glVertex3fv(p1) ;
  glVertex3fv(p2) ;
  glVertex3fv(p3) ;
  glEnd() ;
  glLineWidth(1.0) ;
  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] ;
  for ( int 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] ;
  for ( int 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 <= 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 CALLBACK display() {
  float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
  float vert[] = { 0.0F,1.0F,0.0F,1.0F };
  float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
  float f11[] = { -25.0F,-25.0F } ;
  float f12[] = { -20.0F,20.0F } ;
  float f13[] = { 25.0F,5.0F } ;
  echangeSommet(f11,f12) ;
  echangeSommet(f12,f13) ;
  echangeSommet(f11,f12) ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glRotatef(anglex,1.0F,0.0F,0.0F) ;
  glRotatef(angley,0.0F,1.0F,0.0F) ;
  glRotatef(anglez,0.0F,0.0F,1.0F) ;
  glPushMatrix();
  switch (aff) {
    case 0 : Facette(rouge,f11,f12,f13) ;
             break ;
    case 1 : CoteFacette(rouge,f11,f12) ;
             break ;
    case 2 : CoteFacette(vert,f11,f13) ;
             break ;
    case 3 : CoteFacette(bleu,f12,f13) ;
             break ;
    case 4 : CotesFacette(rouge,f11,vert,f12,bleu,f13) ;
             break ;
    case 5 : FacetteDiscreteTrame(rouge,bleu,vert,f11,f12,f13,yy) ;
             break ;
    case 6 : FacetteDiscrete(rouge,f11,f12,f13) ;
             break ; }
  glPopMatrix();
  glPushMatrix();
  axes() ;
  glPopMatrix();
  glPopMatrix();
  glFlush();
  auxSwapBuffers() ;
}

void CALLBACK reshape(int w,int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if( w <= h) 
    glOrtho(-30.0,30.0,-30.0*(double) h/w,30.0*(double) h/w,-40.0,40.0);
    else 
    glOrtho(-30.0*(double) h/w,30.0*(double) h/w,-30.0,30.0,-40.0,40.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void CALLBACK auxKeyLeft() {
  angley-=2 ;
}

void CALLBACK auxKeyRight() {
  angley+=2 ;
}

void CALLBACK auxKeyUp() {
  anglex-=2 ;
}

void CALLBACK auxKeyDown() {
  anglex+=2 ;
}

void CALLBACK auxKeyz() {
  anglez+=2 ;
}

void CALLBACK auxKeyZ() {
  anglez-=2 ;
}

void CALLBACK auxKeyReturn() {
  yy = 0 ;
  aff++ ;
  if ( aff == 7 )
    aff = 0 ;
}

void CALLBACK auxKeySpace() {
  yy++ ;
  if ( yy == 30 )
    yy = 0 ;
}

void main(void) {
  auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH);
  auxInitPosition (0,0,300,300);
  auxInitWindow("Remplissage d'une facette 2D") ;
  init();
  auxKeyFunc(AUX_RETURN,auxKeyReturn) ;
  auxKeyFunc(AUX_SPACE,auxKeySpace) ;
  auxKeyFunc(AUX_UP,auxKeyUp) ;
  auxKeyFunc(AUX_DOWN,auxKeyDown) ;
  auxKeyFunc(AUX_RIGHT,auxKeyRight) ;
  auxKeyFunc(AUX_LEFT,auxKeyLeft) ;
  auxKeyFunc(AUX_z,auxKeyz) ;
  auxKeyFunc(AUX_Z,auxKeyZ) ;
  auxReshapeFunc(reshape);
  auxMainLoop(display);
}
WB01624_.gif (281 octets) RETOUR