Advertisement
Guest User

Untitled

a guest
May 26th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. void Triangle::Draw(Surface* mScreen, float*& ZBuffer, const unsigned int LineColour)
  2. {
  3.     unsigned int yMin = SCRHEIGHT - 1;
  4.     unsigned int yMax = 0;
  5.     const int textureWidth = Texture->GetWidth();
  6.     const int textureHeight = Texture->GetHeight();
  7.     const int screenWidth = mScreen->GetWidth();
  8.     const int screenHeight = mScreen->GetHeight();
  9.     Vertex* v0 = nullptr;
  10.     Vertex* v1 = nullptr;
  11.     for (unsigned int i = 0; i < amountOfVerts; i++)
  12.     {
  13.         v0 = &Vertices[i];
  14.         v1 = &Vertices[(i + 1) % amountOfVerts];
  15.  
  16.         if (v0->TransformedPos.y > v1->TransformedPos.y)
  17.         {
  18.             std::swap(v0, v1);
  19.         }
  20.         float x1 = v0->TransformedPos.x;
  21.         float y1 = v0->TransformedPos.y;
  22.         float x2 = v1->TransformedPos.x;
  23.         float y2 = v1->TransformedPos.y;
  24.         float z1 = 1 / v0->TransformedPos.w;
  25.         float z2 = 1 / v1->TransformedPos.w;
  26.         float U1 = v0->U* z1;
  27.         float V1 = v0->V* z1;
  28.         float U2 = v1->U* z2;
  29.         float V2 = v1->V* z2;
  30.  
  31.         const float dxdy = ((x2 - x1) / (y2 - y1));
  32.         const float dzdy = ((z2 - z1) / (y2 - y1));
  33.         const float dudy = ((U2 - U1) / (y2 - y1));
  34.         const float dvdy = ((V2 - V1) / (y2 - y1));
  35.  
  36.         const unsigned int intY1 = (unsigned int)floor(y1);
  37.         const unsigned int intY2 = (unsigned int)y2;
  38.  
  39.         for (unsigned int y = intY1; y < intY2; y++)
  40.         {
  41.             if (y < SCRHEIGHT&&y>0)
  42.             {
  43.                 if (x1 < xMin[y])
  44.                 {
  45.                     xMin[y] = x1;
  46.                     zMin[y] = z1;
  47.                     uMin[y] = U1;
  48.                     vMin[y] = V1;
  49.                 }
  50.                 if (x1 > xMax[y])
  51.                 {
  52.                     xMax[y] = x1;
  53.                     zMax[y] = z1;
  54.                     uMax[y] = U1;
  55.                     vMax[y] = V1;
  56.                 }
  57.             }
  58.             z1 += dzdy;
  59.             U1 += dudy;
  60.             V1 += dvdy;
  61.             x1 += dxdy;
  62.         }
  63.         yMin = min(intY1, yMin);
  64.         yMax = max(intY2, yMax);
  65.     }
  66.     float u;
  67.     float v;
  68.     float z;
  69.    
  70.     for (unsigned int y = yMin; y <= yMax; y++)
  71.     {
  72.         const float dudx = (uMax[y] - uMin[y]) / (xMax[y] - xMin[y]);
  73.         const float dvdx = (vMax[y] - vMin[y]) / (xMax[y] - xMin[y]);
  74.         const float dzdx = (zMax[y] - zMin[y]) / (xMax[y] - xMin[y]);
  75.         const float subTexelCorrection = (const float)(((int)(xMin[y] + 1)) - xMin[y]);
  76.         u = uMin[y] + dudx*subTexelCorrection;
  77.         v = vMin[y] + dvdx*subTexelCorrection;
  78.         z = zMin[y] + dzdx*subTexelCorrection;
  79.         for (unsigned int x = xMin[y]; x < xMax[y]; x++)
  80.         {
  81.             z += dzdx;
  82.             u += dudx;
  83.             v += dvdx;
  84.             const unsigned int screenBufferIndex = y* SCRWIDTH + (int)x;
  85.             if (ZBuffer[screenBufferIndex] < z)
  86.             {
  87.                 const float RealZ = 1.0f / z;
  88.                 const float FinalU = u *RealZ;
  89.                 const float FinalV = 1.0f - v *RealZ;
  90.                 const int textureBufferIndex = max(0, ((const int)(textureWidth*(FinalU)) % textureWidth) + (const int)(textureHeight*FinalV)*textureWidth % (textureWidth*textureHeight));
  91.                 mScreen->GetBuffer()[screenBufferIndex] = Texture->GetBuffer()[textureBufferIndex];
  92.                 ZBuffer[screenBufferIndex] = z;
  93.             }
  94.         }
  95.     }
  96.     memset(xMax, 0, 640 * sizeof(int));
  97.     std::fill(xMin, xMin + 640, SCRWIDTH - 1);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement