LMStaticModel.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "Precompiled.h"
  5. #include "../Core/Context.h"
  6. #include "../Resource/ResourceCache.h"
  7. #include "../Graphics/Technique.h"
  8. #include "LMStaticModel.h"
  9. namespace Atomic
  10. {
  11. extern const char* GEOMETRY_CATEGORY;
  12. LMStaticModel::LMStaticModel(Context* context) :
  13. StaticModel(context),
  14. lightmapTilingOffset_(1.0f, 1.0f, 0.0f, 0.0f)
  15. {
  16. }
  17. LMStaticModel::~LMStaticModel()
  18. {
  19. }
  20. void LMStaticModel::RegisterObject(Context* context)
  21. {
  22. context->RegisterFactory<LMStaticModel>(GEOMETRY_CATEGORY);
  23. ATOMIC_COPY_BASE_ATTRIBUTES(StaticModel);
  24. ATOMIC_ATTRIBUTE("Lightmap Tiling Offset", Vector4, lightmapTilingOffset_ , Vector4(1.0f, 1.0f, 0.0f, 0.0f), AM_DEFAULT);
  25. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("LightmapTexture", GetLightmapTextureAttr, SetLightmapTextureAttr, ResourceRef, ResourceRef(Texture2D::GetTypeStatic()), AM_DEFAULT);
  26. }
  27. void LMStaticModel::SetLightmapTextureAttr(const ResourceRef& value)
  28. {
  29. ResourceCache* cache = GetSubsystem<ResourceCache>();
  30. if (value.name_.Length())
  31. {
  32. lightmapTexture_ = cache->GetResource<Texture2D>(value.name_);
  33. }
  34. else
  35. {
  36. lightmapTexture_ = NULL;
  37. }
  38. // TODO: This is making a separate material for each model
  39. // we should be able to factor this into batching
  40. if (lightmapTexture_.NotNull() && lightmapMaterial_.Null())
  41. {
  42. Material* base = GetMaterial(0);
  43. if (base)
  44. {
  45. ResourceCache* cache = GetSubsystem<ResourceCache>();
  46. lightmapMaterial_ = base->Clone();
  47. Technique* technique = cache->GetResource<Technique>("Techniques/DiffLightMap.xml");
  48. lightmapMaterial_->SetTechnique(0, technique);
  49. lightmapMaterial_->SetTexture(TU_EMISSIVE, lightmapTexture_);
  50. }
  51. }
  52. }
  53. ResourceRef LMStaticModel::GetLightmapTextureAttr() const
  54. {
  55. return GetResourceRef(lightmapTexture_, Texture2D::GetTypeStatic());
  56. }
  57. void LMStaticModel::UpdateBatches(const FrameInfo& frame)
  58. {
  59. /*
  60. StaticModel::UpdateBatches(frame);
  61. if (batches_.Size() > 1)
  62. {
  63. for (unsigned i = 0; i < batches_.Size(); ++i)
  64. {
  65. batches_[i].lightmapTilingOffset_ = &lightmapTilingOffset_;
  66. batches_[i].geometryType_ = GEOM_STATIC_NOINSTANCING;
  67. if (!lightmapMaterial_.Null())
  68. batches_[i].material_ = lightmapMaterial_;
  69. }
  70. }
  71. else if (batches_.Size() == 1)
  72. {
  73. batches_[0].lightmapTilingOffset_ = &lightmapTilingOffset_;
  74. batches_[0].geometryType_ = GEOM_STATIC_NOINSTANCING;
  75. if (!lightmapMaterial_.Null())
  76. batches_[0].material_ = lightmapMaterial_;
  77. }
  78. */
  79. }
  80. }