GlowService.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if (scene_)
  51. {
  52. scene_->LoadLightmaps(true);
  53. }
  54. projectPath_ = String::EMPTY;
  55. glowProcess_ = 0;
  56. scene_ = 0;
  57. }
  58. void GlowService::OnBakeError(const String& result)
  59. {
  60. ATOMIC_LOGERRORF("%s", result.CString());
  61. glowProcess_->Exit();
  62. using namespace AtomicGlowServiceBakeResult;
  63. VariantMap eventData;
  64. eventData[P_RESULT] = result;
  65. eventData[P_SUCCESS] = false;
  66. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  67. }
  68. void GlowService::OnBakeSuccess()
  69. {
  70. glowProcess_->Exit();
  71. using namespace AtomicGlowServiceBakeResult;
  72. VariantMap eventData;
  73. eventData[P_RESULT] = "success";
  74. eventData[P_SUCCESS] = true;
  75. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  76. }
  77. void GlowService::ProcessBakeData(VectorBuffer& bakeData)
  78. {
  79. if (!scene_)
  80. {
  81. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - called with null scene");
  82. return;
  83. }
  84. unsigned count = bakeData.ReadUInt();
  85. PODVector<Node*> children;
  86. scene_->GetChildrenWithComponent<StaticModel>(children, true);
  87. for (unsigned i = 0; i < count; i++)
  88. {
  89. Node* node = 0;
  90. StaticModel* staticModel = 0;
  91. unsigned nodeID = bakeData.ReadUInt();
  92. unsigned staticModelID = bakeData.ReadUInt();
  93. unsigned lightMask = bakeData.ReadUInt();
  94. unsigned lightmapIndex = bakeData.ReadUInt();
  95. Vector4 tilingOffset = bakeData.ReadVector4();
  96. for (unsigned j = 0; j < children.Size(); j++)
  97. {
  98. if (children[j]->GetID() == nodeID)
  99. {
  100. node = children[j];
  101. staticModel = node->GetComponent<StaticModel>();
  102. if (!staticModel || staticModel->GetID() != staticModelID)
  103. {
  104. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - mismatched node <-> static model ID");
  105. return;
  106. }
  107. break;
  108. }
  109. }
  110. if (!node)
  111. {
  112. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - unable to find node ID");
  113. return;
  114. }
  115. staticModel->SetLightMask(lightMask);
  116. staticModel->SetLightmapIndex(lightmapIndex);
  117. staticModel->SetLightmapTilingOffset(tilingOffset);
  118. }
  119. }
  120. bool GlowService::Bake(const String& projectPath, Scene* scene, const GlowSettings& settings)
  121. {
  122. if (!scene)
  123. {
  124. ATOMIC_LOGERROR("GlowService::Bake() - Called with null scene");
  125. return false;
  126. }
  127. String sceneName = scene->GetFileName();
  128. if (!sceneName.Length())
  129. {
  130. ATOMIC_LOGERROR("GlowService::Bake() - Called with unnamed scene");
  131. return false;
  132. }
  133. if (!projectPath.Length())
  134. {
  135. ATOMIC_LOGERROR("GlowService::Bake() - zero length projectPath");
  136. return false;
  137. }
  138. if (glowProcess_.NotNull())
  139. {
  140. ATOMIC_LOGERROR("GlowService::Bake() - Called with existing glow process");
  141. return false;
  142. }
  143. bakeCanceled_ = false;
  144. result_.Clear();
  145. glowProcess_ = new GlowProcess(context_);
  146. projectPath_ = projectPath;
  147. StringVector args;
  148. args.Push("--project");
  149. args.Push(projectPath_);
  150. args += glowBaseArgs_;
  151. if (!glowProcess_->Start(glowBinaryPath_, args))
  152. {
  153. ATOMIC_LOGERROR("GlowService::Bake() - Glow process failed to start");
  154. return false;
  155. }
  156. scene_ = scene;
  157. projectPath_ = projectPath;
  158. using namespace IPCCmd;
  159. VariantMap cmdMap;
  160. cmdMap[P_COMMAND] = "bake";
  161. cmdMap["scenename"] = sceneName;
  162. // settings
  163. VectorBuffer settingsBuffer;
  164. settings.Pack(settingsBuffer);
  165. cmdMap["settings"] = settingsBuffer;
  166. glowProcess_->QueueCommand(cmdMap);
  167. return true;
  168. }
  169. void GlowService::CancelBake()
  170. {
  171. if (glowProcess_.Null())
  172. {
  173. ATOMIC_LOGERROR("GlowService::CancelBake() - Called without existing glow process");
  174. return;
  175. }
  176. bakeCanceled_ = true;
  177. glowProcess_->Exit();
  178. }
  179. bool GlowService::Start()
  180. {
  181. bool failed = false;
  182. if (failed)
  183. {
  184. ATOMIC_LOGERROR("GlowService::Start() - Unable to start AtomicGlow service");
  185. return false;
  186. }
  187. glowBinaryPath_ = String::EMPTY;
  188. glowBaseArgs_.Clear();
  189. #ifdef ATOMIC_DEBUG
  190. glowBinaryPath_ = "/Users/jenge/Dev/atomic/build-AtomicGameEngine-Desktop-Debug/Source/AtomicGlow/AtomicGlow";
  191. #else
  192. glowBinaryPath_ = "/Users/jenge/Dev/atomic/build-AtomicGameEngine-Desktop-Release/Source/AtomicGlow/AtomicGlow";
  193. #endif
  194. return true;
  195. }
  196. }