guiTSControl.cpp 29 KB

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