Advertisement
Guest User

ZBuffering

a guest
Sep 12th, 2011
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.78 KB | None | 0 0
  1. void DrawPolygon(SDL_Surface* buf, Vertex2f ver[3])
  2. {
  3.     int order[3];
  4.    
  5.     // Starting and ending points of the scanlines
  6.     float x0 = 0, x1 = 0;
  7.     float xl = x0, xr = x1;
  8.     float z0 = 0, z1 = 0;
  9.     float zl = z0, zr = z1;
  10.    
  11.     // Slopes
  12.     float d0 = 0;
  13.     float d1 = 0;
  14.    
  15.     // Polygon parts to draw flags
  16.     bool drawHalf1 = true;
  17.     bool drawHalf2 = true;
  18.    
  19.     // Sort the vertices
  20.     // find the lowest Y
  21.     if (ver[0].y < ver[1].y)
  22.     {
  23.         if (ver[0].y < ver[2].y)
  24.         {
  25.             order[0] = 0;
  26.         }
  27.         else
  28.         {
  29.             order[0] = 2;
  30.         }
  31.     }
  32.     else
  33.     {
  34.         if (ver[1].y < ver[2].y)
  35.         {
  36.             order[0] = 1;
  37.         }
  38.         else
  39.         {
  40.             order[0] = 2;
  41.         }
  42.     }
  43.  
  44.     // find the highest Y
  45.     if (ver[0].y > ver[1].y)
  46.     {
  47.         if (ver[0].y > ver[2].y)
  48.         {
  49.             order[2] = 0;
  50.         }
  51.         else
  52.         {
  53.             order[2] = 2;
  54.         }
  55.     }
  56.     else
  57.     {
  58.         if (ver[1].y > ver[2].y)
  59.         {
  60.             order[2] = 1;
  61.         }
  62.         else
  63.         {
  64.             order[2] = 2;
  65.         }
  66.     }
  67.  
  68.     // and the middle one is a matter of deduction
  69.     order[1] = 3 - (order[0] + order[2]);
  70.    
  71.     // Check which parts of polygon to draw
  72.     // Should we render the polygon at all?
  73.     if(ver[order[0]].y - ver[order[2]].y == 0.0f) // The polygon has no height so we don't render it
  74.     {
  75.         return;
  76.     }
  77.     else if(ver[order[1]].y - ver[order[2]].y == 0.0f) // The polygon has only the frist half
  78.     {
  79.         drawHalf2 = false;
  80.     }
  81.     else if(ver[order[0]].y - ver[order[1]].y == 0.0f) // The polygon has only the second half
  82.     {
  83.         drawHalf1 = false;
  84.     }
  85.    
  86.     // slopes
  87.     d1 = (ver[order[2]].x - ver[order[0]].x) / (ver[order[2]].y - ver[order[0]].y);
  88.    
  89.     // Draw the first half of the polygon
  90.     if(drawHalf1)
  91.     {
  92.         d0 = (ver[order[1]].x - ver[order[0]].x) / (ver[order[1]].y - ver[order[0]].y);
  93.        
  94.         x0 = ver[order[0]].x;
  95.         x1 = ver[order[0]].x;
  96.                    
  97.         for(float y = ver[order[0]].y; y < ver[order[1]].y; ++y)
  98.         {
  99.             z0 = ver[order[0]].z - (ver[order[0]].z - ver[order[1]].z)*((ver[order[0]].y - y) / (ver[order[0]].y - ver[order[1]].y));
  100.             z1 = ver[order[0]].z - (ver[order[0]].z - ver[order[2]].z)*((ver[order[0]].y - y) / (ver[order[0]].y - ver[order[2]].y));
  101.             // x sorting
  102.             if(x0 < x1) { xl = x0; xr = x1; zl = z0; zr = z1; } else
  103.                         { xl = x1; xr = x0; zl = z1; zr = z0; }
  104.            
  105.            
  106.            
  107.             //fastline(buf, (int)xl, (int)y, (int)xr, (int)y, ver[order[0]].color);
  108.             DrawSpan(buf, (int)xl, (int)xr, (int)y, zl, zr, ver[order[0]].color);
  109.             /*
  110.             for(float x = xl; x < xr; ++x)
  111.                 putpixel(buf, (int)x, (int)y, ver[order[0]].color);
  112.             */
  113.            
  114.             x0 += d0;
  115.             x1 += d1;
  116.         }
  117.     }
  118.    
  119.     if(drawHalf2)
  120.     {
  121.         d0 = (ver[order[2]].x - ver[order[1]].x) / (ver[order[2]].y - ver[order[1]].y);
  122.        
  123.         x0 = ver[order[2]].x;
  124.         x1 = ver[order[2]].x;
  125.                    
  126.         for(float y = ver[order[2]].y; y > ver[order[1]].y; --y)
  127.         {
  128.             z0 = ver[order[0]].z - (ver[order[0]].z - ver[order[1]].z)*((ver[order[0]].y - y) / (ver[order[0]].y - ver[order[1]].y));
  129.             z1 = ver[order[0]].z - (ver[order[0]].z - ver[order[2]].z)*((ver[order[0]].y - y) / (ver[order[0]].y - ver[order[2]].y));
  130.             // x sorting
  131.             if(x0 < x1) { xl = x0; xr = x1; zl = z0; zr = z1; } else
  132.                         { xl = x1; xr = x0; zl = z1; zr = z0; }
  133.            
  134.            
  135.             //fastline(buf, (int)xl, (int)y, (int)xr, (int)y, ver[order[0]].color);
  136.             DrawSpan(buf, (int)xl, (int)xr, (int)y, zl, zr, ver[order[0]].color);
  137.             /*
  138.             for(float x = xl; x < xr; ++x)
  139.                 putpixel(buf, (int)x, (int)y, ver[order[0]].color);
  140.             */
  141.            
  142.             x0 -= d0;
  143.             x1 -= d1;
  144.         }
  145.     }
  146. }
  147.  
  148. void DrawSpan(SDL_Surface* buf, int xa, int xb, int y, float za, float zb, int col)
  149. {
  150.     float z = 0.0f;
  151.     for(int x = xa; x < xb; ++x)
  152.     {
  153.         z = (zb - (zb - za)*((xb - x)/(xb - xa)));  
  154.         if( z >= depthBuffer->GetZBuffer(x, y) )
  155.         {
  156.             depthBuffer->SetZBuffer(x, y, z);
  157.         //if(buf->line[y][x]) buf->line[y][x] = col;
  158.             DrawPixel(buf, x, y, col);
  159.         }
  160.     }
  161. }
  162.  
  163. void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color)
  164. {
  165.   //Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  166.   if(x > 0 && y > 0 && x < screen->w && y < screen->h)
  167.   switch (screen->format->BytesPerPixel)
  168.   {
  169.     case 1: // Assuming 8-bpp
  170.       {
  171.         Uint8 *bufp;
  172.         bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
  173.         *bufp = color;
  174.       }
  175.       break;
  176.     case 2: // Probably 15-bpp or 16-bpp
  177.       {
  178.         Uint16 *bufp;
  179.         bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
  180.         *bufp = color;
  181.       }
  182.       break;
  183.     case 3: // Slow 24-bpp mode, usually not used
  184.       {
  185.         Uint8 *bufp;
  186.         bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
  187.         if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
  188.         {
  189.           bufp[0] = color;
  190.           bufp[1] = color >> 8;
  191.           bufp[2] = color >> 16;
  192.         } else {
  193.           bufp[2] = color;
  194.           bufp[1] = color >> 8;
  195.           bufp[0] = color >> 16;
  196.         }
  197.       }
  198.       break;
  199.     case 4: // Probably 32-bpp
  200.       {
  201.         Uint32 *bufp;
  202.         bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
  203.         *bufp = color;
  204.       }
  205.       break;
  206.   }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement