guiTSControl.cpp 31 KB

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