BsGUIViewport.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "BsViewport.h"
  8. #include "BsRenderTarget.h"
  9. #include "BsException.h"
  10. namespace BansheeEngine
  11. {
  12. const String& GUIViewport::getGUITypeName()
  13. {
  14. static String name = "Viewport";
  15. return name;
  16. }
  17. GUIViewport::GUIViewport(const String& styleName, const HCamera& camera,
  18. float aspectRatio, Degree fieldOfView, const GUILayoutOptions& layoutOptions)
  19. :GUIElement(styleName, layoutOptions), mCamera(camera), mAspectRatio(aspectRatio),
  20. mFieldOfView(fieldOfView)
  21. {
  22. mVerticalFOV = 2.0f * Math::atan(Math::tan(mFieldOfView.valueRadians() * 0.5f) * (1.0f / mAspectRatio));
  23. }
  24. GUIViewport::~GUIViewport()
  25. {
  26. }
  27. GUIViewport* GUIViewport::create(const HCamera& camera, float aspectRatio, Degree fieldOfView, const String& styleName)
  28. {
  29. return new (bs_alloc<GUIViewport, PoolAlloc>()) GUIViewport(getStyleName<GUIViewport>(styleName), camera, aspectRatio, fieldOfView, GUILayoutOptions::create());
  30. }
  31. GUIViewport* GUIViewport::create(const GUIOptions& layoutOptions, const HCamera& camera,
  32. float aspectRatio, Degree fieldOfView, const String& styleName)
  33. {
  34. return new (bs_alloc<GUIViewport, PoolAlloc>()) GUIViewport(getStyleName<GUIViewport>(styleName), camera, aspectRatio, fieldOfView, GUILayoutOptions::create(layoutOptions));
  35. }
  36. UINT32 GUIViewport::getNumRenderElements() const
  37. {
  38. return 0;
  39. }
  40. const GUIMaterialInfo& GUIViewport::getMaterial(UINT32 renderElementIdx) const
  41. {
  42. BS_EXCEPT(InternalErrorException, "This element has no render element so no material can be retrieved.");
  43. }
  44. UINT32 GUIViewport::getNumQuads(UINT32 renderElementIdx) const
  45. {
  46. return 0;
  47. }
  48. void GUIViewport::updateClippedBounds()
  49. {
  50. Rect2I mBounds = Rect2I(0, 0, mWidth, mHeight);
  51. mBounds.clip(mClipRect);
  52. mBounds.x += mOffset.x;
  53. mBounds.y += mOffset.y;
  54. }
  55. Vector2I GUIViewport::_getOptimalSize() const
  56. {
  57. return Vector2I(0, 0);
  58. }
  59. void GUIViewport::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  60. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  61. {
  62. }
  63. void GUIViewport::updateRenderElementsInternal()
  64. {
  65. // 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)
  66. float currentAspect = mWidth / (float)mHeight;
  67. Radian currentFOV = 2.0f * Math::atan(Math::tan(mVerticalFOV * 0.5f) * currentAspect);
  68. mCamera->setHorzFOV(currentFOV);
  69. ViewportPtr viewport = mCamera->getViewport();
  70. RenderTargetPtr renderTarget = viewport->getTarget();
  71. const RenderTargetProperties& rtProps = renderTarget->getProperties();
  72. float x = mOffset.x / (float)rtProps.getWidth();
  73. float y = mOffset.y / (float)rtProps.getHeight();
  74. float width = mWidth / (float)rtProps.getWidth();
  75. float height = mHeight / (float)rtProps.getHeight();
  76. viewport->setArea(x, y, width, height);
  77. }
  78. void GUIViewport::_changeParentWidget(GUIWidget* widget)
  79. {
  80. GUIElement::_changeParentWidget(widget);
  81. if(widget != nullptr)
  82. {
  83. RenderTargetPtr guiRenderTarget = widget->getTarget()->getTarget();
  84. RenderTargetPtr cameraRenderTarget = mCamera->getViewport()->getTarget();
  85. if(guiRenderTarget != cameraRenderTarget)
  86. BS_EXCEPT(InvalidParametersException, "Camera provided to GUIViewport must use the same render target as the GUIWidget this element is located on.")
  87. }
  88. }
  89. }