guiMaterialPreview.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. // Original header:
  23. // GuiMaterialPreview Control for Material Editor Written by Travis Vroman of Gaslight Studios
  24. // Updated 2-14-09
  25. // Portions based off Constructor viewport code.
  26. #include "console/engineAPI.h"
  27. #include "T3D/guiMaterialPreview.h"
  28. #include "renderInstance/renderPassManager.h"
  29. #include "lighting/lightManager.h"
  30. #include "lighting/lightInfo.h"
  31. #include "core/resourceManager.h"
  32. #include "scene/sceneManager.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "renderInstance/renderProbeMgr.h"
  35. #include "T3D/lighting/skylight.h"
  36. #include "gfx/gfxDrawUtil.h"
  37. // GuiMaterialPreview
  38. GuiMaterialPreview::GuiMaterialPreview()
  39. : mMouseState(None),
  40. mModel(NULL),
  41. runThread(0),
  42. lastRenderTime(0),
  43. mLastMousePoint(0, 0),
  44. mFakeSun(NULL),
  45. mMaxOrbitDist(5.0f),
  46. mMinOrbitDist(0.0f),
  47. mOrbitDist(5.0f)
  48. {
  49. mActive = true;
  50. mCameraMatrix.identity();
  51. mCameraRot.set( mDegToRad(30.0f), 0, mDegToRad(-30.0f) );
  52. mCameraPos.set(0.0f, 1.75f, 1.25f);
  53. mCameraMatrix.setColumn(3, mCameraPos);
  54. mOrbitPos.set(0.0f, 0.0f, 0.0f);
  55. mTransStep = 0.01f;
  56. mTranMult = 4.0;
  57. mLightTransStep = 0.01f;
  58. mLightTranMult = 4.0;
  59. mOrbitRelPos = Point3F(0,0,0);
  60. // By default don't do dynamic reflection
  61. // updates for this viewport.
  62. mReflectPriority = 0.0f;
  63. mMountedModel = NULL;
  64. mSkinTag = 0;
  65. }
  66. GuiMaterialPreview::~GuiMaterialPreview()
  67. {
  68. SAFE_DELETE(mModel);
  69. SAFE_DELETE(mFakeSun);
  70. }
  71. bool GuiMaterialPreview::onWake()
  72. {
  73. if( !Parent::onWake() )
  74. return false;
  75. if (!mFakeSun)
  76. mFakeSun = LightManager::createLightInfo();
  77. mFakeSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
  78. mFakeSun->setAmbient( LinearColorF( 0.5f, 0.5f, 0.5f ) );
  79. mFakeSun->setDirection( VectorF( 0.0f, 0.707f, -0.707f ) );
  80. mFakeSun->setPosition( mFakeSun->getDirection() * -10000.0f );
  81. mFakeSun->setRange( 2000000.0f );
  82. return true;
  83. }
  84. // This function allows the viewport's ambient color to be changed. This is exposed to script below.
  85. void GuiMaterialPreview::setAmbientLightColor( F32 r, F32 g, F32 b )
  86. {
  87. LinearColorF temp(r, g, b);
  88. temp.clamp();
  89. GuiMaterialPreview::mFakeSun->setAmbient( temp );
  90. }
  91. // This function allows the light's color to be changed. This is exposed to script below.
  92. void GuiMaterialPreview::setLightColor( F32 r, F32 g, F32 b )
  93. {
  94. LinearColorF temp(r, g, b);
  95. temp.clamp();
  96. GuiMaterialPreview::mFakeSun->setColor( temp );
  97. }
  98. // This function is for moving the light in the scene. This needs to be adjusted to keep the light
  99. // from getting all out of whack. For now, we'll just rely on the reset function if we need it
  100. // fixed.
  101. void GuiMaterialPreview::setLightTranslate(S32 modifier, F32 xstep, F32 ystep)
  102. {
  103. F32 _lighttransstep = (modifier & SI_SHIFT ? mLightTransStep : (mLightTransStep*mLightTranMult));
  104. Point3F relativeLightDirection = GuiMaterialPreview::mFakeSun->getDirection();
  105. // May be able to get rid of this. For now, it helps to fix the position of the light if i gets messed up.
  106. if (modifier & SI_PRIMARY_CTRL)
  107. {
  108. relativeLightDirection.x += ( xstep * _lighttransstep * -1 );//need to invert this for some reason. Otherwise, it's backwards.
  109. relativeLightDirection.y += ( ystep * _lighttransstep );
  110. GuiMaterialPreview::mFakeSun->setDirection(relativeLightDirection);
  111. }
  112. // Default action taken by mouse wheel clicking.
  113. else
  114. {
  115. relativeLightDirection.x += ( xstep * _lighttransstep * -1 ); //need to invert this for some reason. Otherwise, it's backwards.
  116. relativeLightDirection.z += ( ystep * _lighttransstep );
  117. GuiMaterialPreview::mFakeSun->setDirection(relativeLightDirection);
  118. }
  119. }
  120. // This is for panning the viewport camera.
  121. void GuiMaterialPreview::setTranslate(S32 modifier, F32 xstep, F32 ystep)
  122. {
  123. F32 transstep = (modifier & SI_SHIFT ? mTransStep : (mTransStep*mTranMult));
  124. F32 nominalDistance = 20.0;
  125. Point3F vec = mCameraPos;
  126. vec -= mOrbitPos;
  127. transstep *= vec.len() / nominalDistance;
  128. if (modifier & SI_PRIMARY_CTRL)
  129. {
  130. mOrbitRelPos.x += ( xstep * transstep );
  131. mOrbitRelPos.y += ( ystep * transstep );
  132. }
  133. else
  134. {
  135. mOrbitRelPos.x += ( xstep * transstep );
  136. mOrbitRelPos.z += ( ystep * transstep );
  137. }
  138. }
  139. // Left Click
  140. void GuiMaterialPreview::onMouseDown(const GuiEvent &event)
  141. {
  142. mMouseState = MovingLight;
  143. mLastMousePoint = event.mousePoint;
  144. mouseLock();
  145. }
  146. // Left Click Release
  147. void GuiMaterialPreview::onMouseUp(const GuiEvent &event)
  148. {
  149. mouseUnlock();
  150. mMouseState = None;
  151. }
  152. // Left Click Drag
  153. void GuiMaterialPreview::onMouseDragged(const GuiEvent &event)
  154. {
  155. if(mMouseState != MovingLight)
  156. {
  157. return;
  158. }
  159. // If we are MovingLight...
  160. else
  161. {
  162. Point2I delta = event.mousePoint - mLastMousePoint;
  163. mLastMousePoint = event.mousePoint;
  164. setLightTranslate(event.modifier, delta.x, delta.y);
  165. }
  166. }
  167. // Right Click
  168. void GuiMaterialPreview::onRightMouseDown(const GuiEvent &event)
  169. {
  170. mMouseState = Rotating;
  171. mLastMousePoint = event.mousePoint;
  172. mouseLock();
  173. }
  174. // Right Click Release
  175. void GuiMaterialPreview::onRightMouseUp(const GuiEvent &event)
  176. {
  177. mouseUnlock();
  178. mMouseState = None;
  179. }
  180. // Right Click Drag
  181. void GuiMaterialPreview::onRightMouseDragged(const GuiEvent &event)
  182. {
  183. if (mMouseState != Rotating)
  184. {
  185. return;
  186. }
  187. Point2I delta = event.mousePoint - mLastMousePoint;
  188. mLastMousePoint = event.mousePoint;
  189. mCameraRot.x += (delta.y * 0.01f);
  190. mCameraRot.z += (delta.x * 0.01f);
  191. }
  192. // Mouse Wheel Scroll Up
  193. bool GuiMaterialPreview::onMouseWheelUp(const GuiEvent &event)
  194. {
  195. mOrbitDist = (mOrbitDist - 0.10f);
  196. return true;
  197. }
  198. // Mouse Wheel Scroll Down
  199. bool GuiMaterialPreview::onMouseWheelDown(const GuiEvent &event)
  200. {
  201. mOrbitDist = (mOrbitDist + 0.10f);
  202. return true;
  203. }
  204. // Mouse Wheel Click
  205. void GuiMaterialPreview::onMiddleMouseDown(const GuiEvent &event)
  206. {
  207. if (!mActive || !mVisible || !mAwake)
  208. {
  209. return;
  210. }
  211. mMouseState = Panning;
  212. mLastMousePoint = event.mousePoint;
  213. mouseLock();
  214. }
  215. // Mouse Wheel Click Release
  216. void GuiMaterialPreview::onMiddleMouseUp(const GuiEvent &event)
  217. {
  218. mouseUnlock();
  219. mMouseState = None;
  220. }
  221. // Mouse Wheel Click Drag
  222. void GuiMaterialPreview::onMiddleMouseDragged(const GuiEvent &event)
  223. {
  224. if (mMouseState != Panning)
  225. {
  226. return;
  227. }
  228. Point2I delta = event.mousePoint - mLastMousePoint;
  229. mLastMousePoint = event.mousePoint;
  230. setTranslate(event.modifier, delta.x, delta.y);
  231. }
  232. // This is used to set the model we want to view in the control object.
  233. void GuiMaterialPreview::setObjectModel(const char* modelName)
  234. {
  235. deleteModel();
  236. Resource<TSShape> model = ResourceManager::get().load(modelName);
  237. if (! bool(model))
  238. {
  239. Con::warnf(avar("GuiMaterialPreview: Failed to load model %s. Please check your model name and load a valid model.", modelName));
  240. return;
  241. }
  242. mModel = new TSShapeInstance(model, true);
  243. AssertFatal(mModel, avar("GuiMaterialPreview: Failed to load model %s. Please check your model name and load a valid model.", modelName));
  244. // Initialize camera values:
  245. mOrbitPos = mModel->getShape()->center;
  246. mMinOrbitDist = mModel->getShape()->mRadius;
  247. lastRenderTime = Platform::getVirtualMilliseconds();
  248. }
  249. void GuiMaterialPreview::deleteModel()
  250. {
  251. SAFE_DELETE(mModel);
  252. runThread = 0;
  253. }
  254. // This is called whenever there is a change in the camera.
  255. bool GuiMaterialPreview::processCameraQuery(CameraQuery* query)
  256. {
  257. MatrixF xRot, zRot;
  258. Point3F vecf, vecu, vecr;;
  259. xRot.set(EulerF(mCameraRot.x, 0.0f, 0.0f));
  260. zRot.set(EulerF(0.0f, 0.0f, mCameraRot.z));
  261. if(mMouseState != Panning)
  262. {
  263. // Adjust the camera so that we are still facing the model:
  264. Point3F vec;
  265. mCameraMatrix.mul(zRot, xRot);
  266. mCameraMatrix.getColumn(1, &vec);
  267. vec *= mOrbitDist;
  268. mCameraPos = mOrbitPos - vec;
  269. query->farPlane = 2100.0f;
  270. query->nearPlane = query->farPlane / 5000.0f;
  271. query->fov = 45.0f;
  272. mCameraMatrix.setColumn(3, mCameraPos);
  273. query->cameraMatrix = mCameraMatrix;
  274. }
  275. else
  276. {
  277. mCameraMatrix.mul( zRot, xRot );
  278. mCameraMatrix.getColumn( 1, &vecf ); // Forward vector
  279. mCameraMatrix.getColumn( 2, &vecu ); // Up vector
  280. mCameraMatrix.getColumn( 0, &vecr ); // Right vector
  281. Point3F flatVecf(vecf.x, vecf.y, 0.0f);
  282. Point3F modvecf = flatVecf * mOrbitRelPos.y;
  283. Point3F modvecu = vecu * mOrbitRelPos.z;
  284. Point3F modvecr = vecr * mOrbitRelPos.x;
  285. // Change the orbit position
  286. mOrbitPos += modvecu - modvecr + modvecf;
  287. F32 vecfmul = mOrbitDist;
  288. Point3F virtualVecF = vecf * mOrbitDist;
  289. vecf *= vecfmul;
  290. mCameraPos = mOrbitPos - virtualVecF;
  291. // Update the camera's position
  292. mCameraMatrix.setColumn( 3, (mOrbitPos - vecf) );
  293. query->farPlane = 2100.0f;
  294. query->nearPlane = query->farPlane / 5000.0f;
  295. query->fov = 45.0f;
  296. query->cameraMatrix = mCameraMatrix;
  297. // Reset the relative position
  298. mOrbitRelPos = Point3F(0,0,0);
  299. }
  300. return true;
  301. }
  302. void GuiMaterialPreview::onMouseEnter(const GuiEvent & event)
  303. {
  304. Con::executef(this, "onMouseEnter");
  305. }
  306. void GuiMaterialPreview::onMouseLeave(const GuiEvent & event)
  307. {
  308. Con::executef(this, "onMouseLeave");
  309. }
  310. void GuiMaterialPreview::renderWorld(const RectI &updateRect)
  311. {
  312. // nothing to render, punt
  313. if ( !mModel && !mMountedModel )
  314. return;
  315. S32 time = Platform::getVirtualMilliseconds();
  316. //S32 dt = time - lastRenderTime;
  317. lastRenderTime = time;
  318. F32 left, right, top, bottom, nearPlane, farPlane;
  319. bool isOrtho;
  320. GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho);
  321. mSaveFrustum = Frustum( isOrtho, left, right, bottom, top, nearPlane, farPlane, MatrixF::Identity );
  322. mSaveProjection = GFX->getProjectionMatrix();
  323. mSaveWorldToScreenScale = GFX->getWorldToScreenScale();
  324. FogData savedFogData = gClientSceneGraph->getFogData();
  325. gClientSceneGraph->setFogData( FogData() ); // no fog in preview window
  326. if (Skylight::smSkylightProbe.isValid())
  327. PROBEMGR->submitProbe(Skylight::smSkylightProbe->getProbeInfo());
  328. RenderPassManager* renderPass = gClientSceneGraph->getDefaultRenderPass();
  329. SceneRenderState state
  330. (
  331. gClientSceneGraph,
  332. SPT_Diffuse,
  333. SceneCameraState( GFX->getViewport(), mSaveFrustum, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
  334. renderPass,
  335. true
  336. );
  337. // Set up our TS render state here.
  338. TSRenderState rdata;
  339. rdata.setSceneState( &state );
  340. // We might have some forward lit materials
  341. // so pass down a query to gather lights.
  342. LightQuery query;
  343. query.init( SphereF( Point3F::Zero, 1.0f ) );
  344. rdata.setLightQuery( &query );
  345. // Set up pass transforms
  346. renderPass->assignSharedXform(RenderPassManager::View, MatrixF::Identity);
  347. renderPass->assignSharedXform(RenderPassManager::Projection, GFX->getProjectionMatrix());
  348. LIGHTMGR->unregisterAllLights();
  349. LIGHTMGR->setSpecialLight( LightManager::slSunLightType, mFakeSun );
  350. if ( mModel )
  351. mModel->render( rdata );
  352. if ( mMountedModel )
  353. {
  354. // render a weapon
  355. /*
  356. MatrixF mat;
  357. GFX->pushWorldMatrix();
  358. GFX->multWorld( mat );
  359. GFX->popWorldMatrix();
  360. */
  361. }
  362. renderPass->renderPass( &state );
  363. if (mMouseState == MovingLight)
  364. {
  365. renderSunDirection();
  366. }
  367. gClientSceneGraph->setFogData( savedFogData ); // restore fog setting
  368. // Make sure to remove our fake sun
  369. LIGHTMGR->unregisterAllLights();
  370. }
  371. void GuiMaterialPreview::renderSunDirection() const
  372. {
  373. // Render four arrows aiming in the direction of the sun's light
  374. ColorI color = LinearColorF(mFakeSun->getColor()).toColorI();
  375. F32 length = mModel->getShape()->mBounds.len() * 0.8f;
  376. // Get the sun's vectors
  377. Point3F fwd = mFakeSun->getTransform().getForwardVector();
  378. Point3F up = mFakeSun->getTransform().getUpVector() * length / 8;
  379. Point3F right = mFakeSun->getTransform().getRightVector() * length / 8;
  380. // Calculate the start and end points of the first arrow (bottom left)
  381. Point3F start = mModel->getShape()->center - fwd * length - up / 2 - right / 2;
  382. Point3F end = mModel->getShape()->center - fwd * length / 3 - up / 2 - right / 2;
  383. GFXStateBlockDesc desc;
  384. desc.setZReadWrite(true, true);
  385. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  386. drawUtil->drawArrow(desc, start, end, color);
  387. drawUtil->drawArrow(desc, start + up, end + up, color);
  388. drawUtil->drawArrow(desc, start + right, end + right, color);
  389. drawUtil->drawArrow(desc, start + up + right, end + up + right, color);
  390. }
  391. // Make sure the orbit distance is within the acceptable range.
  392. void GuiMaterialPreview::setOrbitDistance(F32 distance)
  393. {
  394. mOrbitDist = mClampF(distance, mMinOrbitDist, mMaxOrbitDist);
  395. }
  396. // This function is meant to be used with a button to put everything back to default settings.
  397. void GuiMaterialPreview::resetViewport()
  398. {
  399. // Reset the camera's orientation.
  400. mCameraRot.set( mDegToRad(30.0f), 0, mDegToRad(-30.0f) );
  401. mCameraPos.set(0.0f, 1.75f, 1.25f);
  402. mOrbitDist = 5.0f;
  403. mOrbitPos = mModel->getShape()->center;
  404. // Reset the viewport's lighting.
  405. GuiMaterialPreview::mFakeSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
  406. GuiMaterialPreview::mFakeSun->setAmbient( LinearColorF( 0.5f, 0.5f, 0.5f ) );
  407. GuiMaterialPreview::mFakeSun->setDirection( VectorF( 0.0f, 0.707f, -0.707f ) );
  408. }
  409. // Expose the class and functions to the console.
  410. IMPLEMENT_CONOBJECT(GuiMaterialPreview);
  411. ConsoleDocClass( GuiMaterialPreview,
  412. "@brief Visual preview of a specified Material\n\n"
  413. "Editor use only.\n\n"
  414. "@internal"
  415. );
  416. // Set the model.
  417. DefineEngineMethod(GuiMaterialPreview, setModel, void, ( const char* shapeName ),,
  418. "Sets the model to be displayed in this control\n\n"
  419. "@param shapeName Name of the model to display.\n")
  420. {
  421. object->setObjectModel(shapeName);
  422. }
  423. DefineEngineMethod(GuiMaterialPreview, deleteModel, void, (),,
  424. "Deletes the preview model.\n")
  425. {
  426. object->deleteModel();
  427. }
  428. // Set orbit distance around the model.
  429. DefineEngineMethod(GuiMaterialPreview, setOrbitDistance, void, ( F32 distance ),,
  430. "Sets the distance at which the camera orbits the object. Clamped to the "
  431. "acceptable range defined in the class by min and max orbit distances.\n\n"
  432. "@param distance The distance to set the orbit to (will be clamped).")
  433. {
  434. object->setOrbitDistance(distance);
  435. }
  436. // Reset control to default values. Meant to be used with a button.
  437. DefineEngineMethod(GuiMaterialPreview, reset, void, (),,
  438. "Resets the viewport to default zoom, pan, rotate and lighting.")
  439. {
  440. object->resetViewport();
  441. }
  442. // This function allows the user to change the light's color.
  443. DefineEngineMethod(GuiMaterialPreview, setLightColor, void, ( LinearColorF color ),,
  444. "Sets the color of the light in the scene.\n")
  445. {
  446. object->setLightColor( color.red, color.green, color.blue );
  447. }
  448. // This function allows the user to change the viewports's ambient color.
  449. DefineEngineMethod(GuiMaterialPreview, setAmbientLightColor, void, ( LinearColorF color ),,
  450. "Sets the color of the ambient light in the scene.\n")
  451. {
  452. object->setAmbientLightColor( color.red, color.green, color.blue );
  453. }