Model2D.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #ifndef Model2D_H
  2. #define Model2D_H
  3. #include <functional>
  4. #include "Array.h"
  5. #include "Drawing.h"
  6. #include "Point.h"
  7. #include "TriangleList.h"
  8. #include "Vec3.h"
  9. #include "World2D.h"
  10. namespace Framework
  11. {
  12. class Texture2D;
  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>* tCoordinates;
  21. Vertex* center;
  22. };
  23. //! The data for a 2D model
  24. class Model2DData : public virtual ReferenceCounter
  25. {
  26. private:
  27. RCArray<Array<Point>> 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 isPointInside(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 isLineInside(Vertex a, Vertex b, int polygonId = -1) const;
  36. public:
  37. Array<Polygon2D>* polygons;
  38. RCArray<RCArray<TriangleList<Vertex>>>* vLists;
  39. Point 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 createModell(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
  71. //! store the other half (output) \param posA The position of one new
  72. //! polygon (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. Point& posA,
  80. Point& 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<Texture2D>* texture;
  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 setTexture(Texture2D* 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 setTexture(
  106. Texture2D* 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 isPointInside(
  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 isLineInside(
  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
  128. //! reference counter \param sp A pointer to a point where the
  129. //! intersection point is stored \param end 0 if all corners of both
  130. //! objects should be checked. 1 if only the points of this model should
  131. //! be searched in the other \param ignoreTransparentFlag if 1,
  132. //! collisions with transparent polygons are also considered
  133. __declspec(dllexport) virtual bool isModelInside(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 getAirResistance() const override;
  146. //! Returns the mass of the 2D model (sum of the areas of the
  147. //! non-transparent polygons)
  148. __declspec(dllexport) float getMass() const override;
  149. //! Returns the texture of the first polygon
  150. __declspec(dllexport) Texture2D* getTexture() const;
  151. //! Returns the texture of a polygon
  152. //! \param polygonName The name of the polygon
  153. __declspec(dllexport) Texture2D* getTexture(
  154. const char* polygonName) const;
  155. //! Returns the texture of the first polygon without increased
  156. //! reference counter
  157. __declspec(dllexport) Texture2D* zTexture() const;
  158. //! Returns the texture of a polygon without increased reference
  159. //! counter \param polygonName The name of the polygon
  160. __declspec(dllexport) Texture2D* zTexture(
  161. const char* polygonName) const;
  162. //! Returns the model data
  163. __declspec(dllexport) Model2DData* getModel() const;
  164. //! Returns the model data without increased reference counter
  165. __declspec(dllexport) Model2DData* zModel() const;
  166. };
  167. //! A drawing of a model
  168. class Model2D : public Drawable
  169. {
  170. public:
  171. class Style : public Drawable::Style
  172. {
  173. public:
  174. static const __int64 Texture
  175. = 0x8; //! If this flag is set, a texture is used when drawing
  176. static const __int64 Border
  177. = 0x10; //! If this flag is set, the polygon borders are drawn
  178. static const __int64 Alpha = 0x40; //! If this flag is set, alpha
  179. //! blending is used when drawing
  180. static const __int64 Mesh
  181. = 0x20; //! If this flag is set, the triangle borders are drawn
  182. };
  183. private:
  184. Model2DData* rData;
  185. float rotation;
  186. float size;
  187. int color;
  188. RCArray<Texture2D>* textur;
  189. public:
  190. //! Constructor
  191. __declspec(dllexport) Model2D();
  192. //! Destructor
  193. __declspec(dllexport) virtual ~Model2D();
  194. //! Sets the model data
  195. //! \param mdl The model data
  196. __declspec(dllexport) void setModel(Model2DData* mdl);
  197. //! Sets the counter-clockwise rotation of the model
  198. //! \param rotation The angle in radians
  199. __declspec(dllexport) void setRotation(float rotation);
  200. //! Adds to the current rotation angle
  201. //! \param rotation The angle in radians to add
  202. __declspec(dllexport) void addRotation(float rotation);
  203. //! Sets the scaling of the model
  204. //! \param size The scaling factor
  205. __declspec(dllexport) void setSize(float size);
  206. //! Adds a value to the scaling
  207. //! \param size The value to add to the scaling
  208. __declspec(dllexport) void addSize(float size);
  209. //! Sets the texture
  210. //! \param t The image to use as texture
  211. __declspec(dllexport) void setTexture(Texture2D* t);
  212. //! Sets the texture
  213. //! \param t The image to use as texture
  214. //! \param polygonName The name of the polygon to receive the texture
  215. __declspec(dllexport) void setTexture(
  216. Texture2D* t, const char* polygonName);
  217. //! Sets the color
  218. //! \param f The color in A8R8G8B8 format
  219. __declspec(dllexport) void setColor(int f);
  220. //! Processes the time elapsed since the last call of this function
  221. //! \param tickVal The elapsed time in seconds
  222. __declspec(dllexport) bool tick(double tickVal) override;
  223. //! Draws the drawing into a specific image
  224. //! \param zRObj The image to draw into
  225. __declspec(dllexport) void render(Image& zRObj) override;
  226. //! Returns the rotation of the model
  227. __declspec(dllexport) float getRotation() const;
  228. //! Returns the scaling factor
  229. __declspec(dllexport) float getSize() const;
  230. //! Returns whether a point is inside the model
  231. //! \param p The point
  232. __declspec(dllexport) bool isPointInside(int x, int y) const override;
  233. //! Returns whether a point is inside the model
  234. //! \param p The point
  235. __declspec(dllexport) bool isPointInside(Vertex p) const;
  236. //! Checks whether a line is inside the model
  237. //! \param a The start point of the line
  238. //! \param b The end point of the line
  239. __declspec(dllexport) bool isLineInside(Vertex a, Vertex b) const;
  240. //! Checks whether the model intersects with another
  241. //! \param zMdl A pointer to the other model without increased reference
  242. //! counter \param end 0 if all corners of both models should be
  243. //! checked. 1 if only the points of this model should be searched in
  244. //! the other
  245. __declspec(dllexport) bool isModelInside(
  246. const Model2D* zMdl, bool end = 0) const;
  247. //! Returns the model data
  248. __declspec(dllexport) Model2DData* getModel() const;
  249. //! Returns the model data without increased reference counter
  250. __declspec(dllexport) Model2DData* zModel() const;
  251. };
  252. } // namespace Framework
  253. #endif