Model.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 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. #ifndef DFPSR_RENDER_MODEL_POLYGONMODEL
  24. #define DFPSR_RENDER_MODEL_POLYGONMODEL
  25. #include <cstdint>
  26. #include "../../api/stringAPI.h"
  27. #include "../../image/Texture.h"
  28. #include "../shader/Shader.h"
  29. #include "../Camera.h"
  30. #include "../ResourcePool.h"
  31. #include "../renderCore.h"
  32. #include "../../math/FVector.h"
  33. namespace dsr {
  34. // Only used when constructing new polygons
  35. struct VertexData {
  36. FVector4D texCoord; // Two 2D coordinates or one 3D coordinate
  37. FVector4D color; // RGBA
  38. VertexData() : texCoord(FVector4D(0.0f, 0.0f, 0.0f, 0.0f)), color(FVector4D(1.0f, 1.0f, 1.0f, 1.0f)) {}
  39. VertexData(const FVector4D &texCoord, const FVector4D &color) : texCoord(texCoord), color(color) {}
  40. };
  41. // Only used when constructing new polygons
  42. struct Vertex {
  43. int32_t pointIndex = -1; // Empty
  44. VertexData data;
  45. Vertex() {}
  46. Vertex(int32_t pointIndex, const VertexData &data) : pointIndex(pointIndex), data(data) {}
  47. };
  48. struct Polygon {
  49. static const int maxCorners = 4;
  50. int32_t pointIndices[maxCorners]; // pointIndices[3] equals -1 for triangles
  51. FVector4D texCoords[maxCorners];
  52. FVector4D colors[maxCorners];
  53. Polygon(const Vertex &vertA, const Vertex &vertB, const Vertex &vertC);
  54. Polygon(const Vertex &vertA, const Vertex &vertB, const Vertex &vertC, const Vertex &vertD);
  55. Polygon(int indexA, int indexB, int indexC);
  56. Polygon(int indexA, int indexB, int indexC, int indexD);
  57. int getVertexCount() const;
  58. };
  59. struct Part {
  60. TextureRgbaU8 diffuseMap, lightMap;
  61. List<Polygon> polygonBuffer;
  62. String name;
  63. explicit Part(String name);
  64. Part(const TextureRgbaU8 &diffuseMap, const TextureRgbaU8 &lightMap, const List<Polygon> &polygonBuffer, const String &name);
  65. Part clone() const;
  66. void render(CommandQueue *commandQueue, ImageRgbaU8* targetImage, ImageF32* depthBuffer, const Transform3D &modelToWorldTransform, const Camera &camera, Filter filter, const ProjectedPoint* projected) const;
  67. void renderDepth(ImageF32* depthBuffer, const Transform3D &modelToWorldTransform, const Camera &camera, const ProjectedPoint* projected) const;
  68. int getPolygonCount() const;
  69. int getPolygonVertexCount(int polygonIndex) const;
  70. };
  71. class ModelImpl {
  72. public:
  73. Filter filter = Filter::Solid;
  74. List<FVector3D> positionBuffer; // Also called points
  75. List<Part> partBuffer;
  76. FVector3D minBound, maxBound;
  77. private:
  78. // TODO: A method for recalculating a possibly tighter bounding box
  79. void expandBound(const FVector3D& point);
  80. public:
  81. ModelImpl();
  82. ModelImpl(Filter filter, const List<Part> &partBuffer, const List<FVector3D> &positionBuffer);
  83. ModelImpl(const ModelImpl &old);
  84. // Geometry interface
  85. // TODO: Make a sweep-and-clean garbage collection of unused points so that indices are remapped once for all changes
  86. // TODO: Add empty quads and triangles and fill them later using setters
  87. // TODO: Make a texture slot index instead of having getters and setters for each texture slot
  88. // Part interface
  89. int addEmptyPart(const String& name);
  90. int getNumberOfParts() const;
  91. void setPartName(int partIndex, const String &name);
  92. String getPartName(int partIndex) const;
  93. // TODO: Make an array of texture slots using a class enum for index
  94. TextureRgbaU8 getDiffuseMap(int partIndex) const;
  95. void setDiffuseMap(const TextureRgbaU8 &diffuseMap, int partIndex);
  96. void setDiffuseMapByName(ResourcePool &pool, const String &filename, int partIndex);
  97. TextureRgbaU8 getLightMap(int partIndex) const;
  98. void setLightMap(const TextureRgbaU8 &lightMap, int partIndex);
  99. void setLightMapByName(ResourcePool &pool, const String &filename, int partIndex);
  100. // Polygon interface
  101. int addPolygon(Polygon polygon, int partIndex);
  102. int getNumberOfPolygons(int partIndex) const;
  103. int getPolygonVertexCount(int partIndex, int polygonIndex) const;
  104. // Point interface
  105. int getNumberOfPoints() const;
  106. FVector3D getPoint(int pointIndex) const;
  107. void setPoint(int pointIndex, const FVector3D& position);
  108. int findPoint(const FVector3D &position, float threshold) const;
  109. int addPoint(const FVector3D &position);
  110. int addPointIfNeeded(const FVector3D &position, float threshold); // Returns the index of a new point or the first existing within threshold in euclidean 3D distance
  111. // Vertex interface
  112. int getVertexPointIndex(int partIndex, int polygonIndex, int vertexIndex) const;
  113. void setVertexPointIndex(int partIndex, int polygonIndex, int vertexIndex, int pointIndex);
  114. FVector3D getVertexPosition(int partIndex, int polygonIndex, int vertexIndex) const; // Returning getPoint using the point index shared by other polygons
  115. FVector4D getVertexColor(int partIndex, int polygonIndex, int vertexIndex) const;
  116. void setVertexColor(int partIndex, int polygonIndex, int vertexIndex, const FVector4D& color);
  117. FVector4D getTexCoord(int partIndex, int polygonIndex, int vertexIndex) const;
  118. void setTexCoord(int partIndex, int polygonIndex, int vertexIndex, const FVector4D& texCoord);
  119. // Rendering
  120. void render(CommandQueue *commandQueue, ImageRgbaU8* targetImage, ImageF32* depthBuffer, const Transform3D &modelToWorldTransform, const Camera &camera) const;
  121. void renderDepth(ImageF32* depthBuffer, const Transform3D &modelToWorldTransform, const Camera &camera) const;
  122. };
  123. }
  124. #endif