modelAPI.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 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. #include "modelAPI.h"
  24. #include "imageAPI.h"
  25. #include "drawAPI.h"
  26. #include "../render/model/Model.h"
  27. #include <limits>
  28. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.get() == nullptr) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  29. namespace dsr {
  30. Model model_create() {
  31. return std::make_shared<ModelImpl>();
  32. }
  33. Model model_clone(const Model& model) {
  34. MUST_EXIST(model,model_clone);
  35. return std::make_shared<ModelImpl>(model->filter, model->partBuffer, model->positionBuffer);
  36. }
  37. void model_setFilter(const Model& model, Filter filter) {
  38. MUST_EXIST(model,model_setFilter);
  39. model->filter = filter;
  40. }
  41. Filter model_getFilter(const Model& model) {
  42. MUST_EXIST(model,model_getFilter);
  43. return model->filter;
  44. }
  45. bool model_exists(const Model& model) {
  46. return model.get() != nullptr;
  47. }
  48. int model_addEmptyPart(Model& model, const String &name) {
  49. MUST_EXIST(model,model_addEmptyPart);
  50. return model->addEmptyPart(name);
  51. }
  52. int model_getNumberOfParts(const Model& model) {
  53. MUST_EXIST(model,model_getNumberOfParts);
  54. return model->getNumberOfParts();
  55. }
  56. void model_setPartName(Model& model, int partIndex, const String &name) {
  57. MUST_EXIST(model,model_setPartName);
  58. model->setPartName(partIndex, name);
  59. }
  60. String model_getPartName(const Model& model, int partIndex) {
  61. MUST_EXIST(model,model_getPartName);
  62. return model->getPartName(partIndex);
  63. }
  64. int model_getNumberOfPoints(const Model& model) {
  65. MUST_EXIST(model,model_getNumberOfPoints);
  66. return model->getNumberOfPoints();
  67. }
  68. FVector3D model_getPoint(const Model& model, int pointIndex) {
  69. MUST_EXIST(model,model_getPoint);
  70. return model->getPoint(pointIndex);
  71. }
  72. void model_setPoint(Model& model, int pointIndex, const FVector3D& position) {
  73. MUST_EXIST(model,model_setPoint);
  74. model->setPoint(pointIndex, position);
  75. }
  76. int model_findPoint(const Model& model, const FVector3D &position, float threshold) {
  77. MUST_EXIST(model,model_findPoint);
  78. return model->findPoint(position, threshold);
  79. }
  80. int model_addPoint(const Model& model, const FVector3D &position) {
  81. MUST_EXIST(model,model_addPoint);
  82. return model->addPoint(position);
  83. }
  84. int model_addPointIfNeeded(Model& model, const FVector3D &position, float threshold) {
  85. MUST_EXIST(model,model_addPointIfNeeded);
  86. return model->addPointIfNeeded(position, threshold);
  87. }
  88. int model_getVertexPointIndex(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  89. MUST_EXIST(model,model_getVertexPointIndex);
  90. return model->getVertexPointIndex(partIndex, polygonIndex, vertexIndex);
  91. }
  92. void model_setVertexPointIndex(Model& model, int partIndex, int polygonIndex, int vertexIndex, int pointIndex) {
  93. MUST_EXIST(model,model_setVertexPointIndex);
  94. model->setVertexPointIndex(partIndex, polygonIndex, vertexIndex, pointIndex);
  95. }
  96. FVector3D model_getVertexPosition(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  97. MUST_EXIST(model,model_getVertexPosition);
  98. return model->getVertexPosition(partIndex, polygonIndex, vertexIndex);
  99. }
  100. FVector4D model_getVertexColor(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  101. MUST_EXIST(model,model_getVertexColor);
  102. return model->getVertexColor(partIndex, polygonIndex, vertexIndex);
  103. }
  104. void model_setVertexColor(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& color) {
  105. MUST_EXIST(model,model_setVertexColor);
  106. model->setVertexColor(partIndex, polygonIndex, vertexIndex, color);
  107. }
  108. FVector4D model_getTexCoord(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  109. MUST_EXIST(model,model_getTexCoord);
  110. return model->getTexCoord(partIndex, polygonIndex, vertexIndex);
  111. }
  112. void model_setTexCoord(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& texCoord) {
  113. MUST_EXIST(model,model_setTexCoord);
  114. model->setTexCoord(partIndex, polygonIndex, vertexIndex, texCoord);
  115. }
  116. int model_addTriangle(Model& model, int partIndex, int pointA, int pointB, int pointC) {
  117. MUST_EXIST(model,model_addTriangle);
  118. return model->addPolygon(Polygon(pointA, pointB, pointC), partIndex);
  119. }
  120. int model_addQuad(Model& model, int partIndex, int pointA, int pointB, int pointC, int pointD) {
  121. MUST_EXIST(model,model_addQuad);
  122. return model->addPolygon(Polygon(pointA, pointB, pointC, pointD), partIndex);
  123. }
  124. int model_getNumberOfPolygons(const Model& model, int partIndex) {
  125. MUST_EXIST(model,model_getNumberOfPolygons);
  126. return model->getNumberOfPolygons(partIndex);
  127. }
  128. int model_getPolygonVertexCount(const Model& model, int partIndex, int polygonIndex) {
  129. MUST_EXIST(model,model_getPolygonVertexCount);
  130. return model->getPolygonVertexCount(partIndex, polygonIndex);
  131. }
  132. ImageRgbaU8 model_getDiffuseMap(const Model& model, int partIndex) {
  133. MUST_EXIST(model,model_getDiffuseMap);
  134. return model->getDiffuseMap(partIndex);
  135. }
  136. void model_setDiffuseMap(Model& model, int partIndex, const ImageRgbaU8 &diffuseMap) {
  137. MUST_EXIST(model,model_setDiffuseMap);
  138. model->setDiffuseMap(diffuseMap, partIndex);
  139. }
  140. void model_setDiffuseMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename) {
  141. MUST_EXIST(model,model_setDiffuseMapByName);
  142. model->setDiffuseMapByName(pool, filename, partIndex);
  143. }
  144. ImageRgbaU8 model_getLightMap(Model& model, int partIndex) {
  145. MUST_EXIST(model,model_getLightMap);
  146. return model->getLightMap(partIndex);
  147. }
  148. void model_setLightMap(Model& model, int partIndex, const ImageRgbaU8 &lightMap) {
  149. MUST_EXIST(model,model_setLightMap);
  150. model->setLightMap(lightMap, partIndex);
  151. }
  152. void model_setLightMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename) {
  153. MUST_EXIST(model,model_setLightMapByName);
  154. model->setLightMapByName(pool, filename, partIndex);
  155. }
  156. // Single-threaded rendering for the simple cases where you just want it to work
  157. void model_render(const Model& model, const Transform3D &modelToWorldTransform, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer, const Camera &camera) {
  158. if (model.get() != nullptr) {
  159. model->render((CommandQueue*)nullptr, colorBuffer, depthBuffer, modelToWorldTransform, camera);
  160. }
  161. }
  162. void model_renderDepth(const Model& model, const Transform3D &modelToWorldTransform, ImageF32& depthBuffer, const Camera &camera) {
  163. if (model.get() != nullptr) {
  164. model->renderDepth(depthBuffer, modelToWorldTransform, camera);
  165. }
  166. }
  167. void model_getBoundingBox(const Model& model, FVector3D& minimum, FVector3D& maximum) {
  168. MUST_EXIST(model,model_getBoundingBox);
  169. minimum = model->minBound;
  170. maximum = model->maxBound;
  171. }
  172. static const int cellSize = 16;
  173. // Context for rendering multiple models at the same time for improved speed
  174. class RendererImpl {
  175. private:
  176. bool receiving = false; // Preventing version dependency by only allowing calls in the expected order
  177. ImageRgbaU8 colorBuffer; // The color image being rendered to
  178. ImageF32 depthBuffer; // Linear depth for isometric cameras, 1 / depth for perspective cameras
  179. ImageF32 depthGrid; // An occlusion grid of cellSize² cells representing the longest linear depth where something might be visible
  180. CommandQueue commandQueue;
  181. int width = 0, height = 0, gridWidth = 0, gridHeight = 0;
  182. bool occluded = false;
  183. public:
  184. RendererImpl() {}
  185. void beginFrame(ImageRgbaU8& colorBuffer, ImageF32& depthBuffer) {
  186. if (this->receiving) {
  187. throwError("Called renderer_begin on the same renderer twice without ending the previous batch!\n");
  188. }
  189. this->receiving = true;
  190. this->colorBuffer = colorBuffer;
  191. this->depthBuffer = depthBuffer;
  192. if (image_exists(this->colorBuffer)) {
  193. this->width = image_getWidth(this->colorBuffer);
  194. this->height = image_getHeight(this->colorBuffer);
  195. } else if (image_exists(this->depthBuffer)) {
  196. this->width = image_getWidth(this->depthBuffer);
  197. this->height = image_getHeight(this->depthBuffer);
  198. }
  199. this->gridWidth = (this->width + (cellSize - 1)) / cellSize;
  200. this->gridHeight = (this->height + (cellSize - 1)) / cellSize;
  201. this->occluded = false;
  202. }
  203. void giveTask(const Model& model, const Transform3D &modelToWorldTransform, const Camera &camera) {
  204. if (!this->receiving) {
  205. throwError("Cannot call renderer_giveTask before renderer_begin!\n");
  206. }
  207. // TODO: Make an algorithm for selecting if the model should be queued as an instance or triangulated at once
  208. // An extra argument may choose to force an instance directly into the command queue
  209. // Because the model is being borrowed for vertex animation
  210. // To prevent the command queue from getting full hold as much as possible in a sorted list of instances
  211. // When the command queue is full, the solid instances will be drawn front to back before filtered is drawn back to front
  212. model->render(&this->commandQueue, this->colorBuffer, this->depthBuffer, modelToWorldTransform, camera);
  213. }
  214. bool pointInsideOfEdge(const LVector2D &edgeA, const LVector2D &edgeB, const LVector2D &point) {
  215. LVector2D edgeDirection = LVector2D(edgeB.y - edgeA.y, edgeA.x - edgeB.x);
  216. LVector2D relativePosition = point - edgeA;
  217. int64_t dotProduct = (edgeDirection.x * relativePosition.x) + (edgeDirection.y * relativePosition.y);
  218. return dotProduct <= 0;
  219. }
  220. // Returns true iff the point is inside of the triangle
  221. bool pointInsideOfTriangle(const ITriangle2D &triangle, const LVector2D &point) {
  222. return pointInsideOfEdge(triangle.position[0].flat, triangle.position[1].flat, point)
  223. && pointInsideOfEdge(triangle.position[1].flat, triangle.position[2].flat, point)
  224. && pointInsideOfEdge(triangle.position[2].flat, triangle.position[0].flat, point);
  225. }
  226. // Returns true iff all cornets of the rectangle are inside of the triangle
  227. bool rectangleInsideOfTriangle(const ITriangle2D &triangle, const IRect &rectangle) {
  228. return pointInsideOfTriangle(triangle, LVector2D(rectangle.left(), rectangle.top()))
  229. && pointInsideOfTriangle(triangle, LVector2D(rectangle.right(), rectangle.top()))
  230. && pointInsideOfTriangle(triangle, LVector2D(rectangle.left(), rectangle.bottom()))
  231. && pointInsideOfTriangle(triangle, LVector2D(rectangle.right(), rectangle.bottom()));
  232. }
  233. IRect getOuterCellBound(const ITriangle2D &triangle) {
  234. int minCellX = triangle.wholeBound.left() / cellSize;
  235. int maxCellX = triangle.wholeBound.right() / cellSize + 1;
  236. int minCellY = triangle.wholeBound.top() / cellSize;
  237. int maxCellY = triangle.wholeBound.bottom() / cellSize + 1;
  238. if (minCellX < 0) { minCellX = 0; }
  239. if (minCellY < 0) { minCellY = 0; }
  240. if (maxCellX > this->gridWidth) { maxCellX = this->gridWidth; }
  241. if (maxCellY > this->gridHeight) { maxCellY = this->gridHeight; }
  242. return IRect(minCellX, minCellY, maxCellX - minCellX, maxCellY - minCellY);
  243. }
  244. // Called before occluding so that the grid is initialized once when used and skipped when not used
  245. void prepareForOcclusion() {
  246. if (!this->occluded) {
  247. // Allocate the grid if a sufficiently large one does not already exist
  248. if (!(image_exists(this->depthGrid) && image_getWidth(this->depthGrid) >= gridWidth && image_getHeight(this->depthGrid) >= gridHeight)) {
  249. this->depthGrid = image_create_F32(gridWidth, gridHeight);
  250. }
  251. // Use inifnite depth in camera space
  252. image_fill(this->depthGrid, std::numeric_limits<float>::infinity());
  253. }
  254. this->occluded = true;
  255. }
  256. // If any occluder has been used during this pass, all triangles in the buffer will be filtered based using depthGrid
  257. void completeOcclusion() {
  258. if (this->occluded) {
  259. for (int t = this->commandQueue.buffer.length() - 1; t >= 0; t--) {
  260. bool anyVisible = false;
  261. ITriangle2D triangle = this->commandQueue.buffer[t].triangle;
  262. IRect outerBound = getOuterCellBound(triangle);
  263. for (int cellY = outerBound.top(); cellY < outerBound.bottom(); cellY++) {
  264. for (int cellX = outerBound.left(); cellX < outerBound.right(); cellX++) {
  265. // TODO: Optimize access using SafePointer iteration
  266. float backgroundDepth = image_readPixel_clamp(this->depthGrid, cellX, cellY);
  267. float triangleDepth = triangle.position[0].cs.z;
  268. replaceWithSmaller(triangleDepth, triangle.position[1].cs.z);
  269. replaceWithSmaller(triangleDepth, triangle.position[2].cs.z);
  270. if (triangleDepth < backgroundDepth + 0.001) {
  271. anyVisible = true;
  272. }
  273. }
  274. }
  275. if (!anyVisible) {
  276. // TODO: Make triangle swapping work so that the list can be sorted
  277. this->commandQueue.buffer[t].occluded = true;
  278. }
  279. }
  280. }
  281. }
  282. void occludeFromExistingTriangles() {
  283. prepareForOcclusion();
  284. // Generate a depth grid to remove many small triangles behind larger triangles
  285. // This will leave triangles along seams but at least begin to remove the worst unwanted drawing
  286. for (int t = 0; t < this->commandQueue.buffer.length(); t++) {
  287. // Get the current triangle from the queue
  288. Filter filter = this->commandQueue.buffer[t].filter;
  289. if (filter == Filter::Solid) {
  290. ITriangle2D triangle = this->commandQueue.buffer[t].triangle;
  291. // Loop over all cells within the bound
  292. IRect outerBound = getOuterCellBound(triangle);
  293. // Loop over the outer bound while excluding the outmost rows and columns that are unlikely to be fully occluded by the triangle
  294. if (triangle.wholeBound.width() > cellSize && triangle.wholeBound.height() > cellSize) {
  295. for (int cellY = outerBound.top(); cellY < outerBound.bottom(); cellY++) {
  296. for (int cellX = outerBound.left(); cellX < outerBound.right(); cellX++) {
  297. IRect pixelRegion = IRect(cellX * cellSize, cellY * cellSize, cellSize, cellSize);
  298. IRect subPixelRegion = pixelRegion * constants::unitsPerPixel;
  299. if (rectangleInsideOfTriangle(triangle, subPixelRegion)) {
  300. float oldDepth = image_readPixel_clamp(this->depthGrid, cellX, cellY);
  301. float newDepth = triangle.position[0].cs.z;
  302. replaceWithLarger(newDepth, triangle.position[1].cs.z);
  303. replaceWithLarger(newDepth, triangle.position[2].cs.z);
  304. if (newDepth < oldDepth) {
  305. image_writePixel(this->depthGrid, cellX, cellY, newDepth);
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }
  314. void debugDrawTriangles() {
  315. if (image_exists(this->colorBuffer)) {
  316. if (image_exists(this->depthGrid)) {
  317. for (int cellY = 0; cellY < this->gridHeight; cellY++) {
  318. for (int cellX = 0; cellX < this->gridWidth; cellX++) {
  319. float depth = image_readPixel_clamp(this->depthGrid, cellX, cellY);
  320. if (depth < std::numeric_limits<float>::infinity()) {
  321. int intensity = depth;
  322. draw_rectangle(this->colorBuffer, IRect(cellX * cellSize + 4, cellY * cellSize + 4, cellSize - 8, cellSize - 8), ColorRgbaI32(intensity, intensity, 0, 255));
  323. }
  324. }
  325. }
  326. }
  327. for (int t = 0; t < this->commandQueue.buffer.length(); t++) {
  328. if (!this->commandQueue.buffer[t].occluded) {
  329. ITriangle2D *triangle = &(this->commandQueue.buffer[t].triangle);
  330. draw_line(this->colorBuffer,
  331. triangle->position[0].flat.x / constants::unitsPerPixel, triangle->position[0].flat.y / constants::unitsPerPixel,
  332. triangle->position[1].flat.x / constants::unitsPerPixel, triangle->position[1].flat.y / constants::unitsPerPixel,
  333. ColorRgbaI32(255, 255, 255, 255)
  334. );
  335. draw_line(this->colorBuffer,
  336. triangle->position[1].flat.x / constants::unitsPerPixel, triangle->position[1].flat.y / constants::unitsPerPixel,
  337. triangle->position[2].flat.x / constants::unitsPerPixel, triangle->position[2].flat.y / constants::unitsPerPixel,
  338. ColorRgbaI32(255, 255, 255, 255)
  339. );
  340. draw_line(this->colorBuffer,
  341. triangle->position[2].flat.x / constants::unitsPerPixel, triangle->position[2].flat.y / constants::unitsPerPixel,
  342. triangle->position[0].flat.x / constants::unitsPerPixel, triangle->position[0].flat.y / constants::unitsPerPixel,
  343. ColorRgbaI32(255, 255, 255, 255)
  344. );
  345. }
  346. }
  347. }
  348. }
  349. void endFrame(bool debugWireframe) {
  350. if (!this->receiving) {
  351. throwError("Called renderer_end without renderer_begin!\n");
  352. }
  353. this->receiving = false;
  354. completeOcclusion();
  355. this->commandQueue.execute(IRect::FromSize(this->width, this->height));
  356. if (debugWireframe) {
  357. this->debugDrawTriangles();
  358. }
  359. this->commandQueue.clear();
  360. }
  361. };
  362. Renderer renderer_create() {
  363. return std::make_shared<RendererImpl>();
  364. }
  365. bool renderer_exists(const Renderer& renderer) {
  366. return renderer.get() != nullptr;
  367. }
  368. void renderer_begin(Renderer& renderer, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer) {
  369. MUST_EXIST(renderer,renderer_begin);
  370. renderer->beginFrame(colorBuffer, depthBuffer);
  371. }
  372. // TODO: Synchronous setting
  373. // * Asynchronous (default)
  374. // Only works on models that are locked from further editing
  375. // Locked models can also be safely pooled for reuse then (ResourcePool)
  376. // * Synced (for animation)
  377. // Dispatch triangles directly to the command queue so that the current state of the model is captured
  378. // This allow rendering many instances using the same model at different times
  379. // Enabling vertex light, reflection maps and bone animation
  380. void renderer_giveTask(Renderer& renderer, const Model& model, const Transform3D &modelToWorldTransform, const Camera &camera) {
  381. MUST_EXIST(renderer,renderer_giveTask);
  382. if (model.get() != nullptr) {
  383. renderer->giveTask(model, modelToWorldTransform, camera);
  384. }
  385. }
  386. void renderer_occludeFromExistingTriangles(Renderer& renderer) {
  387. MUST_EXIST(renderer,renderer_optimize);
  388. renderer->occludeFromExistingTriangles();
  389. }
  390. void renderer_end(Renderer& renderer, bool debugWireframe) {
  391. MUST_EXIST(renderer,renderer_end);
  392. renderer->endFrame(debugWireframe);
  393. }
  394. }