2
0

View3D.cpp 4.1 KB

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