guiMaterialPreview.cpp 15 KB

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