L'exécutable


Les deux facettes à afficher

 
Les deux facettes rasterisées individuellement


Les deux facettes rasterisées après Z-Buffer

 
Les deux images ci-dessus montrent les pixels comme des tuiles carrées placées en z selon leurs profondeurs écran.
La projection n'est pas réalisée selon l'axe -z qui est l'axe de projection normale mais selon un autre axe qui permet de voir le calcul en 3D des pixels.
Dans l'image de droite les triangles réels sont de plus matérialisés par leurs bords.

Le source: ZBuffer2.cpp

static void pixel(int x,int y,int z,int *xmin,int *xmax,int *zmin,int *zmax) {
  if ( x < xmin[y] ) {
    xmin[y] = x;
    zmin[y] = z; }
  if ( x > xmax[y] ) {
    xmax[y] = x;
    zmax[y] = z; }
}

static void cote(int xi,int yi,int zi,int xf,int yf,int zf,
                 int *xmin,int *xmax,int *zmin,int *zmax) {
  int i,cumul,cumulz;
  int x = xi;
  int y = yi;
  int z = zi;
  int dx = xf - xi;
  int dy = yf - yi;
  int dz = zf - zi;
  int xinc = (dx > 0) ? 1 : -1;
  int yinc = (dy > 0) ? 1 : -1;
  int zinc = (dz > 0) ? 1 : -1;
  dx = abs(dx);
  dy = abs(dy);
  dz = abs(dz);
  pixel(x,y,z,xmin,xmax,zmin,zmax);
  if ( dx > dy ) {
    cumul = dx / 2;
    cumulz = 0;
    for ( i = 1 ; i <= dx ; i++ ) {
      x += xinc;
      cumul += dy;
      cumulz += dz;
      if ( cumul >= dx ) {
        cumul -= dx;
        y += yinc; }
      while ( cumulz >= dx ) {
        cumulz -= dx;
        z += zinc; }
      pixel(x,y,z,xmin,xmax,zmin,zmax); } }
  else {
    cumul = dy / 2;
    cumulz = 0;
    for ( i = 1 ; i <= dy ; i++ ) {
      y += yinc;
      cumul += dx;
      cumulz += dz;
      if ( cumul >= dy ) {
        cumul -= dy;
        x += xinc; }
      while ( cumulz >= dy ) {
        cumulz -= dy;
        z += zinc; }
      pixel(x,y,z,xmin,xmax,zmin,zmax); } }
  pixel(xi,yi,zi,xmin,xmax,zmin,zmax);
  pixel(xf,yf,zf,xmin,xmax,zmin,zmax);
}

static void trame(int y,int x1,int z1,int x2,int z2,
                  void (*pixel)(int,int,int)) {
  int i,cumul;
  int x = x1;
  int z = z1;
  int dx = x2 - x1;
  int dz = z2 - z1;
  int xinc = ( dx > 0 ) ? 1 : -1;
  int zinc = ( dz > 0 ) ? 1 : -1;
  dx = abs(dx);
  dz = abs(dz);
  pixel(x,y,z);
  cumul = 0;
  for ( i = 1 ; i <= dx ; i++ ) {
    x += xinc;
    cumul += dz;
    while ( cumul > dx ) {
      cumul -= dx;
      z += zinc; }
    pixel(x,y,z); }
}

static void triangle(int x1,int y1,int z1,
                     int x2,int y2,int z2,
                     int x3,int y3,int z3,
                     void (*pixel)(int,int,int)) {
  int dim = y3-y1+1;
  int dy = -y1;
  int *xmin = new int[dim];
  int *xmax = new int[dim];
  int *zmin = new int[dim];
  int *zmax = new int[dim];
  for ( int y = 0 ; y < dim ; y++ ) {
    xmin[y] = 8192;
    xmax[y] = -1; }
  cote(x1,y1+dy,z1,x2,y2+dy,z2,xmin,xmax,zmin,zmax);
  cote(x2,y2+dy,z2,x3,y3+dy,z3,xmin,xmax,zmin,zmax);
  cote(x1,y1+dy,z1,x3,y3+dy,z3,xmin,xmax,zmin,zmax);
  for ( int y = 0 ; y < dim ; y++ )
    trame(y-dy,xmin[y],zmin[y],xmax[y],zmax[y],pixel);
  delete(xmin);
  delete(xmax);
  delete(zmin);
  delete(zmax);
}

Les modules utilitaires : Modules.zip

WB01624_.gif (281 octets) RETOUR