Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Rasterizer::DrawSolidTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color&color)
- {
- // Sort our points into order of y
- // 0 top
- // 2 middle
- // 1 bottom
- if (y1 < y0)
- {
- swap(y1, y0);
- swap(x1, x0);
- }
- if (y2 < y0)
- {
- swap(y2, y0);
- swap(x2, x0);
- }
- if (y1 < y2)
- {
- swap(y2, y1);
- swap(x2, x1);
- }
- float xl_edge = (float)x0; // left edge
- float xr_edge = (float)x0; // right edge
- float dxldy;
- float dxrdy;
- float dxdy1 = (float)(x2 - x0) / (y2 - y0);
- float dxdy2 = (float)(x1 - x0) / (y1 - y0);
- if (dxdy1 < dxdy2)
- {
- dxldy = dxdy1;
- dxrdy = dxdy2;
- }
- else
- {
- dxldy = dxdy2;
- dxrdy = dxdy1;
- }
- // Top of the triangle
- for (int y = y0; y<y2; y++)
- {
- for (int x = xl_edge; x<xr_edge; x++)
- {
- SetPixel((unsigned int)x, (unsigned int)y, color);
- }//end for loop x
- xl_edge = xl_edge + dxldy;
- xr_edge = xr_edge + dxrdy;
- }// end for loop y
- // Bottom half of the triangle
- if (dxdy1 < dxdy2)
- {
- dxldy = (float)(x2 - x1) / (y2 - y1);
- }
- else
- {
- dxrdy = (float)(x2 - x1) / (y2 - y1);
- }
- for (int y = y2; y<y1; y++)
- {
- for (int x = xl_edge; x<xr_edge; x++)
- {
- SetPixel((unsigned int)x, (unsigned int)y, color);
- }//end for loop x
- xl_edge = xl_edge + dxldy;
- xr_edge = xr_edge + dxrdy;
- }// end for loop y
- }
- void Rasterizer::DrawTopTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color color)
- {
- int temp_x;
- // test order of x1 and x2
- if (x1 < x0)
- {
- temp_x = x1;
- x1 = x0;
- x0 = temp_x;
- } // end if swap
- // compute deltas
- float dxy_left = (x2 - x0) / (y2 - y0);
- float dxy_right = (x2 - x1) / (y2 - y1);
- // set starting and ending points for edge trace
- float xs = x0;
- float xe = x1;
- // draw each scanline
- for (int y = y0; y <= y2; y++)
- {
- // draw a line from xs to xe at y in color c
- DrawLine(color, (int)(xs + 0.5), y,color, (int)(xe + 0.5), y);
- // move down one scanline
- xs += dxy_left;
- xe += dxy_right;
- } // end for y
- }
- void Rasterizer::DrawBottomTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color color)
- {
- int temp_x;
- // test order of x1 and x2
- if (x2 < x1)
- {
- temp_x = x1;
- x1 = x2;
- x2 = temp_x;
- } //
- float dxy_left = (x2 - x0) / (y2 - y0);
- float dxy_right = (x2 - x0) / (y2 - y0);
- // set starting and ending points for edge trace
- float xs = x0;
- float xe = x0;
- // draw each scanline
- for (int y = y0; y <= y1; y++)
- {
- // draw a line from xs to xe at y in color c
- DrawLine(color, (int)xs, y, color, (int)xe, y);
- // move down one scanline
- xs += dxy_left;
- xe += dxy_right;
- } // end for y
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement