modelAPI.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // Normalized texture coordinates:
  32. // (0.0f, 0.0f) is the texture coordinate for the upper left corner of the upper left pixel in the 2D texture.
  33. // (1.0f, 0.0f) is the texture coordinate for the upper right corner of the upper right pixel in the 2D texture.
  34. // (0.0f, 1.0f) is the texture coordinate for the lower left corner of the lower left pixel in the 2D texture.
  35. // (1.0f, 1.0f) is the texture coordinate for the lower right corner of the lower right pixel in the 2D texture.
  36. // (0.5f, 0.5f) is the texture coordinate for the center of the 2D texture.
  37. // Texture sampling:
  38. // By default, texture sampling is looped around the edges with bilinear with mip-maps for diffuse textures when available.
  39. // In bi-linear interpolation, the center of the pixel has the full weight of the color while the sides may show pixels from the other end of the texture.
  40. // When getting further away or viewing from the side, a lower resolution version of the texture will be taken from the pyramid if it was generated.
  41. // Seams between resolutions will be hard seams, so avoid using hard lines in textures if you want it to look natural.
  42. // Loading textures from a ResourcePool will automatically call image_generatePyramid.
  43. // Images without the power of two dimensions needed to generate a pyramid can not be used as textures in models.
  44. // image_isTexture can be used to know if an image has the supported dimensions for the current version of the renderer.
  45. // Side-effect: Creates a new empty model.
  46. // Post-condition: Returns a reference counted handle to the new model.
  47. // The model will be deleted automatically when all handles are gone.
  48. Model model_create();
  49. // Clones the geometry but refers to the same textures to save memory.
  50. // Pre-condition: model must refer to an existing model.
  51. // Post-condition: Returns a reference counted handle to the clone of model.
  52. Model model_clone(const Model& model);
  53. // Assign a filter to the whole model.
  54. // Assigning filters per model makes it easier to draw solid models before filtered models.
  55. // Two separate models can be used if you need both solid and filtered geometry.
  56. // Filters:
  57. // Filter::Alpha uses the alpha channel from the shader as opacity.
  58. // Filter::Solid is the default setting for newly created models.
  59. // Pre-condition: model must refer to an existing model.
  60. // Side-effect: Sets the given model's filter to the given filter argument.
  61. void model_setFilter(const Model& model, Filter filter);
  62. // Get back the filter enumeration, which was assigned to the model using model_setFilter.
  63. // This is useful for knowing in which pass to render your model.
  64. // Pre-condition: model must refer to an existing model.
  65. // Post-condition: Returns the model's filter enumeration
  66. Filter model_getFilter(const Model& model);
  67. // Post-condition: Returns true iff the model exists.
  68. bool model_exists(const Model& model);
  69. // Each part contains material settings and a list of polygons.
  70. // Each polygon contains 3 or 4 vertices. (triangles and quads)
  71. // Each vertex has with its own color, texture coordinates and position index.
  72. // Position indices refers to the model's list of points,
  73. // which is shared across multiple parts to avoid gaps between materials.
  74. // Pre-condition: model must refer to an existing model.
  75. // Side-effect: Adds an empty part without any polygons and returns its new local part index.
  76. // The returned part index is relative to the model and goes from 0 to model_getNumberOfParts(model) - 1.
  77. int model_addEmptyPart(Model& model, const String &name);
  78. // Pre-condition: model must refer to an existing model.
  79. // Post-condition: Returns the number of parts in model.
  80. int model_getNumberOfParts(const Model& model);
  81. // Pre-condition: model must refer to an existing model.
  82. // Side-effect: Sets the part at partIndex in model to the new name.
  83. void model_setPartName(Model& model, int partIndex, const String &name);
  84. // Pre-condition: model must refer to an existing model.
  85. // Post-condition: Returns the name of the part at partIndex in model.
  86. String model_getPartName(const Model& model, int partIndex);
  87. // Pre-condition: model must refer to an existing model.
  88. // Post-condition: Returns the number of points in model.
  89. int model_getNumberOfPoints(const Model& model);
  90. // Pre-condition: model must refer to an existing model.
  91. // Post-condition: Returns the 3D position of the point at pointIndex in model.
  92. FVector3D model_getPoint(const Model& model, int pointIndex);
  93. // Pre-condition: model must refer to an existing model.
  94. void model_setPoint(Model& model, int pointIndex, const FVector3D& position);
  95. // Pre-condition: model must refer to an existing model.
  96. // Post-condition:
  97. // Returns an index to the closest point in model relative to position in euclidean distance.
  98. // Returns -1 if none was inside of threshold.
  99. // A point p is inside of threshold iff |p - position| < threshold.
  100. // If multiple points have the same distance approximated, the point with the lowest index will be preferred.
  101. int model_findPoint(const Model& model, const FVector3D &position, float threshold);
  102. // Add a point even if it overlaps an existing point.
  103. // Can be used for animation where the initial position might not always be the same.
  104. // Pre-condition: model must refer to an existing model.
  105. // Side-effect: Adds a new point to model at position.
  106. // Post-condition: Returns a local index to the new point.
  107. int model_addPoint(const Model& model, const FVector3D &position);
  108. // Add a point, only if it does not overlap.
  109. // Can be used to seal small gaps and reduce the time needed to transform vertex positions.
  110. // Pre-condition: model must refer to an existing model.
  111. // Side-effect: Adds a new point to model at position unless another point already exists within threshold so that model_findPoint(model, position, threshold) > -1.
  112. // Post-condition:
  113. // If a new point was created then its new index is returned.
  114. // Otherwise, if any existing point was within threshold,
  115. // then the index of the closest existing point in euclidean distance is returned.
  116. // If multiple existing points are within the same distance,
  117. // then the point with the lowest index is preferred, just like in model_findPoint.
  118. int model_addPointIfNeeded(Model& model, const FVector3D &position, float threshold);
  119. // Get the bounding box, which expands automatically when adding or moving points in the model.
  120. // Side-effect: Writes model's bounding box to minimum and maximum by reference.
  121. void model_getBoundingBox(const Model& model, FVector3D& minimum, FVector3D& maximum);
  122. // Get the vertex position's index, which refers to a shared point in the model.
  123. // Pre-condition: model must refer to an existing model.
  124. // Post-condition: Returns the position index of the vertex. (At vertexIndex in the polygon at polygonIndex in the part at partIndex in model.)
  125. int model_getVertexPointIndex(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  126. // Pre-condition: model must refer to an existing model.
  127. // Side-effect: Sets the position index of the vertex to pointIndex. (At vertexIndex in the polygon at polygonIndex in the part at partIndex in model.)
  128. void model_setVertexPointIndex(Model& model, int partIndex, int polygonIndex, int vertexIndex, int pointIndex);
  129. // Get the vertex position directly, without having to look it up by index using model_getPoint.
  130. // Pre-condition: model must refer to an existing model.
  131. // Post-condition: Returns the position of the vertex. (At vertexIndex in the polygon at polygonIndex in the part at partIndex in model.)
  132. FVector3D model_getVertexPosition(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  133. // Get the vertex color, which is not shared with any other polygons. (Red, green, blue, alpha) channels are packed as (x, y, z, w) in FVector4D.
  134. // Vertex colors use a normalized scale from 0.0f to 1.0f.
  135. // Transparent black is FVector4D(0.0f, 0.0f, 0.0f, 0.0f).
  136. // Solid red is FVector4D(1.0f, 0.0f, 0.0f, 1.0f).
  137. // Solid green is FVector4D(0.0f, 1.0f, 0.0f, 1.0f).
  138. // Solid blue is FVector4D(0.0f, 0.0f, 1.0f, 1.0f).
  139. // Half opaque orange is FVector4D(1.0f, 0.5f, 0.0f, 0.5f).
  140. // Pre-condition: model must refer to an existing model.
  141. // Post-condition: Returns the color of the vertex. (At vertexIndex in the polygon at polygonIndex in the part at partIndex in model.)
  142. FVector4D model_getVertexColor(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  143. // Set the vertex color using the same system as model_getVertexColor.
  144. // Pre-condition: model must refer to an existing model.
  145. // Side-effect: Sets the color of the vertex to color. (At vertexIndex in the polygon at polygonIndex in the part at partIndex in model.)
  146. void model_setVertexColor(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& color);
  147. // Get (U1, V1, U2, V2) texture coordinates packed as (x, y, z, w) in FVector4D.
  148. // UV1 coordinates (x, y) refers to normalized texture sampling coordinates for the diffuse-map.
  149. // UV2 coordinates (z, w) refers to normalized texture sampling coordinates for the light-map.
  150. // Light-maps do not use mip-map layers, which allow generating light-maps dynamically.
  151. // Pre-condition: model must refer to an existing model.
  152. FVector4D model_getTexCoord(const Model& model, int partIndex, int polygonIndex, int vertexIndex);
  153. // Pre-condition: model must refer to an existing model.
  154. // Side-effect: Sets the texture coordinates of the vertex to texCoord for both UV1 and UV2.
  155. // (At vertexIndex in the polygon at polygonIndex in the part at partIndex in model.)
  156. void model_setTexCoord(Model& model, int partIndex, int polygonIndex, int vertexIndex, const FVector4D& texCoord);
  157. // Create a triangle surface at given position indices.
  158. // The fourth vertex is used as padding, so quads and triangles take the same amount of memory per polygon.
  159. // Using two triangles instead of one quad would use twice as much memory.
  160. // Pre-condition: model must refer to an existing model.
  161. // Side-effect:
  162. // Adds a new polygon in the model's part at partIndex.
  163. // The new polygon contains three vertices.
  164. // Each new vertex has texture coordinates set to the upper left corner using (0.0f, 0.0f, 0.0f, 0.0f).
  165. // Each new vertex has the color set to solid white using (1.0f, 1.0f, 1.0f, 1.0f).
  166. // Post-condition:
  167. // Returns the new polygon's local index within the part at partIndex in model.
  168. int model_addTriangle(Model& model, int partIndex, int pointA, int pointB, int pointC);
  169. // Create a quad surface at given position indices.
  170. // Pre-condition: model must refer to an existing model.
  171. // Side-effect:
  172. // Adds a new polygon in the model's part at partIndex.
  173. // The new polygon contains four vertices.
  174. // Each new vertex has texture coordinates set to the upper left corner using (0.0f, 0.0f, 0.0f, 0.0f).
  175. // Each new vertex has the color set to solid white using (1.0f, 1.0f, 1.0f, 1.0f).
  176. // Post-condition:
  177. // Returns the new polygon's local index within the part at partIndex in model.
  178. int model_addQuad(Model& model, int partIndex, int pointA, int pointB, int pointC, int pointD);
  179. // Pre-condition: model must refer to an existing model.
  180. // Post-condition: Returns the number of polygons (triangles + quads) in the part at partIndex in model.
  181. int model_getNumberOfPolygons(const Model& model, int partIndex);
  182. // Pre-condition: model must refer to an existing model.
  183. // Post-condition: Returns the number of vertices in the polygon at polygonIndex in the part at partIndex in model.
  184. int model_getPolygonVertexCount(const Model& model, int partIndex, int polygonIndex);
  185. // Get the part's diffuse texture.
  186. // Pre-condition: model must refer to an existing model.
  187. // Post-condition:
  188. // Returns an image handle to the diffuse texture in the part at partIndex in model.
  189. // If the part has no diffuse image then an empth handle is returned.
  190. ImageRgbaU8 model_getDiffuseMap(const Model& model, int partIndex);
  191. // Set the part's diffuse texture.
  192. // A texture is just an image fulfilling the criterias of image_isTexture to allow fast texture sampling and pyramid generation.
  193. // Pre-condition:
  194. // model must refer to an existing model.
  195. // diffuseMap must be either empty or have power-of-two dimensions accepted by image_isTexture.
  196. // Side-effect:
  197. // Sets the diffuse texture in the part at partIndex in model to diffuseMap.
  198. // If diffuseMap is an empty image handle, then the diffuse texture will be replaced by the default solid white color.
  199. void model_setDiffuseMap(Model& model, int partIndex, const ImageRgbaU8 &diffuseMap);
  200. // Automatically find the diffuse texture by name in the resource pool and assign it.
  201. // Pre-condition:
  202. // model must refer to an existing model.
  203. // pool must refer to an existing resource pool.
  204. // filename must be the image's filename without any extension nor path.
  205. // "Car" is accepted.
  206. // "Car.png" is rejected for having an extension.
  207. // "myFolder/Car" is rejected for having a path.
  208. // "myFolder\\Car" is rejected for having a path.
  209. // "Car_1.2" is rejected for using a dot in the actual name, just to catch more mistakes with file extensions.
  210. // Side-effect:
  211. // Sets the diffuse texture in the part at partIndex in model to the image looked up by filename in pool.
  212. void model_setDiffuseMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename);
  213. // Get the part's light texture.
  214. // Pre-condition:
  215. // model must refer to an existing model.
  216. // Post-condition:
  217. // Returns an image handle to the light texture in the part at partIndex in model.
  218. // If the part has no light image then an empth handle is returned.
  219. ImageRgbaU8 model_getLightMap(Model& model, int partIndex);
  220. // Set the part's light texture.
  221. // A texture is just an image fulfilling the criterias of image_isTexture to allow fast texture sampling.
  222. // Even though no texture-pyramid is used for light-maps, it still has to look up
  223. // pixels quickly using bit-shifts with base two logarithms of power of two widths.
  224. // Pre-condition:
  225. // model must refer to an existing model.
  226. // lightMap must be either empty or have power-of-two dimensions accepted by image_isTexture.
  227. // Side-effect:
  228. // Sets the diffuse texture in the part at partIndex in model to diffuseMap.
  229. // If diffuseMap is an empty image handle, then the diffuse texture will be replaced by the default solid white color.
  230. void model_setLightMap(Model& model, int partIndex, const ImageRgbaU8 &lightMap);
  231. // Automatically find the light texture by name in the resource pool and assign it.
  232. // Pre-condition:
  233. // model must refer to an existing model.
  234. // pool must refer to an existing resource pool.
  235. // filename must be the image's filename without any extension nor path.
  236. // Side-effect:
  237. // Sets the light texture in the part at partIndex in model to the image looked up by filename in pool.
  238. void model_setLightMapByName(Model& model, int partIndex, ResourcePool &pool, const String &filename);
  239. // In order to draw two adjacent polygons without any missing pixels along the seam, they must:
  240. // * Share two position indices in opposite directions.
  241. // (Rounding the same value to integers twice can be rounded differently,
  242. // even though it's highly unlikely to actually happen.)
  243. // * Have each vertex position inside of the camera's clipping frustum.
  244. // (Far outside of the view frustum, triangles must be clipped in
  245. // floating-point 3D space to prevent integer overflows when converted
  246. // to sub-pixel integer coordinates.)
  247. // * Avoid colliding with near or far clip planes.
  248. // (This would also cause clipping in floating-point 3D space, because a
  249. // location behind the camera cannot be represented as a screen coordinate)
  250. // If your clipped polygons are fully outside of the view-frustum,
  251. // then you will not see the seam nor the polygons.
  252. // To solve this:
  253. // * use model_addPointIfNeeded instead of model_addPoint when adding points.
  254. // * Split polygons that are way too big and use them to produce more details.
  255. // (This will also increase precision for texture coordinates by splitting up seemingly infinite planes.)
  256. // If this does not hold true then there is either an exception missing
  257. // or a bug in the renderer, which should be reported as soon as possible.
  258. // Single-threaded rendering (Slow but easy to use for small tasks)
  259. // Can be executed on different threads if targetImage and depthBuffer doesn't have overlapping memory lines between the threads
  260. // Pre-condition: colorBuffer and depthBuffer must have the same dimensions.
  261. // Side-effect: Render any model transformed by modelToWorldTransform, seen from camera, to any colorBuffer using any depthBuffer.
  262. // An empty model handle will be skipped silently, which can be used instead of an model with zero polygons.
  263. void model_render(const Model& model, const Transform3D &modelToWorldTransform, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer, const Camera &camera);
  264. // Simpler rendering without colorBuffer, for shadows and other depth effects
  265. // Equivalent to model_render with a non-existing colorBuffer and filter forced to solid.
  266. // Skip this call conditionally for filtered models (using model_getFilter) if you want full equivalence with model_render.
  267. // Side-effect: Render any model transformed by modelToWorldTransform, seen from camera, to any depthBuffer.
  268. // An empty model handle will be skipped silently, which can be used instead of an model with zero polygons.
  269. void model_renderDepth(const Model& model, const Transform3D &modelToWorldTransform, ImageF32& depthBuffer, const Camera &camera);
  270. // Multi-threaded rendering (Huge performance boost with more CPU cores!)
  271. // Post-condition: Returns the handle to a new multi-threaded rendering context.
  272. // It is basically a list of triangles to be drawn in parallel using a single call.
  273. // After creating a renderer, you may execute a number of batches using it.
  274. // Each batch may execute a number of tasks in parallel.
  275. // Call pattern:
  276. // renderer_create (renderer_begin renderer_giveTask* renderer_end)*
  277. Renderer renderer_create();
  278. // Post-condition: Returns true iff the renderer exists.
  279. bool renderer_exists(const Renderer& renderer);
  280. // Prepares for rendering by giving the target images to draw pixels on.
  281. // This step makes sure that nobody changes the target dimensions while rendering,
  282. // which could otherwise happen if someone requests a new canvas too often.
  283. // Pre-condition:
  284. // renderer must refer to an existing renderer.
  285. // colorBuffer and depthBuffer must have the same dimensions.
  286. void renderer_begin(Renderer& renderer, ImageRgbaU8& colorBuffer, ImageF32& depthBuffer);
  287. // Once an object passed game-specific occlusion tests, give it to the renderer using renderer_giveTask.
  288. // The render job will be performed during the next call to renderer_end.
  289. // Pre-condition: renderer must refer to an existing renderer.
  290. // An empty model handle will be skipped silently, which can be used instead of an model with zero polygons.
  291. // Side-effect: The visible triangles are queued up in the renderer.
  292. void renderer_giveTask(Renderer& renderer, const Model& model, const Transform3D &modelToWorldTransform, const Camera &camera);
  293. // Side-effect: Finishes all the jobs in the rendering context so that triangles are rasterized to the targets given to renderer_begin.
  294. // Pre-condition: renderer must refer to an existing renderer.
  295. void renderer_end(Renderer& renderer);
  296. // Imports a DMF model from file content.
  297. // Use in combination with string_load or your own system for storing files.
  298. // Example:
  299. // Model crateModel = importFromContent_DMF1(string_load(mediaPath + U"Model_Crate.dmf"), pool);
  300. // Pre-condition:
  301. // fileContent must be the content of a DMF 1.0 model file.
  302. // pool must refer to an existing resource pool.
  303. // 0 <= detailLevel <= 2 (0 = low, 1 = medium, 2 = high)
  304. // Post-condition:
  305. // Returns a handle to a model imported from fileContent, using pool to access resources, with parts not visible in detailLevel excluded.
  306. // How to import from the DMF1 format:
  307. // * Only use M_Diffuse_0Tex, M_Diffuse_1Tex or M_Diffuse_2Tex as shaders.
  308. // Place any diffuse texture in texture slot 0 and any lightmap in slot 1.
  309. // Remove any textures that are not used by the shaders.
  310. // The fixed pipeline only checks which textures are used.
  311. // * Make sure that texture names are spelled case sensitive or they might not be found on some operating systems like Linux.
  312. // See renderer/model/format/dmf1.cpp for the implementation. (It does not exist in api/modelAPI.cpp)
  313. Model importFromContent_DMF1(const String &fileContent, ResourcePool &pool, int detailLevel = 2);
  314. }
  315. #endif