BaseGraphicsObjects.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include <atomic>
  3. #include "GraphicsDataSets.h"
  4. #include "Loaders.h"
  5. #include "Math.h"
  6. #include "NullSystemObjects.h"
  7. #include "System.h"
  8. class ModelObject;
  9. class EnvironmentMapObject;
  10. class ShaderModelGraphicsObject;
  11. class BaseGraphicsObject : public SystemObject
  12. {
  13. public:
  14. BaseGraphicsObject(SystemScene *p_systemScene, const std::string &p_name, Properties::PropertyID p_objectType)
  15. : SystemObject(p_systemScene, p_name, p_objectType), m_needsUpdate(true), m_affectedByLighting(true), m_loadedToVideoMemory(false), m_loadedToMemory(false), m_isActive(false) { }
  16. virtual ~BaseGraphicsObject() { }
  17. BitMask getSystemType() { return Systems::Graphics; }
  18. virtual BitMask getDesiredSystemChanges() { return Systems::Changes::Spatial::All; }
  19. virtual BitMask getPotentialSystemChanges() { return Systems::Changes::None; }
  20. // Processes any spacial changes
  21. virtual void changeOccurred(ObservedSubject *p_subject, BitMask p_changeType)
  22. {
  23. if(p_changeType & Systems::Changes::Spatial::WorldPosition)
  24. {
  25. m_baseObjectData.m_position =
  26. p_subject->getVec3(this, Systems::Changes::Spatial::WorldPosition) + m_baseObjectData.m_offsetPosition;
  27. m_needsUpdate = true;
  28. }
  29. if(p_changeType & Systems::Changes::Spatial::WorldRotation)
  30. {
  31. m_baseObjectData.m_rotation =
  32. p_subject->getVec3(this, Systems::Changes::Spatial::WorldRotation) + m_baseObjectData.m_offsetRotation;
  33. m_needsUpdate = true;
  34. }
  35. if(p_changeType & Systems::Changes::Spatial::WorldScale)
  36. {
  37. m_baseObjectData.m_scale = p_subject->getVec3(this, Systems::Changes::Spatial::WorldScale);
  38. m_needsUpdate = true;
  39. }
  40. if(p_changeType & Systems::Changes::Spatial::WorldModelMatrix)
  41. {
  42. m_baseObjectData.m_modelMat = p_subject->getMat4(this, Systems::Changes::Spatial::WorldModelMatrix);
  43. m_needsUpdate = true;
  44. }
  45. if(p_changeType & Systems::Changes::Graphics::Lighting)
  46. {
  47. m_affectedByLighting = p_subject->getBool(this, Systems::Changes::Graphics::Lighting);
  48. }
  49. }
  50. // Has the object been already loaded to memory (RAM)?
  51. const inline bool isLoadedToMemory() const { return m_loadedToMemory; }
  52. // Has the object been already loaded to video memory (GPU VRAM)
  53. const inline bool isLoadedToVideoMemory() const { return m_loadedToVideoMemory; }
  54. // Is the object active (i.e. should be drawned, updated, etc...)
  55. const inline bool isObjectActive() const { return m_isActive; }
  56. // Getters
  57. const virtual Math::Vec3f &getVec3(const Observer *p_observer, BitMask p_changedBits) const
  58. {
  59. switch(p_changedBits)
  60. {
  61. case Systems::Changes::Spatial::Position:
  62. return m_baseObjectData.m_position;
  63. break;
  64. case Systems::Changes::Spatial::Rotation:
  65. return m_baseObjectData.m_rotation;
  66. break;
  67. case Systems::Changes::Spatial::Scale:
  68. return m_baseObjectData.m_scale;
  69. break;
  70. }
  71. return ObservedSubject::getVec3(p_observer, p_changedBits);
  72. }
  73. const virtual bool getBool(const Observer *p_observer, BitMask p_changedBits) const
  74. {
  75. switch(p_changedBits)
  76. {
  77. case Systems::Changes::Graphics::Lighting:
  78. return m_affectedByLighting;
  79. break;
  80. }
  81. return ObservedSubject::getBool(p_observer, p_changedBits);
  82. }
  83. const inline GraphicsData &getBaseObjectData() const { return m_baseObjectData; }
  84. // Setters for spacial data
  85. inline void setScale(const Math::Vec3f &p_scale) { m_baseObjectData.m_scale = p_scale; }
  86. inline void setPosition(const Math::Vec3f &p_position) { m_baseObjectData.m_position = p_position; }
  87. inline void setRotation(const Math::Vec3f &p_rotation) { m_baseObjectData.m_rotation = p_rotation; }
  88. inline void setOffsetPosition(const Math::Vec3f &p_position) { m_baseObjectData.m_offsetPosition = p_position; }
  89. inline void setOffsetRotation(const Math::Vec3f &p_rotation) { m_baseObjectData.m_offsetRotation = p_rotation; }
  90. // Setters for misc data
  91. inline void setAffectedByLighting(const bool p_flag) { m_affectedByLighting = p_flag; }
  92. inline void setAlphaThreshold(const float p_value) { m_baseObjectData.m_alphaThreshold = p_value; }
  93. inline void setEmissiveThreshold(const float p_value) { m_baseObjectData.m_emissiveThreshold = p_value; }
  94. inline void setHeightScale(const float p_value) { m_baseObjectData.m_heightScale = p_value; }
  95. inline void setLoadedToMemory(const bool p_loadedToMemory) { m_loadedToMemory = p_loadedToMemory; }
  96. inline void setLoadedToVideoMemory(const bool p_loaded) { m_loadedToVideoMemory = p_loaded; }
  97. inline void setObjectActive(const bool p_objectIsActive) { m_isActive = p_objectIsActive; }
  98. inline void setTextureTilingFactor(const float p_value) { m_baseObjectData.m_textureTilingFactor = p_value; }
  99. protected:
  100. // A flag telling if this object should be rendered during geometry pass or as a post-process (i.e. after lighting)
  101. bool m_affectedByLighting;
  102. // Does the object need to be updated after any of its data has been changed
  103. bool m_needsUpdate;
  104. // Is the object active (i.e. should be drawn, updated, etc...)
  105. bool m_isActive;
  106. // Is the object loaded to GPU
  107. bool m_loadedToVideoMemory;
  108. // Atomic, so it can be changed from different threads (loading to memory is multi-threaded)
  109. std::atomic<bool> m_loadedToMemory;
  110. // Spatial and misc data of an object
  111. GraphicsData m_baseObjectData;
  112. };
  113. // Used to hold objects that need to be loaded or are already being loaded, in a list
  114. // Holds any of the graphics object (in a union) so the data of an object can be access and be loaded
  115. class LoadableGraphicsObject
  116. {
  117. friend class RendererScene;
  118. public:
  119. LoadableGraphicsObject(ModelObject *p_modelObject, size_t p_index);
  120. LoadableGraphicsObject(EnvironmentMapObject *p_envMapStatic, size_t p_index);
  121. LoadableGraphicsObject(ShaderModelGraphicsObject *p_shaderModelObject, size_t p_index);
  122. // Load object data to memory (RAM)
  123. void LoadToMemory();
  124. // Has the object been already loaded to memory (RAM)?
  125. const inline bool isLoadedToMemory() const { return m_baseGraphicsObject->isLoadedToMemory(); }
  126. // Has the object been already loaded to video memory (GPU VRAM)?
  127. const inline bool isLoadedToVideoMemory() const { return m_baseGraphicsObject->isLoadedToVideoMemory(); }
  128. // Should the object be activated after loading
  129. const inline bool isActivatedAfterLoading() const { return m_activateAfterLoading; }
  130. // Is the object active (i.e. should be drawn, updated, etc...)
  131. const inline bool isObjectActive() const { return m_baseGraphicsObject->isObjectActive(); }
  132. // Getters
  133. const inline size_t getIndex() const { return m_index; }
  134. const inline size_t getObjectID() const { return m_objectID; }
  135. const inline std::string &getName() const { return m_name; }
  136. const inline LoadableObjectType getObjectType() const { return m_objectType; }
  137. // Setters
  138. inline void setActivateAfterLoading(const bool p_activateAfterLoading) { m_activateAfterLoading = p_activateAfterLoading; }
  139. inline void setLoadedToVideoMemory(const bool p_loaded) { m_baseGraphicsObject->setLoadedToVideoMemory(p_loaded); }
  140. inline void setObjectActive(const bool p_objectIsActive) { m_baseGraphicsObject->setObjectActive(p_objectIsActive); }
  141. // Comparator operators; uses object ID to determine if the object is the same
  142. bool operator==(const SystemObject &p_systemObject) const { return m_objectID == p_systemObject.getObjectID() ? true : false; }
  143. bool operator==(const SystemObject *p_systemObject) const { return m_objectID == p_systemObject->getObjectID() ? true : false; }
  144. private:
  145. union ObjectData
  146. {
  147. ModelObject *m_modelObject;
  148. EnvironmentMapObject *m_envMapStatic;
  149. ShaderModelGraphicsObject *m_shaderModelObject;
  150. };
  151. ObjectData m_objectData;
  152. LoadableObjectType m_objectType;
  153. // Holds the object's name so it doesn't have to be retrieved every time
  154. std::string m_name;
  155. // This should always be true, unless object was set to be removed before loading completed
  156. bool m_activateAfterLoading;
  157. // Unique index of the object in corresponding pool (used for fast access)
  158. size_t m_index;
  159. // Holds the base class that the objects are derived from, so it's faster to access the base data
  160. BaseGraphicsObject *m_baseGraphicsObject;
  161. // Pointer to the base class of an object, so some functionality can be easily accessed
  162. SystemObject *m_baseSystemObject;
  163. // A copy of system object ID
  164. size_t m_objectID;
  165. };