Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1.  
  2.  
  3. void Rasterizer::DrawSolidTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color&color)
  4. {
  5.     // Sort our points into order of y
  6.     // 0 top
  7.     // 2 middle
  8.     // 1 bottom
  9.     if (y1 < y0)
  10.     {
  11.         swap(y1, y0);
  12.         swap(x1, x0);
  13.     }
  14.     if (y2 < y0)
  15.     {
  16.         swap(y2, y0);
  17.         swap(x2, x0);
  18.     }
  19.     if (y1 < y2)
  20.     {
  21.         swap(y2, y1);
  22.         swap(x2, x1);
  23.     }
  24.  
  25.     float xl_edge = (float)x0;  // left edge
  26.     float xr_edge = (float)x0;  // right edge
  27.  
  28.     float dxldy;
  29.     float dxrdy;
  30.  
  31.     float dxdy1 = (float)(x2 - x0) / (y2 - y0);
  32.     float dxdy2 = (float)(x1 - x0) / (y1 - y0);
  33.  
  34.     if (dxdy1 < dxdy2)
  35.     {
  36.         dxldy = dxdy1;
  37.         dxrdy = dxdy2;
  38.     }
  39.     else
  40.     {
  41.         dxldy = dxdy2;
  42.         dxrdy = dxdy1;
  43.     }
  44.  
  45.  
  46.     // Top of the triangle
  47.     for (int y = y0; y<y2; y++)
  48.     {
  49.  
  50.         for (int x = xl_edge; x<xr_edge; x++)
  51.         {
  52.             SetPixel((unsigned int)x, (unsigned int)y, color);
  53.  
  54.         }//end for loop x
  55.  
  56.         xl_edge = xl_edge + dxldy;
  57.         xr_edge = xr_edge + dxrdy;
  58.  
  59.     }// end for loop y
  60.  
  61.  
  62.     // Bottom half of the triangle
  63.  
  64.     if (dxdy1 < dxdy2)
  65.     {
  66.         dxldy = (float)(x2 - x1) / (y2 - y1);
  67.     }
  68.     else
  69.     {
  70.         dxrdy = (float)(x2 - x1) / (y2 - y1);
  71.     }
  72.  
  73.  
  74.     for (int y = y2; y<y1; y++)
  75.     {
  76.  
  77.         for (int x = xl_edge; x<xr_edge; x++)
  78.         {
  79.             SetPixel((unsigned int)x, (unsigned int)y, color);
  80.         }//end for loop x
  81.  
  82.         xl_edge = xl_edge + dxldy;
  83.         xr_edge = xr_edge + dxrdy;
  84.  
  85.     }// end for loop y
  86.  
  87. }
  88.  
  89.  
  90.  
  91. void Rasterizer::DrawTopTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color color)
  92. {
  93.     int temp_x;
  94.     // test order of x1 and x2
  95.     if (x1 < x0)
  96.     {
  97.         temp_x = x1;
  98.         x1 = x0;
  99.         x0 = temp_x;
  100.     } // end if swap
  101.  
  102.     // compute deltas
  103.     float dxy_left = (x2 - x0) / (y2 - y0);
  104.     float dxy_right = (x2 - x1) / (y2 - y1);
  105.     // set starting and ending points for edge trace
  106.     float xs = x0;
  107.     float xe = x1;
  108.     // draw each scanline
  109.     for (int y = y0; y <= y2; y++)
  110.     {
  111.         // draw a line from xs to xe at y in color c
  112.         DrawLine(color, (int)(xs + 0.5), y,color, (int)(xe + 0.5), y);
  113.         // move down one scanline
  114.         xs += dxy_left;
  115.         xe += dxy_right;
  116.     } // end for y
  117. }
  118.  
  119.  
  120. void Rasterizer::DrawBottomTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color color)
  121. {
  122.     int temp_x;
  123.     // test order of x1 and x2
  124.     if (x2 < x1)
  125.     {
  126.         temp_x = x1;
  127.         x1 = x2;
  128.         x2 = temp_x;
  129.     } //
  130.  
  131.     float dxy_left = (x2 - x0) / (y2 - y0);
  132.     float dxy_right = (x2 - x0) / (y2 - y0);
  133.  
  134.     // set starting and ending points for edge trace
  135.     float xs = x0;
  136.     float xe = x0;
  137.     // draw each scanline
  138.     for (int y = y0; y <= y1; y++)
  139.     {
  140.         // draw a line from xs to xe at y in color c
  141.         DrawLine(color, (int)xs, y, color, (int)xe, y);
  142.         // move down one scanline
  143.         xs += dxy_left;
  144.         xe += dxy_right;
  145.     } // end for y
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement