Viewport.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Copyright (c) 2008-2020 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 "../Graphics/Camera.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../Graphics/Renderer.h"
  26. #include "../Graphics/RenderPath.h"
  27. #include "../Graphics/View.h"
  28. #include "../Resource/ResourceCache.h"
  29. #include "../Resource/XMLFile.h"
  30. #include "../Scene/Scene.h"
  31. #include "../DebugNew.h"
  32. namespace Urho3D
  33. {
  34. Viewport::Viewport(Context* context) :
  35. Object(context),
  36. rect_(IntRect::ZERO),
  37. drawDebug_(true)
  38. {
  39. SetRenderPath((RenderPath*)nullptr);
  40. }
  41. Viewport::Viewport(Context* context, Scene* scene, Camera* camera, RenderPath* renderPath) :
  42. Object(context),
  43. scene_(scene),
  44. camera_(camera),
  45. rect_(IntRect::ZERO),
  46. drawDebug_(true)
  47. {
  48. SetRenderPath(renderPath);
  49. }
  50. Viewport::Viewport(Context* context, Scene* scene, Camera* camera, const IntRect& rect, RenderPath* renderPath) : // NOLINT(modernize-pass-by-value)
  51. Object(context),
  52. scene_(scene),
  53. camera_(camera),
  54. rect_(rect),
  55. drawDebug_(true)
  56. {
  57. SetRenderPath(renderPath);
  58. }
  59. Viewport::~Viewport() = default;
  60. void Viewport::SetScene(Scene* scene)
  61. {
  62. scene_ = scene;
  63. }
  64. void Viewport::SetCamera(Camera* camera)
  65. {
  66. camera_ = camera;
  67. }
  68. void Viewport::SetCullCamera(Camera* camera)
  69. {
  70. cullCamera_ = camera;
  71. }
  72. void Viewport::SetRect(const IntRect& rect)
  73. {
  74. rect_ = rect;
  75. }
  76. void Viewport::SetDrawDebug(bool enable)
  77. {
  78. drawDebug_ = enable;
  79. }
  80. void Viewport::SetRenderPath(RenderPath* renderPath)
  81. {
  82. if (renderPath)
  83. renderPath_ = renderPath;
  84. else
  85. {
  86. auto* renderer = GetSubsystem<Renderer>();
  87. if (renderer)
  88. renderPath_ = renderer->GetDefaultRenderPath();
  89. }
  90. }
  91. bool Viewport::SetRenderPath(XMLFile* file)
  92. {
  93. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  94. if (newRenderPath->Load(file))
  95. {
  96. renderPath_ = newRenderPath;
  97. return true;
  98. }
  99. return false;
  100. }
  101. Scene* Viewport::GetScene() const
  102. {
  103. return scene_;
  104. }
  105. Camera* Viewport::GetCamera() const
  106. {
  107. return camera_;
  108. }
  109. Camera* Viewport::GetCullCamera() const
  110. {
  111. return cullCamera_;
  112. }
  113. View* Viewport::GetView() const
  114. {
  115. return view_;
  116. }
  117. RenderPath* Viewport::GetRenderPath() const
  118. {
  119. return renderPath_;
  120. }
  121. Ray Viewport::GetScreenRay(int x, int y) const
  122. {
  123. if (!camera_)
  124. return Ray();
  125. float screenX;
  126. float screenY;
  127. if (rect_ == IntRect::ZERO)
  128. {
  129. auto* graphics = GetSubsystem<Graphics>();
  130. screenX = (float)x / (float)graphics->GetWidth();
  131. screenY = (float)y / (float)graphics->GetHeight();
  132. }
  133. else
  134. {
  135. screenX = float(x - rect_.left_) / (float)rect_.Width();
  136. screenY = float(y - rect_.top_) / (float)rect_.Height();
  137. }
  138. return camera_->GetScreenRay(screenX, screenY);
  139. }
  140. IntVector2 Viewport::WorldToScreenPoint(const Vector3& worldPos) const
  141. {
  142. if (!camera_)
  143. return IntVector2::ZERO;
  144. Vector2 screenPoint = camera_->WorldToScreenPoint(worldPos);
  145. int x;
  146. int y;
  147. if (rect_ == IntRect::ZERO)
  148. {
  149. /// \todo This is incorrect if the viewport is used on a texture rendertarget instead of the backbuffer, as it may have different dimensions.
  150. auto* graphics = GetSubsystem<Graphics>();
  151. x = (int)(screenPoint.x_ * graphics->GetWidth());
  152. y = (int)(screenPoint.y_ * graphics->GetHeight());
  153. }
  154. else
  155. {
  156. x = (int)(rect_.left_ + screenPoint.x_ * rect_.Width());
  157. y = (int)(rect_.top_ + screenPoint.y_ * rect_.Height());
  158. }
  159. return IntVector2(x, y);
  160. }
  161. Vector3 Viewport::ScreenToWorldPoint(int x, int y, float depth) const
  162. {
  163. if (!camera_)
  164. return Vector3::ZERO;
  165. float screenX;
  166. float screenY;
  167. if (rect_ == IntRect::ZERO)
  168. {
  169. /// \todo This is incorrect if the viewport is used on a texture rendertarget instead of the backbuffer, as it may have different dimensions.
  170. auto* graphics = GetSubsystem<Graphics>();
  171. screenX = (float)x / (float)graphics->GetWidth();
  172. screenY = (float)y / (float)graphics->GetHeight();
  173. }
  174. else
  175. {
  176. screenX = float(x - rect_.left_) / (float)rect_.Width();
  177. screenY = float(y - rect_.top_) / (float)rect_.Height();
  178. }
  179. return camera_->ScreenToWorldPoint(Vector3(screenX, screenY, depth));
  180. }
  181. void Viewport::AllocateView()
  182. {
  183. view_ = new View(context_);
  184. }
  185. }