RenderComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #pragma once
  2. //========================================================================
  3. // RenderComponent.cpp : classes that define renderable components of actors like Meshes, Skyboxes, Lights, etc.
  4. // Author: David "Rez" Graham
  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 "../Unix/GameCodeStd.h"
  41. #include "../Graphics3D/Mesh.h"
  42. #include "../Graphics3D/Sky.h"
  43. #include "../EventManager/Events.h"
  44. #include "RenderComponent.h"
  45. #include "TransformComponent.h"
  46. // const char* MeshRenderComponent::g_Name = "MeshRenderComponent";
  47. // const char* SphereRenderComponent::g_Name = "SphereRenderComponent";
  48. const char* TeapotRenderComponent::g_Name = "TeapotRenderComponent";
  49. const char* GridRenderComponent::g_Name = "GridRenderComponent";
  50. const char* LightRenderComponent::g_Name = "LightRenderComponent";
  51. const char* SkyRenderComponent::g_Name = "SkyRenderComponent";
  52. //---------------------------------------------------------------------------------------------------------------------
  53. // RenderComponent
  54. //---------------------------------------------------------------------------------------------------------------------
  55. bool BaseRenderComponent::VInit(TiXmlElement* pData)
  56. {
  57. // color
  58. TiXmlElement* pColorNode = pData->FirstChildElement("Color");
  59. if (pColorNode)
  60. m_color = LoadColor(pColorNode);
  61. return VDelegateInit(pData);
  62. }
  63. void BaseRenderComponent::VPostInit(void)
  64. {
  65. std::shared_ptr<SceneNode> pSceneNode(VGetSceneNode());
  66. std::shared_ptr<EvtData_New_Render_Component> pEvent(GCC_NEW EvtData_New_Render_Component(m_pOwner->GetId(), pSceneNode));
  67. IEventManager::Get()->VTriggerEvent(pEvent);
  68. }
  69. void BaseRenderComponent::VOnChanged(void)
  70. {
  71. std::shared_ptr<EvtData_Modified_Render_Component> pEvent(GCC_NEW EvtData_Modified_Render_Component(m_pOwner->GetId()));
  72. IEventManager::Get()->VTriggerEvent(pEvent);
  73. }
  74. TiXmlElement* BaseRenderComponent::VGenerateXml(void)
  75. {
  76. TiXmlElement* pBaseElement = VCreateBaseElement();
  77. // color
  78. TiXmlElement* pColor = GCC_NEW TiXmlElement("Color");
  79. pColor->SetAttribute("r", ToStr(m_color.r).c_str());
  80. pColor->SetAttribute("g", ToStr(m_color.g).c_str());
  81. pColor->SetAttribute("b", ToStr(m_color.b).c_str());
  82. pColor->SetAttribute("a", ToStr(m_color.a).c_str());
  83. pBaseElement->LinkEndChild(pColor);
  84. // create XML for inherited classes
  85. VCreateInheritedXmlElements(pBaseElement);
  86. return pBaseElement;
  87. }
  88. std::shared_ptr<SceneNode> BaseRenderComponent::VGetSceneNode(void)
  89. {
  90. if (!m_pSceneNode)
  91. m_pSceneNode = VCreateSceneNode();
  92. return m_pSceneNode;
  93. }
  94. Color BaseRenderComponent::LoadColor(TiXmlElement* pData)
  95. {
  96. Color color;
  97. double r = 1.0;
  98. double g = 1.0;
  99. double b = 1.0;
  100. double a = 1.0;
  101. pData->Attribute("r", &r);
  102. pData->Attribute("g", &g);
  103. pData->Attribute("b", &b);
  104. pData->Attribute("a", &a);
  105. color.r = (float)r;
  106. color.g = (float)g;
  107. color.b = (float)b;
  108. color.a = (float)a;
  109. return color;
  110. }
  111. //---------------------------------------------------------------------------------------------------------------------
  112. // TeapotRenderComponent
  113. //---------------------------------------------------------------------------------------------------------------------
  114. std::shared_ptr<SceneNode> TeapotRenderComponent::VCreateSceneNode(void)
  115. {
  116. // get the transform component
  117. std::shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name));
  118. if (pTransformComponent)
  119. {
  120. WeakBaseRenderComponentPtr weakThis(this);
  121. glm::mat4 rot90 = geometry::BuildRotationY(GCC_PI/2.0f);
  122. std::shared_ptr<SceneNode> parent(GCC_NEW SceneNode(m_pOwner->GetId(), weakThis, RenderPass_Actor, pTransformComponent->GetTransform()));
  123. std::shared_ptr<SceneNode> teapot(GCC_NEW OpenGLTeapotMeshNode(INVALID_ACTOR_ID, weakThis, RenderPass_Actor, rot90));
  124. parent->VAddChild(teapot);
  125. return parent;
  126. }
  127. return std::shared_ptr<SceneNode>();
  128. }
  129. void TeapotRenderComponent::VCreateInheritedXmlElements(TiXmlElement *)
  130. {
  131. }
  132. //---------------------------------------------------------------------------------------------------------------------
  133. // GridRenderComponent
  134. //---------------------------------------------------------------------------------------------------------------------
  135. GridRenderComponent::GridRenderComponent(void)
  136. {
  137. m_textureResource = "";
  138. m_squares = 0;
  139. }
  140. bool GridRenderComponent::VDelegateInit(TiXmlElement* pData)
  141. {
  142. TiXmlElement* pTexture = pData->FirstChildElement("Texture");
  143. if (pTexture)
  144. {
  145. m_textureResource = pTexture->FirstChild()->Value();
  146. }
  147. TiXmlElement* pDivision = pData->FirstChildElement("Division");
  148. if (pDivision)
  149. {
  150. m_squares = atoi(pDivision->FirstChild()->Value());
  151. }
  152. return true;
  153. }
  154. std::shared_ptr<SceneNode> GridRenderComponent::VCreateSceneNode(void)
  155. {
  156. std::shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name));
  157. if (pTransformComponent)
  158. {
  159. WeakBaseRenderComponentPtr weakThis(this);
  160. return std::shared_ptr<SceneNode>(GCC_NEW OpenGLGrid(m_pOwner->GetId(), weakThis, pTransformComponent->GetTransform()));
  161. }
  162. return std::shared_ptr<SceneNode>();
  163. }
  164. void GridRenderComponent::VCreateInheritedXmlElements(TiXmlElement *pBaseElement)
  165. {
  166. TiXmlElement* pTextureNode = GCC_NEW TiXmlElement("Texture");
  167. TiXmlText* pTextureText = GCC_NEW TiXmlText(m_textureResource.c_str());
  168. pTextureNode->LinkEndChild(pTextureText);
  169. pBaseElement->LinkEndChild(pTextureNode);
  170. TiXmlElement* pDivisionNode = GCC_NEW TiXmlElement("Division");
  171. TiXmlText* pDivisionText = GCC_NEW TiXmlText(ToStr(m_squares).c_str());
  172. pDivisionNode->LinkEndChild(pDivisionText);
  173. pBaseElement->LinkEndChild(pDivisionNode);
  174. }
  175. //---------------------------------------------------------------------------------------------------------------------
  176. // LightRenderComponent
  177. //---------------------------------------------------------------------------------------------------------------------
  178. LightRenderComponent::LightRenderComponent(void)
  179. {
  180. }
  181. bool LightRenderComponent::VDelegateInit(TiXmlElement* pData)
  182. {
  183. TiXmlElement* pLight = pData->FirstChildElement("Light");
  184. double temp;
  185. TiXmlElement* pAttenuationNode = NULL;
  186. pAttenuationNode = pLight->FirstChildElement("Attenuation");
  187. if (pAttenuationNode)
  188. {
  189. double temp;
  190. pAttenuationNode->Attribute("const", &temp);
  191. m_Props.m_Attenuation[0] = (float) temp;
  192. pAttenuationNode->Attribute("linear", &temp);
  193. m_Props.m_Attenuation[1] = (float) temp;
  194. pAttenuationNode->Attribute("exp", &temp);
  195. m_Props.m_Attenuation[2] = (float) temp;
  196. }
  197. TiXmlElement* pShapeNode = NULL;
  198. pShapeNode = pLight->FirstChildElement("Shape");
  199. if (pShapeNode)
  200. {
  201. pShapeNode->Attribute("range", &temp);
  202. m_Props.m_Range = (float) temp;
  203. pShapeNode->Attribute("falloff", &temp);
  204. m_Props.m_Falloff = (float) temp;
  205. pShapeNode->Attribute("theta", &temp);
  206. m_Props.m_Theta = (float) temp;
  207. pShapeNode->Attribute("phi", &temp);
  208. m_Props.m_Phi = (float) temp;
  209. }
  210. return true;
  211. }
  212. std::shared_ptr<SceneNode> LightRenderComponent::VCreateSceneNode(void)
  213. {
  214. std::shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name));
  215. if (pTransformComponent)
  216. {
  217. WeakBaseRenderComponentPtr weakThis(this);
  218. return std::shared_ptr<SceneNode>(GCC_NEW OpenGLLightNode46(m_pOwner->GetId(), weakThis, m_Props, pTransformComponent->GetTransform()));
  219. }
  220. return std::shared_ptr<SceneNode>();
  221. }
  222. void LightRenderComponent::VCreateInheritedXmlElements(TiXmlElement *pBaseElement)
  223. {
  224. TiXmlElement* pSceneNode = GCC_NEW TiXmlElement("Light");
  225. // attenuation
  226. TiXmlElement* pAttenuation = GCC_NEW TiXmlElement("Attenuation");
  227. pAttenuation->SetAttribute("const", ToStr(m_Props.m_Attenuation[0]).c_str());
  228. pAttenuation->SetAttribute("linear", ToStr(m_Props.m_Attenuation[1]).c_str());
  229. pAttenuation->SetAttribute("exp", ToStr(m_Props.m_Attenuation[2]).c_str());
  230. pSceneNode->LinkEndChild(pAttenuation);
  231. // shape
  232. TiXmlElement* pShape = GCC_NEW TiXmlElement("Shape");
  233. pShape->SetAttribute("range", ToStr(m_Props.m_Range).c_str());
  234. pShape->SetAttribute("falloff", ToStr(m_Props.m_Falloff).c_str());
  235. pShape->SetAttribute("theta", ToStr(m_Props.m_Theta).c_str());
  236. pShape->SetAttribute("phi", ToStr(m_Props.m_Phi).c_str());
  237. pSceneNode->LinkEndChild(pShape);
  238. pBaseElement->LinkEndChild(pSceneNode);
  239. }
  240. SkyRenderComponent::SkyRenderComponent(void)
  241. {
  242. }
  243. bool SkyRenderComponent::VDelegateInit(TiXmlElement* pData)
  244. {
  245. TiXmlElement* pTexture = pData->FirstChildElement("Texture");
  246. if (pTexture)
  247. {
  248. m_textureResource = pTexture->FirstChild()->Value();
  249. }
  250. return true;
  251. }
  252. std::shared_ptr<SceneNode> SkyRenderComponent::VCreateSceneNode(void)
  253. {
  254. std::shared_ptr<SkyNode> sky;
  255. if (GameCodeApp::GetRendererImpl()==GameCodeApp::Renderer_OpenGL46)
  256. {
  257. sky = std::shared_ptr<SkyNode>(GCC_NEW OpenGLSkyNode46(m_textureResource.c_str() ));
  258. }
  259. else
  260. {
  261. GCC_ERROR("Unknown Renderer Implementation in VLoadGameDelegate");
  262. }
  263. return sky;
  264. }
  265. void SkyRenderComponent::VCreateInheritedXmlElements(TiXmlElement *pBaseElement)
  266. {
  267. TiXmlElement* pTextureNode = GCC_NEW TiXmlElement("Texture");
  268. TiXmlText* pTextureText = GCC_NEW TiXmlText(m_textureResource.c_str());
  269. pTextureNode->LinkEndChild(pTextureText);
  270. pBaseElement->LinkEndChild(pTextureNode);
  271. }