View3D.cpp 4.6 KB

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