Holland

Triangle

Nov 22nd, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. void Triangle::DrawFillMode(Surface* a_Target)
  2. {
  3. // Backface culling
  4.  
  5. unsigned int t_Width = a_Target->GetWidth();
  6. unsigned int t_Height = a_Target->GetHeight();
  7.  
  8. int t_FirstLine = SCRHEIGHT;
  9.  
  10. for(int i = 0; i<SCRHEIGHT; i++)
  11. {
  12. t_Lowest[i] = SCRWIDTH;
  13. t_Highest[i] = 0.0f;
  14. }
  15.  
  16. Pixel * t_Buffer = a_Target->GetBuffer();
  17. for ( int k = 0; k < 3; k++ )
  18. {
  19. float x1 = m_Vertex[k]->GetScreenX(),
  20. y1 = m_Vertex[k]->GetScreenY(),
  21. x2 = m_Vertex[(k + 1) % 3]->GetScreenX(),
  22. y2 = m_Vertex[(k + 1) % 3]->GetScreenY();
  23.  
  24. float b = x2 - x1;
  25. float h = y2 - y1;
  26. float l = fabsf( b );
  27. if (fabsf ( h ) > l) l = fabsf( h );
  28. int il = (int)l;
  29. float dx = b / (float)l;
  30. float dy = h / (float)l;
  31. for ( int i = 0; i <= il; i++ )
  32. {
  33. int t_Y = (int)y1;
  34. if(t_Y<t_Height && t_Y>=0)
  35. {
  36. if(x1>t_Highest[t_Y]) t_Highest[t_Y] = x1;
  37. if(x1<t_Lowest[t_Y]) t_Lowest[t_Y] = x1;
  38.  
  39.  
  40. if((t_Lowest[t_Y]!=SCRWIDTH || t_Highest[t_Y]!=0) && t_Y<t_FirstLine)
  41. t_FirstLine = t_Y;
  42. }
  43.  
  44. if(x1>=0 && x1<t_Width && y1>=0 && y1<t_Height)
  45. {
  46. *(t_Buffer + (int)x1 + (int)y1 * t_Width) = 0xAAAAAA;
  47. }
  48. x1 += dx, y1 += dy;
  49. }
  50. }
  51.  
  52. for(int y = t_FirstLine; y<SCRHEIGHT; y++)
  53. {
  54. for(int i = t_Lowest[y]; i<t_Highest[y] && i<SCRWIDTH; i++)
  55. {
  56. if(i>=0 && i<=t_Width && y>=0 && y<t_Height)
  57. {
  58. if(*(t_Buffer + (int)i + (int)y * t_Width)==0)
  59. {
  60. *(t_Buffer + (int)i + (int)y * t_Width) = 0xffffff;
  61. }
  62. }
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment