guiTSControl.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GUITSCONTROL_H_
  23. #define _GUITSCONTROL_H_
  24. #ifndef _GUICONTAINER_H_
  25. #include "gui/containers/guiContainer.h"
  26. #endif
  27. #ifndef _MMATH_H_
  28. #include "math/mMath.h"
  29. #endif
  30. #ifndef _MATTEXTURETARGET_H_
  31. #include "materials/matTextureTarget.h"
  32. #endif
  33. class IDisplayDevice;
  34. class GuiOffscreenCanvas;
  35. struct CameraQuery
  36. {
  37. SimObject* object;
  38. F32 nearPlane;
  39. F32 farPlane;
  40. F32 fov;
  41. FovPort fovPort[2]; // fov for each eye
  42. Point2F projectionOffset;
  43. Point3F eyeOffset[2];
  44. MatrixF eyeTransforms[2];
  45. bool ortho;
  46. bool hasFovPort;
  47. bool hasStereoTargets;
  48. MatrixF cameraMatrix;
  49. RectI stereoViewports[2]; // destination viewports
  50. GFXTextureTarget* stereoTargets[2];
  51. GuiCanvas* drawCanvas; // Canvas we are drawing to. Needed for VR
  52. };
  53. /// Abstract base class for 3D viewport GUIs.
  54. class GuiTSCtrl : public GuiContainer
  55. {
  56. typedef GuiContainer Parent;
  57. public:
  58. enum RenderStyles {
  59. RenderStyleStandard = 0,
  60. RenderStyleStereoSideBySide = (1<<0)
  61. };
  62. protected:
  63. static U32 smFrameCount;
  64. static bool smUseLatestDisplayTransform;
  65. F32 mCameraZRot;
  66. F32 mForceFOV;
  67. /// A list of GuiTSCtrl which are awake and
  68. /// most likely rendering.
  69. static Vector<GuiTSCtrl*> smAwakeTSCtrls;
  70. /// A scalar which controls how much of the reflection
  71. /// update timeslice for this viewport to get.
  72. F32 mReflectPriority;
  73. /// The current render type
  74. U32 mRenderStyle;
  75. F32 mOrthoWidth;
  76. F32 mOrthoHeight;
  77. MatrixF mSaveModelview;
  78. MatrixF mSaveProjection;
  79. RectI mSaveViewport;
  80. Frustum mSaveFrustum;
  81. /// The saved world to screen space scale.
  82. /// @see getWorldToScreenScale
  83. Point2F mSaveWorldToScreenScale;
  84. /// The last camera query set in onRender.
  85. /// @see getLastCameraQuery
  86. CameraQuery mLastCameraQuery;
  87. NamedTexTargetRef mStereoGuiTarget;
  88. GFXVertexBufferHandle<GFXVertexPCT> mStereoOverlayVB;
  89. GFXStateBlockRef mStereoGuiSB;
  90. public:
  91. GuiTSCtrl();
  92. void onPreRender();
  93. void onRender(Point2I offset, const RectI &updateRect);
  94. virtual bool processCameraQuery(CameraQuery *query);
  95. /// Subclasses can override this to perform 3D rendering.
  96. virtual void renderWorld(const RectI &updateRect);
  97. /// Subclasses can override this to perform 2D rendering.
  98. virtual void renderGui(Point2I offset, const RectI &updateRect) {}
  99. static void initPersistFields();
  100. static void consoleInit();
  101. virtual bool onWake();
  102. virtual void onSleep();
  103. /// Returns the last World Matrix set in onRender.
  104. const MatrixF& getLastWorldMatrix() const { return mSaveModelview; }
  105. /// Returns the last Projection Matrix set in onRender.
  106. const MatrixF& getLastProjectionMatrix() const { return mSaveProjection; }
  107. /// Returns the last Viewport Rect set in onRender.
  108. const RectI& getLastViewportRect() const { return mSaveViewport; }
  109. /// Returns the last Frustum set in onRender.
  110. const Frustum& getLastFrustum() const { return mSaveFrustum; }
  111. /// Returns the scale for converting world space
  112. /// units to screen space units... aka pixels.
  113. /// @see GFXDevice::getWorldToScreenScale
  114. const Point2F& getWorldToScreenScale() const { return mSaveWorldToScreenScale; }
  115. /// Returns the last camera query set in onRender.
  116. const CameraQuery& getLastCameraQuery() const { return mLastCameraQuery; }
  117. /// Returns the screen space X,Y and Z for world space point.
  118. /// The input z coord is depth, from 0 to 1.
  119. bool project( const Point3F &pt, Point3F *dest ) const;
  120. /// Returns the world space point for X, Y and Z. The ouput
  121. /// z coord is depth, from 0 to 1
  122. bool unproject( const Point3F &pt, Point3F *dest ) const;
  123. ///
  124. F32 projectRadius( F32 dist, F32 radius ) const;
  125. /// Returns the distance required to fit the given
  126. /// radius within the camera's view.
  127. F32 calculateViewDistance(F32 radius);
  128. /// Takes Points in World Space representing a Line or LineList.
  129. /// These will be projected into screen space and rendered with the requested
  130. /// width in pixels.
  131. ///
  132. /// This is a 2D drawing operation and should not be called from within
  133. /// renderScene without preparing the GFX for 2D rendering first.
  134. ///
  135. /// These methods are NOT optimized for performance in any way and are only
  136. /// intended for debug rendering, editor rendering, or infrequent rendering.
  137. ///
  138. void drawLine( Point3F p0, Point3F p1, const ColorI &color, F32 width );
  139. void drawLineList( const Vector<Point3F> &points, const ColorI color, F32 width );
  140. static const U32& getFrameCount() { return smFrameCount; }
  141. bool shouldRenderChildControls() { return mRenderStyle == RenderStyleStandard; }
  142. void setStereoGui(GuiOffscreenCanvas *canvas);
  143. DECLARE_CONOBJECT(GuiTSCtrl);
  144. DECLARE_CATEGORY( "Gui 3D" );
  145. DECLARE_DESCRIPTION( "Abstract base class for controls that render a 3D viewport." );
  146. };
  147. typedef GuiTSCtrl::RenderStyles GuiTSRenderStyles;
  148. DefineEnumType( GuiTSRenderStyles );
  149. #endif // _GUITSCONTROL_H_