guiTSControl.h 6.8 KB

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