BsGUIViewport.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "BsGUIViewport.h"
  2. #include "BsGUIWidget.h"
  3. #include "BsGUISkin.h"
  4. #include "BsSpriteTexture.h"
  5. #include "BsGUILayoutOptions.h"
  6. #include "BsCamera.h"
  7. #include "CmViewport.h"
  8. #include "CmRenderTarget.h"
  9. #include "CmException.h"
  10. using namespace CamelotFramework;
  11. namespace BansheeEngine
  12. {
  13. const String& GUIViewport::getGUITypeName()
  14. {
  15. static String name = "Viewport";
  16. return name;
  17. }
  18. GUIViewport::GUIViewport(GUIWidget& parent, const GUIElementStyle* style, const HCamera& camera,
  19. float aspectRatio, CM::Degree fieldOfView, const GUILayoutOptions& layoutOptions)
  20. :GUIElement(parent, style, layoutOptions), mCamera(camera), mAspectRatio(aspectRatio),
  21. mFieldOfView(fieldOfView)
  22. {
  23. RenderTargetPtr guiRenderTarget = parent.getTarget()->getTarget();
  24. RenderTargetPtr cameraRenderTarget = camera->getViewport()->getTarget();
  25. if(guiRenderTarget != cameraRenderTarget)
  26. CM_EXCEPT(InvalidParametersException, "Camera provided to GUIViewport must use the same render target as the GUIWidget this element is located on.")
  27. mVerticalFOV = 2.0f * Math::atan(Math::tan(mFieldOfView.valueRadians() * 0.5f) * (1.0f / mAspectRatio));
  28. }
  29. GUIViewport::~GUIViewport()
  30. {
  31. }
  32. GUIViewport* GUIViewport::create(GUIWidget& parent, const HCamera& camera, float aspectRatio, CM::Degree fieldOfView, const GUIElementStyle* style)
  33. {
  34. if(style == nullptr)
  35. {
  36. const GUISkin& skin = parent.getSkin();
  37. style = skin.getStyle(getGUITypeName());
  38. }
  39. return new (cm_alloc<GUIViewport, PoolAlloc>()) GUIViewport(parent, style, camera, aspectRatio, fieldOfView, GUILayoutOptions::create(style));
  40. }
  41. GUIViewport* GUIViewport::create(GUIWidget& parent, const GUIOptions& layoutOptions, const HCamera& camera,
  42. float aspectRatio, CM::Degree fieldOfView, const GUIElementStyle* style)
  43. {
  44. if(style == nullptr)
  45. {
  46. const GUISkin& skin = parent.getSkin();
  47. style = skin.getStyle(getGUITypeName());
  48. }
  49. return new (cm_alloc<GUIViewport, PoolAlloc>()) GUIViewport(parent, style, camera, aspectRatio, fieldOfView, GUILayoutOptions::create(layoutOptions, style));
  50. }
  51. UINT32 GUIViewport::getNumRenderElements() const
  52. {
  53. return 0;
  54. }
  55. const GUIMaterialInfo& GUIViewport::getMaterial(UINT32 renderElementIdx) const
  56. {
  57. CM_EXCEPT(InternalErrorException, "This element has no render element so no material can be retrieved.");
  58. }
  59. UINT32 GUIViewport::getNumQuads(UINT32 renderElementIdx) const
  60. {
  61. return 0;
  62. }
  63. void GUIViewport::updateClippedBounds()
  64. {
  65. RectI mBounds = RectI(0, 0, mWidth, mHeight);
  66. mBounds.clip(mClipRect);
  67. mBounds.x += mOffset.x;
  68. mBounds.y += mOffset.y;
  69. }
  70. Vector2I GUIViewport::_getOptimalSize() const
  71. {
  72. return Vector2I(0, 0);
  73. }
  74. void GUIViewport::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  75. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  76. {
  77. }
  78. void GUIViewport::updateRenderElementsInternal()
  79. {
  80. // TODO - This doesn't get called if element mesh is dirty!!! and I need to update the viewport when offset changes (in which case mesh is marked as dirty)
  81. float currentAspect = mWidth / (float)mHeight;
  82. Radian currentFOV = 2.0f * Math::atan(Math::tan(mVerticalFOV * 0.5f) * currentAspect);
  83. mCamera->setHorzFOV(currentFOV);
  84. ViewportPtr viewport = mCamera->getViewport();
  85. RenderTargetPtr renderTarget = viewport->getTarget();
  86. float x = mOffset.x / (float)renderTarget->getWidth();
  87. float y = mOffset.y / (float)renderTarget->getHeight();
  88. float width = mWidth / (float)renderTarget->getWidth();
  89. float height = mHeight / (float)renderTarget->getHeight();
  90. viewport->setArea(x, y, width, height);
  91. }
  92. }