Model3D.h 18 KB

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