| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- //
- // Copyright (c) 2014-2017 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/IO/Log.h>
- #include <Atomic/IPC/IPCEvents.h>
- #include <Atomic/Graphics/StaticModel.h>
- #include "GlowProcess.h"
- #include "GlowServiceEvents.h"
- #include "GlowService.h"
- namespace AtomicGlow
- {
- GlowService::GlowService(Context* context) : Object(context),
- bakeCanceled_(false)
- {
- }
- GlowService::~GlowService()
- {
- }
- void GlowService::OnGlowProcessExited()
- {
- if (glowProcess_.NotNull())
- {
- if (!glowProcess_->GetExitCalled() || bakeCanceled_)
- {
- using namespace AtomicGlowServiceBakeResult;
- VariantMap eventData;
- eventData[P_RESULT] = bakeCanceled_ ? "Bake canceled" : "GlowService::OnGlowProcessExited() - Glow process exited unexpectedly";
- eventData[P_SUCCESS] = false;
- SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
- }
- }
- if (scene_)
- {
- scene_->LoadLightmaps(true);
- }
- projectPath_ = String::EMPTY;
- glowProcess_ = 0;
- scene_ = 0;
- }
- void GlowService::OnBakeError(const String& result)
- {
- ATOMIC_LOGERRORF("%s", result.CString());
- glowProcess_->Exit();
- using namespace AtomicGlowServiceBakeResult;
- VariantMap eventData;
- eventData[P_RESULT] = result;
- eventData[P_SUCCESS] = false;
- SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
- }
- void GlowService::OnBakeSuccess()
- {
- glowProcess_->Exit();
- using namespace AtomicGlowServiceBakeResult;
- VariantMap eventData;
- eventData[P_RESULT] = "success";
- eventData[P_SUCCESS] = true;
- SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
- }
- void GlowService::ProcessBakeData(VectorBuffer& bakeData)
- {
- if (!scene_)
- {
- ATOMIC_LOGERROR("GlowService::ProcessBakeData() - called with null scene");
- return;
- }
- unsigned count = bakeData.ReadUInt();
- PODVector<Node*> children;
- scene_->GetChildrenWithComponent<StaticModel>(children, true);
- for (unsigned i = 0; i < count; i++)
- {
- Node* node = 0;
- StaticModel* staticModel = 0;
- unsigned nodeID = bakeData.ReadUInt();
- unsigned staticModelID = bakeData.ReadUInt();
- unsigned lightMask = bakeData.ReadUInt();
- unsigned lightmapIndex = bakeData.ReadUInt();
- Vector4 tilingOffset = bakeData.ReadVector4();
- for (unsigned j = 0; j < children.Size(); j++)
- {
- if (children[j]->GetID() == nodeID)
- {
- node = children[j];
- staticModel = node->GetComponent<StaticModel>();
- if (!staticModel || staticModel->GetID() != staticModelID)
- {
- ATOMIC_LOGERROR("GlowService::ProcessBakeData() - mismatched node <-> static model ID");
- return;
- }
- break;
- }
- }
- if (!node)
- {
- ATOMIC_LOGERROR("GlowService::ProcessBakeData() - unable to find node ID");
- return;
- }
- staticModel->SetLightMask(lightMask);
- staticModel->SetLightmapIndex(lightmapIndex);
- staticModel->SetLightmapTilingOffset(tilingOffset);
- }
- }
- bool GlowService::Bake(const String& projectPath, Scene* scene, const GlowSettings& settings)
- {
- if (!scene)
- {
- ATOMIC_LOGERROR("GlowService::Bake() - Called with null scene");
- return false;
- }
- String sceneName = scene->GetFileName();
- if (!sceneName.Length())
- {
- ATOMIC_LOGERROR("GlowService::Bake() - Called with unnamed scene");
- return false;
- }
- if (!projectPath.Length())
- {
- ATOMIC_LOGERROR("GlowService::Bake() - zero length projectPath");
- return false;
- }
- if (glowProcess_.NotNull())
- {
- ATOMIC_LOGERROR("GlowService::Bake() - Called with existing glow process");
- return false;
- }
- bakeCanceled_ = false;
- result_.Clear();
- glowProcess_ = new GlowProcess(context_);
- projectPath_ = projectPath;
- StringVector args;
- args.Push("--project");
- args.Push(projectPath_);
- args += glowBaseArgs_;
- if (!glowProcess_->Start(glowBinaryPath_, args))
- {
- ATOMIC_LOGERROR("GlowService::Bake() - Glow process failed to start");
- return false;
- }
- scene_ = scene;
- projectPath_ = projectPath;
- using namespace IPCCmd;
- VariantMap cmdMap;
- cmdMap[P_COMMAND] = "bake";
- cmdMap["scenename"] = sceneName;
- // settings
- VectorBuffer settingsBuffer;
- settings.Pack(settingsBuffer);
- cmdMap["settings"] = settingsBuffer;
- glowProcess_->QueueCommand(cmdMap);
- return true;
- }
- void GlowService::CancelBake()
- {
- if (glowProcess_.Null())
- {
- ATOMIC_LOGERROR("GlowService::CancelBake() - Called without existing glow process");
- return;
- }
- bakeCanceled_ = true;
- glowProcess_->Exit();
- }
- bool GlowService::Start()
- {
- bool failed = false;
- if (failed)
- {
- ATOMIC_LOGERROR("GlowService::Start() - Unable to start AtomicGlow service");
- return false;
- }
- glowBinaryPath_ = String::EMPTY;
- glowBaseArgs_.Clear();
- #ifdef ATOMIC_DEBUG
- glowBinaryPath_ = "/Users/jenge/Dev/atomic/build-AtomicGameEngine-Desktop-Debug/Source/AtomicGlow/AtomicGlow";
- #else
- glowBinaryPath_ = "/Users/jenge/Dev/atomic/build-AtomicGameEngine-Desktop-Release/Source/AtomicGlow/AtomicGlow";
- #endif
- return true;
- }
- }
|