guiTSControl.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. #include "platform/platform.h"
  23. #include "gui/3d/guiTSControl.h"
  24. #include "gui/core/guiOffscreenCanvas.h"
  25. #include "console/engineAPI.h"
  26. #include "scene/sceneManager.h"
  27. #include "lighting/lightManager.h"
  28. #include "gfx/sim/debugDraw.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "gfx/screenshot.h"
  31. #include "math/mathUtils.h"
  32. #include "gui/core/guiCanvas.h"
  33. #include "scene/reflectionManager.h"
  34. #include "postFx/postEffectManager.h"
  35. #include "gfx/gfxTransformSaver.h"
  36. #include "gfx/gfxDrawUtil.h"
  37. #include "gfx/gfxDebugEvent.h"
  38. GFXTextureObject *gLastStereoTexture = NULL;
  39. #define TS_OVERLAY_SCREEN_WIDTH 0.75
  40. IMPLEMENT_CONOBJECT( GuiTSCtrl );
  41. ConsoleDocClass( GuiTSCtrl,
  42. "@brief Abstract base class for controls that render 3D scenes.\n\n"
  43. "GuiTSCtrl is the base class for controls that render 3D camera views in Torque. The class itself "
  44. "does not implement a concrete scene rendering. Use GuiObjectView to display invidiual shapes in "
  45. "the Gui and GameTSCtrl to render full scenes.\n\n"
  46. "@see GameTSCtrl\n"
  47. "@see GuiObjectView\n"
  48. "@ingroup Gui3D\n"
  49. );
  50. U32 GuiTSCtrl::smFrameCount = 0;
  51. bool GuiTSCtrl::smUseLatestDisplayTransform = true;
  52. Vector<GuiTSCtrl*> GuiTSCtrl::smAwakeTSCtrls;
  53. ImplementEnumType( GuiTSRenderStyles,
  54. "Style of rendering for a GuiTSCtrl.\n\n"
  55. "@ingroup Gui3D" )
  56. { GuiTSCtrl::RenderStyleStandard, "standard" },
  57. { GuiTSCtrl::RenderStyleStereoSideBySide, "stereo side by side" },
  58. EndImplementEnumType;
  59. //-----------------------------------------------------------------------------
  60. namespace
  61. {
  62. void _drawLine( const Point3F &p0, const Point3F &p1, const ColorI &color, F32 width )
  63. {
  64. F32 x1, x2, y1, y2, z1, z2;
  65. x1 = p0.x;
  66. y1 = p0.y;
  67. z1 = p0.z;
  68. x2 = p1.x;
  69. y2 = p1.y;
  70. z2 = p1.z;
  71. //
  72. // Convert Line a----------b
  73. //
  74. // Into Quad v0---------v1
  75. // a b
  76. // v2---------v3
  77. //
  78. Point2F start(x1, y1);
  79. Point2F end(x2, y2);
  80. Point2F perp, lineVec;
  81. // handle degenerate case where point a = b
  82. if(x1 == x2 && y1 == y2)
  83. {
  84. perp.set(0.0f, width * 0.5f);
  85. lineVec.set(0.1f, 0.0f);
  86. }
  87. else
  88. {
  89. perp.set(start.y - end.y, end.x - start.x);
  90. lineVec.set(end.x - start.x, end.y - start.y);
  91. perp.normalize(width * 0.5f);
  92. lineVec.normalize(0.1f);
  93. }
  94. start -= lineVec;
  95. end += lineVec;
  96. GFXVertexBufferHandle<GFXVertexPC> verts(GFX, 4, GFXBufferTypeVolatile);
  97. verts.lock();
  98. verts[0].point.set( start.x+perp.x, start.y+perp.y, z1 );
  99. verts[1].point.set( end.x+perp.x, end.y+perp.y, z2 );
  100. verts[2].point.set( start.x-perp.x, start.y-perp.y, z1 );
  101. verts[3].point.set( end.x-perp.x, end.y-perp.y, z2 );
  102. verts[0].color = color;
  103. verts[1].color = color;
  104. verts[2].color = color;
  105. verts[3].color = color;
  106. verts.unlock();
  107. GFX->setVertexBuffer( verts );
  108. GFXStateBlockDesc desc;
  109. desc.setCullMode(GFXCullNone);
  110. desc.setZReadWrite(false);
  111. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  112. GFX->setStateBlockByDesc( desc );
  113. GFX->setupGenericShaders();
  114. GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. GuiTSCtrl::GuiTSCtrl()
  119. {
  120. mCameraZRot = 0;
  121. mForceFOV = 0;
  122. mReflectPriority = 1.0f;
  123. mRenderStyle = RenderStyleStandard;
  124. mSaveModelview.identity();
  125. mSaveProjection.identity();
  126. mSaveViewport.set( 0, 0, 10, 10 );
  127. mSaveWorldToScreenScale.set( 0, 0 );
  128. mLastCameraQuery.cameraMatrix.identity();
  129. mLastCameraQuery.fov = 45.0f;
  130. mLastCameraQuery.object = NULL;
  131. mLastCameraQuery.farPlane = 10.0f;
  132. mLastCameraQuery.nearPlane = 0.01f;
  133. mLastCameraQuery.projectionOffset = Point2F::Zero;
  134. mLastCameraQuery.ortho = false;
  135. }
  136. //-----------------------------------------------------------------------------
  137. void GuiTSCtrl::initPersistFields()
  138. {
  139. addGroup( "Camera" );
  140. addField("cameraZRot", TypeF32, Offset(mCameraZRot, GuiTSCtrl),
  141. "Z rotation angle of camera." );
  142. addField("forceFOV", TypeF32, Offset(mForceFOV, GuiTSCtrl),
  143. "The vertical field of view in degrees or zero to use the normal camera FOV." );
  144. endGroup( "Camera" );
  145. addGroup( "Rendering" );
  146. addField( "reflectPriority", TypeF32, Offset( mReflectPriority, GuiTSCtrl ),
  147. "The share of the per-frame reflection update work this control's rendering should run.\n"
  148. "The reflect update priorities of all visible GuiTSCtrls are added together and each control is assigned "
  149. "a share of the per-frame reflection update time according to its percentage of the total priority value." );
  150. addField("renderStyle", TYPEID< RenderStyles >(), Offset(mRenderStyle, GuiTSCtrl),
  151. "Indicates how this control should render its contents." );
  152. endGroup( "Rendering" );
  153. Parent::initPersistFields();
  154. }
  155. //-----------------------------------------------------------------------------
  156. void GuiTSCtrl::consoleInit()
  157. {
  158. Con::addVariable("$TSControl::frameCount", TypeS32, &smFrameCount, "The number of frames that have been rendered since this control was created.\n"
  159. "@ingroup Rendering\n");
  160. Con::addVariable("$TSControl::useLatestDisplayTransform", TypeBool, &smUseLatestDisplayTransform, "Use the latest view transform when rendering stereo instead of the one calculated by the last move.\n"
  161. "@ingroup Rendering\n");
  162. }
  163. //-----------------------------------------------------------------------------
  164. bool GuiTSCtrl::onWake()
  165. {
  166. if ( !Parent::onWake() )
  167. return false;
  168. // Add ourselves to the active viewport list.
  169. AssertFatal( !smAwakeTSCtrls.contains( this ),
  170. "GuiTSCtrl::onWake - This control is already in the awake list!" );
  171. smAwakeTSCtrls.push_back( this );
  172. // For VR
  173. mLastCameraQuery.drawCanvas = getRoot();
  174. return true;
  175. }
  176. //-----------------------------------------------------------------------------
  177. void GuiTSCtrl::onSleep()
  178. {
  179. Parent::onSleep();
  180. AssertFatal( smAwakeTSCtrls.contains( this ),
  181. "GuiTSCtrl::onSleep - This control is not in the awake list!" );
  182. smAwakeTSCtrls.remove( this );
  183. }
  184. //-----------------------------------------------------------------------------
  185. void GuiTSCtrl::onPreRender()
  186. {
  187. setUpdate();
  188. }
  189. //-----------------------------------------------------------------------------
  190. bool GuiTSCtrl::processCameraQuery(CameraQuery *)
  191. {
  192. return false;
  193. }
  194. //-----------------------------------------------------------------------------
  195. void GuiTSCtrl::renderWorld(const RectI& /*updateRect*/)
  196. {
  197. }
  198. //-----------------------------------------------------------------------------
  199. F32 GuiTSCtrl::projectRadius( F32 dist, F32 radius ) const
  200. {
  201. // Fixup any negative or zero distance so we
  202. // don't get a divide by zero.
  203. dist = dist > 0.0f ? dist : 0.001f;
  204. return ( radius / dist ) * mSaveWorldToScreenScale.y;
  205. }
  206. //-----------------------------------------------------------------------------
  207. bool GuiTSCtrl::project( const Point3F &pt, Point3F *dest ) const
  208. {
  209. return MathUtils::mProjectWorldToScreen(pt,dest,mSaveViewport,mSaveModelview,mSaveProjection);
  210. }
  211. //-----------------------------------------------------------------------------
  212. bool GuiTSCtrl::unproject( const Point3F &pt, Point3F *dest ) const
  213. {
  214. MathUtils::mProjectScreenToWorld(pt,dest,mSaveViewport,mSaveModelview,mSaveProjection,mLastCameraQuery.farPlane,mLastCameraQuery.nearPlane);
  215. return true;
  216. }
  217. //-----------------------------------------------------------------------------
  218. F32 GuiTSCtrl::calculateViewDistance(F32 radius)
  219. {
  220. F32 fov = mLastCameraQuery.fov;
  221. F32 wwidth;
  222. F32 wheight;
  223. F32 renderWidth = (mRenderStyle == RenderStyleStereoSideBySide) ? F32(getWidth())*0.5f : F32(getWidth());
  224. F32 renderHeight = F32(getHeight());
  225. F32 aspectRatio = renderWidth / renderHeight;
  226. // Use the FOV to calculate the viewport height scale
  227. // then generate the width scale from the aspect ratio.
  228. if(!mLastCameraQuery.ortho)
  229. {
  230. wheight = mLastCameraQuery.nearPlane * mTan(mLastCameraQuery.fov / 2.0f);
  231. wwidth = aspectRatio * wheight;
  232. }
  233. else
  234. {
  235. wheight = mLastCameraQuery.fov;
  236. wwidth = aspectRatio * wheight;
  237. }
  238. // Now determine if we should use the width
  239. // fov or height fov.
  240. //
  241. // If the window is taller than it is wide, use the
  242. // width fov to keep the object completely in view.
  243. if (wheight > wwidth)
  244. fov = mAtan( wwidth / mLastCameraQuery.nearPlane ) * 2.0f;
  245. return radius / mTan(fov / 2.0f);
  246. }
  247. //-----------------------------------------------------------------------------
  248. void GuiTSCtrl::onRender(Point2I offset, const RectI &updateRect)
  249. {
  250. // Save the current transforms so we can restore
  251. // it for child control rendering below.
  252. GFXTransformSaver saver;
  253. bool renderingToTarget = false;
  254. if(!processCameraQuery(&mLastCameraQuery))
  255. {
  256. // We have no camera, but render the GUI children
  257. // anyway. This makes editing GuiTSCtrl derived
  258. // controls easier in the GuiEditor.
  259. renderChildControls( offset, updateRect );
  260. return;
  261. }
  262. GFXTargetRef origTarget = GFX->getActiveRenderTarget();
  263. // Set up the appropriate render style
  264. U32 prevRenderStyle = GFX->getCurrentRenderStyle();
  265. Point2F prevProjectionOffset = GFX->getCurrentProjectionOffset();
  266. Point2I renderSize = getExtent();
  267. if(mRenderStyle == RenderStyleStereoSideBySide)
  268. {
  269. GFX->setCurrentRenderStyle(GFXDevice::RS_StereoSideBySide);
  270. GFX->setCurrentProjectionOffset(mLastCameraQuery.projectionOffset);
  271. GFX->setStereoEyeOffsets(mLastCameraQuery.eyeOffset);
  272. GFX->setFovPort(mLastCameraQuery.fovPort); // NOTE: this specifies fov for BOTH eyes
  273. GFX->setSteroViewports(mLastCameraQuery.stereoViewports);
  274. GFX->setStereoTargets(mLastCameraQuery.stereoTargets);
  275. MatrixF myTransforms[2];
  276. if (smUseLatestDisplayTransform)
  277. {
  278. // Use the view matrix determined from the display device
  279. myTransforms[0] = mLastCameraQuery.eyeTransforms[0];
  280. myTransforms[1] = mLastCameraQuery.eyeTransforms[1];
  281. }
  282. else
  283. {
  284. // Use the view matrix determined from the control object
  285. myTransforms[0] = mLastCameraQuery.cameraMatrix;
  286. myTransforms[1] = mLastCameraQuery.cameraMatrix;
  287. QuatF qrot = mLastCameraQuery.cameraMatrix;
  288. Point3F pos = mLastCameraQuery.cameraMatrix.getPosition();
  289. Point3F rotEyePos;
  290. myTransforms[0].setPosition(pos + qrot.mulP(mLastCameraQuery.eyeOffset[0], &rotEyePos));
  291. myTransforms[1].setPosition(pos + qrot.mulP(mLastCameraQuery.eyeOffset[1], &rotEyePos));
  292. }
  293. GFX->setStereoEyeTransforms(myTransforms);
  294. // Allow render size to originate from the render target
  295. if (mLastCameraQuery.stereoTargets[0])
  296. {
  297. renderSize = mLastCameraQuery.stereoViewports[0].extent;
  298. renderingToTarget = true;
  299. }
  300. }
  301. else
  302. {
  303. GFX->setCurrentRenderStyle(GFXDevice::RS_Standard);
  304. }
  305. if ( mReflectPriority > 0 )
  306. {
  307. // Get the total reflection priority.
  308. F32 totalPriority = 0;
  309. for ( U32 i=0; i < smAwakeTSCtrls.size(); i++ )
  310. if ( smAwakeTSCtrls[i]->isVisible() )
  311. totalPriority += smAwakeTSCtrls[i]->mReflectPriority;
  312. REFLECTMGR->update( mReflectPriority / totalPriority,
  313. getExtent(),
  314. mLastCameraQuery );
  315. }
  316. if(mForceFOV != 0)
  317. mLastCameraQuery.fov = mDegToRad(mForceFOV);
  318. if(mCameraZRot)
  319. {
  320. MatrixF rotMat(EulerF(0, 0, mDegToRad(mCameraZRot)));
  321. mLastCameraQuery.cameraMatrix.mul(rotMat);
  322. }
  323. // set up the camera and viewport stuff:
  324. F32 wwidth;
  325. F32 wheight;
  326. F32 renderWidth = F32(renderSize.x);
  327. F32 renderHeight = F32(renderSize.y);
  328. F32 aspectRatio = renderWidth / renderHeight;
  329. // Use the FOV to calculate the viewport height scale
  330. // then generate the width scale from the aspect ratio.
  331. if(!mLastCameraQuery.ortho)
  332. {
  333. wheight = mLastCameraQuery.nearPlane * mTan(mLastCameraQuery.fov / 2.0f);
  334. wwidth = aspectRatio * wheight;
  335. }
  336. else
  337. {
  338. wheight = mLastCameraQuery.fov;
  339. wwidth = aspectRatio * wheight;
  340. }
  341. F32 hscale = wwidth * 2.0f / renderWidth;
  342. F32 vscale = wheight * 2.0f / renderHeight;
  343. Frustum frustum;
  344. if(mRenderStyle == RenderStyleStereoSideBySide)
  345. {
  346. // NOTE: these calculations are essentially overridden later by the fov port settings when rendering each eye.
  347. MathUtils::makeFovPortFrustum(&frustum, mLastCameraQuery.ortho, mLastCameraQuery.nearPlane, mLastCameraQuery.farPlane, mLastCameraQuery.fovPort[0]);
  348. }
  349. else
  350. {
  351. F32 left = (updateRect.point.x - offset.x) * hscale - wwidth;
  352. F32 right = (updateRect.point.x + updateRect.extent.x - offset.x) * hscale - wwidth;
  353. F32 top = wheight - vscale * (updateRect.point.y - offset.y);
  354. F32 bottom = wheight - vscale * (updateRect.point.y + updateRect.extent.y - offset.y);
  355. frustum.set( mLastCameraQuery.ortho, left, right, top, bottom, mLastCameraQuery.nearPlane, mLastCameraQuery.farPlane );
  356. }
  357. // Manipulate the frustum for tiled screenshots
  358. const bool screenShotMode = gScreenShot && gScreenShot->isPending();
  359. if ( screenShotMode )
  360. {
  361. gScreenShot->tileFrustum( frustum );
  362. GFX->setViewMatrix(MatrixF::Identity);
  363. }
  364. RectI tempRect = updateRect;
  365. if (!renderingToTarget)
  366. {
  367. #ifdef TORQUE_OS_MAC
  368. Point2I screensize = getRoot()->getWindowSize();
  369. tempRect.point.y = screensize.y - (tempRect.point.y + tempRect.extent.y);
  370. #endif
  371. GFX->setViewport( tempRect );
  372. }
  373. else
  374. {
  375. // Activate stereo RT
  376. GFX->activateStereoTarget(-1);
  377. }
  378. // Clear the zBuffer so GUI doesn't hose object rendering accidentally
  379. GFX->clear( GFXClearZBuffer , ColorI(20,20,20), 1.0f, 0 );
  380. //GFX->clear( GFXClearTarget, ColorI(255,0,0), 1.0f, 0);
  381. GFX->setFrustum( frustum );
  382. if(mLastCameraQuery.ortho)
  383. {
  384. mOrthoWidth = frustum.getWidth();
  385. mOrthoHeight = frustum.getHeight();
  386. }
  387. // We're going to be displaying this render at size of this control in
  388. // pixels - let the scene know so that it can calculate e.g. reflections
  389. // correctly for that final display result.
  390. gClientSceneGraph->setDisplayTargetResolution(renderSize);
  391. // Set the GFX world matrix to the world-to-camera transform, but don't
  392. // change the cameraMatrix in mLastCameraQuery. This is because
  393. // mLastCameraQuery.cameraMatrix is supposed to contain the camera-to-world
  394. // transform. In-place invert would save a copy but mess up any GUIs that
  395. // depend on that value.
  396. MatrixF worldToCamera = mLastCameraQuery.cameraMatrix;
  397. worldToCamera.inverse();
  398. GFX->setWorldMatrix( worldToCamera );
  399. mSaveProjection = GFX->getProjectionMatrix();
  400. mSaveModelview = GFX->getWorldMatrix();
  401. mSaveViewport = updateRect;
  402. mSaveWorldToScreenScale = GFX->getWorldToScreenScale();
  403. mSaveFrustum = GFX->getFrustum();
  404. mSaveFrustum.setTransform( mLastCameraQuery.cameraMatrix );
  405. // Set the default non-clip projection as some
  406. // objects depend on this even in non-reflect cases.
  407. gClientSceneGraph->setNonClipProjection( mSaveProjection );
  408. // Give the post effect manager the worldToCamera, and cameraToScreen matrices
  409. PFXMGR->setFrameMatrices( mSaveModelview, mSaveProjection );
  410. renderWorld(updateRect);
  411. DebugDrawer::get()->render();
  412. // Render the canvas overlay if its available
  413. if (mRenderStyle == RenderStyleStereoSideBySide && mStereoGuiTarget.getPointer())
  414. {
  415. GFXDEBUGEVENT_SCOPE( StereoGui_Render, ColorI( 255, 0, 0 ) );
  416. MatrixF proj(1);
  417. Frustum originalFrustum = GFX->getFrustum();
  418. GFXTextureObject *texObject = mStereoGuiTarget->getTexture(0);
  419. const FovPort *currentFovPort = GFX->getSteroFovPort();
  420. const MatrixF *eyeTransforms = GFX->getStereoEyeTransforms();
  421. const MatrixF *worldEyeTransforms = GFX->getInverseStereoEyeTransforms();
  422. const Point3F *eyeOffset = GFX->getStereoEyeOffsets();
  423. for (U32 i=0; i<2; i++)
  424. {
  425. GFX->activateStereoTarget(i);
  426. Frustum gfxFrustum = originalFrustum;
  427. const F32 frustumDepth = gfxFrustum.getNearDist();
  428. MathUtils::makeFovPortFrustum(&gfxFrustum, true, gfxFrustum.getNearDist(), gfxFrustum.getFarDist(), currentFovPort[i], eyeTransforms[i]);
  429. GFX->setFrustum(gfxFrustum);
  430. MatrixF eyeWorldTrans(1);
  431. eyeWorldTrans.setPosition(Point3F(eyeOffset[i].x,eyeOffset[i].y,eyeOffset[i].z));
  432. MatrixF eyeWorld(1);
  433. eyeWorld.mul(eyeWorldTrans);
  434. eyeWorld.inverse();
  435. GFX->setWorldMatrix(eyeWorld);
  436. GFX->setViewMatrix(MatrixF::Identity);
  437. if (!mStereoOverlayVB.getPointer())
  438. {
  439. mStereoOverlayVB.set(GFX, 4, GFXBufferTypeStatic);
  440. GFXVertexPCT *verts = mStereoOverlayVB.lock(0, 4);
  441. F32 texLeft = 0.0f;
  442. F32 texRight = 1.0f;
  443. F32 texTop = 1.0f;
  444. F32 texBottom = 0.0f;
  445. F32 rectRatio = gfxFrustum.getWidth() / gfxFrustum.getHeight();
  446. F32 rectWidth = gfxFrustum.getWidth() * TS_OVERLAY_SCREEN_WIDTH;
  447. F32 rectHeight = rectWidth * rectRatio;
  448. F32 screenLeft = -rectWidth * 0.5;
  449. F32 screenRight = rectWidth * 0.5;
  450. F32 screenTop = -rectHeight * 0.5;
  451. F32 screenBottom = rectHeight * 0.5;
  452. const F32 fillConv = 0.0f;
  453. const F32 frustumDepth = gfxFrustum.getNearDist() + 0.012;
  454. verts[0].point.set( screenLeft - fillConv, frustumDepth, screenTop - fillConv );
  455. verts[1].point.set( screenRight - fillConv, frustumDepth, screenTop - fillConv );
  456. verts[2].point.set( screenLeft - fillConv, frustumDepth, screenBottom - fillConv );
  457. verts[3].point.set( screenRight - fillConv, frustumDepth, screenBottom - fillConv );
  458. verts[0].color = verts[1].color = verts[2].color = verts[3].color = ColorI(255,255,255,255);
  459. verts[0].texCoord.set( texLeft, texTop );
  460. verts[1].texCoord.set( texRight, texTop );
  461. verts[2].texCoord.set( texLeft, texBottom );
  462. verts[3].texCoord.set( texRight, texBottom );
  463. mStereoOverlayVB.unlock();
  464. }
  465. if (!mStereoGuiSB.getPointer())
  466. {
  467. // DrawBitmapStretchSR
  468. GFXStateBlockDesc bitmapStretchSR;
  469. bitmapStretchSR.setCullMode(GFXCullNone);
  470. bitmapStretchSR.setZReadWrite(false, false);
  471. bitmapStretchSR.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  472. bitmapStretchSR.samplersDefined = true;
  473. bitmapStretchSR.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  474. bitmapStretchSR.samplers[0].minFilter = GFXTextureFilterPoint;
  475. bitmapStretchSR.samplers[0].mipFilter = GFXTextureFilterPoint;
  476. bitmapStretchSR.samplers[0].magFilter = GFXTextureFilterPoint;
  477. mStereoGuiSB = GFX->createStateBlock(bitmapStretchSR);
  478. }
  479. GFX->setVertexBuffer(mStereoOverlayVB);
  480. GFX->setStateBlock(mStereoGuiSB);
  481. GFX->setTexture( 0, texObject );
  482. GFX->setupGenericShaders( GFXDevice::GSModColorTexture );
  483. GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
  484. }
  485. }
  486. // Restore the previous matrix state before
  487. // we begin rendering the child controls.
  488. saver.restore();
  489. // Restore the render style and any stereo parameters
  490. GFX->setActiveRenderTarget(origTarget);
  491. GFX->setCurrentRenderStyle(prevRenderStyle);
  492. GFX->setCurrentProjectionOffset(prevProjectionOffset);
  493. if(mRenderStyle == RenderStyleStereoSideBySide && gLastStereoTexture)
  494. {
  495. GFX->setClipRect(updateRect);
  496. GFX->getDrawUtil()->drawBitmapStretch(gLastStereoTexture, updateRect);
  497. }
  498. // Allow subclasses to render 2D elements.
  499. GFX->setClipRect(updateRect);
  500. renderGui( offset, updateRect );
  501. if (shouldRenderChildControls())
  502. {
  503. renderChildControls(offset, updateRect);
  504. }
  505. smFrameCount++;
  506. }
  507. //-----------------------------------------------------------------------------
  508. void GuiTSCtrl::drawLine( Point3F p0, Point3F p1, const ColorI &color, F32 width )
  509. {
  510. if ( !mSaveFrustum.clipSegment( p0, p1 ) )
  511. return;
  512. MathUtils::mProjectWorldToScreen( p0, &p0, mSaveViewport, mSaveModelview, mSaveProjection );
  513. MathUtils::mProjectWorldToScreen( p1, &p1, mSaveViewport, mSaveModelview, mSaveProjection );
  514. p0.x = mClampF( p0.x, 0.0f, mSaveViewport.extent.x );
  515. p0.y = mClampF( p0.y, 0.0f, mSaveViewport.extent.y );
  516. p1.x = mClampF( p1.x, 0.0f, mSaveViewport.extent.x );
  517. p1.y = mClampF( p1.y, 0.0f, mSaveViewport.extent.y );
  518. p0.z = p1.z = 0.0f;
  519. _drawLine( p0, p1, color, width );
  520. }
  521. //-----------------------------------------------------------------------------
  522. void GuiTSCtrl::drawLineList( const Vector<Point3F> &points, const ColorI color, F32 width )
  523. {
  524. for ( S32 i = 0; i < points.size() - 1; i++ )
  525. drawLine( points[i], points[i+1], color, width );
  526. }
  527. void GuiTSCtrl::setStereoGui(GuiOffscreenCanvas *canvas)
  528. {
  529. mStereoGuiTarget = canvas ? canvas->getTarget() : NULL;
  530. }
  531. //=============================================================================
  532. // Console Methods.
  533. //=============================================================================
  534. // MARK: ---- Console Methods ----
  535. //-----------------------------------------------------------------------------
  536. DefineEngineMethod( GuiTSCtrl, unproject, Point3F, ( Point3F screenPosition ),,
  537. "Transform 3D screen-space coordinates (x, y, depth) to world space.\n"
  538. "This method can be, for example, used to find the world-space position relating to the current mouse cursor position.\n"
  539. "@param screenPosition The x/y position on the screen plus the depth from the screen-plane outwards.\n"
  540. "@return The world-space position corresponding to the given screen-space coordinates." )
  541. {
  542. Point3F worldPos;
  543. object->unproject( screenPosition, &worldPos );
  544. return worldPos;
  545. }
  546. //-----------------------------------------------------------------------------
  547. DefineEngineMethod( GuiTSCtrl, project, Point3F, ( Point3F worldPosition ),,
  548. "Transform world-space coordinates to screen-space (x, y, depth) coordinates.\n"
  549. "@param worldPosition The world-space position to transform to screen-space.\n"
  550. "@return The " )
  551. {
  552. Point3F screenPos;
  553. object->project( worldPosition, &screenPos );
  554. return screenPos;
  555. }
  556. //-----------------------------------------------------------------------------
  557. DefineEngineMethod( GuiTSCtrl, getWorldToScreenScale, Point2F, (),,
  558. "Get the ratio between world-space units and pixels.\n"
  559. "@return The amount of world-space units covered by the extent of a single pixel." )
  560. {
  561. return object->getWorldToScreenScale();
  562. }
  563. //-----------------------------------------------------------------------------
  564. DefineEngineMethod( GuiTSCtrl, calculateViewDistance, F32, ( F32 radius ),,
  565. "Given the camera's current FOV, get the distance from the camera's viewpoint at which the given radius will fit in the render area.\n"
  566. "@param radius Radius in world-space units which should fit in the view.\n"
  567. "@return The distance from the viewpoint at which the given radius would be fully visible." )
  568. {
  569. return object->calculateViewDistance( radius );
  570. }
  571. DefineEngineMethod( GuiTSCtrl, setStereoGui, void, ( GuiOffscreenCanvas* canvas ),,
  572. "Sets the current stereo texture to an offscreen canvas\n"
  573. "@param canvas The desired canvas." )
  574. {
  575. object->setStereoGui(canvas);
  576. }