View3D.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "Precompiled.h"
  2. #include "Context.h"
  3. #include "Camera.h"
  4. #include "Graphics.h"
  5. #include "Octree.h"
  6. #include "Scene.h"
  7. #include "Texture2D.h"
  8. #include "Viewport.h"
  9. #include "View3D.h"
  10. #include "UI.h"
  11. #include "UIEvents.h"
  12. #include "Zone.h"
  13. namespace Urho3D
  14. {
  15. extern const char* UI_CATEGORY;
  16. View3D::View3D(Context* context) :
  17. Window(context),
  18. rttFormat_(Graphics::GetRGBFormat()),
  19. autoUpdate_(true)
  20. {
  21. renderTexture_ = new Texture2D(context_);
  22. viewport_ = new Viewport(context_);
  23. }
  24. View3D::~View3D()
  25. {
  26. }
  27. void View3D::RegisterObject(Context* context)
  28. {
  29. context->RegisterFactory<View3D>(UI_CATEGORY);
  30. COPY_BASE_ATTRIBUTES(View3D, Window);
  31. // The texture format is API specific, so do not register it as a serializable attribute
  32. ACCESSOR_ATTRIBUTE(View3D, VAR_BOOL, "Auto Update", GetAutoUpdate, SetAutoUpdate, bool, true, AM_FILE);
  33. UPDATE_ATTRIBUTE_DEFAULT_VALUE(View3D, "Clip Children", true);
  34. UPDATE_ATTRIBUTE_DEFAULT_VALUE(View3D, "Is Enabled", true);
  35. }
  36. void View3D::OnResize()
  37. {
  38. int width = GetWidth();
  39. int height = GetHeight();
  40. if (width > 0 && height >> 0)
  41. {
  42. renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET);
  43. RenderSurface* surface = renderTexture_->GetRenderSurface();
  44. surface->SetViewport(0, viewport_);
  45. surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE);
  46. SetTexture(renderTexture_);
  47. SetImageRect(IntRect(0, 0, width, height));
  48. if (!autoUpdate_)
  49. surface->QueueUpdate();
  50. }
  51. }
  52. void View3D::SetView(Scene* scene, Camera* camera)
  53. {
  54. scene_ = scene;
  55. cameraNode_ = camera ? camera->GetNode() : 0;
  56. viewport_->SetScene(scene_);
  57. viewport_->SetCamera(camera);
  58. }
  59. void View3D::SetFormat(unsigned format)
  60. {
  61. if (format != rttFormat_)
  62. {
  63. rttFormat_ = format;
  64. OnResize();
  65. }
  66. }
  67. void View3D::SetAutoUpdate(bool enable)
  68. {
  69. if (enable != autoUpdate_)
  70. {
  71. autoUpdate_ = enable;
  72. RenderSurface* surface = renderTexture_->GetRenderSurface();
  73. if (surface)
  74. surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE);
  75. }
  76. }
  77. void View3D::QueueUpdate()
  78. {
  79. if (!autoUpdate_)
  80. {
  81. RenderSurface* surface = renderTexture_->GetRenderSurface();
  82. if (surface)
  83. surface->QueueUpdate();
  84. }
  85. }
  86. Scene* View3D::GetScene() const
  87. {
  88. return scene_;
  89. }
  90. Node* View3D::GetCameraNode() const
  91. {
  92. return cameraNode_;
  93. }
  94. Texture2D* View3D::GetRenderTexture() const
  95. {
  96. return renderTexture_;
  97. }
  98. Viewport* View3D::GetViewport() const
  99. {
  100. return viewport_;
  101. }
  102. }