guiMaterialPreview.cpp 14 KB

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