View3D.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Camera.h"
  25. #include "../Graphics/Graphics.h"
  26. #include "../Graphics/GraphicsEvents.h"
  27. #include "../Graphics/Octree.h"
  28. #include "../Graphics/Texture2D.h"
  29. #include "../Graphics/Zone.h"
  30. #include "../Scene/Scene.h"
  31. #include "../UI/View3D.h"
  32. #include "../UI/UI.h"
  33. #include "../UI/UIEvents.h"
  34. namespace Urho3D
  35. {
  36. extern const char* UI_CATEGORY;
  37. View3D::View3D(Context* context) :
  38. Window(context),
  39. ownScene_(true),
  40. rttFormat_(Graphics::GetRGBFormat()),
  41. autoUpdate_(true)
  42. {
  43. renderTexture_ = new Texture2D(context_);
  44. depthTexture_ = new Texture2D(context_);
  45. viewport_ = new Viewport(context_);
  46. SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(View3D, HandleRenderSurfaceUpdate));
  47. }
  48. View3D::~View3D()
  49. {
  50. ResetScene();
  51. }
  52. void View3D::RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<View3D>(UI_CATEGORY);
  55. URHO3D_COPY_BASE_ATTRIBUTES(Window);
  56. // The texture format is API specific, so do not register it as a serializable attribute
  57. URHO3D_ACCESSOR_ATTRIBUTE("Auto Update", GetAutoUpdate, SetAutoUpdate, bool, true, AM_FILE);
  58. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Clip Children", true);
  59. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  60. }
  61. void View3D::OnResize()
  62. {
  63. int width = GetWidth();
  64. int height = GetHeight();
  65. if (width > 0 && height > 0)
  66. {
  67. renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET);
  68. depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  69. RenderSurface* surface = renderTexture_->GetRenderSurface();
  70. surface->SetViewport(0, viewport_);
  71. surface->SetUpdateMode(SURFACE_MANUALUPDATE);
  72. surface->SetLinkedDepthStencil(depthTexture_->GetRenderSurface());
  73. SetTexture(renderTexture_);
  74. SetImageRect(IntRect(0, 0, width, height));
  75. if (!autoUpdate_)
  76. surface->QueueUpdate();
  77. }
  78. }
  79. void View3D::SetView(Scene* scene, Camera* camera, bool ownScene)
  80. {
  81. ResetScene();
  82. scene_ = scene;
  83. cameraNode_ = camera ? camera->GetNode() : 0;
  84. ownScene_ = ownScene;
  85. viewport_->SetScene(scene_);
  86. viewport_->SetCamera(camera);
  87. QueueUpdate();
  88. }
  89. void View3D::SetFormat(unsigned format)
  90. {
  91. if (format != rttFormat_)
  92. {
  93. rttFormat_ = format;
  94. OnResize();
  95. }
  96. }
  97. void View3D::SetAutoUpdate(bool enable)
  98. {
  99. autoUpdate_ = enable;
  100. }
  101. void View3D::QueueUpdate()
  102. {
  103. RenderSurface* surface = renderTexture_->GetRenderSurface();
  104. if (surface)
  105. surface->QueueUpdate();
  106. }
  107. Scene* View3D::GetScene() const
  108. {
  109. return scene_;
  110. }
  111. Node* View3D::GetCameraNode() const
  112. {
  113. return cameraNode_;
  114. }
  115. Texture2D* View3D::GetRenderTexture() const
  116. {
  117. return renderTexture_;
  118. }
  119. Texture2D* View3D::GetDepthTexture() const
  120. {
  121. return depthTexture_;
  122. }
  123. Viewport* View3D::GetViewport() const
  124. {
  125. return viewport_;
  126. }
  127. void View3D::ResetScene()
  128. {
  129. if (!scene_)
  130. return;
  131. if (!ownScene_)
  132. {
  133. RefCount* refCount = scene_->RefCountPtr();
  134. ++refCount->refs_;
  135. scene_ = 0;
  136. --refCount->refs_;
  137. }
  138. else
  139. scene_ = 0;
  140. }
  141. void View3D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  142. {
  143. if (autoUpdate_ && IsVisibleEffective())
  144. QueueUpdate();
  145. }
  146. }