Model2D.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #ifndef Model2D_H
  2. #define Model2D_H
  3. #include <functional>
  4. #include "Array.h"
  5. #include "TriangleList.h"
  6. #include "Point.h"
  7. #include "Vec3.h"
  8. #include "World2D.h"
  9. #include "Drawing.h"
  10. namespace Framework
  11. {
  12. class Textur2D;
  13. //! A polygon structure used by Model2D.
  14. //! Model2D class deletes the pointers
  15. struct Polygon2D
  16. {
  17. bool transparent;
  18. Text* name;
  19. Array<Vertex>* vertex;
  20. Array<Vertex>* tKordinaten;
  21. Vertex* schwerpunkt;
  22. };
  23. //! The data for a 2D model
  24. class Model2DData : public virtual ReferenceCounter
  25. {
  26. private:
  27. RCArray<Array<Punkt>> outList;
  28. //! Checks whether a point is inside the model
  29. //! \param p The point
  30. //! \param polygonId The ID of the polygon to check
  31. bool istPunktInnen(Vertex p, int polygonId = -1) const;
  32. //! Checks whether a line is inside the model
  33. //! \param a The start point of the line
  34. //! \param b The end point of the line
  35. bool istLinieInnen(Vertex a, Vertex b, int polygonId = -1) const;
  36. public:
  37. Array<Polygon2D>* polygons;
  38. RCArray<RCArray<DreieckListe<Vertex>>>* vListen;
  39. Punkt minP, maxP;
  40. //! Constructor
  41. __declspec(dllexport) Model2DData();
  42. //! Destructor
  43. __declspec(dllexport) ~Model2DData();
  44. //! Creates the triangle lists from all given vertices of the
  45. //! polygons \param polygons An array of polygons \return always
  46. //! returns 1
  47. __declspec(dllexport) bool erstelleModell(Array<Polygon2D>* polygons);
  48. //! Deletes the created triangle lists and vertices
  49. __declspec(dllexport) void removeModell();
  50. //! Returns the polygon with a specific name
  51. //! \param pos The support vector of the line
  52. //! \param dir The direction vector of the line
  53. //! \param polygonName The name of the polygon to calculate the
  54. //! hit point with \param hitPoint A reference to the variable
  55. //! where the hit point should be stored \param moveSpeed The
  56. //! movement speed resulting from the force \param rotSpeed The
  57. //! rotation speed resulting from the force
  58. //! \return 1 if a hit point exists
  59. __declspec(dllexport) bool calcHitPoint(Vertex pos,
  60. Vertex dir,
  61. const char* polygonName,
  62. Vertex& hitpoint,
  63. Vertex& moveSpeed,
  64. float& rotSpeed) const;
  65. //! Splits a specific polygon into two parts
  66. //! \param pos Start position of the split
  67. //! \param dir Start direction of the split
  68. //! \param polygonName The name of the polygon
  69. //! \param partA A pointer to a Model2DData object to store one
  70. //! half (output) \param partB A pointer to a Model2DData object to store
  71. //! the other half (output) \param posA The position of one new polygon
  72. //! (output) \param posB The position of the other new polygon
  73. //! (output) \param random A function that returns random values
  74. __declspec(dllexport) bool split(Vertex pos,
  75. Vertex dir,
  76. char* polygonName,
  77. Polygon2D& partA,
  78. Polygon2D& partB,
  79. Punkt& posA,
  80. Punkt& posB,
  81. std::function<double()> random) const;
  82. //! Returns the mass of the 2D model (sum of the areas of the
  83. //! non-transparent polygons)
  84. __declspec(dllexport) float getMasse() const;
  85. };
  86. class Model2DObject : public Object2D
  87. {
  88. private:
  89. Model2DData* rData;
  90. RCArray<Textur2D>* textur;
  91. public:
  92. //! Constructor
  93. __declspec(dllexport) Model2DObject();
  94. //! Destructor
  95. __declspec(dllexport) virtual ~Model2DObject();
  96. //! Sets the model data
  97. //! \param mdl The model data
  98. __declspec(dllexport) void setModel(Model2DData* mdl);
  99. //! Sets the texture
  100. //! \param t The image to use as texture
  101. __declspec(dllexport) void setTextur(Textur2D* t);
  102. //! Sets the texture
  103. //! \param t The image to use as texture
  104. //! \param polygonName The name of the polygon to receive the texture
  105. __declspec(dllexport) void setTextur(
  106. Textur2D* t, const char* polygonName);
  107. __declspec(dllexport) void impuls(
  108. Vertex start, Vertex speed, float strength = 1.f) override;
  109. //! Draws the drawing into a specific image
  110. //! \param zRObj The image to draw into
  111. __declspec(dllexport) void render(
  112. Mat3<float>& kamMat, Image& zRObj, const char* kamName) override;
  113. //! Returns whether a point is inside the model
  114. //! \param p The point
  115. //! \param ignoreTransparentFlag if 1, collisions with transparent
  116. //! polygons are also considered
  117. __declspec(dllexport) bool istPunktInnen(
  118. Vertex p, bool ignoreTransparent = 0) const override;
  119. //! Checks whether a line is inside the model
  120. //! \param a The start point of the line
  121. //! \param b The end point of the line
  122. //! \param ignoreTransparentFlag if 1, collisions with transparent
  123. //! polygons are also considered
  124. __declspec(dllexport) bool istLinieInnen(
  125. Vertex a, Vertex b, bool ignoreTransparent = 0) const override;
  126. //! Checks whether the object intersects with another
  127. //! \param zObj A pointer to the other object without increased reference
  128. //! counter \param sp A pointer to a point where the intersection
  129. //! point is stored \param end 0 if all corners of both objects should be
  130. //! checked. 1 if only the points of this model should be searched
  131. //! in the other \param ignoreTransparentFlag if 1, collisions with
  132. //! transparent polygons are also considered
  133. __declspec(dllexport) virtual bool istModelInnen(const Object2D* zObj,
  134. Vertex* sp = 0,
  135. bool end = 0,
  136. bool ignoreTransparent = 0) const;
  137. __declspec(dllexport) Rect2<float> getBoundingBox() const override;
  138. //! Determines the hit point of a ray emitted from pos in the
  139. //! direction dir. \param pos The support vector of the line
  140. //! \param dir The direction vector of the line
  141. //! \param hitPoint A reference to the variable where the
  142. //! hit point should be stored \return 1 if a hit point exists
  143. __declspec(dllexport) bool calcHitPoint(
  144. Vertex pos, Vertex dir, Vertex& hitpoint) const override;
  145. __declspec(dllexport) float getLuftWiederstand() const override;
  146. //! Returns the mass of the 2D model (sum of the areas of the
  147. //! non-transparent polygons)
  148. __declspec(dllexport) float getMasse() const override;
  149. //! Returns the texture of the first polygon
  150. __declspec(dllexport) Textur2D* getTextur() const;
  151. //! Returns the texture of a polygon
  152. //! \param polygonName The name of the polygon
  153. __declspec(dllexport) Textur2D* getTextur(
  154. const char* polygonName) const;
  155. //! Returns the texture of the first polygon without increased
  156. //! reference counter
  157. __declspec(dllexport) Textur2D* zTextur() const;
  158. //! Returns the texture of a polygon without increased reference
  159. //! counter \param polygonName The name of the polygon
  160. __declspec(dllexport) Textur2D* zTextur(const char* polygonName) const;
  161. //! Returns the model data
  162. __declspec(dllexport) Model2DData* getModel() const;
  163. //! Returns the model data without increased reference counter
  164. __declspec(dllexport) Model2DData* zModel() const;
  165. };
  166. //! A drawing of a model
  167. class Model2D : public Drawable
  168. {
  169. public:
  170. class Style : public Drawable::Style
  171. {
  172. public:
  173. static const __int64 Textur
  174. = 0x8; //! If this flag is set, a texture is used when drawing
  175. static const __int64 Border
  176. = 0x10; //! If this flag is set, the polygon borders are drawn
  177. static const __int64 Alpha
  178. = 0x40; //! If this flag is set, alpha blending is used when drawing
  179. static const __int64 Mesh
  180. = 0x20; //! If this flag is set, the triangle borders are drawn
  181. };
  182. private:
  183. Model2DData* rData;
  184. float drehung;
  185. float size;
  186. int farbe;
  187. RCArray<Textur2D>* textur;
  188. public:
  189. //! Constructor
  190. __declspec(dllexport) Model2D();
  191. //! Destructor
  192. __declspec(dllexport) virtual ~Model2D();
  193. //! Sets the model data
  194. //! \param mdl The model data
  195. __declspec(dllexport) void setModel(Model2DData* mdl);
  196. //! Sets the counter-clockwise rotation of the model
  197. //! \param drehung The angle in radians
  198. __declspec(dllexport) void setDrehung(float drehung);
  199. //! Adds to the current rotation angle
  200. //! \param drehung The angle in radians to add
  201. __declspec(dllexport) void addDrehung(float drehung);
  202. //! Sets the scaling of the model
  203. //! \param size The scaling factor
  204. __declspec(dllexport) void setSize(float size);
  205. //! Adds a value to the scaling
  206. //! \param size The value to add to the scaling
  207. __declspec(dllexport) void addSize(float size);
  208. //! Sets the texture
  209. //! \param t The image to use as texture
  210. __declspec(dllexport) void setTextur(Textur2D* t);
  211. //! Sets the texture
  212. //! \param t The image to use as texture
  213. //! \param polygonName The name of the polygon to receive the texture
  214. __declspec(dllexport) void setTextur(
  215. Textur2D* t, const char* polygonName);
  216. //! Sets the color
  217. //! \param f The color in A8R8G8B8 format
  218. __declspec(dllexport) void setFarbe(int f);
  219. //! Processes the time elapsed since the last call of this function
  220. //! \param tickVal The elapsed time in seconds
  221. __declspec(dllexport) bool tick(double tickVal) override;
  222. //! Draws the drawing into a specific image
  223. //! \param zRObj The image to draw into
  224. __declspec(dllexport) void render(Image& zRObj) override;
  225. //! Returns the rotation of the model
  226. __declspec(dllexport) float getDrehung() const;
  227. //! Returns the scaling factor
  228. __declspec(dllexport) float getSize() const;
  229. //! Returns whether a point is inside the model
  230. //! \param p The point
  231. __declspec(dllexport) bool istPunktInnen(int x, int y) const override;
  232. //! Returns whether a point is inside the model
  233. //! \param p The point
  234. __declspec(dllexport) bool istPunktInnen(Vertex p) const;
  235. //! Checks whether a line is inside the model
  236. //! \param a The start point of the line
  237. //! \param b The end point of the line
  238. __declspec(dllexport) bool istLinieInnen(Vertex a, Vertex b) const;
  239. //! Checks whether the model intersects with another
  240. //! \param zMdl A pointer to the other model without increased reference
  241. //! counter \param end 0 if all corners of both models should be checked.
  242. //! 1 if only the points of this model should be searched in the other
  243. __declspec(dllexport) bool istModelInnen(
  244. const Model2D* zMdl, bool end = 0) const;
  245. //! Returns the model data
  246. __declspec(dllexport) Model2DData* getModel() const;
  247. //! Returns the model data without increased reference counter
  248. __declspec(dllexport) Model2DData* zModel() const;
  249. };
  250. } // namespace Framework
  251. #endif