Model3D.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #pragma once
  2. #include "Array.h"
  3. #include "Mat4.h"
  4. #include "Vec2.h"
  5. #include "Zeichnung3D.h"
  6. struct ID3D11Buffer;
  7. namespace Framework
  8. {
  9. struct Polygon2D; //! Model2D.h
  10. class Textur; //! Textur.h
  11. class Model2DData; //! Model2D.h
  12. class DXBuffer; //! DXBuffer.h
  13. class Render3D; //! Render3D.h
  14. class Model3DTextur; //! Model3D.h
  15. class Model3DList; //! Model3DList.h
  16. class Welt3D; //! Welt3D.h
  17. class DXBuffer;
  18. class Shader;
  19. class GraphicsApi;
  20. class M3Datei;
  21. //! Represents a bone of a 3D model. Can be animated
  22. class Bone
  23. {
  24. private:
  25. Vec3<float> pos;
  26. Vec3<float> rot;
  27. Bone* sibling;
  28. Bone* child;
  29. int id;
  30. public:
  31. //! Constructor
  32. //! \param id the id of the bone
  33. DLLEXPORT Bone(int id);
  34. //! Destructor
  35. DLLEXPORT ~Bone();
  36. //! set the position of the bone relative to the parent bone
  37. //! \param pos the position
  38. DLLEXPORT void setPosition(const Vec3<float>& pos);
  39. //! Set the rotation of the bone relative to the parent bone
  40. //! \param rot thr rotation
  41. DLLEXPORT void setRotation(const Vec3<float>& rot);
  42. //! add a sibling bone to this bone that shares the same parent bone
  43. //! \param b The bone to add
  44. void addSiblingBone(Bone* b);
  45. //! add a child bone to a specific child bone
  46. //! \param id the id of the bone the new bone should be a child of
  47. //! \param b the bone that should be added
  48. //! \return true if the bone was added, false if the bone with the given
  49. //! parent id was not found
  50. DLLEXPORT bool addChildBone(int id, Bone* b);
  51. //! calculates the matrixes of this bone, all child bones and sibling
  52. //! bones
  53. //! \param elternMat the already calculated matrix of the parent bone
  54. //! \param matBuffer the array to store the calculated matrixes
  55. //! \param scaleFactor the scaling of the object
  56. //! \param camMatrix the view-projection matrix of the used camera
  57. DLLEXPORT void calculateMatrix(const Mat4<float>& elternMat,
  58. Mat4<float>* matBuffer,
  59. float scaleFactor,
  60. const Mat4<float>& camMatrix);
  61. //! \return the first sibling bone
  62. DLLEXPORT Bone* zFirstSibling() const;
  63. //! \return the first child bone
  64. DLLEXPORT Bone* zFirstChild() const;
  65. //! returns a copy of this bone with copies of all child and
  66. //! sibling bones
  67. DLLEXPORT Bone* copyBone() const;
  68. //! \return the id of this bone
  69. DLLEXPORT int getId() const;
  70. //! \return the rotation of this bone
  71. DLLEXPORT Vec3<float> getRotation() const;
  72. //! \return the position of this bone
  73. DLLEXPORT Vec3<float> getPosition() const;
  74. //! \return the radius of this bone
  75. float getRadius() const;
  76. };
  77. //! Represents all bones of a model that can be used for animation
  78. class Skeleton : public virtual ReferenceCounter
  79. {
  80. private:
  81. Bone* rootBone;
  82. int nextId;
  83. Bone* zBone(Bone* zCurrent, int id) const;
  84. public:
  85. //! Constructor
  86. DLLEXPORT Skeleton();
  87. //! Destructor
  88. DLLEXPORT ~Skeleton();
  89. //! add a bone to the sceleton
  90. //! \param pos the position of the bone
  91. //! \param rot the rotation of the bone
  92. //! \param the id of the parent bone where the new bone should be added
  93. //! as a child
  94. //! \return the id of the added bone or -1 if the bone could not be
  95. //! added
  96. DLLEXPORT int addBone(
  97. Vec3<float> pos, Vec3<float> rot, int parentId = -1);
  98. //! calculates the matrices of all bones in this sceleton
  99. //! \param modelMatrix the already calculated matrix of the used 3d
  100. //! model \param matBuffer the array to store the calculated matrixes
  101. //! \param scaleFactor the scaling of the object
  102. //! \param camMatrix the view-projection matrix of the used camera
  103. DLLEXPORT int calculateMatrix(const Mat4<float>& modelMatrix,
  104. Mat4<float>* matBuffer,
  105. float scaleFactor,
  106. const Mat4<float>& camMatrix);
  107. //! \return the radius of the sceleton
  108. DLLEXPORT float getRadius() const;
  109. //! \return the root bone of the sceleton
  110. DLLEXPORT Bone* zRootBone() const;
  111. //! \return the bone with a specific id
  112. DLLEXPORT Bone* zBone(int id) const;
  113. //! \return a deep copy of the sceleton
  114. DLLEXPORT Skeleton* copySceleton() const;
  115. //! \return the next id for a bone ther can be only MAX_KNOCHEN_ANZ
  116. //! bones in a sceleton. if the sceleton is full -1 is returned
  117. DLLEXPORT int getNextBoneId() const;
  118. friend M3Datei;
  119. };
  120. //! A structure that stores the spatial position, texture coordinates,
  121. //! and associated bone for a vertex of a 3D model
  122. struct Vertex3D
  123. {
  124. Vec3<float>
  125. pos; //! The position of the vertex based on the bone's position
  126. Vec2<float> tPos; //! The texture coordinates of the vertex
  127. Vec3<float> normal; //! The normal (points outward and is perpendicular
  128. //! to the model's surface)
  129. int knochenId; //! The ID of the bone with which the vertex moves
  130. //! during animation
  131. int id; //! The index of the vertex in the vertex buffer
  132. };
  133. //! A structure that stores all triangles of a 3D polygon
  134. struct Polygon3D
  135. {
  136. int* indexList; //! The list of vertex IDs
  137. int indexAnz; //! The length of the list of vertex IDs
  138. //! Constructor
  139. DLLEXPORT Polygon3D();
  140. //! Destructor
  141. DLLEXPORT ~Polygon3D();
  142. };
  143. //! Stores all geometric data of a model, including spatial and texture
  144. //! coordinates and bone associations of all vertices
  145. class Model3DData : public virtual ReferenceCounter
  146. {
  147. private:
  148. Skeleton* skelett;
  149. Vertex3D* vertexList;
  150. int vertexCount;
  151. Array<Polygon3D*>* polygons;
  152. float ambientFactor;
  153. float diffusFactor;
  154. float specularFactor;
  155. float radius;
  156. int* indexBuffer;
  157. int indexCount;
  158. DXBuffer* dxIndexBuffer;
  159. DXBuffer* dxVertexBuffer;
  160. Vec3<float> minPos;
  161. Vec3<float> maxPos;
  162. int id;
  163. public:
  164. //! Constructor
  165. DLLEXPORT Model3DData(
  166. DXBuffer* dxVertexBuffer, DXBuffer* dxIndexBuffer, int id);
  167. //! Destructor
  168. DLLEXPORT ~Model3DData();
  169. //! updates the DX Buffer gpu memory if changed
  170. DLLEXPORT void updateGPUMemory();
  171. //! Deletes all model data
  172. DLLEXPORT void clearModel();
  173. //! Calculates the normals for the model's vertices
  174. DLLEXPORT void calculateNormals();
  175. //! Creates a buffer for all polygon indices
  176. DLLEXPORT void buildIndexBuffer();
  177. //! Sets a pointer to a default skeleton to use
  178. //! \param s The skeleton to use
  179. DLLEXPORT void setSkelettZ(Skeleton* s);
  180. //! Sets a pointer to a list of all vertices of the model
  181. //! \param vertexList An array of vertices
  182. //! \param anz The number of vertices in the array
  183. DLLEXPORT void setVertecies(Vertex3D* vertexList, int anz);
  184. //! Adds a polygon to the model
  185. //! \param polygon The polygon to add
  186. DLLEXPORT void addPolygon(Polygon3D* polygon);
  187. //! Sets the factor by which the ambient light (texture color)
  188. //! is multiplied \param f The new factor (from 0 to 1, ambient +
  189. //! specular + diffuse = 1)
  190. DLLEXPORT void setAmbientFactor(float f);
  191. //! Sets the factor by which the light color of light sources
  192. //! is multiplied \param f The new factor (from 0 to 1, ambient +
  193. //! specular + diffuse = 1)
  194. DLLEXPORT void setDiffusFactor(float f);
  195. //! Sets the factor by which the reflection of light sources
  196. //! is multiplied \param f The new factor (from 0 to 1, ambient +
  197. //! specular + diffuse = 1)
  198. DLLEXPORT void setSpecularFactor(float f);
  199. //! Converts a 2D model to 3D
  200. //! \param model The 2D model to convert to 3D
  201. //! \param z The z coordinate of all points of the model
  202. DLLEXPORT void copyModel2D(Model2DData* model, float z);
  203. //! Removes a polygon
  204. //! \param index The index of the polygon
  205. DLLEXPORT void removePolygon(int index);
  206. //! Calculates the matrices of the default skeleton's bones
  207. //! \param modelMatrix The matrix that transforms the skeleton into
  208. //! world space \param matBuffer An array of matrices filled with
  209. //! bone matrices \param scaleFactor The scaling of the model
  210. //! \param kamMatrix The combined view and projection matrices
  211. //! \return The number of matrices used. 0 if no default skeleton was set
  212. int kalkulateMatrix(const Mat4<float>& modelMatrix,
  213. Mat4<float>* matBuffer,
  214. float scaleFactor,
  215. const Mat4<float>& kamMatrix) const;
  216. //! Returns the number of polygons
  217. DLLEXPORT int getPolygonAnzahl() const;
  218. //! Returns a specific polygon
  219. //! \param index The index of the polygon
  220. DLLEXPORT Polygon3D* getPolygon(int index) const;
  221. //! Returns an iterator to list the polygons
  222. DLLEXPORT ArrayIterator<Polygon3D*> getPolygons() const;
  223. //! Returns the radius of a sphere that encloses the entire model
  224. DLLEXPORT float getRadius() const;
  225. //! Returns the ID of the data if registered in a Model3DList.
  226. //! (see Framework::zM3DRegister())
  227. DLLEXPORT int getId() const;
  228. //! Returns the factor by which ambient light (texture color)
  229. //! is multiplied
  230. DLLEXPORT float getAmbientFactor() const;
  231. //! Returns the factor by which the light color of light sources
  232. //! is multiplied
  233. DLLEXPORT float getDiffusFactor() const;
  234. //! Returns the factor by which the reflection of light sources
  235. //! is multiplied
  236. DLLEXPORT float getSpecularFactor() const;
  237. //! Returns a copy of the skeleton that can be used for animations
  238. DLLEXPORT Skeleton* copySkelett() const;
  239. //! Returns the number of vertices
  240. DLLEXPORT int getVertexAnzahl() const;
  241. //! Returns a buffer with all vertices of the model
  242. DLLEXPORT const Vertex3D* zVertexBuffer() const;
  243. //! Returns a reference to the start of the index buffer
  244. DLLEXPORT const int* getIndexBuffer() const;
  245. //! Returns the number of indices in the index buffer
  246. DLLEXPORT int getIndexCount() const;
  247. //! Returns the index buffer
  248. DLLEXPORT DXBuffer* zDXIndexBuffer() const;
  249. //! Returns the vertex buffer
  250. DLLEXPORT DXBuffer* zDXVertexBuffer() const;
  251. //! Returns the minimum point of the model's bounding box
  252. DLLEXPORT Vec3<float> getMinPos() const;
  253. //! Returns the maximum point of the model's bounding box
  254. DLLEXPORT Vec3<float> getMaxPos() const;
  255. };
  256. //! Stores a list of textures and which texture to use for which polygon
  257. class Model3DTextur : public virtual ReferenceCounter
  258. {
  259. private:
  260. Textur** textures;
  261. int textureCount;
  262. public:
  263. //! Constructor
  264. DLLEXPORT Model3DTextur();
  265. //! Destructor
  266. DLLEXPORT ~Model3DTextur();
  267. //! Sets which texture is for which polygon
  268. //! \param pI The index of the polygon
  269. //! \param txt The texture of the polygon
  270. DLLEXPORT void setPolygonTextur(int pI, Textur* txt);
  271. //! Returns a pointer to the texture of a polygon without increased
  272. //! reference counter \param i The index of the polygon
  273. DLLEXPORT Textur* zPolygonTextur(int i) const;
  274. };
  275. //! A drawing of the 3D framework that can display a 3D model with
  276. //! texture and animation
  277. class Model3D : public Zeichnung3D
  278. {
  279. protected:
  280. Skeleton* skelett;
  281. Model3DData* model;
  282. Model3DTextur* textur;
  283. float ambientFactor;
  284. float diffusFactor;
  285. float specularFactor;
  286. public:
  287. //! Constructor
  288. DLLEXPORT Model3D();
  289. //! Destructor
  290. DLLEXPORT virtual ~Model3D();
  291. //! Sets the model data
  292. //! \param data The data
  293. DLLEXPORT void setModelDaten(Model3DData* data);
  294. //! Sets the textures to use for drawing
  295. //! \param txt A list of textures assigned to the different polygons
  296. DLLEXPORT void setModelTextur(Model3DTextur* txt);
  297. //! Sets the factor by which the ambient light (texture color)
  298. //! is multiplied \param f The new factor (from 0 to 1, ambient +
  299. //! specular + diffuse = 1)
  300. DLLEXPORT void setAmbientFactor(float f);
  301. //! Sets the factor by which the light color of light sources
  302. //! is multiplied \param f The new factor (from 0 to 1, ambient +
  303. //! specular + diffuse = 1)
  304. DLLEXPORT void setDiffusFactor(float f);
  305. //! Sets the factor by which the reflection of light sources
  306. //! is multiplied \param f The new factor (from 0 to 1, ambient +
  307. //! specular + diffuse = 1)
  308. DLLEXPORT void setSpecularFactor(float f);
  309. //! Calculates the matrices of all bones in the model's skeleton
  310. //! \param viewProj The multiplied camera matrices
  311. //! \param matBuffer An array of matrices to fill
  312. //! \return The number of matrices the model requires
  313. DLLEXPORT int errechneMatrizen(
  314. const Mat4<float>& viewProj, Mat4<float>* matBuffer) override;
  315. //! Processes elapsed time
  316. //! \param tickval The time in seconds since the last call of this
  317. //! function \return true if the object changed, false otherwise.
  318. DLLEXPORT virtual bool tick(double tickval) override;
  319. //! For updating shader data
  320. DLLEXPORT virtual void beforeRender(
  321. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader);
  322. DLLEXPORT virtual void afterRender(
  323. GraphicsApi* api, Shader* zVertexShader, Shader* zPixelShader);
  324. //! Returns the texture
  325. DLLEXPORT Model3DTextur* getTextur();
  326. //! Returns the texture (without increased reference counter)
  327. DLLEXPORT Model3DTextur* zTextur();
  328. //! Returns the model data
  329. DLLEXPORT Model3DData* getModelData();
  330. //! Returns the model data (without increased reference counter)
  331. DLLEXPORT Model3DData* zModelData();
  332. //! Checks whether a ray hits this object
  333. //! \param point The starting point of the ray in world coordinates
  334. //! \param dir The direction of the ray in world coordinates
  335. //! \param maxSqDist The maximum allowed squared distance
  336. //! \param pId The ID of the polygon the intersection belongs to
  337. //! \return The squared distance of the intersection to the ray's
  338. //! origin, or -1 if no intersection exists
  339. DLLEXPORT virtual float traceRay(const Vec3<float>& point,
  340. const Vec3<float>& dir,
  341. float maxSqDist,
  342. int& pId) const;
  343. //! Calculates the color of a ray's intersection point
  344. //! \param point The starting point of the ray in world coordinates
  345. //! \param dir The direction of the ray in world coordinates
  346. //! \param zWelt The world from which the ray originates
  347. //! \return The color of the intersection point
  348. DLLEXPORT virtual int traceRay(
  349. Vec3<float>& point, Vec3<float>& dir, int pId, Welt3D* zWelt) const;
  350. //! Returns the ID of the data if registered in a Model3DList.
  351. //! (see Framework::zM3DRegister())
  352. DLLEXPORT int getDatenId() const;
  353. //! Returns the factor by which ambient light (texture color)
  354. //! is multiplied
  355. DLLEXPORT float getAmbientFactor() const;
  356. //! Returns the factor by which the light color of light sources
  357. //! is multiplied
  358. DLLEXPORT float getDiffusFactor() const;
  359. //! Returns the factor by which the reflection of light sources
  360. //! is multiplied
  361. DLLEXPORT float getSpecularFactor() const;
  362. //! Returns the number of vertices
  363. DLLEXPORT int getVertexAnzahl() const;
  364. //! Returns a buffer with all vertices of the model
  365. DLLEXPORT const Vertex3D* zVertexBuffer() const;
  366. //! Returns true if a specific polygon needs to be drawn
  367. DLLEXPORT virtual bool needRenderPolygon(int index);
  368. DLLEXPORT virtual Textur* zEffectTextur();
  369. DLLEXPORT virtual float getEffectPercentage();
  370. };
  371. } // namespace Framework