modelAPI.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 to 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #define DSR_INTERNAL_ACCESS
  24. #include "modelAPI.h"
  25. #include "imageAPI.h"
  26. #include "drawAPI.h"
  27. #include "../render/model/Model.h"
  28. #include <limits>
  29. #include "../base/virtualStack.h"
  30. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.isNull()) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  31. namespace dsr {
  32. Model model_create() {
  33. return handle_create<ModelImpl>().setName("Model");
  34. }
  35. Model model_clone(const Model& model) {
  36. MUST_EXIST(model,model_clone);
  37. return handle_create<ModelImpl>(model->filter, model->partBuffer, model->positionBuffer).setName("Cloned Model");
  38. }
  39. void model_setFilter(const Model& model, Filter filter) {
  40. MUST_EXIST(model,model_setFilter);
  41. model->filter = filter;
  42. }
  43. Filter model_getFilter(const Model& model) {
  44. MUST_EXIST(model,model_getFilter);
  45. return model->filter;
  46. }
  47. bool model_exists(const Model& model) {
  48. return model.isNotNull();
  49. }
  50. int model_addEmptyPart(Model& model, const String &name) {
  51. MUST_EXIST(model,model_addEmptyPart);
  52. return model->addEmptyPart(name);
  53. }
  54. int model_getNumberOfParts(const Model& model) {
  55. MUST_EXIST(model,model_getNumberOfParts);
  56. return model->getNumberOfParts();
  57. }
  58. void model_setPartName(Model& model, int partIndex, const String &name) {
  59. MUST_EXIST(model,model_setPartName);
  60. model->setPartName(partIndex, name);
  61. }
  62. String model_getPartName(const Model& model, int partIndex) {
  63. MUST_EXIST(model,model_getPartName);
  64. return model->getPartName(partIndex);
  65. }
  66. int model_getNumberOfPoints(const Model& model) {
  67. MUST_EXIST(model,model_getNumberOfPoints);
  68. return model->getNumberOfPoints();
  69. }
  70. FVector3D model_getPoint(const Model& model, int pointIndex) {
  71. MUST_EXIST(model,model_getPoint);
  72. return model->getPoint(pointIndex);
  73. }
  74. void model_setPoint(Model& model, int pointIndex, const FVector3D& position) {
  75. MUST_EXIST(model,model_setPoint);
  76. model->setPoint(pointIndex, position);
  77. }
  78. int model_findPoint(const Model& model, const FVector3D &position, float threshold) {
  79. MUST_EXIST(model,model_findPoint);
  80. return model->findPoint(position, threshold);
  81. }
  82. int model_addPoint(const Model& model, const FVector3D &position) {
  83. MUST_EXIST(model,model_addPoint);
  84. return model->addPoint(position);
  85. }
  86. int model_addPointIfNeeded(Model& model, const FVector3D &position, float threshold) {
  87. MUST_EXIST(model,model_addPointIfNeeded);
  88. return model->addPointIfNeeded(position, threshold);
  89. }
  90. int model_getVertexPointIndex(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  91. MUST_EXIST(model,model_getVertexPointIndex);
  92. return model->getVertexPointIndex(partIndex, polygonIndex, vertexIndex);
  93. }
  94. void model_setVertexPointIndex(Model& model, int partIndex, int polygonIndex, int vertexIndex, int pointIndex) {
  95. MUST_EXIST(model,model_setVertexPointIndex);
  96. model->setVertexPointIndex(partIndex, polygonIndex, vertexIndex, pointIndex);
  97. }
  98. FVector3D model_getVertexPosition(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  99. MUST_EXIST(model,model_getVertexPosition);
  100. return model->getVertexPosition(partIndex, polygonIndex, vertexIndex);
  101. }
  102. FVector4D model_getVertexColor(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  103. MUST_EXIST(model,model_getVertexColor);
  104. return model->getVertexColor(partIndex, polygonIndex, vertexIndex);
  105. }
  106. void model_setVertexColor(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& color) {
  107. MUST_EXIST(model,model_setVertexColor);
  108. model->setVertexColor(partIndex, polygonIndex, vertexIndex, color);
  109. }
  110. FVector4D model_getTexCoord(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  111. MUST_EXIST(model,model_getTexCoord);
  112. return model->getTexCoord(partIndex, polygonIndex, vertexIndex);
  113. }
  114. void model_setTexCoord(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& texCoord) {
  115. MUST_EXIST(model,model_setTexCoord);
  116. model->setTexCoord(partIndex, polygonIndex, vertexIndex, texCoord);
  117. }
  118. int model_addTriangle(Model& model, int partIndex, int pointA, int pointB, int pointC) {
  119. MUST_EXIST(model,model_addTriangle);
  120. return model->addPolygon(Polygon(pointA, pointB, pointC), partIndex);
  121. }
  122. int model_addQuad(Model& model, int partIndex, int pointA, int pointB, int pointC, int pointD) {
  123. MUST_EXIST(model,model_addQuad);
  124. return model->addPolygon(Polygon(pointA, pointB, pointC, pointD), partIndex);
  125. }
  126. int model_getNumberOfPolygons(const Model& model, int partIndex) {
  127. MUST_EXIST(model,model_getNumberOfPolygons);
  128. return model->getNumberOfPolygons(partIndex);
  129. }
  130. int model_getPolygonVertexCount(const Model& model, int partIndex, int polygonIndex) {
  131. MUST_EXIST(model,model_getPolygonVertexCount);
  132. return model->getPolygonVertexCount(partIndex, polygonIndex);
  133. }
  134. TextureRgbaU8 model_getDiffuseMap(const Model& model, int partIndex) {
  135. MUST_EXIST(model,model_getDiffuseMap);
  136. return model->getDiffuseMap(partIndex);
  137. }
  138. void model_setDiffuseMap(Model& model, int partIndex, const TextureRgbaU8 &diffuseMap) {
  139. MUST_EXIST(model,model_setDiffuseMap);
  140. model->setDiffuseMap(diffuseMap, partIndex);
  141. }
  142. void model_setDiffuseMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename) {
  143. MUST_EXIST(model,model_setDiffuseMapByName);
  144. model->setDiffuseMapByName(pool, filename, partIndex);
  145. }
  146. TextureRgbaU8 model_getLightMap(Model& model, int partIndex) {
  147. MUST_EXIST(model,model_getLightMap);
  148. return model->getLightMap(partIndex);
  149. }
  150. void model_setLightMap(Model& model, int partIndex, const TextureRgbaU8 &lightMap) {
  151. MUST_EXIST(model,model_setLightMap);
  152. model->setLightMap(lightMap, partIndex);
  153. }
  154. void model_setLightMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename) {
  155. MUST_EXIST(model,model_setLightMapByName);
  156. model->setLightMapByName(pool, filename, partIndex);
  157. }
  158. // Single-threaded rendering for the simple cases where you just want it to work
  159. void model_render(const Model& model, const Transform3D &modelToWorldTransform, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer, const Camera &camera) {
  160. if (model.isNotNull()) {
  161. model->render((CommandQueue*)nullptr, image_exists(colorBuffer) ? &colorBuffer : nullptr, image_exists(depthBuffer) ? &depthBuffer : nullptr, modelToWorldTransform, camera);
  162. }
  163. }
  164. void model_renderDepth(const Model& model, const Transform3D &modelToWorldTransform, ImageF32& depthBuffer, const Camera &camera) {
  165. if (model.isNotNull()) {
  166. model->renderDepth(image_exists(depthBuffer) ? &depthBuffer : nullptr, modelToWorldTransform, camera);
  167. }
  168. }
  169. void model_getBoundingBox(const Model& model, FVector3D& minimum, FVector3D& maximum) {
  170. MUST_EXIST(model,model_getBoundingBox);
  171. minimum = model->minBound;
  172. maximum = model->maxBound;
  173. }
  174. static const int cellSize = 16;
  175. struct DebugLine {
  176. int64_t x1, y1, x2, y2;
  177. ColorRgbaI32 color;
  178. DebugLine(int64_t x1, int64_t y1, int64_t x2, int64_t y2, const ColorRgbaI32& color)
  179. : x1(x1), y1(y1), x2(x2), y2(y2), color(color) {}
  180. };
  181. // Context for multi-threaded rendering of triangles in a command queue
  182. struct RendererImpl {
  183. bool receiving = false; // Preventing version dependency by only allowing calls in the expected order
  184. ImageRgbaU8 colorBuffer; // The color image being rendered to
  185. ImageF32 depthBuffer; // Linear depth for isometric cameras, 1 / depth for perspective cameras
  186. ImageF32 depthGrid; // An occlusion grid of cellSize² cells representing the longest linear depth where something might be visible
  187. CommandQueue commandQueue; // Triangles to be drawn
  188. List<DebugLine> debugLines; // Additional lines to be drawn as an overlay for debugging occlusion
  189. int width = 0, height = 0, gridWidth = 0, gridHeight = 0;
  190. bool occluded = false;
  191. RendererImpl() {}
  192. void beginFrame(ImageRgbaU8& colorBuffer, ImageF32& depthBuffer) {
  193. if (this->receiving) {
  194. throwError("Called renderer_begin on the same renderer twice without ending the previous batch!\n");
  195. }
  196. this->receiving = true;
  197. this->colorBuffer = colorBuffer;
  198. this->depthBuffer = depthBuffer;
  199. if (image_exists(this->colorBuffer)) {
  200. this->width = image_getWidth(this->colorBuffer);
  201. this->height = image_getHeight(this->colorBuffer);
  202. } else if (image_exists(this->depthBuffer)) {
  203. this->width = image_getWidth(this->depthBuffer);
  204. this->height = image_getHeight(this->depthBuffer);
  205. }
  206. this->gridWidth = (this->width + (cellSize - 1)) / cellSize;
  207. this->gridHeight = (this->height + (cellSize - 1)) / cellSize;
  208. this->occluded = false;
  209. }
  210. bool pointInsideOfEdge(const LVector2D &edgeA, const LVector2D &edgeB, const LVector2D &point) {
  211. LVector2D edgeDirection = LVector2D(edgeB.y - edgeA.y, edgeA.x - edgeB.x);
  212. LVector2D relativePosition = point - edgeA;
  213. return (edgeDirection.x * relativePosition.x) + (edgeDirection.y * relativePosition.y) <= 0;
  214. }
  215. // Returns true iff the point is inside of the hull
  216. // convexHullCorners from 0 to cornerCount-1 must be sorted clockwise and may not include any concave corners
  217. bool pointInsideOfHull(const ProjectedPoint* convexHullCorners, int cornerCount, const LVector2D &point) {
  218. for (int c = 0; c < cornerCount; c++) {
  219. int nc = c + 1;
  220. if (nc == cornerCount) {
  221. nc = 0;
  222. }
  223. if (!pointInsideOfEdge(convexHullCorners[c].flat, convexHullCorners[nc].flat, point)) {
  224. // Outside of one edge, not inside
  225. return false;
  226. }
  227. }
  228. // Passed all edge tests
  229. return true;
  230. }
  231. // Returns true iff all corners of the rectangle are inside of the hull
  232. bool rectangleInsideOfHull(const ProjectedPoint* convexHullCorners, int cornerCount, const IRect &rectangle) {
  233. return pointInsideOfHull(convexHullCorners, cornerCount, LVector2D(rectangle.left(), rectangle.top()))
  234. && pointInsideOfHull(convexHullCorners, cornerCount, LVector2D(rectangle.right(), rectangle.top()))
  235. && pointInsideOfHull(convexHullCorners, cornerCount, LVector2D(rectangle.left(), rectangle.bottom()))
  236. && pointInsideOfHull(convexHullCorners, cornerCount, LVector2D(rectangle.right(), rectangle.bottom()));
  237. }
  238. IRect getOuterCellBound(const IRect &pixelBound) {
  239. int minCellX = pixelBound.left() / cellSize;
  240. int maxCellX = pixelBound.right() / cellSize + 1;
  241. int minCellY = pixelBound.top() / cellSize;
  242. int maxCellY = pixelBound.bottom() / cellSize + 1;
  243. if (minCellX < 0) { minCellX = 0; }
  244. if (minCellY < 0) { minCellY = 0; }
  245. if (maxCellX > this->gridWidth) { maxCellX = this->gridWidth; }
  246. if (maxCellY > this->gridHeight) { maxCellY = this->gridHeight; }
  247. return IRect(minCellX, minCellY, maxCellX - minCellX, maxCellY - minCellY);
  248. }
  249. // Called before occluding so that the grid is initialized once when used and skipped when not used
  250. void prepareForOcclusion() {
  251. if (!this->occluded) {
  252. // Allocate the grid if a sufficiently large one does not already exist
  253. if (!(image_exists(this->depthGrid) && image_getWidth(this->depthGrid) >= gridWidth && image_getHeight(this->depthGrid) >= gridHeight)) {
  254. this->depthGrid = image_create_F32(gridWidth, gridHeight);
  255. }
  256. // Use inifnite depth in camera space
  257. image_fill(this->depthGrid, std::numeric_limits<float>::infinity());
  258. }
  259. this->occluded = true;
  260. }
  261. // If any occluder has been used during this pass, all triangles in the buffer will be filtered based using depthGrid
  262. void completeOcclusion() {
  263. if (this->occluded) {
  264. for (int t = this->commandQueue.buffer.length() - 1; t >= 0; t--) {
  265. bool anyVisible = false;
  266. ITriangle2D triangle = this->commandQueue.buffer[t].triangle;
  267. IRect outerBound = getOuterCellBound(triangle.wholeBound);
  268. for (int cellY = outerBound.top(); cellY < outerBound.bottom(); cellY++) {
  269. for (int cellX = outerBound.left(); cellX < outerBound.right(); cellX++) {
  270. // TODO: Optimize access using SafePointer iteration
  271. float backgroundDepth = image_readPixel_clamp(this->depthGrid, cellX, cellY);
  272. float triangleDepth = triangle.position[0].cs.z;
  273. replaceWithSmaller(triangleDepth, triangle.position[1].cs.z);
  274. replaceWithSmaller(triangleDepth, triangle.position[2].cs.z);
  275. if (triangleDepth < backgroundDepth + 0.001) {
  276. anyVisible = true;
  277. }
  278. }
  279. }
  280. if (!anyVisible) {
  281. // TODO: Make triangle swapping work so that the list can be sorted
  282. this->commandQueue.buffer[t].occluded = true;
  283. }
  284. }
  285. }
  286. }
  287. void occludeFromSortedHull(const ProjectedPoint* convexHullCorners, int cornerCount, const IRect& pixelBound) {
  288. // Loop over the outer bound
  289. if (pixelBound.width() > cellSize && pixelBound.height() > cellSize) {
  290. float distance = 0.0f;
  291. for (int c = 0; c < cornerCount; c++) {
  292. replaceWithLarger(distance, convexHullCorners[c].cs.z);
  293. }
  294. // Loop over all cells within the bound
  295. IRect outerBound = getOuterCellBound(pixelBound);
  296. for (int cellY = outerBound.top(); cellY < outerBound.bottom(); cellY++) {
  297. for (int cellX = outerBound.left(); cellX < outerBound.right(); cellX++) {
  298. IRect pixelRegion = IRect(cellX * cellSize, cellY * cellSize, cellSize, cellSize);
  299. IRect subPixelRegion = pixelRegion * constants::unitsPerPixel;
  300. if (rectangleInsideOfHull(convexHullCorners, cornerCount, subPixelRegion)) {
  301. float oldDepth = image_readPixel_clamp(this->depthGrid, cellX, cellY);
  302. if (distance < oldDepth) {
  303. image_writePixel(this->depthGrid, cellX, cellY, distance);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. IRect getPixelBoundFromProjection(const ProjectedPoint* convexHullCorners, int cornerCount) {
  311. IRect result = IRect(convexHullCorners[0].flat.x / constants::unitsPerPixel, convexHullCorners[0].flat.y / constants::unitsPerPixel, 1, 1);
  312. for (int p = 1; p < cornerCount; p++) {
  313. result = IRect::merge(result, IRect(convexHullCorners[p].flat.x / constants::unitsPerPixel, convexHullCorners[p].flat.y / constants::unitsPerPixel, 1, 1));
  314. }
  315. return result;
  316. }
  317. void occludeFromSortedHull(const ProjectedPoint* convexHullCorners, int cornerCount) {
  318. occludeFromSortedHull(convexHullCorners, cornerCount, getPixelBoundFromProjection(convexHullCorners, cornerCount));
  319. }
  320. void occludeFromExistingTriangles() {
  321. if (!this->receiving) {
  322. throwError("Cannot call renderer_occludeFromExistingTriangles without first calling renderer_begin!\n");
  323. }
  324. prepareForOcclusion();
  325. // Generate a depth grid to remove many small triangles behind larger triangles
  326. // This will leave triangles along seams but at least begin to remove the worst unwanted drawing
  327. for (int t = 0; t < this->commandQueue.buffer.length(); t++) {
  328. // Get the current triangle from the queue
  329. Filter filter = this->commandQueue.buffer[t].filter;
  330. if (filter == Filter::Solid) {
  331. ITriangle2D triangle = this->commandQueue.buffer[t].triangle;
  332. occludeFromSortedHull(triangle.position, 3, triangle.wholeBound);
  333. }
  334. }
  335. }
  336. bool counterClockwise(const ProjectedPoint& p, const ProjectedPoint& q, const ProjectedPoint& r) {
  337. return (q.flat.y - p.flat.y) * (r.flat.x - q.flat.x) - (q.flat.x - p.flat.x) * (r.flat.y - q.flat.y) < 0;
  338. }
  339. // outputHullCorners must be at least as big as inputHullCorners, so that it can hold the worst case output size.
  340. // Instead of not allowing less than three points, it copies the input as output when it happens to reduce pre-conditions.
  341. void jarvisConvexHullAlgorithm(ProjectedPoint* outputHullCorners, int& outputCornerCount, const ProjectedPoint* inputHullCorners, int inputCornerCount) {
  342. if (inputCornerCount < 3) {
  343. outputCornerCount = inputCornerCount;
  344. for (int p = 0; p < inputCornerCount; p++) {
  345. outputHullCorners[p] = inputHullCorners[p];
  346. }
  347. } else {
  348. int l = 0;
  349. outputCornerCount = 0;
  350. for (int i = 1; i < inputCornerCount; i++) {
  351. if (inputHullCorners[i].flat.x < inputHullCorners[l].flat.x) {
  352. l = i;
  353. }
  354. }
  355. int p = l;
  356. do {
  357. if (outputCornerCount >= inputCornerCount) {
  358. // Prevent getting stuck in an infinite loop from overflow
  359. return;
  360. }
  361. outputHullCorners[outputCornerCount] = inputHullCorners[p]; outputCornerCount++;
  362. int q = (p + 1) % inputCornerCount;
  363. for (int i = 0; i < inputCornerCount; i++) {
  364. if (counterClockwise(inputHullCorners[p], inputHullCorners[i], inputHullCorners[q])) {
  365. q = i;
  366. }
  367. }
  368. p = q;
  369. } while (p != l);
  370. }
  371. }
  372. // Transform and project the corners of a hull, so that the output can be given to the convex hull algorithm and used for occluding
  373. // Returns true if occluder culling passed, which may skip occluders that could have been visible
  374. bool projectHull(ProjectedPoint* outputHullCorners, const FVector3D* inputHullCorners, int cornerCount, const Transform3D &modelToWorldTransform, const Camera &camera) {
  375. for (int p = 0; p < cornerCount; p++) {
  376. FVector3D worldPoint = modelToWorldTransform.transformPoint(inputHullCorners[p]);
  377. FVector3D cameraPoint = camera.worldToCamera(worldPoint);
  378. FVector3D narrowPoint = cameraPoint * FVector3D(0.5f, 0.5f, 1.0f);
  379. for (int s = 0; s < camera.cullFrustum.getPlaneCount(); s++) {
  380. FPlane3D plane = camera.cullFrustum.getPlane(s);
  381. if (!plane.inside(narrowPoint)) {
  382. return false;
  383. }
  384. }
  385. outputHullCorners[p] = camera.cameraToScreen(cameraPoint);
  386. }
  387. return true;
  388. }
  389. #define GENERATE_BOX_CORNERS(TARGET, MIN, MAX) \
  390. TARGET[0] = FVector3D(MIN.x, MIN.y, MIN.z); \
  391. TARGET[1] = FVector3D(MIN.x, MIN.y, MAX.z); \
  392. TARGET[2] = FVector3D(MIN.x, MAX.y, MIN.z); \
  393. TARGET[3] = FVector3D(MIN.x, MAX.y, MAX.z); \
  394. TARGET[4] = FVector3D(MAX.x, MIN.y, MIN.z); \
  395. TARGET[5] = FVector3D(MAX.x, MIN.y, MAX.z); \
  396. TARGET[6] = FVector3D(MAX.x, MAX.y, MIN.z); \
  397. TARGET[7] = FVector3D(MAX.x, MAX.y, MAX.z);
  398. // Fills the occlusion grid using the box, so that things behind it can skip rendering
  399. void occludeFromBox(const FVector3D& minimum, const FVector3D& maximum, const Transform3D &modelToWorldTransform, const Camera &camera, bool debugSilhouette) {
  400. if (!this->receiving) {
  401. throwError("Cannot call renderer_occludeFromBox without first calling renderer_begin!\n");
  402. }
  403. prepareForOcclusion();
  404. static const int pointCount = 8;
  405. FVector3D localPoints[pointCount];
  406. ProjectedPoint projections[pointCount];
  407. ProjectedPoint edgeCorners[pointCount];
  408. GENERATE_BOX_CORNERS(localPoints, minimum, maximum)
  409. if (projectHull(projections, localPoints, 8, modelToWorldTransform, camera)) {
  410. // Get a 2D convex hull from the projected corners
  411. int edgeCornerCount = 0;
  412. jarvisConvexHullAlgorithm(edgeCorners, edgeCornerCount, projections, 8);
  413. occludeFromSortedHull(edgeCorners, edgeCornerCount);
  414. // Allow saving the 2D silhouette for debugging
  415. if (debugSilhouette) {
  416. for (int p = 0; p < edgeCornerCount; p++) {
  417. int q = (p + 1) % edgeCornerCount;
  418. if (projections[p].cs.z > camera.nearClip) {
  419. this->debugLines.pushConstruct(
  420. edgeCorners[p].flat.x / constants::unitsPerPixel, edgeCorners[p].flat.y / constants::unitsPerPixel,
  421. edgeCorners[q].flat.x / constants::unitsPerPixel, edgeCorners[q].flat.y / constants::unitsPerPixel,
  422. ColorRgbaI32(0, 255, 255, 255)
  423. );
  424. }
  425. }
  426. }
  427. }
  428. }
  429. // Occlusion test for whole model bounds.
  430. // Returns false if the convex hull of the corners has a chance to be seen from the camera.
  431. bool isHullOccluded(ProjectedPoint* outputHullCorners, const FVector3D* inputHullCorners, int cornerCount, const Transform3D &modelToWorldTransform, const Camera &camera) {
  432. VirtualStackAllocation<FVector3D> cameraPoints(cornerCount);
  433. for (int p = 0; p < cornerCount; p++) {
  434. cameraPoints[p] = camera.worldToCamera(modelToWorldTransform.transformPoint(inputHullCorners[p]));
  435. outputHullCorners[p] = camera.cameraToScreen(cameraPoints[p]);
  436. }
  437. // Culling test to see if all points are outside of the same plane of the view frustum.
  438. for (int s = 0; s < camera.cullFrustum.getPlaneCount(); s++) {
  439. bool allOutside = true; // True until prooven false.
  440. FPlane3D plane = camera.cullFrustum.getPlane(s);
  441. for (int p = 0; p < cornerCount; p++) {
  442. if (plane.inside(cameraPoints[p])) {
  443. // One point was inside of this plane, so it can not guarantee that all interpolated points between the corners are outside.
  444. allOutside = false;
  445. break;
  446. }
  447. }
  448. // If all points are outside of the same plane in the view frustum...
  449. if (allOutside) {
  450. // ...then we know that all interpolated points in between are also outside of this plane.
  451. return true; // Occluded due to failing culling test.
  452. }
  453. }
  454. IRect pixelBound = getPixelBoundFromProjection(outputHullCorners, cornerCount);
  455. float closestDistance = std::numeric_limits<float>::infinity();
  456. for (int c = 0; c < cornerCount; c++) {
  457. replaceWithSmaller(closestDistance, outputHullCorners[c].cs.z);
  458. }
  459. // Loop over all cells within the bound
  460. IRect outerBound = getOuterCellBound(pixelBound);
  461. for (int cellY = outerBound.top(); cellY < outerBound.bottom(); cellY++) {
  462. for (int cellX = outerBound.left(); cellX < outerBound.right(); cellX++) {
  463. if (closestDistance < image_readPixel_clamp(this->depthGrid, cellX, cellY)) {
  464. return false; // Visible because one cell had a more distant maximum depth.
  465. }
  466. }
  467. }
  468. return true; // Occluded, because none of the cells had a more distant depth.
  469. }
  470. // Checks if the box from minimum to maximum in object space is fully occluded when seen by the camera
  471. // Must be the same camera as when occluders filled the grid with occlusion depth
  472. bool isBoxOccluded(const FVector3D &minimum, const FVector3D &maximum, const Transform3D &modelToWorldTransform, const Camera &camera) {
  473. if (!this->receiving) {
  474. throwError("Cannot call renderer_isBoxVisible without first calling renderer_begin and giving occluder shapes to the pass!\n");
  475. }
  476. FVector3D corners[8];
  477. GENERATE_BOX_CORNERS(corners, minimum, maximum)
  478. ProjectedPoint projections[8];
  479. return isHullOccluded(projections, corners, 8, modelToWorldTransform, camera);
  480. }
  481. void giveTask(const Model& model, const Transform3D &modelToWorldTransform, const Camera &camera) {
  482. if (!this->receiving) {
  483. throwError("Cannot call renderer_giveTask before renderer_begin!\n");
  484. }
  485. // If occluders are present, check if the model's bound is visible
  486. if (this->occluded) {
  487. FVector3D minimum, maximum;
  488. model_getBoundingBox(model, minimum, maximum);
  489. if (isBoxOccluded(minimum, maximum, modelToWorldTransform, camera)) {
  490. // Skip projection of triangles if the whole bounding box is already behind occluders
  491. return;
  492. }
  493. }
  494. // TODO: Make an algorithm for selecting if the model should be queued as an instance or triangulated at once
  495. // An extra argument may choose to force an instance directly into the command queue
  496. // Because the model is being borrowed for vertex animation
  497. // To prevent the command queue from getting full hold as much as possible in a sorted list of instances
  498. // When the command queue is full, the solid instances will be drawn front to back before filtered is drawn back to front
  499. model->render(&this->commandQueue, image_exists(this->colorBuffer) ? &(this->colorBuffer) : nullptr, image_exists(this->depthBuffer) ? &(this->depthBuffer) : nullptr, modelToWorldTransform, camera);
  500. }
  501. void endFrame(bool debugWireframe) {
  502. if (!this->receiving) {
  503. throwError("Called renderer_end without renderer_begin!\n");
  504. }
  505. this->receiving = false;
  506. // Mark occluded triangles to prevent them from being rendered
  507. completeOcclusion();
  508. this->commandQueue.execute(IRect::FromSize(this->width, this->height));
  509. if (image_exists(this->colorBuffer)) {
  510. // Debug drawn triangles
  511. if (debugWireframe) {
  512. /*if (image_exists(this->depthGrid)) {
  513. for (int cellY = 0; cellY < this->gridHeight; cellY++) {
  514. for (int cellX = 0; cellX < this->gridWidth; cellX++) {
  515. float depth = image_readPixel_clamp(this->depthGrid, cellX, cellY);
  516. if (depth < std::numeric_limits<float>::infinity()) {
  517. int intensity = depth;
  518. draw_rectangle(this->colorBuffer, IRect(cellX * cellSize + 4, cellY * cellSize + 4, cellSize - 8, cellSize - 8), ColorRgbaI32(intensity, intensity, 0, 255));
  519. }
  520. }
  521. }
  522. }*/
  523. for (int t = 0; t < this->commandQueue.buffer.length(); t++) {
  524. if (!this->commandQueue.buffer[t].occluded) {
  525. ITriangle2D *triangle = &(this->commandQueue.buffer[t].triangle);
  526. draw_line(this->colorBuffer,
  527. triangle->position[0].flat.x / constants::unitsPerPixel, triangle->position[0].flat.y / constants::unitsPerPixel,
  528. triangle->position[1].flat.x / constants::unitsPerPixel, triangle->position[1].flat.y / constants::unitsPerPixel,
  529. ColorRgbaI32(255, 255, 255, 255)
  530. );
  531. draw_line(this->colorBuffer,
  532. triangle->position[1].flat.x / constants::unitsPerPixel, triangle->position[1].flat.y / constants::unitsPerPixel,
  533. triangle->position[2].flat.x / constants::unitsPerPixel, triangle->position[2].flat.y / constants::unitsPerPixel,
  534. ColorRgbaI32(255, 255, 255, 255)
  535. );
  536. draw_line(this->colorBuffer,
  537. triangle->position[2].flat.x / constants::unitsPerPixel, triangle->position[2].flat.y / constants::unitsPerPixel,
  538. triangle->position[0].flat.x / constants::unitsPerPixel, triangle->position[0].flat.y / constants::unitsPerPixel,
  539. ColorRgbaI32(255, 255, 255, 255)
  540. );
  541. }
  542. }
  543. }
  544. // Debug anything else added to debugLines
  545. for (int l = 0; l < this->debugLines.length(); l++) {
  546. draw_line(this->colorBuffer, this->debugLines[l].x1, this->debugLines[l].y1, this->debugLines[l].x2, this->debugLines[l].y2, this->debugLines[l].color);
  547. }
  548. this->debugLines.clear();
  549. }
  550. this->commandQueue.clear();
  551. }
  552. void occludeFromTopRows(const Camera &camera) {
  553. // Make sure that the depth grid exists with the correct dimensions.
  554. this->prepareForOcclusion();
  555. if (!this->receiving) {
  556. throwError("Cannot call renderer_occludeFromTopRows without first calling renderer_begin!\n");
  557. }
  558. if (!image_exists(this->depthBuffer)) {
  559. throwError("Cannot call renderer_occludeFromTopRows without having given a depth buffer in renderer_begin!\n");
  560. }
  561. SafePointer<float> depthRow = image_getSafePointer(this->depthBuffer);
  562. int depthStride = image_getStride(this->depthBuffer);
  563. SafePointer<float> gridRow = image_getSafePointer(this->depthGrid);
  564. int gridStride = image_getStride(this->depthGrid);
  565. if (camera.perspective) {
  566. // Perspective case using 1/depth for the depth buffer.
  567. for (int y = 0; y < this->height; y += cellSize) {
  568. SafePointer<float> gridPixel = gridRow;
  569. SafePointer<float> depthPixel = depthRow;
  570. int x = 0;
  571. int right = cellSize - 1;
  572. float maxInvDistance;
  573. // Scan bottom row of whole cell width
  574. for (int gridX = 0; gridX < this->gridWidth; gridX++) {
  575. maxInvDistance = std::numeric_limits<float>::infinity();
  576. if (right >= this->width) { right = this->width; }
  577. while (x < right) {
  578. float newInvDistance = *depthPixel;
  579. if (newInvDistance < maxInvDistance) { maxInvDistance = newInvDistance; }
  580. depthPixel += 1;
  581. x += 1;
  582. }
  583. float maxDistance = 1.0f / maxInvDistance;
  584. float oldDistance = *gridPixel;
  585. if (maxDistance < oldDistance) {
  586. *gridPixel = maxDistance;
  587. }
  588. gridPixel += 1;
  589. right += cellSize;
  590. }
  591. // Go to the next grid row
  592. depthRow.increaseBytes(depthStride * cellSize);
  593. gridRow.increaseBytes(gridStride);
  594. }
  595. } else {
  596. // Orthogonal case where linear depth is used for both grid and depth buffer.
  597. // TODO: Create test cases for many ways to use occlusion, even these strange cases like isometric occlusion where plain culling does not leave many occluded models.
  598. for (int y = 0; y < this->height; y += cellSize) {
  599. SafePointer<float> gridPixel = gridRow;
  600. SafePointer<float> depthPixel = depthRow;
  601. int x = 0;
  602. int right = cellSize - 1;
  603. float maxDistance;
  604. // Scan bottom row of whole cell width
  605. for (int gridX = 0; gridX < this->gridWidth; gridX++) {
  606. maxDistance = 0.0f;
  607. if (right >= this->width) { right = this->width; }
  608. while (x < right) {
  609. float newDistance = *depthPixel;
  610. if (newDistance > maxDistance) { maxDistance = newDistance; }
  611. depthPixel += 1;
  612. x += 1;
  613. }
  614. float oldDistance = *gridPixel;
  615. if (maxDistance < oldDistance) {
  616. *gridPixel = maxDistance;
  617. }
  618. gridPixel += 1;
  619. right += cellSize;
  620. }
  621. // Go to the next grid row
  622. depthRow.increaseBytes(depthStride * cellSize);
  623. gridRow.increaseBytes(gridStride);
  624. }
  625. }
  626. }
  627. };
  628. Renderer renderer_create() {
  629. return handle_create<RendererImpl>().setName("Renderer");
  630. }
  631. bool renderer_exists(const Renderer& renderer) {
  632. return renderer.isNotNull();
  633. }
  634. void renderer_begin(Renderer& renderer, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer) {
  635. MUST_EXIST(renderer, renderer_begin);
  636. renderer->beginFrame(colorBuffer, depthBuffer);
  637. }
  638. // TODO: Synchronous setting
  639. // * Asynchronous (default)
  640. // Only works on models that are locked from further editing
  641. // Locked models can also be safely pooled for reuse then (ResourcePool)
  642. // * Synced (for animation)
  643. // Dispatch triangles directly to the command queue so that the current state of the model is captured
  644. // This allow rendering many instances using the same model at different times
  645. // Enabling vertex light, reflection maps and bone animation
  646. void renderer_giveTask(Renderer& renderer, const Model& model, const Transform3D &modelToWorldTransform, const Camera &camera) {
  647. MUST_EXIST(renderer, renderer_giveTask);
  648. if (model.isNotNull()) {
  649. renderer->giveTask(model, modelToWorldTransform, camera);
  650. }
  651. }
  652. void renderer_giveTask_triangle(Renderer& renderer,
  653. const ProjectedPoint &posA, const ProjectedPoint &posB, const ProjectedPoint &posC,
  654. const FVector4D &colorA, const FVector4D &colorB, const FVector4D &colorC,
  655. const FVector4D &texCoordA, const FVector4D &texCoordB, const FVector4D &texCoordC,
  656. const TextureRgbaU8& diffuseMap, const TextureRgbaU8& lightMap,
  657. Filter filter, const Camera &camera) {
  658. #ifndef NDEBUG
  659. MUST_EXIST(renderer, renderer_addTriangle);
  660. #endif
  661. renderTriangleFromData(
  662. &(renderer->commandQueue), &(renderer->colorBuffer), &(renderer->depthBuffer), camera,
  663. posA, posB, posC,
  664. filter, &(diffuseMap), &(lightMap),
  665. TriangleTexCoords(texCoordA, texCoordB, texCoordC),
  666. TriangleColors(colorA, colorB, colorC)
  667. );
  668. }
  669. void renderer_occludeFromBox(Renderer& renderer, const FVector3D& minimum, const FVector3D& maximum, const Transform3D &modelToWorldTransform, const Camera &camera, bool debugSilhouette) {
  670. MUST_EXIST(renderer, renderer_occludeFromBox);
  671. renderer->occludeFromBox(minimum, maximum, modelToWorldTransform, camera, debugSilhouette);
  672. }
  673. void renderer_occludeFromExistingTriangles(Renderer& renderer) {
  674. MUST_EXIST(renderer, renderer_optimize);
  675. renderer->occludeFromExistingTriangles();
  676. }
  677. void renderer_occludeFromTopRows(Renderer& renderer, const Camera &camera) {
  678. MUST_EXIST(renderer, renderer_occludeFromTopRows);
  679. renderer->occludeFromTopRows(camera);
  680. }
  681. bool renderer_isBoxVisible(Renderer& renderer, const FVector3D &minimum, const FVector3D &maximum, const Transform3D &modelToWorldTransform, const Camera &camera) {
  682. MUST_EXIST(renderer, renderer_isBoxVisible);
  683. return !(renderer->isBoxOccluded(minimum, maximum, modelToWorldTransform, camera));
  684. }
  685. void renderer_end(Renderer& renderer, bool debugWireframe) {
  686. MUST_EXIST(renderer, renderer_end);
  687. renderer->endFrame(debugWireframe);
  688. }
  689. }