2
0

PointLightComponent.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "PointLightComponent.h"
  9. #include "Shader.h"
  10. #include "Game.h"
  11. #include "Renderer.h"
  12. #include "Mesh.h"
  13. #include "VertexArray.h"
  14. #include "Actor.h"
  15. PointLightComponent::PointLightComponent(Actor* owner)
  16. :Component(owner)
  17. {
  18. owner->GetGame()->GetRenderer()->AddPointLight(this);
  19. }
  20. PointLightComponent::~PointLightComponent()
  21. {
  22. mOwner->GetGame()->GetRenderer()->RemovePointLight(this);
  23. }
  24. void PointLightComponent::Draw(Shader* shader, Mesh* mesh)
  25. {
  26. // We assume, coming into this function, that the shader is active
  27. // and the sphere mesh is active
  28. // World transform is scaled to the outer radius (divided by the mesh radius)
  29. // and positioned to the world position
  30. Matrix4 scale = Matrix4::CreateScale(mOwner->GetScale() *
  31. mOuterRadius / mesh->GetRadius());
  32. Matrix4 trans = Matrix4::CreateTranslation(mOwner->GetPosition());
  33. Matrix4 worldTransform = scale * trans;
  34. shader->SetMatrixUniform("uWorldTransform", worldTransform);
  35. // Set point light shader constants
  36. shader->SetVectorUniform("uPointLight.mWorldPos", mOwner->GetPosition());
  37. shader->SetVectorUniform("uPointLight.mDiffuseColor", mDiffuseColor);
  38. shader->SetFloatUniform("uPointLight.mInnerRadius", mInnerRadius);
  39. shader->SetFloatUniform("uPointLight.mOuterRadius", mOuterRadius);
  40. // Draw the sphere
  41. glDrawElements(GL_TRIANGLES, mesh->GetVertexArray()->GetNumIndices(),
  42. GL_UNSIGNED_INT, nullptr);
  43. }