PolyEntity.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. #include "PolyMatrix4.h"
  23. #include "PolyQuaternion.h"
  24. #include "PolyColor.h"
  25. #include <vector>
  26. namespace Polycode {
  27. class Renderer;
  28. class _PolyExport EntityProp {
  29. public:
  30. String propName;
  31. String propValue;
  32. };
  33. /**
  34. * Base class for both 2D and 3D objects in Polycode. It provides position and color transformations as well as hierarchy for all Polycode objects.
  35. */
  36. class _PolyExport Entity {
  37. public:
  38. Entity();
  39. ~Entity();
  40. /**
  41. * Main render method. Override this to do your own drawing.
  42. */
  43. virtual void Render(){};
  44. /**
  45. * Main update method. Override this to do your updates before the render cycle.
  46. */
  47. virtual void Update(){};
  48. virtual void transformAndRender();
  49. void renderChildren();
  50. // ----------------------------------------------------------------------------------------------------------------
  51. /** @name Matrix operations.
  52. * These methods operate directly on the entity's matrix.
  53. */
  54. //@{
  55. /**
  56. * Sets the state of the dirty matrix flag. The dirty matrix flag gets set automatically when a transformation is applied to the entity and the transfrom matrix needs to be rebuilt. This method is provided to manually override that flag.
  57. @param val New value of the dirty matrix flag.
  58. */
  59. void dirtyMatrix(bool val);
  60. /**
  61. * Forces the transformation matrix to be rebuilt.
  62. */
  63. void rebuildTransformMatrix();
  64. /**
  65. * Forces the matrix to be rebuilt if the matrix flag is dirty. This is also called on all of the entity's children.
  66. */
  67. void updateEntityMatrix();
  68. /**
  69. * Returns the entity's transform matrix.
  70. @return Transform matrix.
  71. */
  72. const Matrix4& getTransformMatrix() const;
  73. /**
  74. * Returns the entity's matrix multiplied by its parent's concatenated matrix. This, in effect, returns the entity's actual world transformation.
  75. @return Entity's concatenated matrix.
  76. */
  77. Matrix4 getConcatenatedMatrix() const;
  78. /**
  79. * Returns Same as getConcatenatedMatrix(), but contains only roll information for rotation. Used internally for billboards.
  80. @return Entity's concatenated roll matrix.
  81. @see getConcatenatedMatrix()
  82. */
  83. Matrix4 getConcatenatedRollMatrix() const;
  84. /**
  85. * Sets all of the individual transform properties from the matrix and rebuilds the transform matrix.
  86. @param matrix 4x4 transform matrix to apply.
  87. */
  88. void setTransformByMatrix(const Matrix4& matrix);
  89. /**
  90. * Sets the transform matrix directly, without setting all of the individual transfrom properties of the entity.
  91. @param matrix 4x4 transform matrix to apply.
  92. */
  93. void setTransformByMatrixPure(const Matrix4& matrix);
  94. /** Returns the matrix for the entity looking at a location based on a location and an up vector.
  95. * @param loc Location to look at.
  96. * @param upVector Up vector.
  97. * @return The resulting lookAt matrix.
  98. */
  99. Matrix4 getLookAtMatrix(const Vector3 &loc, const Vector3 &upVector = Vector3(0,1,0));
  100. //@}
  101. // ----------------------------------------------------------------------------------------------------------------
  102. /** @name Hierarchy operations.
  103. * These methods add and remove entities to and from each other.
  104. */
  105. //@{
  106. /**
  107. * @see addChild()
  108. */
  109. void addEntity(Entity *newChild);
  110. /**
  111. * Adds another entity as a child. The children inherit the parent's transforms.
  112. @param newChild The entity to be added.
  113. */
  114. void addChild(Entity *newChild);
  115. /**
  116. * Removes an entity from the entity's children.
  117. @param entityToRemove Entity to be removed.
  118. */
  119. void removeChild(Entity *entityToRemove);
  120. /**
  121. * Manually sets the entity's parent. This method does not add the entity to the parent and should not be called manually.
  122. @param entity Parent entity.
  123. */
  124. void setParentEntity(Entity *entity);
  125. /**
  126. * Returns the parent entity of the entity.
  127. @return Parent entity of this entity.
  128. */
  129. Entity *getParentEntity() const;
  130. //@}
  131. // ----------------------------------------------------------------------------------------------------------------
  132. /** @name Transform operations.
  133. * These methods apply various transformations to the entity.
  134. */
  135. //@{
  136. /**
  137. * Returns the entity's position.
  138. @return Entity's position as a vector.
  139. */
  140. Vector3 getPosition() const;
  141. /**
  142. * Returns the entity's position added to the combined position of its parent. This method is here only for convenience of calculating certain properties and should not be used to get an entity's actual position in the world. To get the actual world position of the entity, use the entity's concatendated matrix.
  143. @see getConcatenatedMatrix()
  144. @return Entity's position as a vector.
  145. */
  146. Vector3 getCombinedPosition() const;
  147. /**
  148. * Sets the entity's position.
  149. @param x X-axis value.
  150. @param y Y-axis value.
  151. @param z Z-axis value.
  152. */
  153. void setPosition(Number x, Number y, Number z);
  154. /**
  155. * Sets the entity's position with a vector.
  156. @param posVec New position as a vector.
  157. */
  158. void setPosition(Vector3 posVec);
  159. /**
  160. * Returns the entity's position on the X axis.
  161. @param x X-axis value.
  162. */
  163. void setPositionX(Number x);
  164. /**
  165. * Returns the entity's position on the Y axis.
  166. @param y Y-axis value.
  167. */
  168. void setPositionY(Number y);
  169. /**
  170. * Translates the entity relative to its current position.
  171. @param x X-axis value.
  172. @param y Y-axis value.
  173. @param z Z-axis value.
  174. */
  175. void Translate(Number x, Number y, Number z);
  176. /**
  177. * Translates the entity relative to its current position with a vector.
  178. @param tVec New position as a vector.
  179. */
  180. void Translate(Vector3 tVec);
  181. /**
  182. * Returns the entity's position on the Z axis.
  183. @param z Z-axis value.
  184. */
  185. void setPositionZ(Number z);
  186. /**
  187. * Returns the entity's scale on the X axis.
  188. @param x X-axis scale value.
  189. */
  190. void setScaleX(Number x);
  191. /**
  192. * Returns the entity's scale on the Y axis.
  193. @param y Y-axis scale value.
  194. */
  195. void setScaleY(Number y);
  196. /**
  197. * Returns the entity's scale on the Z axis.
  198. @param z Z-axis scale value.
  199. */
  200. void setScaleZ(Number z);
  201. /**
  202. * Scales the entity relative to its current scale.
  203. @param x X-axis value.
  204. @param y Y-axis value.
  205. @param z Z-axis value.
  206. */
  207. void Scale(Number x, Number y, Number z);
  208. /**
  209. * Sets the entity's scale.
  210. @param x X-axis value.
  211. @param y Y-axis value.
  212. @param z Z-axis value.
  213. */
  214. void setScale(Number x, Number y, Number z);
  215. /**
  216. * Returns the entity's scale multiplied by its parent's compound scale.
  217. * @return Compound scale as vector.
  218. */
  219. Vector3 getCompoundScale() const;
  220. /**
  221. * Returns the entity's scale.
  222. @return Entity's scale as a vector.
  223. */
  224. Vector3 getScale() const;
  225. /**
  226. * Returns the entity's pitch combined with the combined pitch of its parent.
  227. @return Entity's combined pitch.
  228. */
  229. Number getCombinedPitch() const;
  230. /**
  231. * Returns the entity's yaw combined with the combined yaw of its parent.
  232. @return Entity's combined yaw.
  233. */
  234. Number getCombinedYaw() const;
  235. /**
  236. * Returns the entity's roll combined with the combined roll of its parent.
  237. @return Entity's combined roll.
  238. */
  239. Number getCombinedRoll() const;
  240. /**
  241. * Forces the rotation quaternion to be rebuilt.
  242. */
  243. void rebuildRotation();
  244. /**
  245. * Sets the pitch rotation of the entity.
  246. * @param pitch New pitch value in degrees.
  247. */
  248. void setPitch(Number pitch);
  249. /**
  250. * Sets the yaw rotation of the entity.
  251. * @param yaw New yaw value in degrees.
  252. */
  253. void setYaw(Number yaw);
  254. /**
  255. * Sets the roll rotation of the entity.
  256. * @param roll New roll value in degrees.
  257. */
  258. void setRoll(Number roll);
  259. /**
  260. * Rolls the entity relative to its current roll.
  261. * @param roll Roll value in degrees.
  262. */
  263. void Roll(Number roll);
  264. /**
  265. * Yaws the entity relative to its current yaw.
  266. * @param yaw Yaw value in degrees.
  267. */
  268. void Yaw(Number yaw);
  269. /**
  270. * Pitches the entity relative to its current pitch.
  271. * @param pitch Pitch value in degrees.
  272. */
  273. void Pitch(Number pitch);
  274. /**
  275. * Returns the current pitch of the entity.
  276. * @return Current pitch value.
  277. */
  278. Number getPitch() const;
  279. /**
  280. * Returns the current yaw of the entity.
  281. * @return Current yaw value.
  282. */
  283. Number getYaw() const;
  284. /**
  285. * Returns the current roll of the entity.
  286. * @return Current roll value.
  287. */
  288. Number getRoll() const;
  289. /**
  290. * Sets the rotation with quaternion value.
  291. * @param Current yaw value.
  292. */
  293. void setRotationQuat(Number w, Number x, Number y, Number z);
  294. /**
  295. * Returns the current rotation as a quaternion.
  296. * @return Current rotation value.
  297. */
  298. Quaternion getRotationQuat() const;
  299. /**
  300. * Orients the entity towards the specified location with the provided up vector. The up vector determines which side of the entity will be pointing in that direction.
  301. * @param loc Location to look at.
  302. * @param upVector The up vector.
  303. */
  304. void lookAt(const Vector3 &loc, const Vector3 &upVector = Vector3(0,1,0));
  305. /**
  306. * Orients the entity towards another entity with the provided up vector. The up vector determines which side of the entity will be pointing in that direction.
  307. * @param loc Location to look at.
  308. * @param upVector The up vector.
  309. * @see lookAt()
  310. */
  311. void lookAtEntity(Entity *entity, const Vector3 &upVector = Vector3(0,1,0));
  312. /**
  313. * Returns the entity's color multiplied by its parent entity's combined color.
  314. * @return Entity's combined color.
  315. */
  316. Color getCombinedColor() const;
  317. /**
  318. * Sets the color of the entity as normalized floating point values.
  319. * @param r Red value as a 0-1 floating point number.
  320. * @param g Green value as a 0-1 floating point number.
  321. * @param b Blue value as a 0-1 floating point number.
  322. * @param a Alpha value as a 0-1 floating point number.
  323. */
  324. void setColor(Number r, Number g, Number b, Number a);
  325. /**
  326. * Sets the color of the entity as 0-255 integers.
  327. * @param r Red value as a 0-255 integer.
  328. * @param g Green value as a 0-255 integer.
  329. * @param b Blue value as a 0-255 integer.
  330. * @param a Alpha value as a 0-255 integer.
  331. */
  332. void setColorInt(int r, int g, int b, int a);
  333. /**
  334. * Sets the color of the entity as 0-255 integers.
  335. * @param r Red value as a 0-255 integer.
  336. * @param g Green value as a 0-255 integer.
  337. * @param b Blue value as a 0-255 integer.
  338. * @param a Alpha value as a 0-255 integer.
  339. */
  340. void setColor(Color color);
  341. //@}
  342. // ----------------------------------------------------------------------------------------------------------------
  343. /** @name Bounding box operations.
  344. * These methods modify the bounding box of the entity. The bounding box is used for culling and collision detection.
  345. */
  346. //@{
  347. /**
  348. * Recalculates the bounding box of the entity based on its size.
  349. */
  350. void recalculateBBox();
  351. /**
  352. * Returns the bounding box radius.
  353. * @return The bounding box radius.
  354. */
  355. Number getBBoxRadius() const;
  356. /**
  357. * Returns the entity's bounding box radius compounded from its children's bounding box radii.
  358. * @return The compound bounding box radius.
  359. */
  360. Number getCompoundBBoxRadius() const;
  361. /**
  362. * Sets the bounding box radius.
  363. * @param rad New bounding box radius.
  364. */
  365. void setBBoxRadius(Number rad);
  366. //@}
  367. // ----------------------------------------------------------------------------------------------------------------
  368. /** @name Rendering properties
  369. * Methods and properties affecting the way the entity is rendered.
  370. */
  371. //@{
  372. /**
  373. * Sets another entity as a mask for this entity (This is not really working properly right now).
  374. */
  375. void setMask(Entity *mask);
  376. /**
  377. * Removes the entity's mask.
  378. */
  379. void clearMask();
  380. /**
  381. * You can set a custom string identifier for user purposes.
  382. */
  383. String custEntityType;
  384. /**
  385. * If this flag is true, the entity will always face the camera. False by default.
  386. */
  387. bool billboardMode;
  388. /**
  389. * Normally, if billboardMode is on, no rotation is allowed at all. If this flag is also true, you can rotate the entity around the axis pointing to the camera.
  390. */
  391. bool billboardRoll;
  392. /**
  393. * Normally, translucent textures do not affect the depth buffer, but if this flag is set to true, this entity's alpha channel is written to the depth buffer at a preset threshold. This flag is set to false by default.
  394. */
  395. bool alphaTest;
  396. /**
  397. * If this flag is set to false, backface culling is disabled when rendering this entity, rendering both sides of each face. Set to true by default.
  398. */
  399. bool backfaceCulled;
  400. /**
  401. * If this flag is set to true, the entity will render in wireframe.
  402. */
  403. bool renderWireframe;
  404. /**
  405. * The entity's color.
  406. */
  407. Color color;
  408. /**
  409. * If this flag is set to false, this entity will not be rendered or updated.
  410. */
  411. bool enabled;
  412. /**
  413. * If this flag is set to false, this entity will not be rendered.
  414. */
  415. bool visible;
  416. /**
  417. * If this flag is set to false, this entity will not write to the depth buffer when it's rendered.
  418. */
  419. bool depthWrite;
  420. /**
  421. * If this flag is set to false, this entity will not check the depth buffer when it's rendering.
  422. */
  423. bool depthTest;
  424. /**
  425. * Blending mode for rendering this entity. Possible blending modes are:
  426. * Renderer::BLEND_MODE_NORMAL - Draw entity normally
  427. * Renderer::BLEND_MODE_LIGHTEN - Add the entity's color on top of the background color.
  428. * Renderer::BLEND_MODE_COLOR - Only change the color of the background.
  429. */
  430. int blendingMode;
  431. /**
  432. * If set to false, the children of this entity will not multiply by this entity's color. Set to true by default.
  433. */
  434. bool colorAffectsChildren;
  435. /**
  436. * If set to false, the children will be rendered even if the entity is invisible.
  437. */
  438. bool visibilityAffectsChildren;
  439. /**
  440. * If this flag is set to true, this entity will render only into the depth buffer. This, effectively, means that it will be invisible, but still obscuring other entities.
  441. */
  442. bool depthOnly;
  443. /**
  444. * If this flag is set to true, this entity's transformations will not take into account its parent, making its transforms always relative to 0.
  445. */
  446. //@}
  447. // ----------------------------------------------------------------------------------------------------------------
  448. void setBlendingMode(int newBlendingMode);
  449. Vector3 getChildCenter() const;
  450. std::vector <EntityProp> entityProps;
  451. String getEntityProp(const String& propName);
  452. void doUpdates();
  453. virtual Matrix4 buildPositionMatrix();
  454. virtual void adjustMatrixForChildren(){}
  455. void setRenderer(Renderer *renderer);
  456. Vector3 bBox;
  457. bool ignoreParentMatrix;
  458. bool isMask;
  459. protected:
  460. std::vector<Entity*> children;
  461. Vector3 childCenter;
  462. Number bBoxRadius;
  463. Vector3 position;
  464. Vector3 scale;
  465. bool hasMask;
  466. bool lockMatrix;
  467. bool matrixDirty;
  468. Matrix4 transformMatrix;
  469. Number matrixAdj;
  470. Number pitch;
  471. Number yaw;
  472. Number roll;
  473. Entity *parentEntity;
  474. Quaternion qYaw;
  475. Quaternion qPitch;
  476. Quaternion qRoll;
  477. Quaternion rotationQuat;
  478. Entity *maskEntity;
  479. Renderer *renderer;
  480. };
  481. }