Scene.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. //========================================================================
  3. // File: Scene.h - implements the container class for 3D Graphics scenes
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include "Geometry.h"
  40. #include "SceneNodes.h"
  41. #include "../EventManager/Events.h"
  42. #include <map>
  43. #include <stack>
  44. // Forward declarations
  45. ////////////////////////////////////////////////////
  46. //
  47. // SceneActorMap Description - Chapter 16, page Y
  48. //
  49. // An STL map that allows fast lookup of a scene node given an ActorId.
  50. //
  51. ////////////////////////////////////////////////////
  52. typedef std::map<ActorId, std::shared_ptr<ISceneNode> > SceneActorMap;
  53. ////////////////////////////////////////////////////
  54. //
  55. // Scene Description - Chapter 16, page 536
  56. //
  57. // A heirarchical container of scene nodes, which
  58. // are classes that implemente the ISceneNode interface
  59. //
  60. ////////////////////////////////////////////////////
  61. class CameraNode;
  62. class SkyNode;
  63. class LightNode;
  64. class LightManager;
  65. class Scene
  66. {
  67. protected:
  68. std::shared_ptr<SceneNode> m_Root;
  69. std::shared_ptr<CameraNode> m_Camera;
  70. std::shared_ptr<IRenderer> m_Renderer;
  71. std::stack<glm::mat4> m_MatrixStack;
  72. SceneActorMap m_ActorMap;
  73. LightManager *m_LightManager;
  74. public:
  75. Scene(std::shared_ptr<IRenderer> renderer);
  76. virtual ~Scene();
  77. int OnRender();
  78. int OnRestore();
  79. int OnLostDevice();
  80. int OnUpdate(const int deltaMilliseconds);
  81. std::shared_ptr<ISceneNode> FindActor(ActorId id);
  82. bool AddChild(ActorId id, std::shared_ptr<ISceneNode> kid);
  83. bool RemoveChild(ActorId id);
  84. // event delegates
  85. void NewRenderComponentDelegate(IEventDataPtr pEventData);
  86. void ModifiedRenderComponentDelegate(IEventDataPtr pEventData); // added post-press!
  87. // void DestroyActorDelegate(IEventDataPtr pEventData);
  88. // void MoveActorDelegate(IEventDataPtr pEventData);
  89. void SetCamera(std::shared_ptr<CameraNode> camera) { m_Camera = camera; }
  90. const std::shared_ptr<CameraNode> GetCamera() const { return m_Camera; }
  91. void PushAndSetMatrix(const glm::mat4 &toWorld)
  92. {
  93. // Note this code carefully!!!!! It is COMPLETELY different
  94. // from some DirectX 9 documentation out there....
  95. // Scene::PushAndSetMatrix - Chapter 16, page 541
  96. m_MatrixStack.push(toWorld * m_MatrixStack.top());
  97. // [munnellg] This isn't necessary for OpenGL, but we'll preserve it from teh book
  98. // anyway as GCC4 does something similar with this call for DirectX 11 being a noop
  99. glm::mat4 mat = m_MatrixStack.top();
  100. m_Renderer->VSetWorldTransform(mat);
  101. }
  102. void PopMatrix()
  103. {
  104. m_MatrixStack.pop();
  105. // [munnellg] This isn't necessary for OpenGL, but we'll preserve it from teh book
  106. // anyway as GCC4 does something similar with this call for DirectX 11 being a noop
  107. glm::mat4 mat = m_MatrixStack.top();
  108. m_Renderer->VSetWorldTransform(mat);
  109. }
  110. const glm::mat4 GetTopMatrix()
  111. {
  112. // Scene::GetTopMatrix - Chapter 16, page 541
  113. return m_MatrixStack.top();
  114. }
  115. LightManager *GetLightManager() { return m_LightManager; }
  116. std::shared_ptr<IRenderer> GetRenderer() { return m_Renderer; }
  117. };