GlowService.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // Copyright (c) 2014-2017 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IPC/IPCEvents.h>
  24. #include <Atomic/Graphics/StaticModel.h>
  25. #include "GlowProcess.h"
  26. #include "GlowServiceEvents.h"
  27. #include "GlowService.h"
  28. namespace AtomicGlow
  29. {
  30. GlowService::GlowService(Context* context) : Object(context),
  31. bakeCanceled_(false)
  32. {
  33. }
  34. GlowService::~GlowService()
  35. {
  36. }
  37. void GlowService::OnGlowProcessExited()
  38. {
  39. if (glowProcess_.NotNull())
  40. {
  41. if (!glowProcess_->GetExitCalled() || bakeCanceled_)
  42. {
  43. using namespace AtomicGlowServiceBakeResult;
  44. VariantMap eventData;
  45. eventData[P_RESULT] = bakeCanceled_ ? "Bake canceled" : "GlowService::OnGlowProcessExited() - Glow process exited unexpectedly";
  46. eventData[P_SUCCESS] = false;
  47. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  48. }
  49. }
  50. glowProcess_ = 0;
  51. scene_ = 0;
  52. }
  53. void GlowService::OnBakeError(const String& result)
  54. {
  55. ATOMIC_LOGERRORF("%s", result.CString());
  56. glowProcess_->Exit();
  57. using namespace AtomicGlowServiceBakeResult;
  58. VariantMap eventData;
  59. eventData[P_RESULT] = result;
  60. eventData[P_SUCCESS] = false;
  61. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  62. }
  63. void GlowService::OnBakeSuccess()
  64. {
  65. glowProcess_->Exit();
  66. using namespace AtomicGlowServiceBakeResult;
  67. VariantMap eventData;
  68. eventData[P_RESULT] = "success";
  69. eventData[P_SUCCESS] = true;
  70. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  71. }
  72. void GlowService::ProcessBakeData(VectorBuffer& bakeData)
  73. {
  74. if (!scene_)
  75. {
  76. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - called with null scene");
  77. return;
  78. }
  79. unsigned count = bakeData.ReadUInt();
  80. PODVector<Node*> children;
  81. scene_->GetChildrenWithComponent<StaticModel>(children, true);
  82. for (unsigned i = 0; i < count; i++)
  83. {
  84. Node* node = 0;
  85. StaticModel* staticModel = 0;
  86. unsigned nodeID = bakeData.ReadUInt();
  87. unsigned staticModelID = bakeData.ReadUInt();
  88. unsigned lightMask = bakeData.ReadUInt();
  89. unsigned lightmapIndex = bakeData.ReadUInt();
  90. Vector4 tilingOffset = bakeData.ReadVector4();
  91. for (unsigned j = 0; j < children.Size(); j++)
  92. {
  93. if (children[j]->GetID() == nodeID)
  94. {
  95. node = children[j];
  96. staticModel = node->GetComponent<StaticModel>();
  97. if (!staticModel || staticModel->GetID() != staticModelID)
  98. {
  99. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - mismatched node <-> static model ID");
  100. return;
  101. }
  102. break;
  103. }
  104. }
  105. if (!node)
  106. {
  107. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - unable to find node ID");
  108. return;
  109. }
  110. staticModel->SetLightMask(lightMask);
  111. staticModel->SetLightmapIndex(lightmapIndex);
  112. staticModel->SetLightmapTilingOffset(tilingOffset);
  113. }
  114. }
  115. bool GlowService::Bake(Scene* scene, const GlowSettings& settings)
  116. {
  117. if (!scene)
  118. {
  119. ATOMIC_LOGERROR("GlowService::Bake() - Called with null scene");
  120. return false;
  121. }
  122. if (glowProcess_.NotNull())
  123. {
  124. ATOMIC_LOGERROR("GlowService::Bake() - Called with existing glow process");
  125. return false;
  126. }
  127. bakeCanceled_ = false;
  128. result_.Clear();
  129. glowProcess_ = new GlowProcess(context_);
  130. if (!glowProcess_->Start(glowBinaryPath_, glowBaseArgs_))
  131. {
  132. ATOMIC_LOGERROR("GlowService::Bake() - Glow process failed to start");
  133. return false;
  134. }
  135. scene_ = scene;
  136. using namespace IPCCmd;
  137. VariantMap cmdMap;
  138. cmdMap[P_COMMAND] = "bake";
  139. // settings
  140. VectorBuffer settingsBuffer;
  141. settings.Pack(settingsBuffer);
  142. cmdMap["settings"] = settingsBuffer;
  143. glowProcess_->QueueCommand(cmdMap);
  144. return true;
  145. }
  146. void GlowService::CancelBake()
  147. {
  148. if (glowProcess_.Null())
  149. {
  150. ATOMIC_LOGERROR("GlowService::CancelBake() - Called without existing glow process");
  151. return;
  152. }
  153. bakeCanceled_ = true;
  154. glowProcess_->Exit();
  155. }
  156. bool GlowService::Start()
  157. {
  158. bool failed = false;
  159. if (failed)
  160. {
  161. ATOMIC_LOGERROR("GlowService::Start() - Unable to start AtomicGlow service");
  162. return false;
  163. }
  164. glowBinaryPath_ = String::EMPTY;
  165. glowBaseArgs_.Clear();
  166. #ifdef ATOMIC_DEBUG
  167. glowBinaryPath_ = "/Users/jenge/Dev/atomic/build-AtomicGameEngine-Desktop-Debug/Source/AtomicGlow/AtomicGlow";
  168. #else
  169. glowBinaryPath_ = "/Users/jenge/Dev/atomic/build-AtomicGameEngine-Desktop-Release/Source/AtomicGlow/AtomicGlow";
  170. #endif
  171. glowBaseArgs_.Push("--project");
  172. glowBaseArgs_.Push("/Users/jenge/Dev/atomic/AtomicExamplesPrivate/AtomicGlowTests/TestScene1");
  173. return true;
  174. }
  175. }