Viewport.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "../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 Atomic
  33. {
  34. Viewport::Viewport(Context* context) :
  35. Object(context),
  36. rect_(IntRect::ZERO),
  37. drawDebug_(true)
  38. {
  39. SetRenderPath((RenderPath*)0);
  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) :
  51. Object(context),
  52. scene_(scene),
  53. camera_(camera),
  54. rect_(rect),
  55. drawDebug_(true)
  56. {
  57. SetRenderPath(renderPath);
  58. }
  59. Viewport::~Viewport()
  60. {
  61. }
  62. void Viewport::SetScene(Scene* scene)
  63. {
  64. scene_ = scene;
  65. }
  66. void Viewport::SetCamera(Camera* camera)
  67. {
  68. camera_ = camera;
  69. }
  70. void Viewport::SetRect(const IntRect& rect)
  71. {
  72. rect_ = rect;
  73. }
  74. void Viewport::SetDrawDebug(bool enable)
  75. {
  76. drawDebug_ = enable;
  77. }
  78. void Viewport::SetRenderPath(RenderPath* renderPath)
  79. {
  80. if (renderPath)
  81. renderPath_ = renderPath;
  82. else
  83. {
  84. Renderer* renderer = GetSubsystem<Renderer>();
  85. if (renderer)
  86. renderPath_ = renderer->GetDefaultRenderPath();
  87. }
  88. }
  89. void Viewport::SetRenderPath(XMLFile* file)
  90. {
  91. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  92. if (newRenderPath->Load(file))
  93. renderPath_ = newRenderPath;
  94. }
  95. Scene* Viewport::GetScene() const
  96. {
  97. return scene_;
  98. }
  99. Camera* Viewport::GetCamera() const
  100. {
  101. return camera_;
  102. }
  103. View* Viewport::GetView() const
  104. {
  105. return view_;
  106. }
  107. RenderPath* Viewport::GetRenderPath() const
  108. {
  109. return renderPath_;
  110. }
  111. Ray Viewport::GetScreenRay(int x, int y) const
  112. {
  113. if (!camera_)
  114. return Ray();
  115. float screenX;
  116. float screenY;
  117. if (rect_ == IntRect::ZERO)
  118. {
  119. Graphics* graphics = GetSubsystem<Graphics>();
  120. screenX = (float)x / (float)graphics->GetWidth();
  121. screenY = (float)y / (float)graphics->GetHeight();
  122. }
  123. else
  124. {
  125. screenX = float(x - rect_.left_) / (float)rect_.Width();
  126. screenY = float(y - rect_.top_) / (float)rect_.Height();
  127. }
  128. return camera_->GetScreenRay(screenX, screenY);
  129. }
  130. IntVector2 Viewport::WorldToScreenPoint(const Vector3& worldPos) const
  131. {
  132. if (!camera_)
  133. return IntVector2::ZERO;
  134. Vector2 screenPoint = camera_->WorldToScreenPoint(worldPos);
  135. int x;
  136. int y;
  137. if (rect_ == IntRect::ZERO)
  138. {
  139. Graphics* graphics = GetSubsystem<Graphics>();
  140. x = (int)(screenPoint.x_ * graphics->GetWidth());
  141. y = (int)(screenPoint.y_ * graphics->GetHeight());
  142. }
  143. else
  144. {
  145. x = (int)(rect_.left_ + screenPoint.x_ * rect_.Width());
  146. y = (int)(rect_.top_ + screenPoint.y_ * rect_.Height());
  147. }
  148. return IntVector2(x, y);
  149. }
  150. Vector3 Viewport::ScreenToWorldPoint(int x, int y, float depth) const
  151. {
  152. if (!camera_)
  153. return Vector3::ZERO;
  154. float screenX;
  155. float screenY;
  156. if (rect_ == IntRect::ZERO)
  157. {
  158. Graphics* graphics = GetSubsystem<Graphics>();
  159. screenX = (float)x / (float)graphics->GetWidth();
  160. screenY = (float)y / (float)graphics->GetHeight();
  161. }
  162. else
  163. {
  164. screenX = float(x - rect_.left_) / (float)rect_.Width();
  165. screenY = float(y - rect_.top_) / (float)rect_.Height();
  166. }
  167. return camera_->ScreenToWorldPoint(Vector3(screenX, screenY, depth));
  168. }
  169. void Viewport::AllocateView()
  170. {
  171. view_ = new View(context_);
  172. }
  173. }