SceneNodes.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #pragma once
  2. //========================================================================
  3. // SceneNodes.h - defines the base class for the 3D graphics scene graph, and
  4. // derived classes like RootNode, Alpha scene nodes, Camera, Grids, etc.
  5. //
  6. // Part of the GameCode4 Application
  7. //
  8. // GameCode4 is the sample application that encapsulates much of the source code
  9. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  10. // "Rez" Graham, published by Charles River Media.
  11. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  12. //
  13. // If this source code has found it's way to you, and you think it has helped you
  14. // in any way, do the authors a favor and buy a new copy of the book - there are
  15. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  16. // by clicking here:
  17. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  18. //
  19. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  20. //
  21. // The source code is managed and maintained through Google Code:
  22. // http://code.google.com/p/gamecode4/
  23. //
  24. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  25. //
  26. // This program is free software; you can redistribute it and/or
  27. // modify it under the terms of the GNU Lesser GPL v3
  28. // as published by the Free Software Foundation.
  29. //
  30. // This program is distributed in the hope that it will be useful,
  31. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  33. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  34. //
  35. // You should have received a copy of the GNU Lesser GPL v3
  36. // along with this program; if not, write to the Free Software
  37. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  38. //
  39. //========================================================================
  40. #include "../GameCode4/interfaces.h"
  41. #include "Geometry.h"
  42. #include "Material.h"
  43. #include "Shaders.h"
  44. #include <optional>
  45. #include <vector>
  46. #include <glm/glm.hpp>
  47. // Forward declarations
  48. class SceneNode;
  49. class Scene;
  50. // class RayCast;
  51. // class MovementController;
  52. // class IResourceExtraData;
  53. // class ActorComponent;
  54. class BaseRenderComponent;
  55. // FUTURE WORK - Smart pointers don't work right....going to use a naked pointer for now!
  56. typedef BaseRenderComponent* WeakBaseRenderComponentPtr;
  57. ////////////////////////////////////////////////////
  58. //
  59. // AlphaType - Chapter X, page Y
  60. //
  61. // This enum defines the different types of alpha blending
  62. // types that can be set on a scene node.
  63. //
  64. //
  65. ////////////////////////////////////////////////////
  66. enum AlphaType
  67. {
  68. AlphaOpaque,
  69. AlphaTexture,
  70. AlphaMaterial,
  71. AlphaVertex
  72. };
  73. //////////////////////////////////////////////////////////////////////
  74. // class SceneNodeProperties - Chapter 16, page 527
  75. //
  76. // This class is contained in the SceneNode class, and gains
  77. // easy access to common scene node properties such as its ActorId
  78. // or render pass, with a single ISceneNode::VGet() method.
  79. //
  80. //////////////////////////////////////////////////////////////////////
  81. class SceneNodeProperties
  82. {
  83. friend class SceneNode;
  84. protected:
  85. ActorId m_ActorId;
  86. std::string m_Name;
  87. glm::mat4 m_ToWorld, m_FromWorld;
  88. float m_Radius;
  89. RenderPass m_RenderPass;
  90. Material m_Material;
  91. AlphaType m_AlphaType;
  92. void SetAlpha(const float alpha)
  93. { m_AlphaType=AlphaMaterial; m_Material.SetAlpha(alpha); }
  94. public:
  95. SceneNodeProperties(void);
  96. const ActorId &GetActorId() const { return m_ActorId; }
  97. glm::mat4 const &ToWorld() const { return m_ToWorld; }
  98. glm::mat4 const &FromWorld() const { return m_FromWorld; }
  99. void Transform(glm::mat4 *toWorld, glm::mat4 *fromWorld) const;
  100. const char * Name() const { return m_Name.c_str(); }
  101. bool HasAlpha() const { return m_Material.HasAlpha(); }
  102. float Alpha() const { return m_Material.GetAlpha(); }
  103. AlphaType GetAlphaType() const { return m_AlphaType; }
  104. RenderPass GetRenderPass() const { return m_RenderPass; }
  105. float Radius() const { return m_Radius; }
  106. Material GetMaterial() const { return m_Material; }
  107. };
  108. //////////////////////////////////////////////////////////////
  109. //
  110. // SceneNodeList - Chapter 16, page 529
  111. //
  112. // Every scene node has a list of its children - this
  113. // is implemented as a vector since it is expected that
  114. // we won't add/delete nodes very often, and we'll want
  115. // fast random access to each child.
  116. //
  117. //////////////////////////////////////////////////////////////
  118. typedef std::vector<std::shared_ptr<ISceneNode> > SceneNodeList;
  119. //////////////////////////////////////////////////////////////
  120. //
  121. // SceneNode - Chapter 16, page 529
  122. //
  123. // Implements ISceneNode. Forms the base class for any node
  124. // that can exist in the 3D scene graph managed by class Scene.
  125. //
  126. //////////////////////////////////////////////////////////////
  127. class SceneNode : public ISceneNode
  128. {
  129. friend class Scene;
  130. protected:
  131. SceneNodeList m_Children;
  132. SceneNode *m_pParent;
  133. SceneNodeProperties m_Props;
  134. WeakBaseRenderComponentPtr m_RenderComponent;
  135. public:
  136. SceneNode(
  137. ActorId actorId,
  138. WeakBaseRenderComponentPtr renderComponent,
  139. RenderPass renderPass,
  140. const glm::mat4 to,
  141. std::optional<const glm::mat4> from=std::nullopt
  142. );
  143. virtual ~SceneNode();
  144. virtual const SceneNodeProperties* const VGet() const { return &m_Props; }
  145. virtual void VSetTransform(
  146. const glm::mat4 toWorld,
  147. std::optional<const glm::mat4> fromWorld=std::nullopt
  148. );
  149. virtual int VOnRestore(Scene *pScene);
  150. virtual int VOnUpdate(Scene *, uint64_t const elapsedMs);
  151. virtual int VPreRender(Scene *pScene);
  152. virtual bool VIsVisible(Scene *pScene) const;
  153. virtual int VRender(Scene *pScene) { return 0; }
  154. virtual int VRenderChildren(Scene *pScene);
  155. virtual int VPostRender(Scene *pScene);
  156. virtual bool VAddChild(std::shared_ptr<ISceneNode> kid);
  157. virtual bool VRemoveChild(ActorId id);
  158. glm::vec3 GetDirection() const { return geometry::GetDirection(m_Props.m_ToWorld); }
  159. void SetRadius(const float radius) { m_Props.m_Radius = radius; }
  160. void SetMaterial(const Material &mat) { m_Props.m_Material = mat; }
  161. };
  162. ////////////////////////////////////////////////////
  163. //
  164. // class RootNode - Chapter 16, page 545
  165. //
  166. // This is the root node of the scene graph.
  167. //
  168. ////////////////////////////////////////////////////
  169. class RootNode : public SceneNode
  170. {
  171. public:
  172. RootNode();
  173. virtual bool VAddChild(std::shared_ptr<ISceneNode> kid);
  174. virtual int VRenderChildren(Scene *pScene);
  175. virtual bool VRemoveChild(ActorId id);
  176. virtual bool VIsVisible(Scene *pScene) const { return true; }
  177. };
  178. ////////////////////////////////////////////////////
  179. //
  180. // class CameraNode - Chapter 16, page 548
  181. //
  182. // A camera node controls the D3D view transform and holds the view
  183. // frustum definition
  184. //
  185. ////////////////////////////////////////////////////
  186. class CameraNode : public SceneNode
  187. {
  188. public:
  189. CameraNode(glm::mat4 const t, Frustum const &frustum)
  190. : SceneNode(INVALID_ACTOR_ID, WeakBaseRenderComponentPtr(), RenderPass_0, t),
  191. m_Frustum(frustum),
  192. m_bActive(true),
  193. m_DebugCamera(false),
  194. m_pTarget(std::shared_ptr<SceneNode>()),
  195. m_CamOffsetVector(0.0f, 1.0f, 10.0f, 0.0f)
  196. {
  197. }
  198. virtual int VRender(Scene *pScene);
  199. virtual int VOnRestore(Scene *pScene);
  200. virtual bool VIsVisible(Scene *pScene) { return m_bActive; }
  201. void SetTarget(std::shared_ptr<SceneNode> pTarget)
  202. { m_pTarget = pTarget; }
  203. void ClearTarget() { m_pTarget = std::shared_ptr<SceneNode>(); }
  204. std::shared_ptr<SceneNode> GetTarget() { return m_pTarget; }
  205. glm::mat4 GetWorldViewProjection(Scene *pScene);
  206. int SetViewTransform(Scene *pScene);
  207. protected:
  208. Frustum m_Frustum;
  209. glm::mat4 m_Projection;
  210. glm::mat4 m_View;
  211. bool m_bActive;
  212. bool m_DebugCamera;
  213. std::shared_ptr<SceneNode> m_pTarget;
  214. glm::vec4 m_CamOffsetVector; //Direction of camera relative to target.
  215. };
  216. ////////////////////////////////////////////////////
  217. //
  218. // D3DGrid9 Description
  219. //
  220. // A slightly modified version of Grid from
  221. // the beginning 3D chapter
  222. //
  223. ////////////////////////////////////////////////////
  224. class OpenGLGrid : public SceneNode
  225. {
  226. protected:
  227. size_t m_numIndices;
  228. unsigned int m_numVerts;
  229. unsigned int m_numPolys;
  230. GLuint m_vbo;
  231. GLuint m_vao;
  232. GLuint m_ebo;
  233. OpenGLShaderProgram m_ShaderProgram;
  234. public:
  235. bool m_bTextureHasAlpha;
  236. OpenGLGrid(ActorId actorId, WeakBaseRenderComponentPtr renderComponent, glm::mat4 pMatrix);
  237. virtual ~OpenGLGrid();
  238. virtual int VOnRestore(Scene *pScene);
  239. virtual int VRender(Scene *pScene);
  240. virtual int VOnUpdate(Scene *pScene, unsigned long elapsedMs) { return 0; }
  241. // virtual int VPick(Scene *pScene, RayCast *pRayCast) { return 1; }
  242. bool VHasAlpha() const { return m_bTextureHasAlpha; }
  243. };