Model.h 6.2 KB

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