Viewport.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "Camera.h"
  24. #include "Graphics.h"
  25. #include "Log.h"
  26. #include "Renderer.h"
  27. #include "RenderPath.h"
  28. #include "ResourceCache.h"
  29. #include "Scene.h"
  30. #include "View.h"
  31. #include "Viewport.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. Viewport::Viewport(Context* context) :
  37. Object(context),
  38. rect_(IntRect::ZERO),
  39. drawDebug_(true)
  40. {
  41. SetRenderPath((RenderPath*)0);
  42. }
  43. Viewport::Viewport(Context* context, Scene* scene, Camera* camera, RenderPath* renderPath) :
  44. Object(context),
  45. scene_(scene),
  46. camera_(camera),
  47. rect_(IntRect::ZERO),
  48. drawDebug_(true)
  49. {
  50. SetRenderPath(renderPath);
  51. }
  52. Viewport::Viewport(Context* context, Scene* scene, Camera* camera, const IntRect& rect, RenderPath* renderPath) :
  53. Object(context),
  54. scene_(scene),
  55. camera_(camera),
  56. rect_(rect),
  57. drawDebug_(true)
  58. {
  59. SetRenderPath(renderPath);
  60. }
  61. Viewport::~Viewport()
  62. {
  63. }
  64. void Viewport::SetScene(Scene* scene)
  65. {
  66. scene_ = scene;
  67. }
  68. void Viewport::SetCamera(Camera* camera)
  69. {
  70. camera_ = 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. Renderer* renderer = GetSubsystem<Renderer>();
  87. if (renderer)
  88. renderPath_ = renderer->GetDefaultRenderPath();
  89. }
  90. }
  91. void Viewport::SetRenderPath(XMLFile* file)
  92. {
  93. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  94. if (newRenderPath->Load(file))
  95. renderPath_ = newRenderPath;
  96. }
  97. Scene* Viewport::GetScene() const
  98. {
  99. return scene_;
  100. }
  101. Camera* Viewport::GetCamera() const
  102. {
  103. return camera_;
  104. }
  105. View* Viewport::GetView() const
  106. {
  107. return view_;
  108. }
  109. RenderPath* Viewport::GetRenderPath() const
  110. {
  111. return renderPath_;
  112. }
  113. Ray Viewport::GetScreenRay(int x, int y) const
  114. {
  115. if (!camera_)
  116. return Ray();
  117. float screenX;
  118. float screenY;
  119. if (rect_ == IntRect::ZERO)
  120. {
  121. Graphics* graphics = GetSubsystem<Graphics>();
  122. screenX = (float)x / (float)graphics->GetWidth();
  123. screenY = (float)y / (float)graphics->GetHeight();
  124. }
  125. else
  126. {
  127. screenX = float(x - rect_.left_) / (float)rect_.Width();
  128. screenY = float(y - rect_.top_) / (float)rect_.Height();
  129. }
  130. return camera_->GetScreenRay(screenX, screenY);
  131. }
  132. IntVector2 Viewport::WorldToScreenPoint(const Vector3& worldPos) const
  133. {
  134. if (!camera_)
  135. return IntVector2::ZERO;
  136. Vector2 screenPoint = camera_->WorldToScreenPoint(worldPos);
  137. int x;
  138. int y;
  139. if (rect_ == IntRect::ZERO)
  140. {
  141. Graphics* graphics = GetSubsystem<Graphics>();
  142. x = (int)(screenPoint.x_ * graphics->GetWidth());
  143. y = (int)(screenPoint.y_ * graphics->GetHeight());
  144. }
  145. else
  146. {
  147. x = (int)(rect_.left_ + screenPoint.x_ * rect_.Width());
  148. y = (int)(rect_.top_ + screenPoint.y_ * rect_.Height());
  149. }
  150. return IntVector2(x, y);
  151. }
  152. Vector3 Viewport::ScreenToWorldPoint(int x, int y, float depth) const
  153. {
  154. if (!camera_)
  155. return Vector3::ZERO;
  156. float screenX;
  157. float screenY;
  158. if (rect_ == IntRect::ZERO)
  159. {
  160. Graphics* graphics = GetSubsystem<Graphics>();
  161. screenX = (float)x / (float)graphics->GetWidth();
  162. screenY = (float)y / (float)graphics->GetHeight();
  163. }
  164. else
  165. {
  166. screenX = float(x - rect_.left_) / (float)rect_.Width();
  167. screenY = float(y - rect_.top_) / (float)rect_.Height();
  168. }
  169. return camera_->ScreenToWorldPoint(Vector3(screenX, screenY, depth));
  170. }
  171. void Viewport::AllocateView()
  172. {
  173. view_ = new View(context_);
  174. }
  175. }