| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- #pragma once
- //========================================================================
- // RenderComponent.cpp : classes that define renderable components of actors like Meshes, Skyboxes, Lights, etc.
- // Author: David "Rez" Graham
- //
- // Part of the GameCode4 Application
- //
- // GameCode4 is the sample application that encapsulates much of the source code
- // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
- // "Rez" Graham, published by Charles River Media.
- // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
- //
- // If this source code has found it's way to you, and you think it has helped you
- // in any way, do the authors a favor and buy a new copy of the book - there are
- // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
- // by clicking here:
- // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
- //
- // There's a companion web site at http://www.mcshaffry.com/GameCode/
- //
- // The source code is managed and maintained through Google Code:
- // http://code.google.com/p/gamecode4/
- //
- // (c) Copyright 2012 Michael L. McShaffry and David Graham
- //
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Lesser GPL v3
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
- //
- // You should have received a copy of the GNU Lesser GPL v3
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- //
- //========================================================================
- #include "../Unix/GameCodeStd.h"
- #include "../Graphics3D/Mesh.h"
- #include "../Graphics3D/Sky.h"
- #include "../EventManager/Events.h"
- #include "RenderComponent.h"
- #include "TransformComponent.h"
- // const char* MeshRenderComponent::g_Name = "MeshRenderComponent";
- // const char* SphereRenderComponent::g_Name = "SphereRenderComponent";
- const char* TeapotRenderComponent::g_Name = "TeapotRenderComponent";
- const char* GridRenderComponent::g_Name = "GridRenderComponent";
- const char* LightRenderComponent::g_Name = "LightRenderComponent";
- const char* SkyRenderComponent::g_Name = "SkyRenderComponent";
- //---------------------------------------------------------------------------------------------------------------------
- // RenderComponent
- //---------------------------------------------------------------------------------------------------------------------
- bool BaseRenderComponent::VInit(TiXmlElement* pData)
- {
- // color
- TiXmlElement* pColorNode = pData->FirstChildElement("Color");
- if (pColorNode)
- m_color = LoadColor(pColorNode);
- return VDelegateInit(pData);
- }
- void BaseRenderComponent::VPostInit(void)
- {
- std::shared_ptr<SceneNode> pSceneNode(VGetSceneNode());
- std::shared_ptr<EvtData_New_Render_Component> pEvent(GCC_NEW EvtData_New_Render_Component(m_pOwner->GetId(), pSceneNode));
- IEventManager::Get()->VTriggerEvent(pEvent);
- }
- void BaseRenderComponent::VOnChanged(void)
- {
- std::shared_ptr<EvtData_Modified_Render_Component> pEvent(GCC_NEW EvtData_Modified_Render_Component(m_pOwner->GetId()));
- IEventManager::Get()->VTriggerEvent(pEvent);
- }
- TiXmlElement* BaseRenderComponent::VGenerateXml(void)
- {
- TiXmlElement* pBaseElement = VCreateBaseElement();
- // color
- TiXmlElement* pColor = GCC_NEW TiXmlElement("Color");
- pColor->SetAttribute("r", ToStr(m_color.r).c_str());
- pColor->SetAttribute("g", ToStr(m_color.g).c_str());
- pColor->SetAttribute("b", ToStr(m_color.b).c_str());
- pColor->SetAttribute("a", ToStr(m_color.a).c_str());
- pBaseElement->LinkEndChild(pColor);
- // create XML for inherited classes
- VCreateInheritedXmlElements(pBaseElement);
- return pBaseElement;
- }
- std::shared_ptr<SceneNode> BaseRenderComponent::VGetSceneNode(void)
- {
- if (!m_pSceneNode)
- m_pSceneNode = VCreateSceneNode();
- return m_pSceneNode;
- }
- Color BaseRenderComponent::LoadColor(TiXmlElement* pData)
- {
- Color color;
- double r = 1.0;
- double g = 1.0;
- double b = 1.0;
- double a = 1.0;
- pData->Attribute("r", &r);
- pData->Attribute("g", &g);
- pData->Attribute("b", &b);
- pData->Attribute("a", &a);
- color.r = (float)r;
- color.g = (float)g;
- color.b = (float)b;
- color.a = (float)a;
- return color;
- }
- //---------------------------------------------------------------------------------------------------------------------
- // TeapotRenderComponent
- //---------------------------------------------------------------------------------------------------------------------
- std::shared_ptr<SceneNode> TeapotRenderComponent::VCreateSceneNode(void)
- {
- // get the transform component
- std::shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name));
- if (pTransformComponent)
- {
- WeakBaseRenderComponentPtr weakThis(this);
- glm::mat4 rot90 = geometry::BuildRotationY(GCC_PI/2.0f);
- std::shared_ptr<SceneNode> parent(GCC_NEW SceneNode(m_pOwner->GetId(), weakThis, RenderPass_Actor, pTransformComponent->GetTransform()));
- std::shared_ptr<SceneNode> teapot(GCC_NEW OpenGLTeapotMeshNode(INVALID_ACTOR_ID, weakThis, RenderPass_Actor, rot90));
- parent->VAddChild(teapot);
- return parent;
- }
- return std::shared_ptr<SceneNode>();
- }
- void TeapotRenderComponent::VCreateInheritedXmlElements(TiXmlElement *)
- {
- }
- //---------------------------------------------------------------------------------------------------------------------
- // GridRenderComponent
- //---------------------------------------------------------------------------------------------------------------------
- GridRenderComponent::GridRenderComponent(void)
- {
- m_textureResource = "";
- m_squares = 0;
- }
- bool GridRenderComponent::VDelegateInit(TiXmlElement* pData)
- {
- TiXmlElement* pTexture = pData->FirstChildElement("Texture");
- if (pTexture)
- {
- m_textureResource = pTexture->FirstChild()->Value();
- }
- TiXmlElement* pDivision = pData->FirstChildElement("Division");
- if (pDivision)
- {
- m_squares = atoi(pDivision->FirstChild()->Value());
- }
- return true;
- }
- std::shared_ptr<SceneNode> GridRenderComponent::VCreateSceneNode(void)
- {
- std::shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name));
- if (pTransformComponent)
- {
- WeakBaseRenderComponentPtr weakThis(this);
- return std::shared_ptr<SceneNode>(GCC_NEW OpenGLGrid(m_pOwner->GetId(), weakThis, pTransformComponent->GetTransform()));
- }
- return std::shared_ptr<SceneNode>();
- }
- void GridRenderComponent::VCreateInheritedXmlElements(TiXmlElement *pBaseElement)
- {
- TiXmlElement* pTextureNode = GCC_NEW TiXmlElement("Texture");
- TiXmlText* pTextureText = GCC_NEW TiXmlText(m_textureResource.c_str());
- pTextureNode->LinkEndChild(pTextureText);
- pBaseElement->LinkEndChild(pTextureNode);
- TiXmlElement* pDivisionNode = GCC_NEW TiXmlElement("Division");
- TiXmlText* pDivisionText = GCC_NEW TiXmlText(ToStr(m_squares).c_str());
- pDivisionNode->LinkEndChild(pDivisionText);
- pBaseElement->LinkEndChild(pDivisionNode);
- }
- //---------------------------------------------------------------------------------------------------------------------
- // LightRenderComponent
- //---------------------------------------------------------------------------------------------------------------------
- LightRenderComponent::LightRenderComponent(void)
- {
- }
- bool LightRenderComponent::VDelegateInit(TiXmlElement* pData)
- {
- TiXmlElement* pLight = pData->FirstChildElement("Light");
- double temp;
- TiXmlElement* pAttenuationNode = NULL;
- pAttenuationNode = pLight->FirstChildElement("Attenuation");
- if (pAttenuationNode)
- {
- double temp;
- pAttenuationNode->Attribute("const", &temp);
- m_Props.m_Attenuation[0] = (float) temp;
- pAttenuationNode->Attribute("linear", &temp);
- m_Props.m_Attenuation[1] = (float) temp;
- pAttenuationNode->Attribute("exp", &temp);
- m_Props.m_Attenuation[2] = (float) temp;
- }
- TiXmlElement* pShapeNode = NULL;
- pShapeNode = pLight->FirstChildElement("Shape");
- if (pShapeNode)
- {
- pShapeNode->Attribute("range", &temp);
- m_Props.m_Range = (float) temp;
- pShapeNode->Attribute("falloff", &temp);
- m_Props.m_Falloff = (float) temp;
- pShapeNode->Attribute("theta", &temp);
- m_Props.m_Theta = (float) temp;
- pShapeNode->Attribute("phi", &temp);
- m_Props.m_Phi = (float) temp;
- }
- return true;
- }
- std::shared_ptr<SceneNode> LightRenderComponent::VCreateSceneNode(void)
- {
- std::shared_ptr<TransformComponent> pTransformComponent = MakeStrongPtr(m_pOwner->GetComponent<TransformComponent>(TransformComponent::g_Name));
- if (pTransformComponent)
- {
- WeakBaseRenderComponentPtr weakThis(this);
- return std::shared_ptr<SceneNode>(GCC_NEW OpenGLLightNode46(m_pOwner->GetId(), weakThis, m_Props, pTransformComponent->GetTransform()));
- }
- return std::shared_ptr<SceneNode>();
- }
- void LightRenderComponent::VCreateInheritedXmlElements(TiXmlElement *pBaseElement)
- {
- TiXmlElement* pSceneNode = GCC_NEW TiXmlElement("Light");
- // attenuation
- TiXmlElement* pAttenuation = GCC_NEW TiXmlElement("Attenuation");
- pAttenuation->SetAttribute("const", ToStr(m_Props.m_Attenuation[0]).c_str());
- pAttenuation->SetAttribute("linear", ToStr(m_Props.m_Attenuation[1]).c_str());
- pAttenuation->SetAttribute("exp", ToStr(m_Props.m_Attenuation[2]).c_str());
- pSceneNode->LinkEndChild(pAttenuation);
- // shape
- TiXmlElement* pShape = GCC_NEW TiXmlElement("Shape");
- pShape->SetAttribute("range", ToStr(m_Props.m_Range).c_str());
- pShape->SetAttribute("falloff", ToStr(m_Props.m_Falloff).c_str());
- pShape->SetAttribute("theta", ToStr(m_Props.m_Theta).c_str());
- pShape->SetAttribute("phi", ToStr(m_Props.m_Phi).c_str());
- pSceneNode->LinkEndChild(pShape);
- pBaseElement->LinkEndChild(pSceneNode);
- }
- SkyRenderComponent::SkyRenderComponent(void)
- {
- }
- bool SkyRenderComponent::VDelegateInit(TiXmlElement* pData)
- {
- TiXmlElement* pTexture = pData->FirstChildElement("Texture");
- if (pTexture)
- {
- m_textureResource = pTexture->FirstChild()->Value();
- }
- return true;
- }
- std::shared_ptr<SceneNode> SkyRenderComponent::VCreateSceneNode(void)
- {
- std::shared_ptr<SkyNode> sky;
- if (GameCodeApp::GetRendererImpl()==GameCodeApp::Renderer_OpenGL46)
- {
- sky = std::shared_ptr<SkyNode>(GCC_NEW OpenGLSkyNode46(m_textureResource.c_str() ));
- }
- else
- {
- GCC_ERROR("Unknown Renderer Implementation in VLoadGameDelegate");
- }
- return sky;
- }
- void SkyRenderComponent::VCreateInheritedXmlElements(TiXmlElement *pBaseElement)
- {
- TiXmlElement* pTextureNode = GCC_NEW TiXmlElement("Texture");
- TiXmlText* pTextureText = GCC_NEW TiXmlText(m_textureResource.c_str());
- pTextureNode->LinkEndChild(pTextureText);
- pBaseElement->LinkEndChild(pTextureNode);
- }
|