modelAPI.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 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. #ifndef DFPSR_API_MODEL
  24. #define DFPSR_API_MODEL
  25. #include "types.h"
  26. #include "../math/FVector.h"
  27. // TODO: How should these be exposed to the caller?
  28. #include "../render/Camera.h"
  29. #include "../render/ResourcePool.h"
  30. namespace dsr {
  31. // TODO: Document the API
  32. // Construction
  33. Model model_create();
  34. Model model_clone(const Model& model);
  35. // Whole model
  36. void model_setFilter(const Model& model, Filter filter);
  37. Filter model_getFilter(const Model& model);
  38. bool model_exists(const Model& model);
  39. // Part
  40. int model_addEmptyPart(Model& model, const String &name);
  41. int model_getNumberOfParts(const Model& model);
  42. void model_setPartName(Model& model, int partIndex, const String &name);
  43. String model_getPartName(const Model& model, int partIndex);
  44. // Point
  45. int model_getNumberOfPoints(const Model& model);
  46. FVector3D model_getPoint(const Model& model, int pointIndex);
  47. void model_setPoint(Model& model, int pointIndex, const FVector3D& position);
  48. int model_findPoint(const Model& model, const FVector3D &position, float treshold);
  49. int model_addPoint(const Model& model, const FVector3D &position);
  50. int model_addPointIfNeeded(Model& model, const FVector3D &position, float treshold);
  51. // Vertex
  52. int model_getVertexPointIndex(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  53. void model_setVertexPointIndex(Model& model, int partIndex, int polygonIndex, int vertexIndex, int pointIndex);
  54. FVector3D model_getVertexPosition(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  55. FVector4D model_getVertexColor(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  56. void model_setVertexColor(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& color);
  57. FVector4D model_getTexCoord(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  58. void model_setTexCoord(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& texCoord);
  59. // Polygon
  60. int model_addTriangle(Model& model, int partIndex, int pointA, int pointB, int pointC);
  61. int model_addQuad(Model& model, int partIndex, int pointA, int pointB, int pointC, int pointD);
  62. int model_getNumberOfPolygons(const Model& model, int partIndex);
  63. int model_getPolygonVertexCount(const Model& model, int partIndex, int polygonIndex);
  64. // Texture
  65. ImageRgbaU8 model_getDiffuseMap(const Model& model, int partIndex);
  66. void model_setDiffuseMap(Model& model, int partIndex, const ImageRgbaU8 &diffuseMap);
  67. void model_setDiffuseMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename);
  68. ImageRgbaU8 model_getLightMap(Model& model, int partIndex);
  69. void model_setLightMap(Model& model, int partIndex, const ImageRgbaU8 &lightMap);
  70. void model_setLightMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename);
  71. // Single-threaded rendering
  72. // Can be executed on different threads if targetImage and depthBuffer doesn't have overlapping memory lines between the threads
  73. void model_render(const Model& model, const Transform3D &modelToWorldTransform, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer, const Camera &camera);
  74. // Simpler rendering without colorBuffer, for shadows and other depth effects
  75. // Equivalent to model_render with a non-existing colorBuffer and filter forced to solid.
  76. // Skip this call conditionally for filtered models (using model_getFilter) if you want full equivalence with model_render.
  77. void model_renderDepth(const Model& model, const Transform3D &modelToWorldTransform, ImageF32& depthBuffer, const Camera &camera);
  78. // Returns a new rendering context
  79. // After creating a renderer, you may execute a number of batches using it
  80. // Each batch may execute a number of tasks in parallel
  81. // Call pattern:
  82. // create (begin giveTask* end)*
  83. Renderer renderer_create();
  84. // Begin rendering to target color and depth buffers of the same dimensions
  85. void renderer_begin(Renderer& renderer, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer);
  86. // Once an object passed game-specific occlusion tests, give it to the renderer using renderer_giveTask
  87. // The render job will be performed during the next call to renderer_execute
  88. void renderer_giveTask(Renderer& renderer, const Model& model, const Transform3D &modelToWorldTransform, const Camera &camera);
  89. // Finish all the jobs in the rendering context
  90. void renderer_end(Renderer& renderer);
  91. // How to import from the DMF1 format:
  92. // * Only use M_Diffuse_0Tex, M_Diffuse_1Tex or M_Diffuse_2Tex as shaders.
  93. // Place any diffuse texture in texture slot 0 and any lightmap in slot 1.
  94. // Remove any textures that are not used by the shaders.
  95. // The fixed pipeline only checks which textures are used.
  96. // * Make sure that texture names are spelled case sensitive or they might not be found on some operating systems like Linux.
  97. // See dmf1.cpp for the implementation
  98. Model importFromContent_DMF1(const String &fileContent, ResourcePool &pool, int detailLevel = 2);
  99. }
  100. #endif