CubemapGenerator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // Copyright (c) 2014-2016 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/Core/Context.h>
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/IO/File.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #include <Atomic/Graphics/Viewport.h>
  30. #include <Atomic/Graphics/Texture2D.h>
  31. #include <Atomic/Graphics/TextureCube.h>
  32. #include <Atomic/Resource/ResourceCache.h>
  33. #include <Atomic/Resource/XMLFile.h>
  34. #include <Atomic/Scene/Node.h>
  35. #include <ToolCore/ToolSystem.h>
  36. #include <ToolCore/Project/Project.h>
  37. #include "../Editors/SceneEditor3D/SceneEditor3DEvents.h"
  38. #include "../Editors/SceneEditor3D/SceneEditor3D.h"
  39. #include "CubemapGenerator.h"
  40. namespace AtomicEditor
  41. {
  42. CubemapGenerator::CubemapGenerator(Context *context) : EditorComponent(context),
  43. updateCycle_(0),
  44. imageSize_(512),
  45. namePrefix_("Cubemap")
  46. {
  47. }
  48. CubemapGenerator::~CubemapGenerator()
  49. {
  50. }
  51. bool CubemapGenerator::Render()
  52. {
  53. if(!InitRender())
  54. {
  55. ATOMIC_LOGERRORF("Unable to init render");
  56. return false;
  57. }
  58. GetScene()->SendEvent(E_CUBEMAPRENDERBEGIN);
  59. SubscribeToEvent(E_BEGINFRAME, ATOMIC_HANDLER(CubemapGenerator, HandleBeginFrame));
  60. SubscribeToEvent(E_ENDFRAME, ATOMIC_HANDLER(CubemapGenerator, HandleEndFrame));
  61. return true;
  62. }
  63. bool CubemapGenerator::InitPaths()
  64. {
  65. String scenePath = sceneEditor_->GetFullPath();
  66. String pathName;
  67. String fileName;
  68. String ext;
  69. SplitPath(scenePath, pathName, fileName, ext);
  70. outputPathAbsolute_ = pathName + "Cubemaps/" + fileName + "/";
  71. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  72. if (!fileSystem->DirExists(outputPathAbsolute_))
  73. {
  74. if (!fileSystem->CreateDirs(pathName, "Cubemaps/" + fileName + "/"))
  75. {
  76. ATOMIC_LOGERRORF("CubemapGenerator::InitRender - Unable to create path: %s", outputPathAbsolute_.CString());
  77. return false;
  78. }
  79. }
  80. // TODO: There should be a better way of getting the resource path
  81. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  82. Project* project = tsystem->GetProject();
  83. resourcePath_ = outputPathAbsolute_;
  84. resourcePath_.Replace(project->GetResourcePath(), "");
  85. resourcePath_ = AddTrailingSlash(resourcePath_);
  86. return true;
  87. }
  88. bool CubemapGenerator::InitRender()
  89. {
  90. sceneEditor_ = GetSceneEditor();
  91. if (sceneEditor_.Null())
  92. {
  93. ATOMIC_LOGERROR("CubemapGenerator::InitRender - unable to get scene editor");
  94. return false;
  95. }
  96. if (!InitPaths())
  97. return false;
  98. cameraNode_ = node_->CreateChild("CubeMapRenderCamera");
  99. cameraNode_->SetTemporary(true);
  100. camera_ = cameraNode_->CreateComponent<Camera>();
  101. camera_->SetTemporary(true);
  102. camera_->SetFov(90.0f);
  103. camera_->SetNearClip(0.0001f);
  104. camera_->SetAspectRatio(1.0f);
  105. RenderPath* renderPath = sceneEditor_->GetSceneView3D()->GetViewport()->GetRenderPath();
  106. viewport_ = new Viewport(context_, GetScene(), camera_, renderPath);
  107. renderImage_ = new Texture2D(context_);
  108. renderImage_->SetSize(imageSize_, imageSize_, Graphics::GetRGBAFormat(), TEXTURE_RENDERTARGET);
  109. renderSurface_ = renderImage_->GetRenderSurface();
  110. renderSurface_->SetViewport(0, viewport_);
  111. renderSurface_->SetUpdateMode(SURFACE_UPDATEALWAYS);
  112. return true;
  113. }
  114. void CubemapGenerator::SaveCubemapXML()
  115. {
  116. SharedPtr<XMLFile> xmlFile(new XMLFile(context_));
  117. XMLElement rootElem = xmlFile->CreateRoot("cubemap");
  118. String prefix = resourcePath_ + namePrefix_ + "_";
  119. String name = prefix + GetFaceName(FACE_POSITIVE_X) + ".png";
  120. rootElem.CreateChild("face").SetAttribute("name", name);
  121. name = prefix + GetFaceName(FACE_NEGATIVE_X) + ".png";
  122. rootElem.CreateChild("face").SetAttribute("name", name);
  123. name = prefix + GetFaceName(FACE_POSITIVE_Y) + ".png";
  124. rootElem.CreateChild("face").SetAttribute("name", name);
  125. name = prefix + GetFaceName(FACE_NEGATIVE_Y) + ".png";
  126. rootElem.CreateChild("face").SetAttribute("name", name);
  127. name = prefix + GetFaceName(FACE_POSITIVE_Z) + ".png";
  128. rootElem.CreateChild("face").SetAttribute("name", name);
  129. name = prefix + GetFaceName(FACE_NEGATIVE_Z) + ".png";
  130. rootElem.CreateChild("face").SetAttribute("name", name);
  131. String xmlPath = outputPathAbsolute_ + namePrefix_ + ".xml";
  132. SharedPtr<File> file(new File(context_, xmlPath, FILE_WRITE));
  133. xmlFile->Save(*file, " ");
  134. file->Close();
  135. ResourceCache* cache = GetSubsystem<ResourceCache>();
  136. TextureCube* texcube = cache->GetResource<TextureCube>(resourcePath_ + namePrefix_ + ".xml");
  137. if (texcube)
  138. cache->ReloadResource(texcube);
  139. }
  140. void CubemapGenerator::EndRender()
  141. {
  142. UnsubscribeFromEvent(E_BEGINFRAME);
  143. UnsubscribeFromEvent(E_ENDFRAME);
  144. SaveCubemapXML();
  145. cameraNode_->Remove();
  146. cameraNode_ = 0;
  147. camera_ = 0;
  148. viewport_ = 0;
  149. renderSurface_ = 0;
  150. // release renderImage_ after renderSurface_, as it doesn't hold a ref
  151. renderImage_ = 0;
  152. updateCycle_ = 0;
  153. GetScene()->SendEvent(E_CUBEMAPRENDEREND);
  154. }
  155. void CubemapGenerator::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  156. {
  157. updateCycle_++;
  158. if (updateCycle_ < 7)
  159. cameraNode_->SetWorldRotation(RotationOf(GetFaceForCycle(updateCycle_)));
  160. else
  161. {
  162. EndRender();
  163. }
  164. }
  165. void CubemapGenerator::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  166. {
  167. SharedPtr<Image> image(GetImage(renderImage_));
  168. String path = outputPathAbsolute_;
  169. if (namePrefix_.Length())
  170. path += namePrefix_;
  171. path.AppendWithFormat("_%s.png", GetFaceName(GetFaceForCycle(updateCycle_)).CString());
  172. image->SavePNG(path);
  173. }
  174. SharedPtr<Image> CubemapGenerator::GetImage(Texture2D* tex2d)
  175. {
  176. Image* rawImage = new Image(tex2d->GetContext());
  177. const unsigned format = tex2d->GetFormat();
  178. if (format == Graphics::GetRGBAFormat() || format == Graphics::GetRGBA16Format() || format == Graphics::GetRGBAFloat32Format())
  179. rawImage->SetSize(tex2d->GetWidth(), tex2d->GetHeight(), 4);
  180. else if (format == Graphics::GetRGBFormat())
  181. rawImage->SetSize(tex2d->GetWidth(), tex2d->GetHeight(), 3);
  182. else
  183. return SharedPtr<Image>();
  184. tex2d->GetData(0, rawImage->GetData());
  185. return SharedPtr<Image>(rawImage);
  186. }
  187. CubeMapFace CubemapGenerator::GetFaceForCycle(int cycle)
  188. {
  189. switch (cycle)
  190. {
  191. case 1:
  192. return FACE_POSITIVE_X;
  193. case 2:
  194. return FACE_POSITIVE_Y;
  195. case 3:
  196. return FACE_POSITIVE_Z;
  197. case 4:
  198. return FACE_NEGATIVE_X;
  199. case 5:
  200. return FACE_NEGATIVE_Y;
  201. case 6:
  202. return FACE_NEGATIVE_Z;
  203. }
  204. return FACE_POSITIVE_X;
  205. }
  206. String CubemapGenerator::GetFaceName(CubeMapFace face)
  207. {
  208. switch (face)
  209. {
  210. case FACE_POSITIVE_X:
  211. return "PosX";
  212. case FACE_POSITIVE_Y:
  213. return "PosY";
  214. case FACE_POSITIVE_Z:
  215. return "PosZ";
  216. case FACE_NEGATIVE_X:
  217. return "NegX";
  218. case FACE_NEGATIVE_Y:
  219. return "NegY";
  220. case FACE_NEGATIVE_Z:
  221. return "NegZ";
  222. default:
  223. break;
  224. }
  225. return "PosX";
  226. }
  227. Quaternion CubemapGenerator::RotationOf(CubeMapFace face)
  228. {
  229. Quaternion result;
  230. switch (face)
  231. {
  232. // Rotate camera according to probe rotation
  233. case FACE_POSITIVE_X:
  234. result = Quaternion(0, 90, 0);
  235. break;
  236. case FACE_NEGATIVE_X:
  237. result = Quaternion(0, -90, 0);
  238. break;
  239. case FACE_POSITIVE_Y:
  240. result = Quaternion(-90, 0, 0);
  241. break;
  242. case FACE_NEGATIVE_Y:
  243. result = Quaternion(90, 0, 0);
  244. break;
  245. case FACE_POSITIVE_Z:
  246. result = Quaternion(0, 0, 0);
  247. break;
  248. case FACE_NEGATIVE_Z:
  249. result = Quaternion(0, 180, 0);
  250. break;
  251. default:
  252. break;
  253. }
  254. return result;
  255. }
  256. void CubemapGenerator::RegisterObject(Context* context)
  257. {
  258. context->RegisterFactory<CubemapGenerator>();
  259. ATOMIC_ATTRIBUTE("Name Prefix", String, namePrefix_, "Cubemap", AM_DEFAULT);
  260. ATOMIC_ATTRIBUTE("Image Size", int, imageSize_, 512, AM_DEFAULT);
  261. }
  262. }