Viewport.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. {
  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. {
  47. SetRenderPath(renderPath);
  48. }
  49. Viewport::Viewport(Context* context, Scene* scene, Camera* camera, const IntRect& rect, RenderPath* renderPath) :
  50. Object(context),
  51. scene_(scene),
  52. camera_(camera),
  53. rect_(rect)
  54. {
  55. SetRenderPath(renderPath);
  56. }
  57. Viewport::~Viewport()
  58. {
  59. }
  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::SetRect(const IntRect& rect)
  69. {
  70. rect_ = rect;
  71. }
  72. void Viewport::SetRenderPath(RenderPath* renderPath)
  73. {
  74. if (renderPath)
  75. renderPath_ = renderPath;
  76. else
  77. {
  78. Renderer* renderer = GetSubsystem<Renderer>();
  79. if (renderer)
  80. renderPath_ = renderer->GetDefaultRenderPath();
  81. }
  82. }
  83. void Viewport::SetRenderPath(XMLFile* file)
  84. {
  85. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  86. if (newRenderPath->Load(file))
  87. renderPath_ = newRenderPath;
  88. }
  89. Scene* Viewport::GetScene() const
  90. {
  91. return scene_;
  92. }
  93. Camera* Viewport::GetCamera() const
  94. {
  95. return camera_;
  96. }
  97. RenderPath* Viewport::GetRenderPath() const
  98. {
  99. return renderPath_;
  100. }
  101. Ray Viewport::GetScreenRay(int x, int y) const
  102. {
  103. if (!camera_)
  104. return Ray();
  105. float screenX;
  106. float screenY;
  107. if (rect_ == IntRect::ZERO)
  108. {
  109. Graphics* graphics = GetSubsystem<Graphics>();
  110. screenX = (float)x / (float)graphics->GetWidth();
  111. screenY = (float)y / (float)graphics->GetHeight();
  112. }
  113. else
  114. {
  115. screenX = float(x - rect_.left_) / (float)rect_.Width();
  116. screenY = float(y - rect_.top_) / (float)rect_.Height();
  117. }
  118. return camera_->GetScreenRay(screenX, screenY);
  119. }
  120. IntVector2 Viewport::WorldToScreenPoint(const Vector3& worldPos) const
  121. {
  122. if (!camera_)
  123. return IntVector2::ZERO;
  124. Vector2 screenPoint = camera_->WorldToScreenPoint(worldPos);
  125. int x;
  126. int y;
  127. if (rect_ == IntRect::ZERO)
  128. {
  129. Graphics* graphics = GetSubsystem<Graphics>();
  130. x = (int)(screenPoint.x_ * graphics->GetWidth());
  131. y = (int)(screenPoint.y_ * graphics->GetHeight());
  132. }
  133. else
  134. {
  135. x = (int)(rect_.left_ + screenPoint.x_ * rect_.Width());
  136. y = (int)(rect_.top_ + screenPoint.y_ * rect_.Height());
  137. }
  138. return IntVector2(x, y);
  139. }
  140. Vector3 Viewport::ScreenToWorldPoint(int x, int y, float depth) const
  141. {
  142. if (!camera_)
  143. return Vector3::ZERO;
  144. float screenX;
  145. float screenY;
  146. if (rect_ == IntRect::ZERO)
  147. {
  148. Graphics* graphics = GetSubsystem<Graphics>();
  149. screenX = (float)x / (float)graphics->GetWidth();
  150. screenY = (float)y / (float)graphics->GetHeight();
  151. }
  152. else
  153. {
  154. screenX = float(x - rect_.left_) / (float)rect_.Width();
  155. screenY = float(y - rect_.top_) / (float)rect_.Height();
  156. }
  157. return camera_->ScreenToWorldPoint(Vector3(screenX, screenY, depth));
  158. }
  159. }