Raster.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 tex = t1 + dx * (ix - v1.x_) + dy * (iy - v1.y_);
  362. if (!cb(param, ix, iy, tex, 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. if (!cb(param, ix, iy, tex, dx, dy, 1.0))
  388. {
  389. // early out.
  390. return false;
  391. }
  392. }
  393. CX1 -= FDY12;
  394. CX2 -= FDY23;
  395. CX3 -= FDY31;
  396. tex += dx;
  397. }
  398. CY1 += FDX12;
  399. CY2 += FDX23;
  400. CY3 += FDX31;
  401. texRow += dy;
  402. }
  403. }
  404. }
  405. }
  406. return true;
  407. }
  408. #define PX_INSIDE 1.0f/sqrt(2.0f)
  409. #define PX_OUTSIDE -1.0f/sqrt(2.0f)
  410. #define BK_SIZE 8
  411. #define BK_INSIDE sqrt(BK_SIZE*BK_SIZE/2.0f)
  412. #define BK_OUTSIDE -sqrt(BK_SIZE*BK_SIZE/2.0f)
  413. // extents has to be multiple of BK_SIZE!!
  414. bool Triangle::drawAA(const Vector2& extents, bool enableScissors, RasterSamplingCallback cb, void * param)
  415. {
  416. float minx, miny, maxx, maxy;
  417. if (enableScissors) {
  418. // Bounding rectangle
  419. minx = floorf(_max(_min3(v1.x_, v2.x_, v3.x_), 0.0f));
  420. miny = floorf(_max(_min3(v1.y_, v2.y_, v3.y_), 0.0f));
  421. maxx = ceilf( _min(_max3(v1.x_, v2.x_, v3.x_), extents.x_-1.0f));
  422. maxy = ceilf( _min(_max3(v1.y_, v2.y_, v3.y_), extents.y_-1.0f));
  423. }
  424. else {
  425. // Bounding rectangle
  426. minx = floorf(_min3(v1.x_, v2.x_, v3.x_));
  427. miny = floorf(_min3(v1.y_, v2.y_, v3.y_));
  428. maxx = ceilf( _max3(v1.x_, v2.x_, v3.x_));
  429. maxy = ceilf( _max3(v1.y_, v2.y_, v3.y_));
  430. }
  431. // There's no reason to align the blocks to the viewport, instead we align them to the origin of the triangle bounds.
  432. minx = floorf(minx);
  433. miny = floorf(miny);
  434. //minx = (float)(((int)minx) & (~((int)BK_SIZE - 1))); // align to blocksize (we don't need to worry about blocks partially out of viewport)
  435. //miny = (float)(((int)miny) & (~((int)BK_SIZE - 1)));
  436. minx += 0.5; miny +=0.5; // sampling at texel centers!
  437. maxx += 0.5; maxy +=0.5;
  438. // Half-edge constants
  439. float C1 = n1.x_ * (-v1.x_) + n1.y_ * (-v1.y_);
  440. float C2 = n2.x_ * (-v2.x_) + n2.y_ * (-v2.y_);
  441. float C3 = n3.x_ * (-v3.x_) + n3.y_ * (-v3.y_);
  442. // Loop through blocks
  443. for(float y0 = miny; y0 <= maxy; y0 += BK_SIZE)
  444. {
  445. for(float x0 = minx; x0 <= maxx; x0 += BK_SIZE)
  446. {
  447. // Corners of block
  448. float xc = (x0 + (BK_SIZE-1)/2.0f);
  449. float yc = (y0 + (BK_SIZE-1)/2.0f);
  450. // Evaluate half-space functions
  451. float aC = C1 + n1.x_ * xc + n1.y_ * yc;
  452. float bC = C2 + n2.x_ * xc + n2.y_ * yc;
  453. float cC = C3 + n3.x_ * xc + n3.y_ * yc;
  454. // Skip block when outside an edge
  455. if( (aC <= BK_OUTSIDE) || (bC <= BK_OUTSIDE) || (cC <= BK_OUTSIDE) ) continue;
  456. // Accept whole block when totally covered
  457. if( (aC >= BK_INSIDE) && (bC >= BK_INSIDE) && (cC >= BK_INSIDE) )
  458. {
  459. Vector3 texRow = t1 + dy*(y0 - v1.y_) + dx*(x0 - v1.x_);
  460. for (float y = y0; y < y0 + BK_SIZE; y++)
  461. {
  462. Vector3 tex = texRow;
  463. for(float x = x0; x < x0 + BK_SIZE; x++)
  464. {
  465. if (!cb(param, (int)x, (int)y, tex, dx, dy, 1.0f))
  466. {
  467. return false;
  468. }
  469. tex += dx;
  470. }
  471. texRow += dy;
  472. }
  473. }
  474. else // Partially covered block
  475. {
  476. float CY1 = C1 + n1.x_ * x0 + n1.y_ * y0;
  477. float CY2 = C2 + n2.x_ * x0 + n2.y_ * y0;
  478. float CY3 = C3 + n3.x_ * x0 + n3.y_ * y0;
  479. Vector3 texRow = t1 + dy*(y0 - v1.y_) + dx*(x0 - v1.x_);
  480. for(float y = y0; y < y0 + BK_SIZE; y++)
  481. {
  482. float CX1 = CY1;
  483. float CX2 = CY2;
  484. float CX3 = CY3;
  485. Vector3 tex = texRow;
  486. for (float x = x0; x < x0 + BK_SIZE; x++)
  487. {
  488. if (CX1 >= PX_INSIDE && CX2 >= PX_INSIDE && CX3 >= PX_INSIDE)
  489. {
  490. // pixel completely covered
  491. Vector3 tex = t1 + dx * (x - v1.x_) + dy * (y - v1.y_);
  492. if (!cb(param, (int)x, (int)y, tex, dx, dy, 1.0f))
  493. {
  494. return false;
  495. }
  496. }
  497. else if ((CX1 >= PX_OUTSIDE) && (CX2 >= PX_OUTSIDE) && (CX3 >= PX_OUTSIDE))
  498. {
  499. // triangle partially covers pixel. do clipping.
  500. ClippedTriangle ct(v1-Vector2(x,y), v2-Vector2(x,y), v3-Vector2(x,y));
  501. ct.clipAABox(-0.5, -0.5, 0.5, 0.5);
  502. Vector2 centroid = ct.centroid();
  503. float area = ct.area();
  504. if (area > 0.0f)
  505. {
  506. Vector3 texCent = tex - dx*centroid.x_ - dy*centroid.y_;
  507. //nvCheck(texCent.x_ >= -0.1f && texCent.x_ <= 1.1f); // @@ Centroid is not very exact...
  508. //nvCheck(texCent.y_ >= -0.1f && texCent.y_ <= 1.1f);
  509. //nvCheck(texCent.z >= -0.1f && texCent.z <= 1.1f);
  510. //Vector3 texCent2 = t1 + dx * (x - v1.x_) + dy * (y - v1.y_);
  511. if (!cb(param, (int)x, (int)y, texCent, dx, dy, area))
  512. {
  513. return false;
  514. }
  515. }
  516. }
  517. CX1 += n1.x_;
  518. CX2 += n2.x_;
  519. CX3 += n3.x_;
  520. tex += dx;
  521. }
  522. CY1 += n1.y_;
  523. CY2 += n2.y_;
  524. CY3 += n3.y_;
  525. texRow += dy;
  526. }
  527. }
  528. }
  529. }
  530. return true;
  531. }
  532. } // namespace
  533. /// Process the given triangle.
  534. bool Raster::DrawTriangle(bool antialias, const Vector2& extents, bool enableScissors, const Vector2 v[3], RasterSamplingCallback cb, void* param)
  535. {
  536. Triangle tri(v[0], v[1], v[2], Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1));
  537. if (tri.valid) {
  538. if (antialias) {
  539. return tri.drawAA(extents, enableScissors, cb, param);
  540. } else {
  541. return tri.draw(extents, enableScissors, cb, param);
  542. }
  543. }
  544. return true;
  545. }
  546. inline float triangleArea(const Vector2& v1, const Vector2& v2, const Vector2& v3)
  547. {
  548. 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_);
  549. }
  550. /// Process the given quad.
  551. bool Raster::DrawQuad(bool antialias, const Vector2& extents, bool enableScissors, const Vector2 v[4], RasterSamplingCallback cb, void * param)
  552. {
  553. bool sign0 = triangleArea(v[0], v[1], v[2]) > 0.0f;
  554. bool sign1 = triangleArea(v[0], v[2], v[3]) > 0.0f;
  555. // Divide the quad into two non overlapping triangles.
  556. if (sign0 == sign1) {
  557. Triangle tri0(v[0], v[1], v[2], Vector3(0,0,0), Vector3(1,0,0), Vector3(1,1,0));
  558. Triangle tri1(v[0], v[2], v[3], Vector3(0,0,0), Vector3(1,1,0), Vector3(0,1,0));
  559. if (tri0.valid && tri1.valid) {
  560. if (antialias) {
  561. return tri0.drawAA(extents, enableScissors, cb, param) && tri1.drawAA(extents, enableScissors, cb, param);
  562. } else {
  563. return tri0.draw(extents, enableScissors, cb, param) && tri1.draw(extents, enableScissors, cb, param);
  564. }
  565. }
  566. }
  567. else
  568. {
  569. Triangle tri0(v[0], v[1], v[3], Vector3(0,0,0), Vector3(1,0,0), Vector3(0,1,0));
  570. Triangle tri1(v[1], v[2], v[3], Vector3(1,0,0), Vector3(1,1,0), Vector3(0,1,0));
  571. if (tri0.valid && tri1.valid) {
  572. if (antialias) {
  573. return tri0.drawAA(extents, enableScissors, cb, param) && tri1.drawAA(extents, enableScissors, cb, param);
  574. } else {
  575. return tri0.draw(extents, enableScissors, cb, param) && tri1.draw(extents, enableScissors, cb, param);
  576. }
  577. }
  578. }
  579. return true;
  580. }
  581. }