guiObjectView.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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 "T3D/guiObjectView.h"
  23. #include "renderInstance/renderPassManager.h"
  24. #include "lighting/lightManager.h"
  25. #include "lighting/lightInfo.h"
  26. #include "core/resourceManager.h"
  27. #include "scene/sceneManager.h"
  28. #include "scene/sceneRenderState.h"
  29. #include "console/consoleTypes.h"
  30. #include "math/mathTypes.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "console/engineAPI.h"
  33. #include "renderInstance/renderProbeMgr.h"
  34. #include "T3D/lighting/skylight.h"
  35. IMPLEMENT_CONOBJECT( GuiObjectView );
  36. ConsoleDocClass( GuiObjectView,
  37. "@brief GUI control which displays a 3D model.\n\n"
  38. "Model displayed in the control can have other objects mounted onto it, and the light settings can be adjusted.\n\n"
  39. "@tsexample\n"
  40. " new GuiObjectView(ObjectPreview)\n"
  41. " {\n"
  42. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  43. " mountedNode = \"mount0\";\n"
  44. " lightColor = \"1 1 1 1\";\n"
  45. " lightAmbient = \"0.5 0.5 0.5 1\";\n"
  46. " lightDirection = \"0 0.707 -0.707\";\n"
  47. " orbitDiststance = \"2\";\n"
  48. " minOrbitDiststance = \"0.917688\";\n"
  49. " maxOrbitDiststance = \"5\";\n"
  50. " cameraSpeed = \"0.01\";\n"
  51. " cameraZRot = \"0\";\n"
  52. " forceFOV = \"0\";\n"
  53. " reflectPriority = \"0\";\n"
  54. " };\n"
  55. "@endtsexample\n\n"
  56. "@see GuiControl\n\n"
  57. "@ingroup Gui3D\n"
  58. );
  59. IMPLEMENT_CALLBACK( GuiObjectView, onMouseEnter, void, (),(),
  60. "@brief Called whenever the mouse enters the control.\n\n"
  61. "@tsexample\n"
  62. "// The mouse has entered the control, causing the callback to occur\n"
  63. "GuiObjectView::onMouseEnter(%this)\n"
  64. " {\n"
  65. " // Code to run when the mouse enters this control\n"
  66. " }\n"
  67. "@endtsexample\n\n"
  68. "@see GuiControl\n\n"
  69. );
  70. IMPLEMENT_CALLBACK( GuiObjectView, onMouseLeave, void, (),(),
  71. "@brief Called whenever the mouse leaves the control.\n\n"
  72. "@tsexample\n"
  73. "// The mouse has left the control, causing the callback to occur\n"
  74. "GuiObjectView::onMouseLeave(%this)\n"
  75. " {\n"
  76. " // Code to run when the mouse leaves this control\n"
  77. " }\n"
  78. "@endtsexample\n\n"
  79. "@see GuiControl\n\n"
  80. );
  81. //------------------------------------------------------------------------------
  82. GuiObjectView::GuiObjectView()
  83. : mMouseState( None ),
  84. mLastMousePoint( 0, 0 ),
  85. mModelInstance(NULL),
  86. mMaxOrbitDist( 5.0f ),
  87. mMinOrbitDist( 0.0f ),
  88. mCameraRotation( 0.0f, 0.0f, 0.0f ),
  89. mOrbitDist( 5.0f ),
  90. mCameraSpeed( 0.01f ),
  91. mMountedModelInstance( NULL ),
  92. mMountNode( -1 ),
  93. mMountNodeName( "mount0" ),
  94. mAnimationSeq( -1 ),
  95. mRunThread( NULL ),
  96. mLastRenderTime( 0 ),
  97. mLight( NULL ),
  98. mLightColor( 1.0f, 1.0f, 1.0f ),
  99. mLightAmbient( 1.0f, 1.0f, 1.0f ),
  100. mLightDirection( 0.f, 0.707f, -0.707f )
  101. {
  102. mCameraMatrix.identity();
  103. mCameraRot.set( 0.0f, 0.0f, 3.9f );
  104. mCameraPos.set( 0.0f, 0.0f, 0.0f );
  105. mCameraMatrix.setColumn( 3, mCameraPos );
  106. mOrbitPos.set( 0.0f, 0.0f, 0.0f );
  107. // By default don't do dynamic reflection
  108. // updates for this viewport.
  109. mReflectPriority = 0.0f;
  110. INIT_ASSET(Model);
  111. INIT_ASSET(MountedModel);
  112. }
  113. //------------------------------------------------------------------------------
  114. GuiObjectView::~GuiObjectView()
  115. {
  116. if( mLight )
  117. SAFE_DELETE( mLight );
  118. }
  119. //------------------------------------------------------------------------------
  120. void GuiObjectView::initPersistFields()
  121. {
  122. docsURL;
  123. addGroup( "Model" );
  124. INITPERSISTFIELD_SHAPEASSET(Model, GuiObjectView, "The source shape asset.");
  125. addField( "skin", TypeRealString, Offset( mSkinName, GuiObjectView ),
  126. "The skin to use on the object model." );
  127. endGroup( "Model" );
  128. addGroup( "Animation" );
  129. addField( "animSequence", TypeRealString, Offset( mAnimationSeqName, GuiObjectView ),
  130. "The animation sequence to play on the model." );
  131. endGroup( "Animation" );
  132. addGroup( "Mounting" );
  133. INITPERSISTFIELD_SHAPEASSET(MountedModel, GuiObjectView, "The mounted shape asset.");
  134. addField( "mountedSkin", TypeRealString, Offset( mMountSkinName, GuiObjectView ),
  135. "Skin name used on mounted shape file." );
  136. addField( "mountedNode", TypeRealString, Offset( mMountNodeName, GuiObjectView ),
  137. "Name of node on primary model to which to mount the secondary shape." );
  138. endGroup( "Mounting" );
  139. addGroup( "Lighting" );
  140. addField( "lightColor", TypeColorF, Offset( mLightColor, GuiObjectView ),
  141. "Diffuse color of the sunlight used to render the model." );
  142. addField( "lightAmbient", TypeColorF, Offset( mLightAmbient, GuiObjectView ),
  143. "Ambient color of the sunlight used to render the model." );
  144. addField( "lightDirection", TypePoint3F, Offset( mLightDirection, GuiObjectView ),
  145. "Direction from which the model is illuminated." );
  146. endGroup( "Lighting" );
  147. addGroup( "Camera" );
  148. addFieldV( "orbitDiststance", TypeRangedF32, Offset( mOrbitDist, GuiObjectView ), &CommonValidators::PositiveFloat,
  149. "Distance from which to render the model." );
  150. addFieldV( "minOrbitDiststance", TypeRangedF32, Offset( mMinOrbitDist, GuiObjectView ), &CommonValidators::PositiveFloat,
  151. "Maxiumum distance to which the camera can be zoomed out." );
  152. addFieldV( "maxOrbitDiststance", TypeRangedF32, Offset( mMaxOrbitDist, GuiObjectView ), &CommonValidators::PositiveFloat,
  153. "Minimum distance below which the camera will not zoom in further." );
  154. addFieldV( "cameraSpeed", TypeRangedF32, Offset( mCameraSpeed, GuiObjectView ), &CommonValidators::PositiveFloat,
  155. "Multiplier for mouse camera operations." );
  156. addField( "cameraRotation", TypePoint3F, Offset( mCameraRotation, GuiObjectView ),
  157. "Set the camera rotation." );
  158. endGroup( "Camera" );
  159. Parent::initPersistFields();
  160. }
  161. //------------------------------------------------------------------------------
  162. void GuiObjectView::onStaticModified( StringTableEntry slotName, const char* newValue )
  163. {
  164. Parent::onStaticModified( slotName, newValue );
  165. static StringTableEntry sSkin = StringTable->insert( "skin" );
  166. static StringTableEntry sMountedSkin = StringTable->insert( "mountedSkin" );
  167. static StringTableEntry sMountedNode = StringTable->insert( "mountedNode" );
  168. static StringTableEntry sLightColor = StringTable->insert( "lightColor" );
  169. static StringTableEntry sLightAmbient = StringTable->insert( "lightAmbient" );
  170. static StringTableEntry sLightDirection = StringTable->insert( "lightDirection" );
  171. static StringTableEntry sOrbitDistance = StringTable->insert( "orbitDistance" );
  172. static StringTableEntry sMinOrbitDistance = StringTable->insert( "minOrbitDistance" );
  173. static StringTableEntry sMaxOrbitDistance = StringTable->insert( "maxOrbitDistance" );
  174. static StringTableEntry sCameraRotation = StringTable->insert( "cameraRotation" );
  175. static StringTableEntry sAnimSequence = StringTable->insert( "animSequence" );
  176. if( slotName == sSkin )
  177. setSkin( String( mSkinName ) );
  178. else if( slotName == sMountedSkin )
  179. setMountSkin( String( mMountSkinName ) );
  180. else if( slotName == sMountedNode )
  181. setMountNode( String( mMountNodeName ) );
  182. else if( slotName == sLightColor )
  183. setLightColor( mLightColor );
  184. else if( slotName == sLightAmbient )
  185. setLightAmbient( mLightAmbient );
  186. else if( slotName == sLightDirection )
  187. setLightDirection( mLightDirection );
  188. else if( slotName == sOrbitDistance || slotName == sMinOrbitDistance || slotName == sMaxOrbitDistance )
  189. setOrbitDistance( mOrbitDist );
  190. else if( slotName == sCameraRotation )
  191. setCameraRotation( mCameraRotation );
  192. else if( slotName == sAnimSequence )
  193. setObjectAnimation( String( mAnimationSeqName ) );
  194. }
  195. //------------------------------------------------------------------------------
  196. bool GuiObjectView::onWake()
  197. {
  198. if( !Parent::onWake() )
  199. return false;
  200. if( !mLight )
  201. {
  202. mLight = LIGHTMGR->createLightInfo();
  203. mLight->setColor( mLightColor );
  204. mLight->setAmbient( mLightAmbient );
  205. mLight->setDirection( mLightDirection );
  206. }
  207. return true;
  208. }
  209. //------------------------------------------------------------------------------
  210. void GuiObjectView::onMouseDown( const GuiEvent &event )
  211. {
  212. if( !mActive || !mVisible || !mAwake )
  213. return;
  214. mMouseState = Rotating;
  215. mLastMousePoint = event.mousePoint;
  216. mouseLock();
  217. }
  218. //------------------------------------------------------------------------------
  219. void GuiObjectView::onMouseUp( const GuiEvent &event )
  220. {
  221. mouseUnlock();
  222. mMouseState = None;
  223. }
  224. //------------------------------------------------------------------------------
  225. void GuiObjectView::onMouseDragged( const GuiEvent &event )
  226. {
  227. if( mMouseState != Rotating )
  228. return;
  229. Point2I delta = event.mousePoint - mLastMousePoint;
  230. mLastMousePoint = event.mousePoint;
  231. mCameraRot.x += ( delta.y * mCameraSpeed );
  232. mCameraRot.z += ( delta.x * mCameraSpeed );
  233. }
  234. //------------------------------------------------------------------------------
  235. void GuiObjectView::onRightMouseDown( const GuiEvent &event )
  236. {
  237. mMouseState = Zooming;
  238. mLastMousePoint = event.mousePoint;
  239. mouseLock();
  240. }
  241. //------------------------------------------------------------------------------
  242. void GuiObjectView::onRightMouseUp( const GuiEvent &event )
  243. {
  244. mouseUnlock();
  245. mMouseState = None;
  246. }
  247. //------------------------------------------------------------------------------
  248. void GuiObjectView::onRightMouseDragged( const GuiEvent &event )
  249. {
  250. if( mMouseState != Zooming )
  251. return;
  252. S32 delta = event.mousePoint.y - mLastMousePoint.y;
  253. mLastMousePoint = event.mousePoint;
  254. mOrbitDist += ( delta * mCameraSpeed );
  255. }
  256. //------------------------------------------------------------------------------
  257. void GuiObjectView::setObjectAnimation( S32 index )
  258. {
  259. mAnimationSeq = index;
  260. mAnimationSeqName = String();
  261. if(mModelInstance)
  262. _initAnimation();
  263. }
  264. //------------------------------------------------------------------------------
  265. void GuiObjectView::setObjectAnimation( const String& sequenceName )
  266. {
  267. mAnimationSeq = -1;
  268. mAnimationSeqName = sequenceName;
  269. if(mModelInstance)
  270. _initAnimation();
  271. }
  272. //------------------------------------------------------------------------------
  273. bool GuiObjectView::setObjectModel( const String& modelName )
  274. {
  275. mRunThread = 0;
  276. // Load the shape.
  277. _setModel(modelName);
  278. if( !getModelResource())
  279. {
  280. Con::warnf( "GuiObjectView::setObjectModel - Failed to load model '%s'", modelName.c_str() );
  281. return false;
  282. }
  283. if (!getModelResource()->preloadMaterialList(getModelResource().getPath())) return false;
  284. // Instantiate it.
  285. mModelInstance = new TSShapeInstance(getModelResource(), true );
  286. mModelInstance->resetMaterialList();
  287. mModelInstance->cloneMaterialList();
  288. if( !mSkinName.isEmpty() )
  289. mModelInstance->reSkin( mSkinName );
  290. TSMaterialList* pMatList = mModelInstance->getMaterialList();
  291. pMatList->setTextureLookupPath(mModelAsset->getShapeFileName());
  292. mModelInstance->initMaterialList();
  293. // Initialize camera values.
  294. mOrbitPos = getModelResource()->center;
  295. mMinOrbitDist = getModelResource()->mRadius;
  296. // Initialize animation.
  297. _initAnimation();
  298. _initMount();
  299. return true;
  300. }
  301. void GuiObjectView::onModelChanged()
  302. {
  303. }
  304. //------------------------------------------------------------------------------
  305. void GuiObjectView::setSkin( const String& name )
  306. {
  307. if(mModelInstance)
  308. mModelInstance->reSkin( name, mSkinName );
  309. mSkinName = name;
  310. }
  311. //------------------------------------------------------------------------------
  312. bool GuiObjectView::setMountedObject( const String& modelName )
  313. {
  314. // Load the model.
  315. _setMountedModel(modelName);
  316. if (!getMountedModelResource())
  317. {
  318. Con::warnf("GuiObjectView::setMountedObject - Failed to load model '%s'", modelName.c_str());
  319. return false;
  320. }
  321. if (!getMountedModelResource()->preloadMaterialList(getMountedModelResource().getPath())) return false;
  322. mMountedModelInstance = new TSShapeInstance(getMountedModelResource(), true);
  323. mMountedModelInstance->resetMaterialList();
  324. mMountedModelInstance->cloneMaterialList();
  325. if( !mMountSkinName.isEmpty() )
  326. mMountedModelInstance->reSkin( mMountSkinName );
  327. mMountedModelInstance->initMaterialList();
  328. if(mMountedModelInstance)
  329. _initMount();
  330. return true;
  331. }
  332. void GuiObjectView::onMountedModelChanged()
  333. {
  334. }
  335. //------------------------------------------------------------------------------
  336. void GuiObjectView::setMountSkin(const String& name)
  337. {
  338. if (mMountedModelInstance)
  339. mMountedModelInstance->reSkin(name, mMountSkinName);
  340. mMountSkinName = name;
  341. }
  342. //------------------------------------------------------------------------------
  343. void GuiObjectView::setMountNode(S32 index)
  344. {
  345. setMountNode(String::ToString("mount%i", index));
  346. }
  347. //------------------------------------------------------------------------------
  348. void GuiObjectView::setMountNode(const String& name)
  349. {
  350. mMountNodeName = name;
  351. if (mModelInstance)
  352. _initMount();
  353. }
  354. //------------------------------------------------------------------------------
  355. bool GuiObjectView::processCameraQuery( CameraQuery* query )
  356. {
  357. // Adjust the camera so that we are still facing the model.
  358. Point3F vec;
  359. MatrixF xRot, zRot;
  360. xRot.set( EulerF( mCameraRot.x, 0.0f, 0.0f ) );
  361. zRot.set( EulerF( 0.0f, 0.0f, mCameraRot.z ) );
  362. mCameraMatrix.mul( zRot, xRot );
  363. mCameraMatrix.getColumn( 1, &vec );
  364. vec *= mOrbitDist;
  365. mCameraPos = mOrbitPos - vec;
  366. query->farPlane = 2100.0f;
  367. query->nearPlane = query->farPlane / 5000.0f;
  368. query->fov = 45.0f;
  369. mCameraMatrix.setColumn( 3, mCameraPos );
  370. query->cameraMatrix = mCameraMatrix;
  371. return true;
  372. }
  373. //------------------------------------------------------------------------------
  374. void GuiObjectView::onMouseEnter( const GuiEvent & event )
  375. {
  376. onMouseEnter_callback();
  377. }
  378. //------------------------------------------------------------------------------
  379. void GuiObjectView::onMouseLeave( const GuiEvent & event )
  380. {
  381. onMouseLeave_callback();
  382. }
  383. //------------------------------------------------------------------------------
  384. void GuiObjectView::renderWorld( const RectI& updateRect )
  385. {
  386. if( !mModelInstance)
  387. return;
  388. GFXTransformSaver _saveTransforms;
  389. // Determine the camera position, and store off render state.
  390. MatrixF modelview;
  391. MatrixF mv;
  392. Point3F cp;
  393. modelview = GFX->getWorldMatrix();
  394. mv = modelview;
  395. mv.inverse();
  396. mv.getColumn( 3, &cp );
  397. RenderPassManager* renderPass = gClientSceneGraph->getDefaultRenderPass();
  398. S32 time = Platform::getVirtualMilliseconds();
  399. S32 dt = time - mLastRenderTime;
  400. mLastRenderTime = time;
  401. LIGHTMGR->unregisterAllLights();
  402. LIGHTMGR->setSpecialLight( LightManager::slSunLightType, mLight );
  403. GFX->setStateBlock( mDefaultGuiSB );
  404. F32 left, right, top, bottom, nearPlane, farPlane;
  405. bool isOrtho;
  406. GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho );
  407. Frustum frust( false, left, right, top, bottom, nearPlane, farPlane, MatrixF::Identity );
  408. SceneRenderState state
  409. (
  410. gClientSceneGraph,
  411. SPT_Diffuse,
  412. SceneCameraState( GFX->getViewport(), frust, MatrixF::Identity, GFX->getProjectionMatrix() ),
  413. renderPass,
  414. true
  415. );
  416. // Set up our TS render state here.
  417. TSRenderState rdata;
  418. rdata.setSceneState( &state );
  419. // We might have some forward lit materials
  420. // so pass down a query to gather lights.
  421. LightQuery query;
  422. query.init( SphereF( Point3F::Zero, 1.0f ) );
  423. rdata.setLightQuery( &query );
  424. // Render primary model.
  425. if (Skylight::smSkylightProbe.isValid())
  426. PROBEMGR->submitProbe(Skylight::smSkylightProbe->getProbeInfo());
  427. FogData savedFogData = gClientSceneGraph->getFogData();
  428. gClientSceneGraph->setFogData(FogData()); // no fog in preview window
  429. if(mModelInstance)
  430. {
  431. if( mRunThread )
  432. {
  433. mModelInstance->advanceTime( dt / 1000.f, mRunThread );
  434. mModelInstance->animate();
  435. }
  436. mModelInstance->render( rdata );
  437. }
  438. // Render mounted model.
  439. if( mMountedModelInstance && mMountNode != -1 )
  440. {
  441. GFX->pushWorldMatrix();
  442. GFX->multWorld(mModelInstance->mNodeTransforms[ mMountNode ] );
  443. GFX->multWorld( mMountTransform );
  444. mMountedModelInstance->render( rdata );
  445. GFX->popWorldMatrix();
  446. }
  447. renderPass->renderPass( &state );
  448. gClientSceneGraph->setFogData(savedFogData); // restore fog setting
  449. // Make sure to remove our fake sun.
  450. LIGHTMGR->unregisterAllLights();
  451. }
  452. //------------------------------------------------------------------------------
  453. void GuiObjectView::setOrbitDistance( F32 distance )
  454. {
  455. // Make sure the orbit distance is within the acceptable range
  456. mOrbitDist = mClampF( distance, mMinOrbitDist, mMaxOrbitDist );
  457. }
  458. //------------------------------------------------------------------------------
  459. void GuiObjectView::setCameraSpeed( F32 factor )
  460. {
  461. mCameraSpeed = factor;
  462. }
  463. //------------------------------------------------------------------------------
  464. void GuiObjectView::setCameraRotation( const EulerF& rotation )
  465. {
  466. mCameraRot.set(rotation);
  467. }
  468. //------------------------------------------------------------------------------
  469. void GuiObjectView::setLightColor( const LinearColorF& color )
  470. {
  471. mLightColor = color;
  472. if( mLight )
  473. mLight->setColor( color );
  474. }
  475. //------------------------------------------------------------------------------
  476. void GuiObjectView::setLightAmbient( const LinearColorF& color )
  477. {
  478. mLightAmbient = color;
  479. if( mLight )
  480. mLight->setAmbient( color );
  481. }
  482. //------------------------------------------------------------------------------
  483. void GuiObjectView::setLightDirection( const Point3F& direction )
  484. {
  485. mLightDirection = direction;
  486. if( mLight )
  487. mLight->setDirection( direction );
  488. }
  489. //------------------------------------------------------------------------------
  490. void GuiObjectView::_initAnimation()
  491. {
  492. AssertFatal(getModelResource(), "GuiObjectView::_initAnimation - No model loaded!" );
  493. if( mAnimationSeqName.isEmpty() && mAnimationSeq == -1 )
  494. return;
  495. // Look up sequence by name.
  496. if( !mAnimationSeqName.isEmpty() )
  497. {
  498. mAnimationSeq = getModelResource()->findSequence( mAnimationSeqName );
  499. if( mAnimationSeq == -1 )
  500. {
  501. Con::errorf( "GuiObjectView::_initAnimation - Cannot find animation sequence '%s' on '%s'",
  502. mAnimationSeqName.c_str(),
  503. mModelName
  504. );
  505. return;
  506. }
  507. }
  508. // Start sequence.
  509. if( mAnimationSeq != -1 )
  510. {
  511. if( mAnimationSeq >= getModelResource()->sequences.size() )
  512. {
  513. Con::errorf( "GuiObjectView::_initAnimation - Sequence '%i' out of range for model '%s'",
  514. mAnimationSeq,
  515. mModelName
  516. );
  517. mAnimationSeq = -1;
  518. return;
  519. }
  520. if( !mRunThread )
  521. mRunThread = mModelInstance->addThread();
  522. mModelInstance->setSequence( mRunThread, mAnimationSeq, 0.f );
  523. }
  524. mLastRenderTime = Platform::getVirtualMilliseconds();
  525. }
  526. //------------------------------------------------------------------------------
  527. void GuiObjectView::_initMount()
  528. {
  529. AssertFatal(mModelInstance, "GuiObjectView::_initMount - No model loaded!" );
  530. if( !mModelInstance)
  531. return;
  532. mMountTransform.identity();
  533. // Look up the node to which to mount to.
  534. if( !mMountNodeName.isEmpty() )
  535. {
  536. mMountNode = getModelResource()->findNode( mMountNodeName );
  537. if( mMountNode == -1 )
  538. {
  539. Con::errorf( "GuiObjectView::_initMount - No node '%s' on '%s'",
  540. mMountNodeName.c_str(),
  541. mModelName
  542. );
  543. return;
  544. }
  545. }
  546. // Make sure mount node is valid.
  547. if( mMountNode != -1 && mMountNode >= getModelResource()->nodes.size() )
  548. {
  549. Con::errorf( "GuiObjectView::_initMount - Mount node index '%i' out of range for '%s'",
  550. mMountNode,
  551. mModelName
  552. );
  553. mMountNode = -1;
  554. return;
  555. }
  556. // Look up node on the mounted model from
  557. // which to mount to the primary model's node.
  558. if (!getMountedModelResource()) return;
  559. S32 mountPoint = getMountedModelResource()->findNode( "mountPoint" );
  560. if( mountPoint != -1 )
  561. {
  562. getMountedModelResource()->getNodeWorldTransform(mountPoint, &mMountTransform),
  563. mMountTransform.inverse();
  564. }
  565. }
  566. //=============================================================================
  567. // Console Methods.
  568. //=============================================================================
  569. // MARK: ---- Console Methods ----
  570. //-----------------------------------------------------------------------------
  571. DefineEngineMethod( GuiObjectView, getModel, const char*, (),,
  572. "@brief Return the model displayed in this view.\n\n"
  573. "@tsexample\n"
  574. "// Request the displayed model name from the GuiObjectView object.\n"
  575. "%modelName = %thisGuiObjectView.getModel();\n"
  576. "@endtsexample\n\n"
  577. "@return Name of the displayed model.\n\n"
  578. "@see GuiControl")
  579. {
  580. return Con::getReturnBuffer( object->getModel() );
  581. }
  582. //-----------------------------------------------------------------------------
  583. DefineEngineMethod( GuiObjectView, setModel, bool, (const char* shapeName),,
  584. "@brief Sets the model to be displayed in this control.\n\n"
  585. "@param shapeName Name of the model to display.\n"
  586. "@tsexample\n"
  587. "// Define the model we want to display\n"
  588. "%shapeName = \"gideon.dts\";\n\n"
  589. "// Tell the GuiObjectView object to display the defined model\n"
  590. "%thisGuiObjectView.setModel(%shapeName);\n"
  591. "@endtsexample\n\n"
  592. "@see GuiControl")
  593. {
  594. return object->setObjectModel( shapeName );
  595. }
  596. //-----------------------------------------------------------------------------
  597. DefineEngineMethod( GuiObjectView, getMountedModel, const char*, (),,
  598. "@brief Return the name of the mounted model.\n\n"
  599. "@tsexample\n"
  600. "// Request the name of the mounted model from the GuiObjectView object\n"
  601. "%mountedModelName = %thisGuiObjectView.getMountedModel();\n"
  602. "@endtsexample\n\n"
  603. "@return Name of the mounted model.\n\n"
  604. "@see GuiControl")
  605. {
  606. return Con::getReturnBuffer( object->getMountedModel() );
  607. }
  608. //-----------------------------------------------------------------------------
  609. DefineEngineMethod( GuiObjectView, setMountedModel, void, (const char* shapeName),,
  610. "@brief Sets the model to be mounted on the primary model.\n\n"
  611. "@param shapeName Name of the model to mount.\n"
  612. "@tsexample\n"
  613. "// Define the model name to mount\n"
  614. "%modelToMount = \"GideonGlasses.dts\";\n\n"
  615. "// Inform the GuiObjectView object to mount the defined model to the existing model in the control\n"
  616. "%thisGuiObjectView.setMountedModel(%modelToMount);\n"
  617. "@endtsexample\n\n"
  618. "@see GuiControl")
  619. {
  620. object->setObjectModel(shapeName);
  621. }
  622. //-----------------------------------------------------------------------------
  623. DefineEngineMethod( GuiObjectView, getSkin, const char*, (),,
  624. "@brief Return the name of skin used on the primary model.\n\n"
  625. "@tsexample\n"
  626. "// Request the name of the skin used on the primary model in the control\n"
  627. "%skinName = %thisGuiObjectView.getSkin();\n"
  628. "@endtsexample\n\n"
  629. "@return Name of the skin used on the primary model.\n\n"
  630. "@see GuiControl")
  631. {
  632. return Con::getReturnBuffer( object->getSkin() );
  633. }
  634. //-----------------------------------------------------------------------------
  635. DefineEngineMethod( GuiObjectView, setSkin, void, (const char* skinName),,
  636. "@brief Sets the skin to use on the model being displayed.\n\n"
  637. "@param skinName Name of the skin to use.\n"
  638. "@tsexample\n"
  639. "// Define the skin we want to apply to the main model in the control\n"
  640. "%skinName = \"disco_gideon\";\n\n"
  641. "// Inform the GuiObjectView control to update the skin the to defined skin\n"
  642. "%thisGuiObjectView.setSkin(%skinName);\n"
  643. "@endtsexample\n\n"
  644. "@see GuiControl")
  645. {
  646. object->setSkin( skinName );
  647. }
  648. //-----------------------------------------------------------------------------
  649. DefineEngineMethod( GuiObjectView, getMountSkin, const char*, ( S32 param1, S32 param2),,
  650. "@brief Return the name of skin used on the mounted model.\n\n"
  651. "@tsexample\n"
  652. "// Request the skin name from the model mounted on to the main model in the control\n"
  653. "%mountModelSkin = %thisGuiObjectView.getMountSkin();\n"
  654. "@endtsexample\n\n"
  655. "@return Name of the skin used on the mounted model.\n\n"
  656. "@see GuiControl")
  657. {
  658. return Con::getReturnBuffer( object->getMountSkin() );
  659. }
  660. //-----------------------------------------------------------------------------
  661. DefineEngineMethod( GuiObjectView, setMountSkin, void, (const char* skinName),,
  662. "@brief Sets the skin to use on the mounted model.\n\n"
  663. "@param skinName Name of the skin to set on the model mounted to the main model in the control\n"
  664. "@tsexample\n"
  665. "// Define the name of the skin\n"
  666. "%skinName = \"BronzeGlasses\";\n\n"
  667. "// Inform the GuiObjectView Control of the skin to use on the mounted model\n"
  668. "%thisGuiObjectViewCtrl.setMountSkin(%skinName);\n"
  669. "@endtsexample\n\n"
  670. "@see GuiControl")
  671. {
  672. object->setMountSkin(skinName);
  673. }
  674. //-----------------------------------------------------------------------------
  675. DefineEngineMethod( GuiObjectView, setSeq, void, (const char* indexOrName),,
  676. "@brief Sets the animation to play for the viewed object.\n\n"
  677. "@param indexOrName The index or name of the animation to play.\n"
  678. "@tsexample\n"
  679. "// Set the animation index value, or animation sequence name.\n"
  680. "%indexVal = \"3\";\n"
  681. "//OR:\n"
  682. "%indexVal = \"idle\";\n\n"
  683. "// Inform the GuiObjectView object to set the animation sequence of the object in the control.\n"
  684. "%thisGuiObjectVew.setSeq(%indexVal);\n"
  685. "@endtsexample\n\n"
  686. "@see GuiControl")
  687. {
  688. if( dIsdigit( indexOrName[0] ) )
  689. object->setObjectAnimation( dAtoi( indexOrName ) );
  690. else
  691. object->setObjectAnimation( indexOrName );
  692. }
  693. //-----------------------------------------------------------------------------
  694. DefineEngineMethod( GuiObjectView, setMount, void, ( const char* shapeName, const char* mountNodeIndexOrName),,
  695. "@brief Mounts the given model to the specified mount point of the primary model displayed in this control.\n\n"
  696. "Detailed description\n\n"
  697. "@param shapeName Name of the model to mount.\n"
  698. "@param mountNodeIndexOrName Index or name of the mount point to be mounted to. If index, corresponds to \"mountN\" in your shape where N is the number passed here.\n"
  699. "@tsexample\n"
  700. "// Set the shapeName to mount\n"
  701. "%shapeName = \"GideonGlasses.dts\"\n\n"
  702. "// Set the mount node of the primary model in the control to mount the new shape at\n"
  703. "%mountNodeIndexOrName = \"3\";\n"
  704. "//OR:\n"
  705. "%mountNodeIndexOrName = \"Face\";\n\n"
  706. "// Inform the GuiObjectView object to mount the shape at the specified node.\n"
  707. "%thisGuiObjectView.setMount(%shapeName,%mountNodeIndexOrName);\n"
  708. "@endtsexample\n\n"
  709. "@see GuiControl")
  710. {
  711. if( dIsdigit( mountNodeIndexOrName[0] ) )
  712. object->setMountNode( dAtoi( mountNodeIndexOrName ) );
  713. else
  714. object->setMountNode( mountNodeIndexOrName );
  715. object->setMountedObject( shapeName );
  716. }
  717. //-----------------------------------------------------------------------------
  718. DefineEngineMethod( GuiObjectView, getOrbitDistance, F32, (),,
  719. "@brief Return the current distance at which the camera orbits the object.\n\n"
  720. "@tsexample\n"
  721. "// Request the current orbit distance\n"
  722. "%orbitDistance = %thisGuiObjectView.getOrbitDistance();\n"
  723. "@endtsexample\n\n"
  724. "@return The distance at which the camera orbits the object.\n\n"
  725. "@see GuiControl")
  726. {
  727. return object->getOrbitDistance();
  728. }
  729. //-----------------------------------------------------------------------------
  730. DefineEngineMethod( GuiObjectView, setOrbitDistance, void, (F32 distance),,
  731. "@brief Sets the distance at which the camera orbits the object. Clamped to the acceptable range defined in the class by min and max orbit distances.\n\n"
  732. "Detailed description\n\n"
  733. "@param distance The distance to set the orbit to (will be clamped).\n"
  734. "@tsexample\n"
  735. "// Define the orbit distance value\n"
  736. "%orbitDistance = \"1.5\";\n\n"
  737. "// Inform the GuiObjectView object to set the orbit distance to the defined value\n"
  738. "%thisGuiObjectView.setOrbitDistance(%orbitDistance);\n"
  739. "@endtsexample\n\n"
  740. "@see GuiControl")
  741. {
  742. object->setOrbitDistance( distance );
  743. }
  744. //-----------------------------------------------------------------------------
  745. DefineEngineMethod( GuiObjectView, getCameraSpeed, F32, (),,
  746. "@brief Return the current multiplier for camera zooming and rotation.\n\n"
  747. "@tsexample\n"
  748. "// Request the current camera zooming and rotation multiplier value\n"
  749. "%multiplier = %thisGuiObjectView.getCameraSpeed();\n"
  750. "@endtsexample\n\n"
  751. "@return Camera zooming / rotation multiplier value.\n\n"
  752. "@see GuiControl")
  753. {
  754. return object->getCameraSpeed();
  755. }
  756. //-----------------------------------------------------------------------------
  757. DefineEngineMethod( GuiObjectView, setCameraSpeed, void, (F32 factor),,
  758. "@brief Sets the multiplier for the camera rotation and zoom speed.\n\n"
  759. "@param factor Multiplier for camera rotation and zoom speed.\n"
  760. "@tsexample\n"
  761. "// Set the factor value\n"
  762. "%factor = \"0.75\";\n\n"
  763. "// Inform the GuiObjectView object to set the camera speed.\n"
  764. "%thisGuiObjectView.setCameraSpeed(%factor);\n"
  765. "@endtsexample\n\n"
  766. "@see GuiControl")
  767. {
  768. object->setCameraSpeed( factor );
  769. }
  770. //-----------------------------------------------------------------------------
  771. DefineEngineMethod( GuiObjectView, setLightColor, void, ( LinearColorF color),,
  772. "@brief Set the light color on the sun object used to render the model.\n\n"
  773. "@param color Color of sunlight.\n"
  774. "@tsexample\n"
  775. "// Set the color value for the sun\n"
  776. "%color = \"1.0 0.4 0.5\";\n\n"
  777. "// Inform the GuiObjectView object to change the sun color to the defined value\n"
  778. "%thisGuiObjectView.setLightColor(%color);\n"
  779. "@endtsexample\n\n"
  780. "@see GuiControl")
  781. {
  782. object->setLightColor( color );
  783. }
  784. //-----------------------------------------------------------------------------
  785. DefineEngineMethod( GuiObjectView, setLightAmbient, void, (LinearColorF color),,
  786. "@brief Set the light ambient color on the sun object used to render the model.\n\n"
  787. "@param color Ambient color of sunlight.\n"
  788. "@tsexample\n"
  789. "// Define the sun ambient color value\n"
  790. "%color = \"1.0 0.4 0.6\";\n\n"
  791. "// Inform the GuiObjectView object to set the sun ambient color to the requested value\n"
  792. "%thisGuiObjectView.setLightAmbient(%color);\n"
  793. "@endtsexample\n\n"
  794. "@see GuiControl")
  795. {
  796. object->setLightAmbient( color );
  797. }
  798. //-----------------------------------------------------------------------------
  799. DefineEngineMethod( GuiObjectView, setLightDirection, void, (Point3F direction),,
  800. "@brief Set the light direction from which to light the model.\n\n"
  801. "@param direction XYZ direction from which the light will shine on the model\n"
  802. "@tsexample\n"
  803. "// Set the light direction\n"
  804. "%direction = \"1.0 0.2 0.4\"\n\n"
  805. "// Inform the GuiObjectView object to change the light direction to the defined value\n"
  806. "%thisGuiObjectView.setLightDirection(%direction);\n"
  807. "@endtsexample\n\n"
  808. "@see GuiControl")
  809. {
  810. object->setLightDirection( direction );
  811. }