Lights.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. //========================================================================
  3. // Lights.h - implements a simple light class for the GameCode4 scene graph
  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 "OpenGLRenderer.h"
  40. #include "Geometry.h"
  41. #include "Scene.h"
  42. #include "SceneNodes.h"
  43. // Note: Light color is stored in the Material structure, which is already present in all SceneNodes.
  44. //
  45. // struct LightProperties - Chapter 16, page 551
  46. //
  47. struct LightProperties
  48. {
  49. float m_Attenuation[3]; /* Attenuation coefficients */
  50. float m_Range;
  51. float m_Falloff;
  52. float m_Theta;
  53. float m_Phi;
  54. };
  55. //
  56. // class LightNode - Chapter 16, page 551
  57. //
  58. // Note: In the book this class implements the LightNode in D3D11, but here it is a base
  59. // class. The derived classes make it possible to run the engine in D3D9 or D3D11.
  60. //
  61. class LightNode : public SceneNode
  62. {
  63. protected:
  64. LightProperties m_LightProps;
  65. public:
  66. LightNode(const ActorId actorId, WeakBaseRenderComponentPtr renderComponent, const LightProperties &props, glm::mat4 t);
  67. };
  68. // class D3DLightNode9 : public LightNode
  69. // {
  70. // public:
  71. // D3DLightNode9(const ActorId actorId, WeakBaseRenderComponentPtr renderComponent, const LightProperties &lightProps, const Mat4x4 *t)
  72. // : LightNode(actorId, renderComponent, lightProps, t) { }
  73. // D3DLIGHT9 m_d3dLight9;
  74. // virtual HRESULT VOnRestore(Scene *pScene);
  75. // virtual HRESULT VOnUpdate(Scene *, DWORD const elapsedMs);
  76. // };
  77. // class D3DLightNode11 : public LightNode
  78. // {
  79. // public:
  80. // D3DLightNode11(const ActorId actorId, WeakBaseRenderComponentPtr renderComponent, const LightProperties &lightProps, const Mat4x4 *t)
  81. // : LightNode(actorId, renderComponent, lightProps, t) { }
  82. // virtual HRESULT VOnRestore() { return S_OK; };
  83. // virtual HRESULT VOnUpdate(Scene *, DWORD const elapsedMs);
  84. // };
  85. class OpenGLLightNode46 : public LightNode
  86. {
  87. public:
  88. OpenGLLightNode46(const ActorId actorId, WeakBaseRenderComponentPtr renderComponent, const LightProperties &lightProps, const glm::mat4 t)
  89. : LightNode(actorId, renderComponent, lightProps, t) { }
  90. virtual int VOnRestore(Scene *pScene) { return 0; }
  91. virtual int VOnUpdate(Scene *, uint64_t const elapsedMs);
  92. };
  93. struct ConstantBuffer_Lighting;
  94. //
  95. // class LightManager - Chapter 16, 553
  96. //
  97. class LightManager
  98. {
  99. friend class Scene;
  100. protected:
  101. Lights m_Lights;
  102. glm::vec4 m_vLightDir[MAXIMUM_LIGHTS_SUPPORTED];
  103. Color m_vLightDiffuse[MAXIMUM_LIGHTS_SUPPORTED];
  104. glm::vec4 m_vLightAmbient;
  105. public:
  106. void CalcLighting(Scene *pScene);
  107. void CalcLighting(ConstantBuffer_Lighting* pLighting, SceneNode* pNode);
  108. int GetLightCount(const SceneNode *node) { return m_Lights.size(); }
  109. const glm::vec4 *GetLightAmbient(const SceneNode *node) { return &m_vLightAmbient; }
  110. const glm::vec4 *GetLightDirection(const SceneNode *node) { return m_vLightDir; }
  111. const Color *GetLightDiffuse(const SceneNode *node) { return m_vLightDiffuse; }
  112. };