Raster.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #include "Raster.h"
  2. #ifdef ATOMIC_PLATFORM_WINDOWS
  3. #include <float.h>
  4. #endif
  5. namespace AtomicGlow
  6. {
  7. namespace
  8. {
  9. /// Return the maximum of the two arguments. For floating point values, it returns the second value if the first is NaN.
  10. template <typename T>
  11. inline const T & _max(const T & a, const T & b)
  12. {
  13. return (b < a) ? a : b;
  14. }
  15. /// Return the maximum of the three arguments.
  16. template <typename T>
  17. inline const T & _max3(const T & a, const T & b, const T & c)
  18. {
  19. return _max(a, _max(b, c));
  20. }
  21. /// Return the minimum of two values.
  22. template <typename T>
  23. inline const T & _min(const T & a, const T & b)
  24. {
  25. return (a < b) ? a : b;
  26. }
  27. /// Return the maximum of the three arguments.
  28. template <typename T>
  29. inline const T & _min3(const T & a, const T & b, const T & c)
  30. {
  31. return _min(a, _min(b, c));
  32. }
  33. /// Clamp between two values.
  34. template <typename T>
  35. inline const T & _clamp(const T & x, const T & a, const T & b)
  36. {
  37. return _min(_max(x, a), b);
  38. }
  39. inline bool isFinite(const float f)
  40. {
  41. #ifdef ATOMIC_PLATFORM_WINDOWS
  42. return _finite(f) != 0;
  43. #endif
  44. #ifdef ATOMIC_PLATFORM_OSX
  45. return isfinite(f);
  46. #endif
  47. #ifdef ATOMIC_PLATFORM_LINUX
  48. return finitef(f);
  49. #endif
  50. }
  51. static inline float delta(float bot, float top, float ih)
  52. {
  53. return (bot - top) * ih;
  54. }
  55. static inline Vector2 delta(const Vector2& bot, const Vector2& top, float ih)
  56. {
  57. return (bot - top) * ih;
  58. }
  59. static inline Vector3 delta(const Vector3& bot, const Vector3& top, float ih)
  60. {
  61. return (bot - top) * ih;
  62. }
  63. // @@ The implementation in nvmath.h should be equivalent.
  64. static inline int iround(float f)
  65. {
  66. // @@ Optimize this.
  67. return int(floorf(f+0.5f));
  68. //return int(round(f));
  69. //return int(f);
  70. }
  71. class ClippedTriangle
  72. {
  73. public:
  74. ClippedTriangle(const Vector2& a, const Vector2& b, const Vector2& c)
  75. {
  76. m_numVertices = 3;
  77. m_activeVertexBuffer = 0;
  78. m_verticesA[0]=a;
  79. m_verticesA[1]=b;
  80. m_verticesA[2]=c;
  81. m_vertexBuffers[0] = m_verticesA;
  82. m_vertexBuffers[1] = m_verticesB;
  83. }
  84. unsigned vertexCount()
  85. {
  86. return m_numVertices;
  87. }
  88. const Vector2 * vertices()
  89. {
  90. return m_vertexBuffers[m_activeVertexBuffer];
  91. }
  92. inline void clipHorizontalPlane(float offset, float clipdirection)
  93. {
  94. Vector2 * v = m_vertexBuffers[m_activeVertexBuffer];
  95. m_activeVertexBuffer ^= 1;
  96. Vector2 * v2 = m_vertexBuffers[m_activeVertexBuffer];
  97. v[m_numVertices] = v[0];
  98. float dy2, dy1 = offset - v[0].y_;
  99. int dy2in, dy1in = clipdirection*dy1 >= 0;
  100. unsigned p=0;
  101. for (unsigned k=0; k<m_numVertices; k++)
  102. {
  103. dy2 = offset - v[k+1].y_;
  104. dy2in = clipdirection*dy2 >= 0;
  105. if (dy1in) v2[p++] = v[k];
  106. if ( dy1in + dy2in == 1 ) // not both in/out
  107. {
  108. float dx = v[k+1].x_ - v[k].x_;
  109. float dy = v[k+1].y_ - v[k].y_;
  110. v2[p++] = Vector2(v[k].x_ + dy1*(dx/dy), offset);
  111. }
  112. dy1 = dy2; dy1in = dy2in;
  113. }
  114. m_numVertices = p;
  115. //for (uint k=0; k<m_numVertices; k++) printf("(%f, %f)\n", v2[k].x, v2[k].y); printf("\n");
  116. }
  117. inline void clipVerticalPlane(float offset, float clipdirection )
  118. {
  119. Vector2 * v = m_vertexBuffers[m_activeVertexBuffer];
  120. m_activeVertexBuffer ^= 1;
  121. Vector2 * v2 = m_vertexBuffers[m_activeVertexBuffer];
  122. v[m_numVertices] = v[0];
  123. float dx2, dx1 = offset - v[0].x_;
  124. int dx2in, dx1in = clipdirection*dx1 >= 0;
  125. unsigned p=0;
  126. for (unsigned k=0; k<m_numVertices; k++)
  127. {
  128. dx2 = offset - v[k+1].x_;
  129. dx2in = clipdirection*dx2 >= 0;
  130. if (dx1in) v2[p++] = v[k];
  131. if ( dx1in + dx2in == 1 ) // not both in/out
  132. {
  133. float dx = v[k+1].x_ - v[k].x_;
  134. float dy = v[k+1].y_ - v[k].y_;
  135. v2[p++] = Vector2(offset, v[k].y_ + dx1*(dy/dx));
  136. }
  137. dx1 = dx2; dx1in = dx2in;
  138. }
  139. m_numVertices = p;
  140. //for (uint k=0; k<m_numVertices; k++) printf("(%f, %f)\n", v2[k].x, v2[k].y); printf("\n");
  141. }
  142. void computeAreaCentroid()
  143. {
  144. Vector2 * v = m_vertexBuffers[m_activeVertexBuffer];
  145. v[m_numVertices] = v[0];
  146. m_area = 0;
  147. float centroidx=0, centroidy=0;
  148. for (unsigned k=0; k<m_numVertices; k++)
  149. {
  150. // http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
  151. float f = v[k].x_*v[k+1].y_ - v[k+1].x_*v[k].y_;
  152. m_area += f;
  153. centroidx += f * (v[k].x_ + v[k+1].x_);
  154. centroidy += f * (v[k].y_ + v[k+1].y_);
  155. }
  156. m_area = 0.5f * fabs(m_area);
  157. if (m_area==0) {
  158. m_centroid = Vector2(0.0f, 0.0f);
  159. } else {
  160. m_centroid = Vector2(centroidx/(6*m_area), centroidy/(6*m_area));
  161. }
  162. }
  163. void clipAABox(float x0, float y0, float x1, float y1)
  164. {
  165. clipVerticalPlane ( x0, -1);
  166. clipHorizontalPlane( y0, -1);
  167. clipVerticalPlane ( x1, 1);
  168. clipHorizontalPlane( y1, 1);
  169. computeAreaCentroid();
  170. }
  171. Vector2 centroid()
  172. {
  173. return m_centroid;
  174. }
  175. float area()
  176. {
  177. return m_area;
  178. }
  179. private:
  180. Vector2 m_verticesA[7+1];
  181. Vector2 m_verticesB[7+1];
  182. Vector2 * m_vertexBuffers[2];
  183. unsigned m_numVertices;
  184. unsigned m_activeVertexBuffer;
  185. float m_area;
  186. Vector2 m_centroid;
  187. };
  188. /// A triangle vertex.
  189. struct Vertex
  190. {
  191. Vector2 pos; // Position.
  192. Vector3 tex; // Texcoord. (Barycentric coordinate)
  193. };
  194. /// A triangle for rasterization.
  195. struct Triangle
  196. {
  197. Triangle(const Vector2& v0, const Vector2& v1, const Vector2& v2, const Vector3& t0, const Vector3& t1, const Vector3& t2);
  198. bool computeDeltas();
  199. bool draw(const Vector2& extents, bool enableScissors, RasterSamplingCallback cb, void *param);
  200. bool drawAA(const Vector2& extents, bool enableScissors, RasterSamplingCallback cb, void *param);
  201. void flipBackface();
  202. void computeUnitInwardNormals();
  203. // Vertices.
  204. Vector2 v1, v2, v3;
  205. Vector2 n1, n2, n3; // unit inward normals
  206. Vector3 t1, t2, t3;
  207. // Deltas.
  208. Vector3 dx, dy;
  209. float sign;
  210. bool valid;
  211. };
  212. /// Triangle ctor.
  213. Triangle::Triangle(const Vector2& v0, const Vector2& v1, const Vector2& v2, const Vector3& t0, const Vector3& t1, const Vector3& t2)
  214. {
  215. // Init vertices.
  216. this->v1 = v0;
  217. this->v2 = v2;
  218. this->v3 = v1;
  219. // Set barycentric coordinates.
  220. this->t1 = t0;
  221. this->t2 = t2;
  222. this->t3 = t1;
  223. // make sure every triangle is front facing.
  224. flipBackface();
  225. // Compute deltas.
  226. valid = computeDeltas();
  227. computeUnitInwardNormals();
  228. }
  229. /// Compute texture space deltas.
  230. /// This method takes two edge vectors that form a basis, determines the
  231. /// coordinates of the canonic vectors in that basis, and computes the
  232. /// texture gradient that corresponds to those vectors.
  233. bool Triangle::computeDeltas()
  234. {
  235. Vector2 e0 = v3 - v1;
  236. Vector2 e1 = v2 - v1;
  237. Vector3 de0 = t3 - t1;
  238. Vector3 de1 = t2 - t1;
  239. float denom = 1.0f / (e0.y_ * e1.x_ - e1.y_ * e0.x_);
  240. if (!isFinite(denom)) {
  241. return false;
  242. }
  243. float lambda1 = - e1.y_ * denom;
  244. float lambda2 = e0.y_ * denom;
  245. float lambda3 = e1.x_ * denom;
  246. float lambda4 = - e0.x_ * denom;
  247. dx = de0 * lambda1 + de1 * lambda2;
  248. dy = de0 * lambda3 + de1 * lambda4;
  249. return true;
  250. }
  251. // compute unit inward normals for each edge.
  252. void Triangle::computeUnitInwardNormals()
  253. {
  254. n1 = v1 - v2; n1 = Vector2(-n1.y_, n1.x_); n1 = n1 * (1.0f/sqrtf(n1.x_*n1.x_ + n1.y_*n1.y_));
  255. n2 = v2 - v3; n2 = Vector2(-n2.y_, n2.x_); n2 = n2 * (1.0f/sqrtf(n2.x_*n2.x_ + n2.y_*n2.y_));
  256. n3 = v3 - v1; n3 = Vector2(-n3.y_, n3.x_); n3 = n3 * (1.0f/sqrtf(n3.x_*n3.x_ + n3.y_*n3.y_));
  257. }
  258. void Triangle::flipBackface()
  259. {
  260. // check if triangle is backfacing, if so, swap two vertices
  261. if ( ((v3.x_-v1.x_)*(v2.y_-v1.y_) - (v3.y_-v1.y_)*(v2.x_-v1.x_)) < 0 ) {
  262. Vector2 hv=v1; v1=v2; v2=hv; // swap pos
  263. Vector3 ht=t1; t1=t2; t2=ht; // swap tex
  264. }
  265. }
  266. bool Triangle::draw(const Vector2 & extents, bool enableScissors, RasterSamplingCallback cb, void * param)
  267. {
  268. // 28.4 fixed-point coordinates
  269. const int Y1 = iround(16.0f * v1.y_);
  270. const int Y2 = iround(16.0f * v2.y_);
  271. const int Y3 = iround(16.0f * v3.y_);
  272. const int X1 = iround(16.0f * v1.x_);
  273. const int X2 = iround(16.0f * v2.x_);
  274. const int X3 = iround(16.0f * v3.x_);
  275. // Deltas
  276. const int DX12 = X1 - X2;
  277. const int DX23 = X2 - X3;
  278. const int DX31 = X3 - X1;
  279. const int DY12 = Y1 - Y2;
  280. const int DY23 = Y2 - Y3;
  281. const int DY31 = Y3 - Y1;
  282. // Fixed-point deltas
  283. const int FDX12 = DX12 << 4;
  284. const int FDX23 = DX23 << 4;
  285. const int FDX31 = DX31 << 4;
  286. const int FDY12 = DY12 << 4;
  287. const int FDY23 = DY23 << 4;
  288. const int FDY31 = DY31 << 4;
  289. int minx, miny, maxx, maxy;
  290. if (enableScissors) {
  291. int frustumX0 = 0 << 4;
  292. int frustumY0 = 0 << 4;
  293. int frustumX1 = (int)extents.x_ << 4;
  294. int frustumY1 = (int)extents.y_ << 4;
  295. // Bounding rectangle
  296. minx = (_max(_min3(X1, X2, X3), frustumX0) + 0xF) >> 4;
  297. miny = (_max(_min3(Y1, Y2, Y3), frustumY0) + 0xF) >> 4;
  298. maxx = (_min(_max3(X1, X2, X3), frustumX1) + 0xF) >> 4;
  299. maxy = (_min(_max3(Y1, Y2, Y3), frustumY1) + 0xF) >> 4;
  300. }
  301. else {
  302. // Bounding rectangle
  303. minx = (_min3(X1, X2, X3) + 0xF) >> 4;
  304. miny = (_min3(Y1, Y2, Y3) + 0xF) >> 4;
  305. maxx = (_max3(X1, X2, X3) + 0xF) >> 4;
  306. maxy = (_max3(Y1, Y2, Y3) + 0xF) >> 4;
  307. }
  308. // Block size, standard 8x8 (must be power of two)
  309. const int q = 8;
  310. // @@ This won't work when minx,miny are negative. This code path is not used. Leaving as is for now.
  311. assert(minx >= 0);
  312. assert(miny >= 0);
  313. // Start in corner of 8x8 block
  314. minx &= ~(q - 1);
  315. miny &= ~(q - 1);
  316. // Half-edge constants
  317. int C1 = DY12 * X1 - DX12 * Y1;
  318. int C2 = DY23 * X2 - DX23 * Y2;
  319. int C3 = DY31 * X3 - DX31 * Y3;
  320. // Correct for fill convention
  321. if(DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++;
  322. if(DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++;
  323. if(DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++;
  324. // Loop through blocks
  325. for(int y = miny; y < maxy; y += q)
  326. {
  327. for(int x = minx; x < maxx; x += q)
  328. {
  329. // Corners of block
  330. int x0 = x << 4;
  331. int x1 = (x + q - 1) << 4;
  332. int y0 = y << 4;
  333. int y1 = (y + q - 1) << 4;
  334. // Evaluate half-space functions
  335. bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0;
  336. bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0;
  337. bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0;
  338. bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0;
  339. int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3);
  340. bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0;
  341. bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0;
  342. bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0;
  343. bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0;
  344. int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3);
  345. bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0;
  346. bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0;
  347. bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0;
  348. bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0;
  349. int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3);
  350. // Skip block when outside an edge
  351. if(a == 0x0 || b == 0x0 || c == 0x0) continue;
  352. // Accept whole block when totally covered
  353. if(a == 0xF && b == 0xF && c == 0xF)
  354. {
  355. Vector3 texRow = t1 + dy*(y0 - v1.y_) + dx*(x0 - v1.x_);
  356. for(int iy = y; iy < y + q; iy++)
  357. {
  358. Vector3 tex = texRow;
  359. for(int ix = x; ix < x + q; ix++)
  360. {
  361. Vector3 tex2 = t1 + dx * (ix - v1.x_) + dy * (iy - v1.y_);
  362. if (!cb(param, ix, iy, tex2, dx, dy, 1.0)) {
  363. // early out.
  364. return false;
  365. }
  366. tex += dx;
  367. }
  368. texRow += dy;
  369. }
  370. }
  371. else // Partially covered block
  372. {
  373. int CY1 = C1 + DX12 * y0 - DY12 * x0;
  374. int CY2 = C2 + DX23 * y0 - DY23 * x0;
  375. int CY3 = C3 + DX31 * y0 - DY31 * x0;
  376. Vector3 texRow = t1 + dy*(y0 - v1.y_) + dx*(x0 - v1.x_);
  377. for(int iy = y; iy < y + q; iy++)
  378. {
  379. int CX1 = CY1;
  380. int CX2 = CY2;
  381. int CX3 = CY3;
  382. Vector3 tex = texRow;
  383. for(int ix = x; ix < x + q; ix++)
  384. {
  385. if(CX1 > 0 && CX2 > 0 && CX3 > 0)
  386. {
  387. Vector3 tex2 = t1 + dx * (ix - v1.x_) + dy * (iy - v1.y_);
  388. if (!cb(param, ix, iy, tex2, dx, dy, 1.0))
  389. {
  390. // early out.
  391. return false;
  392. }
  393. }
  394. CX1 -= FDY12;
  395. CX2 -= FDY23;
  396. CX3 -= FDY31;
  397. tex += dx;
  398. }
  399. CY1 += FDX12;
  400. CY2 += FDX23;
  401. CY3 += FDX31;
  402. texRow += dy;
  403. }
  404. }
  405. }
  406. }
  407. return true;
  408. }
  409. #define PX_INSIDE 1.0f/sqrt(2.0f)
  410. #define PX_OUTSIDE -1.0f/sqrt(2.0f)
  411. #define BK_SIZE 8
  412. #define BK_INSIDE sqrt(BK_SIZE*BK_SIZE/2.0f)
  413. #define BK_OUTSIDE -sqrt(BK_SIZE*BK_SIZE/2.0f)
  414. // extents has to be multiple of BK_SIZE!!
  415. bool Triangle::drawAA(const Vector2& extents, bool enableScissors, RasterSamplingCallback cb, void * param)
  416. {
  417. float minx, miny, maxx, maxy;
  418. if (enableScissors) {
  419. // Bounding rectangle
  420. minx = floorf(_max(_min3(v1.x_, v2.x_, v3.x_), 0.0f));
  421. miny = floorf(_max(_min3(v1.y_, v2.y_, v3.y_), 0.0f));
  422. maxx = ceilf( _min(_max3(v1.x_, v2.x_, v3.x_), extents.x_-1.0f));
  423. maxy = ceilf( _min(_max3(v1.y_, v2.y_, v3.y_), extents.y_-1.0f));
  424. }
  425. else {
  426. // Bounding rectangle
  427. minx = floorf(_min3(v1.x_, v2.x_, v3.x_));
  428. miny = floorf(_min3(v1.y_, v2.y_, v3.y_));
  429. maxx = ceilf( _max3(v1.x_, v2.x_, v3.x_));
  430. maxy = ceilf( _max3(v1.y_, v2.y_, v3.y_));
  431. }
  432. // There's no reason to align the blocks to the viewport, instead we align them to the origin of the triangle bounds.
  433. minx = floorf(minx);
  434. miny = floorf(miny);
  435. //minx = (float)(((int)minx) & (~((int)BK_SIZE - 1))); // align to blocksize (we don't need to worry about blocks partially out of viewport)
  436. //miny = (float)(((int)miny) & (~((int)BK_SIZE - 1)));
  437. minx += 0.5; miny +=0.5; // sampling at texel centers!
  438. maxx += 0.5; maxy +=0.5;
  439. // Half-edge constants
  440. float C1 = n1.x_ * (-v1.x_) + n1.y_ * (-v1.y_);
  441. float C2 = n2.x_ * (-v2.x_) + n2.y_ * (-v2.y_);
  442. float C3 = n3.x_ * (-v3.x_) + n3.y_ * (-v3.y_);
  443. // Loop through blocks
  444. for(float y0 = miny; y0 <= maxy; y0 += BK_SIZE)
  445. {
  446. for(float x0 = minx; x0 <= maxx; x0 += BK_SIZE)
  447. {
  448. // Corners of block
  449. float xc = (x0 + (BK_SIZE-1)/2.0f);
  450. float yc = (y0 + (BK_SIZE-1)/2.0f);
  451. // Evaluate half-space functions
  452. float aC = C1 + n1.x_ * xc + n1.y_ * yc;
  453. float bC = C2 + n2.x_ * xc + n2.y_ * yc;
  454. float cC = C3 + n3.x_ * xc + n3.y_ * yc;
  455. // Skip block when outside an edge
  456. if( (aC <= BK_OUTSIDE) || (bC <= BK_OUTSIDE) || (cC <= BK_OUTSIDE) ) continue;
  457. // Accept whole block when totally covered
  458. if( (aC >= BK_INSIDE) && (bC >= BK_INSIDE) && (cC >= BK_INSIDE) )
  459. {
  460. Vector3 texRow = t1 + dy*(y0 - v1.y_) + dx*(x0 - v1.x_);
  461. for (float y = y0; y < y0 + BK_SIZE; y++)
  462. {
  463. Vector3 tex = texRow;
  464. for(float x = x0; x < x0 + BK_SIZE; x++)
  465. {
  466. if (!cb(param, (int)x, (int)y, tex, dx, dy, 1.0f))
  467. {
  468. return false;
  469. }
  470. tex += dx;
  471. }
  472. texRow += dy;
  473. }
  474. }
  475. else // Partially covered block
  476. {
  477. float CY1 = C1 + n1.x_ * x0 + n1.y_ * y0;
  478. float CY2 = C2 + n2.x_ * x0 + n2.y_ * y0;
  479. float CY3 = C3 + n3.x_ * x0 + n3.y_ * y0;
  480. Vector3 texRow = t1 + dy*(y0 - v1.y_) + dx*(x0 - v1.x_);
  481. for(float y = y0; y < y0 + BK_SIZE; y++)
  482. {
  483. float CX1 = CY1;
  484. float CX2 = CY2;
  485. float CX3 = CY3;
  486. Vector3 tex = texRow;
  487. for (float x = x0; x < x0 + BK_SIZE; x++)
  488. {
  489. if (CX1 >= PX_INSIDE && CX2 >= PX_INSIDE && CX3 >= PX_INSIDE)
  490. {
  491. // pixel completely covered
  492. Vector3 tex = t1 + dx * (x - v1.x_) + dy * (y - v1.y_);
  493. if (!cb(param, (int)x, (int)y, tex, dx, dy, 1.0f))
  494. {
  495. return false;
  496. }
  497. }
  498. else if ((CX1 >= PX_OUTSIDE) && (CX2 >= PX_OUTSIDE) && (CX3 >= PX_OUTSIDE))
  499. {
  500. // triangle partially covers pixel. do clipping.
  501. ClippedTriangle ct(v1-Vector2(x,y), v2-Vector2(x,y), v3-Vector2(x,y));
  502. ct.clipAABox(-0.5, -0.5, 0.5, 0.5);
  503. Vector2 centroid = ct.centroid();
  504. float area = ct.area();
  505. if (area > 0.0f)
  506. {
  507. Vector3 texCent = tex - dx*centroid.x_ - dy*centroid.y_;
  508. //nvCheck(texCent.x_ >= -0.1f && texCent.x_ <= 1.1f); // @@ Centroid is not very exact...
  509. //nvCheck(texCent.y_ >= -0.1f && texCent.y_ <= 1.1f);
  510. //nvCheck(texCent.z >= -0.1f && texCent.z <= 1.1f);
  511. //Vector3 texCent2 = t1 + dx * (x - v1.x_) + dy * (y - v1.y_);
  512. if (!cb(param, (int)x, (int)y, texCent, dx, dy, area))
  513. {
  514. return false;
  515. }
  516. }
  517. }
  518. CX1 += n1.x_;
  519. CX2 += n2.x_;
  520. CX3 += n3.x_;
  521. tex += dx;
  522. }
  523. CY1 += n1.y_;
  524. CY2 += n2.y_;
  525. CY3 += n3.y_;
  526. texRow += dy;
  527. }
  528. }
  529. }
  530. }
  531. return true;
  532. }
  533. } // namespace
  534. /// Process the given triangle.
  535. bool Raster::DrawTriangle(bool antialias, const Vector2& extents, bool enableScissors, const Vector2 v[3], RasterSamplingCallback cb, void* param)
  536. {
  537. Triangle tri(v[0], v[1], v[2], Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1));
  538. if (tri.valid) {
  539. if (antialias) {
  540. return tri.drawAA(extents, enableScissors, cb, param);
  541. } else {
  542. return tri.draw(extents, enableScissors, cb, param);
  543. }
  544. }
  545. return true;
  546. }
  547. inline float triangleArea(const Vector2& v1, const Vector2& v2, const Vector2& v3)
  548. {
  549. return 0.5f * (v3.x_ * v1.y_ + v1.x_ * v2.y_ + v2.x_ * v3.y_ - v2.x_ * v1.y_ - v3.x_ * v2.y_ - v1.x_ * v3.y_);
  550. }
  551. /// Process the given quad.
  552. bool Raster::DrawQuad(bool antialias, const Vector2& extents, bool enableScissors, const Vector2 v[4], RasterSamplingCallback cb, void * param)
  553. {
  554. bool sign0 = triangleArea(v[0], v[1], v[2]) > 0.0f;
  555. bool sign1 = triangleArea(v[0], v[2], v[3]) > 0.0f;
  556. // Divide the quad into two non overlapping triangles.
  557. if (sign0 == sign1) {
  558. Triangle tri0(v[0], v[1], v[2], Vector3(0,0,0), Vector3(1,0,0), Vector3(1,1,0));
  559. Triangle tri1(v[0], v[2], v[3], Vector3(0,0,0), Vector3(1,1,0), Vector3(0,1,0));
  560. if (tri0.valid && tri1.valid) {
  561. if (antialias) {
  562. return tri0.drawAA(extents, enableScissors, cb, param) && tri1.drawAA(extents, enableScissors, cb, param);
  563. } else {
  564. return tri0.draw(extents, enableScissors, cb, param) && tri1.draw(extents, enableScissors, cb, param);
  565. }
  566. }
  567. }
  568. else
  569. {
  570. Triangle tri0(v[0], v[1], v[3], Vector3(0,0,0), Vector3(1,0,0), Vector3(0,1,0));
  571. Triangle tri1(v[1], v[2], v[3], Vector3(1,0,0), Vector3(1,1,0), Vector3(0,1,0));
  572. if (tri0.valid && tri1.valid) {
  573. if (antialias) {
  574. return tri0.drawAA(extents, enableScissors, cb, param) && tri1.drawAA(extents, enableScissors, cb, param);
  575. } else {
  576. return tri0.draw(extents, enableScissors, cb, param) && tri1.draw(extents, enableScissors, cb, param);
  577. }
  578. }
  579. }
  580. return true;
  581. }
  582. }