2
0

BsGUIViewport.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIViewport.h"
  4. #include "BsGUIWidget.h"
  5. #include "BsGUISkin.h"
  6. #include "BsGUIDimensions.h"
  7. #include "BsCCamera.h"
  8. #include "BsViewport.h"
  9. #include "BsRenderTarget.h"
  10. #include "BsException.h"
  11. namespace BansheeEngine
  12. {
  13. const String& GUIViewport::getGUITypeName()
  14. {
  15. static String name = "Viewport";
  16. return name;
  17. }
  18. GUIViewport::GUIViewport(const String& styleName, const HCamera& camera,
  19. float aspectRatio, Degree fieldOfView, const GUIDimensions& dimensions)
  20. :GUIElement(styleName, dimensions), mCamera(camera), mAspectRatio(aspectRatio),
  21. mFieldOfView(fieldOfView)
  22. {
  23. mVerticalFOV = 2.0f * Math::atan(Math::tan(mFieldOfView.valueRadians() * 0.5f) * (1.0f / mAspectRatio));
  24. }
  25. GUIViewport::~GUIViewport()
  26. {
  27. }
  28. GUIViewport* GUIViewport::create(const HCamera& camera, float aspectRatio, Degree fieldOfView, const String& styleName)
  29. {
  30. return new (bs_alloc<GUIViewport>()) GUIViewport(getStyleName<GUIViewport>(styleName), camera, aspectRatio, fieldOfView, GUIDimensions::create());
  31. }
  32. GUIViewport* GUIViewport::create(const GUIOptions& options, const HCamera& camera,
  33. float aspectRatio, Degree fieldOfView, const String& styleName)
  34. {
  35. return new (bs_alloc<GUIViewport>()) GUIViewport(getStyleName<GUIViewport>(styleName), camera, aspectRatio, fieldOfView, GUIDimensions::create(options));
  36. }
  37. UINT32 GUIViewport::_getNumRenderElements() const
  38. {
  39. return 0;
  40. }
  41. const SpriteMaterialInfo& GUIViewport::_getMaterial(UINT32 renderElementIdx) const
  42. {
  43. BS_EXCEPT(InternalErrorException, "This element has no render element so no material can be retrieved.");
  44. static SpriteMaterialInfo dummy;
  45. return dummy;
  46. }
  47. UINT32 GUIViewport::_getNumQuads(UINT32 renderElementIdx) const
  48. {
  49. return 0;
  50. }
  51. void GUIViewport::updateClippedBounds()
  52. {
  53. mClippedBounds = mLayoutData.area;
  54. mClippedBounds.clip(mLayoutData.clipRect);
  55. }
  56. Vector2I GUIViewport::_getOptimalSize() const
  57. {
  58. return Vector2I(0, 0);
  59. }
  60. void GUIViewport::_fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  61. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  62. {
  63. }
  64. void GUIViewport::updateRenderElementsInternal()
  65. {
  66. // 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)
  67. float currentAspect = mLayoutData.area.width / (float)mLayoutData.area.height;
  68. Radian currentFOV = 2.0f * Math::atan(Math::tan(mVerticalFOV * 0.5f) * currentAspect);
  69. mCamera->setHorzFOV(currentFOV);
  70. ViewportPtr viewport = mCamera->getViewport();
  71. RenderTargetPtr renderTarget = viewport->getTarget();
  72. const RenderTargetProperties& rtProps = renderTarget->getProperties();
  73. float x = mLayoutData.area.x / (float)rtProps.getWidth();
  74. float y = mLayoutData.area.y / (float)rtProps.getHeight();
  75. float width = mLayoutData.area.width / (float)rtProps.getWidth();
  76. float height = mLayoutData.area.height / (float)rtProps.getHeight();
  77. viewport->setArea(x, y, width, height);
  78. }
  79. void GUIViewport::_changeParentWidget(GUIWidget* widget)
  80. {
  81. GUIElement::_changeParentWidget(widget);
  82. if(widget != nullptr)
  83. {
  84. RenderTargetPtr guiRenderTarget = widget->getTarget()->getTarget();
  85. RenderTargetPtr cameraRenderTarget = mCamera->getViewport()->getTarget();
  86. if(guiRenderTarget != cameraRenderTarget)
  87. BS_EXCEPT(InvalidParametersException, "Camera provided to GUIViewport must use the same render target as the GUIWidget this element is located on.")
  88. }
  89. }
  90. }