Viewport.cpp 4.8 KB

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