| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //========================================================================
- // Lights.h - implements a simple light class for the GameCode4 scene graph
- //
- // 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 "../GameCode4/GameCode.h"
- #include "../Actors/RenderComponent.h"
- #include "Lights.h"
- LightNode::LightNode(const ActorId actorId, WeakBaseRenderComponentPtr renderComponent, const LightProperties &props, const glm::mat4 t)
- : SceneNode(actorId, renderComponent, RenderPass_NotRendered, t)
- {
- m_LightProps = props;
- }
- int OpenGLLightNode46::VOnUpdate(Scene*, uint64_t const elapsedMs)
- {
- // light color can change anytime! Check the BaseRenderComponent!
- LightRenderComponent* lrc = static_cast<LightRenderComponent*>(m_RenderComponent);
- m_Props.GetMaterial().SetDiffuse(lrc->GetColor());
- return 0;
- }
- void LightManager::CalcLighting(Scene *pScene)
- {
- // FUTURE WORK: There might be all kinds of things you'd want to do here for optimization, especially turning off lights on actors that can't be seen, etc.
- pScene->GetRenderer()->VCalcLighting(&m_Lights, MAXIMUM_LIGHTS_SUPPORTED);
- int count = 0;
- GCC_ASSERT(m_Lights.size() < MAXIMUM_LIGHTS_SUPPORTED);
- for (Lights::iterator i=m_Lights.begin(); i!=m_Lights.end(); ++i, ++count)
- {
- std::shared_ptr<LightNode> light = *i;
- if (count==0)
- {
- // Light 0 is the only one we use for ambient lighting. The rest are ignored in the simple shaders used for GameCode4.
- Color ambient = light->VGet()->GetMaterial().GetAmbient();
- m_vLightAmbient = glm::vec4(ambient.r, ambient.g, ambient.b, 1.0f);
- }
- glm::vec3 lightDir = light->GetDirection();
- m_vLightDir[count] = glm::vec4(lightDir.x, lightDir.y, lightDir.z, 1.0f);
- m_vLightDiffuse[count] = light->VGet()->GetMaterial().GetDiffuse();
- }
- }
- void LightManager::CalcLighting(ConstantBuffer_Lighting* pLighting, SceneNode* pNode)
- {
- int count = GetLightCount(pNode);
- if (count)
- {
- pLighting->m_vLightAmbient = *GetLightAmbient(pNode);
- memcpy(pLighting->m_vLightDir, GetLightDirection(pNode), sizeof(glm::vec4) * count);
- memcpy(pLighting->m_vLightDiffuse, GetLightDiffuse(pNode), sizeof(glm::vec4) * count);
- pLighting->m_nNumLights = count;
- }
- }
|