modelAPI.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // TODO: Inline as much as possible from Model.h to modelAPI.cpp, to reduce call depth and make it easy to copy and modify the model implementation.
  28. #include "../implementation/render/model/Model.h"
  29. #include "../base/virtualStack.h"
  30. #include <limits>
  31. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.isNull()) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  32. namespace dsr {
  33. Model model_create() {
  34. return handle_create<ModelImpl>().setName("Model");
  35. }
  36. Model model_clone(const Model& model) {
  37. MUST_EXIST(model,model_clone);
  38. return handle_create<ModelImpl>(model->filter, model->partBuffer, model->positionBuffer).setName("Cloned Model");
  39. }
  40. void model_setFilter(const Model& model, Filter filter) {
  41. MUST_EXIST(model,model_setFilter);
  42. model->filter = filter;
  43. }
  44. Filter model_getFilter(const Model& model) {
  45. MUST_EXIST(model,model_getFilter);
  46. return model->filter;
  47. }
  48. bool model_exists(const Model& model) {
  49. return model.isNotNull();
  50. }
  51. int model_addEmptyPart(Model& model, const String &name) {
  52. MUST_EXIST(model,model_addEmptyPart);
  53. return model->addEmptyPart(name);
  54. }
  55. int model_getNumberOfParts(const Model& model) {
  56. MUST_EXIST(model,model_getNumberOfParts);
  57. return model->getNumberOfParts();
  58. }
  59. void model_setPartName(Model& model, int partIndex, const String &name) {
  60. MUST_EXIST(model,model_setPartName);
  61. model->setPartName(partIndex, name);
  62. }
  63. String model_getPartName(const Model& model, int partIndex) {
  64. MUST_EXIST(model,model_getPartName);
  65. return model->getPartName(partIndex);
  66. }
  67. int model_getNumberOfPoints(const Model& model) {
  68. MUST_EXIST(model,model_getNumberOfPoints);
  69. return model->getNumberOfPoints();
  70. }
  71. FVector3D model_getPoint(const Model& model, int pointIndex) {
  72. MUST_EXIST(model,model_getPoint);
  73. return model->getPoint(pointIndex);
  74. }
  75. void model_setPoint(Model& model, int pointIndex, const FVector3D& position) {
  76. MUST_EXIST(model,model_setPoint);
  77. model->setPoint(pointIndex, position);
  78. }
  79. int model_findPoint(const Model& model, const FVector3D &position, float threshold) {
  80. MUST_EXIST(model,model_findPoint);
  81. return model->findPoint(position, threshold);
  82. }
  83. int model_addPoint(const Model& model, const FVector3D &position) {
  84. MUST_EXIST(model,model_addPoint);
  85. return model->addPoint(position);
  86. }
  87. int model_addPointIfNeeded(Model& model, const FVector3D &position, float threshold) {
  88. MUST_EXIST(model,model_addPointIfNeeded);
  89. return model->addPointIfNeeded(position, threshold);
  90. }
  91. int model_getVertexPointIndex(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  92. MUST_EXIST(model,model_getVertexPointIndex);
  93. return model->getVertexPointIndex(partIndex, polygonIndex, vertexIndex);
  94. }
  95. void model_setVertexPointIndex(Model& model, int partIndex, int polygonIndex, int vertexIndex, int pointIndex) {
  96. MUST_EXIST(model,model_setVertexPointIndex);
  97. model->setVertexPointIndex(partIndex, polygonIndex, vertexIndex, pointIndex);
  98. }
  99. FVector3D model_getVertexPosition(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  100. MUST_EXIST(model,model_getVertexPosition);
  101. return model->getVertexPosition(partIndex, polygonIndex, vertexIndex);
  102. }
  103. FVector4D model_getVertexColor(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  104. MUST_EXIST(model,model_getVertexColor);
  105. return model->getVertexColor(partIndex, polygonIndex, vertexIndex);
  106. }
  107. void model_setVertexColor(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& color) {
  108. MUST_EXIST(model,model_setVertexColor);
  109. model->setVertexColor(partIndex, polygonIndex, vertexIndex, color);
  110. }
  111. FVector4D model_getTexCoord(const Model& model, int partIndex, int polygonIndex, int vertexIndex) {
  112. MUST_EXIST(model,model_getTexCoord);
  113. return model->getTexCoord(partIndex, polygonIndex, vertexIndex);
  114. }
  115. void model_setTexCoord(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& texCoord) {
  116. MUST_EXIST(model,model_setTexCoord);
  117. model->setTexCoord(partIndex, polygonIndex, vertexIndex, texCoord);
  118. }
  119. int model_addTriangle(Model& model, int partIndex, int pointA, int pointB, int pointC) {
  120. MUST_EXIST(model,model_addTriangle);
  121. return model->addPolygon(Polygon(pointA, pointB, pointC), partIndex);
  122. }
  123. int model_addQuad(Model& model, int partIndex, int pointA, int pointB, int pointC, int pointD) {
  124. MUST_EXIST(model,model_addQuad);
  125. return model->addPolygon(Polygon(pointA, pointB, pointC, pointD), partIndex);
  126. }
  127. int model_getNumberOfPolygons(const Model& model, int partIndex) {
  128. MUST_EXIST(model,model_getNumberOfPolygons);
  129. return model->getNumberOfPolygons(partIndex);
  130. }
  131. int model_getPolygonVertexCount(const Model& model, int partIndex, int polygonIndex) {
  132. MUST_EXIST(model,model_getPolygonVertexCount);
  133. return model->getPolygonVertexCount(partIndex, polygonIndex);
  134. }
  135. TextureRgbaU8 model_getDiffuseMap(const Model& model, int partIndex) {
  136. MUST_EXIST(model,model_getDiffuseMap);
  137. return model->getDiffuseMap(partIndex);
  138. }
  139. void model_setDiffuseMap(Model& model, int partIndex, const TextureRgbaU8 &diffuseMap) {
  140. MUST_EXIST(model,model_setDiffuseMap);
  141. model->setDiffuseMap(diffuseMap, partIndex);
  142. }
  143. void model_setDiffuseMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename) {
  144. MUST_EXIST(model,model_setDiffuseMapByName);
  145. model->setDiffuseMapByName(pool, filename, partIndex);
  146. }
  147. TextureRgbaU8 model_getLightMap(Model& model, int partIndex) {
  148. MUST_EXIST(model,model_getLightMap);
  149. return model->getLightMap(partIndex);
  150. }
  151. void model_setLightMap(Model& model, int partIndex, const TextureRgbaU8 &lightMap) {
  152. MUST_EXIST(model,model_setLightMap);
  153. model->setLightMap(lightMap, partIndex);
  154. }
  155. void model_setLightMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename) {
  156. MUST_EXIST(model,model_setLightMapByName);
  157. model->setLightMapByName(pool, filename, partIndex);
  158. }
  159. // Single-threaded rendering for the simple cases where you just want it to work
  160. void model_render(const Model& model, const Transform3D &modelToWorldTransform, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer, const Camera &camera) {
  161. if (model.isNotNull()) {
  162. model->render((CommandQueue*)nullptr, colorBuffer, depthBuffer, modelToWorldTransform, camera);
  163. }
  164. }
  165. void model_renderDepth(const Model& model, const Transform3D &modelToWorldTransform, ImageF32& depthBuffer, const Camera &camera) {
  166. if (model.isNotNull()) {
  167. model->renderDepth(depthBuffer, modelToWorldTransform, camera);
  168. }
  169. }
  170. void model_getBoundingBox(const Model& model, FVector3D& minimum, FVector3D& maximum) {
  171. MUST_EXIST(model,model_getBoundingBox);
  172. minimum = model->minBound;
  173. maximum = model->maxBound;
  174. }
  175. void model_render_threaded(const Model& model, const Transform3D &modelToWorldTransform, Renderer& renderer, const Camera &camera) {
  176. MUST_EXIST(renderer, renderer_giveTask);
  177. // Skip rendering if we do not have any model.
  178. if (model.isNull()) {
  179. return;
  180. }
  181. // Check the renderer's state.
  182. #ifndef NDEBUG
  183. if (!renderer_takesTriangles(renderer)) {
  184. throwError("Cannot call renderer_giveTask before renderer_begin!\n");
  185. }
  186. #endif
  187. // Culling.
  188. if (!camera.isBoxSeen(model->minBound, model->maxBound, modelToWorldTransform)) return;
  189. // Occlusion.
  190. if (renderer_hasOccluders(renderer)) {
  191. FVector3D minimum, maximum;
  192. model_getBoundingBox(model, minimum, maximum);
  193. if (!renderer_isBoxVisible(renderer, minimum, maximum, modelToWorldTransform, camera)) return;
  194. }
  195. // Render the model by calling the renderer API for each triangle.
  196. // Get the filter.
  197. Filter filter = model->filter;
  198. // Transform and project all vertices.
  199. int positionCount = model->positionBuffer.length();
  200. VirtualStackAllocation<ProjectedPoint> projected(positionCount, "Projected points in renderer_giveTask");
  201. for (int vert = 0; vert < positionCount; vert++) {
  202. projected[vert] = camera.worldToScreen(modelToWorldTransform.transformPoint(model->positionBuffer[vert]));
  203. }
  204. for (int partIndex = 0; partIndex < model->partBuffer.length(); partIndex++) {
  205. // Get a pointer to the current part.
  206. Part *part = &(model->partBuffer[partIndex]);
  207. // Get textures.
  208. const TextureRgbaU8 diffuse = part->diffuseMap;
  209. const TextureRgbaU8 light = part->lightMap;
  210. for (int p = 0; p < part->polygonBuffer.length(); p++) {
  211. Polygon polygon = part->polygonBuffer[p];
  212. // Render first triangle in the polygon of indices 0, 1, 2.
  213. renderer_giveTask_triangle(renderer,
  214. projected[polygon.pointIndices[0]],
  215. projected[polygon.pointIndices[1]],
  216. projected[polygon.pointIndices[2]],
  217. polygon.colors[0],
  218. polygon.colors[1],
  219. polygon.colors[2],
  220. polygon.texCoords[0],
  221. polygon.texCoords[1],
  222. polygon.texCoords[2],
  223. diffuse, light, filter, camera
  224. );
  225. if (polygon.pointIndices[3] != -1) {
  226. // Render second triangle in the polygon of indices 0, 2, 3 to form a quad polygon.
  227. renderer_giveTask_triangle(renderer,
  228. projected[polygon.pointIndices[0]],
  229. projected[polygon.pointIndices[2]],
  230. projected[polygon.pointIndices[3]],
  231. polygon.colors[0],
  232. polygon.colors[2],
  233. polygon.colors[3],
  234. polygon.texCoords[0],
  235. polygon.texCoords[2],
  236. polygon.texCoords[3],
  237. diffuse, light, filter, camera
  238. );
  239. }
  240. }
  241. }
  242. }
  243. }