PageRenderTime 43ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/lltoolplacer.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 534 lines | 403 code | 71 blank | 60 comment | 33 complexity | 6617eedd05afa4e20075c694ba3b2406 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltoolplacer.cpp
  3. * @brief Tool for placing new objects into the world
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. // self header
  28. #include "lltoolplacer.h"
  29. // viewer headers
  30. #include "llbutton.h"
  31. #include "llviewercontrol.h"
  32. //#include "llfirstuse.h"
  33. #include "llfloatertools.h"
  34. #include "llselectmgr.h"
  35. #include "llstatusbar.h"
  36. #include "lltoolcomp.h"
  37. #include "lltoolmgr.h"
  38. #include "llviewerobject.h"
  39. #include "llviewerregion.h"
  40. #include "llviewerwindow.h"
  41. #include "llworld.h"
  42. #include "llui.h"
  43. //Headers added for functions moved from viewer.cpp
  44. #include "llvograss.h"
  45. #include "llvotree.h"
  46. #include "llvolumemessage.h"
  47. #include "llhudmanager.h"
  48. #include "llagent.h"
  49. #include "llagentcamera.h"
  50. #include "llaudioengine.h"
  51. #include "llhudeffecttrail.h"
  52. #include "llviewerobjectlist.h"
  53. #include "llviewercamera.h"
  54. #include "llviewerstats.h"
  55. #include "llvoavatarself.h"
  56. // linden library headers
  57. #include "llprimitive.h"
  58. #include "llwindow.h" // incBusyCount()
  59. #include "material_codes.h"
  60. const LLVector3 DEFAULT_OBJECT_SCALE(0.5f, 0.5f, 0.5f);
  61. //static
  62. LLPCode LLToolPlacer::sObjectType = LL_PCODE_CUBE;
  63. LLToolPlacer::LLToolPlacer()
  64. : LLTool( "Create" )
  65. {
  66. }
  67. BOOL LLToolPlacer::raycastForNewObjPos( S32 x, S32 y, LLViewerObject** hit_obj, S32* hit_face,
  68. BOOL* b_hit_land, LLVector3* ray_start_region, LLVector3* ray_end_region, LLViewerRegion** region )
  69. {
  70. F32 max_dist_from_camera = gSavedSettings.getF32( "MaxSelectDistance" ) - 1.f;
  71. // Viewer-side pick to find the right sim to create the object on.
  72. // First find the surface the object will be created on.
  73. LLPickInfo pick = gViewerWindow->pickImmediate(x, y, FALSE);
  74. // Note: use the frontmost non-flora version because (a) plants usually have lots of alpha and (b) pants' Havok
  75. // representations (if any) are NOT the same as their viewer representation.
  76. if (pick.mPickType == LLPickInfo::PICK_FLORA)
  77. {
  78. *hit_obj = NULL;
  79. *hit_face = -1;
  80. }
  81. else
  82. {
  83. *hit_obj = pick.getObject();
  84. *hit_face = pick.mObjectFace;
  85. }
  86. *b_hit_land = !(*hit_obj) && !pick.mPosGlobal.isExactlyZero();
  87. LLVector3d land_pos_global = pick.mPosGlobal;
  88. // Make sure there's a surface to place the new object on.
  89. BOOL bypass_sim_raycast = FALSE;
  90. LLVector3d surface_pos_global;
  91. if (*b_hit_land)
  92. {
  93. surface_pos_global = land_pos_global;
  94. bypass_sim_raycast = TRUE;
  95. }
  96. else
  97. if (*hit_obj)
  98. {
  99. surface_pos_global = (*hit_obj)->getPositionGlobal();
  100. }
  101. else
  102. {
  103. return FALSE;
  104. }
  105. // Make sure the surface isn't too far away.
  106. LLVector3d ray_start_global = gAgentCamera.getCameraPositionGlobal();
  107. F32 dist_to_surface_sq = (F32)((surface_pos_global - ray_start_global).magVecSquared());
  108. if( dist_to_surface_sq > (max_dist_from_camera * max_dist_from_camera) )
  109. {
  110. return FALSE;
  111. }
  112. // Find the sim where the surface lives.
  113. LLViewerRegion *regionp = LLWorld::getInstance()->getRegionFromPosGlobal(surface_pos_global);
  114. if (!regionp)
  115. {
  116. llwarns << "Trying to add object outside of all known regions!" << llendl;
  117. return FALSE;
  118. }
  119. // Find the simulator-side ray that will be used to place the object accurately
  120. LLVector3d mouse_direction;
  121. mouse_direction.setVec( gViewerWindow->mouseDirectionGlobal( x, y ) );
  122. *region = regionp;
  123. *ray_start_region = regionp->getPosRegionFromGlobal( ray_start_global );
  124. F32 near_clip = LLViewerCamera::getInstance()->getNear() + 0.01f; // Include an epsilon to avoid rounding issues.
  125. *ray_start_region += LLViewerCamera::getInstance()->getAtAxis() * near_clip;
  126. if( bypass_sim_raycast )
  127. {
  128. // Hack to work around Havok's inability to ray cast onto height fields
  129. *ray_end_region = regionp->getPosRegionFromGlobal( surface_pos_global ); // ray end is the viewer's intersection point
  130. }
  131. else
  132. {
  133. LLVector3d ray_end_global = ray_start_global + (1.f + max_dist_from_camera) * mouse_direction; // add an epsilon to the sim version of the ray to avoid rounding problems.
  134. *ray_end_region = regionp->getPosRegionFromGlobal( ray_end_global );
  135. }
  136. return TRUE;
  137. }
  138. BOOL LLToolPlacer::addObject( LLPCode pcode, S32 x, S32 y, U8 use_physics )
  139. {
  140. LLVector3 ray_start_region;
  141. LLVector3 ray_end_region;
  142. LLViewerRegion* regionp = NULL;
  143. BOOL b_hit_land = FALSE;
  144. S32 hit_face = -1;
  145. LLViewerObject* hit_obj = NULL;
  146. U8 state = 0;
  147. BOOL success = raycastForNewObjPos( x, y, &hit_obj, &hit_face, &b_hit_land, &ray_start_region, &ray_end_region, &regionp );
  148. if( !success )
  149. {
  150. return FALSE;
  151. }
  152. if( hit_obj && (hit_obj->isAvatar() || hit_obj->isAttachment()) )
  153. {
  154. // Can't create objects on avatars or attachments
  155. return FALSE;
  156. }
  157. if (NULL == regionp)
  158. {
  159. llwarns << "regionp was NULL; aborting function." << llendl;
  160. return FALSE;
  161. }
  162. if (regionp->getRegionFlags() & REGION_FLAGS_SANDBOX)
  163. {
  164. //LLFirstUse::useSandbox();
  165. }
  166. // Set params for new object based on its PCode.
  167. LLQuaternion rotation;
  168. LLVector3 scale = DEFAULT_OBJECT_SCALE;
  169. U8 material = LL_MCODE_WOOD;
  170. BOOL create_selected = FALSE;
  171. LLVolumeParams volume_params;
  172. switch (pcode)
  173. {
  174. case LL_PCODE_LEGACY_GRASS:
  175. // Randomize size of grass patch
  176. scale.setVec(10.f + ll_frand(20.f), 10.f + ll_frand(20.f), 1.f + ll_frand(2.f));
  177. state = rand() % LLVOGrass::sMaxGrassSpecies;
  178. break;
  179. case LL_PCODE_LEGACY_TREE:
  180. case LL_PCODE_TREE_NEW:
  181. state = rand() % LLVOTree::sMaxTreeSpecies;
  182. break;
  183. case LL_PCODE_SPHERE:
  184. case LL_PCODE_CONE:
  185. case LL_PCODE_CUBE:
  186. case LL_PCODE_CYLINDER:
  187. case LL_PCODE_TORUS:
  188. case LLViewerObject::LL_VO_SQUARE_TORUS:
  189. case LLViewerObject::LL_VO_TRIANGLE_TORUS:
  190. default:
  191. create_selected = TRUE;
  192. break;
  193. }
  194. // Play creation sound
  195. if (gAudiop)
  196. {
  197. gAudiop->triggerSound( LLUUID(gSavedSettings.getString("UISndObjectCreate")),
  198. gAgent.getID(), 1.0f, LLAudioEngine::AUDIO_TYPE_UI);
  199. }
  200. gMessageSystem->newMessageFast(_PREHASH_ObjectAdd);
  201. gMessageSystem->nextBlockFast(_PREHASH_AgentData);
  202. gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
  203. gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
  204. gMessageSystem->addUUIDFast(_PREHASH_GroupID, gAgent.getGroupID());
  205. gMessageSystem->nextBlockFast(_PREHASH_ObjectData);
  206. gMessageSystem->addU8Fast(_PREHASH_Material, material);
  207. U32 flags = 0; // not selected
  208. if (use_physics)
  209. {
  210. flags |= FLAGS_USE_PHYSICS;
  211. }
  212. if (create_selected)
  213. {
  214. flags |= FLAGS_CREATE_SELECTED;
  215. }
  216. gMessageSystem->addU32Fast(_PREHASH_AddFlags, flags );
  217. LLPCode volume_pcode; // ...PCODE_VOLUME, or the original on error
  218. switch (pcode)
  219. {
  220. case LL_PCODE_SPHERE:
  221. rotation.setQuat(90.f * DEG_TO_RAD, LLVector3::y_axis);
  222. volume_params.setType( LL_PCODE_PROFILE_CIRCLE_HALF, LL_PCODE_PATH_CIRCLE );
  223. volume_params.setBeginAndEndS( 0.f, 1.f );
  224. volume_params.setBeginAndEndT( 0.f, 1.f );
  225. volume_params.setRatio ( 1, 1 );
  226. volume_params.setShear ( 0, 0 );
  227. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  228. volume_pcode = LL_PCODE_VOLUME;
  229. break;
  230. case LL_PCODE_TORUS:
  231. rotation.setQuat(90.f * DEG_TO_RAD, LLVector3::y_axis);
  232. volume_params.setType( LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_CIRCLE );
  233. volume_params.setBeginAndEndS( 0.f, 1.f );
  234. volume_params.setBeginAndEndT( 0.f, 1.f );
  235. volume_params.setRatio ( 1.f, 0.25f ); // "top size"
  236. volume_params.setShear ( 0, 0 );
  237. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  238. volume_pcode = LL_PCODE_VOLUME;
  239. break;
  240. case LLViewerObject::LL_VO_SQUARE_TORUS:
  241. rotation.setQuat(90.f * DEG_TO_RAD, LLVector3::y_axis);
  242. volume_params.setType( LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_CIRCLE );
  243. volume_params.setBeginAndEndS( 0.f, 1.f );
  244. volume_params.setBeginAndEndT( 0.f, 1.f );
  245. volume_params.setRatio ( 1.f, 0.25f ); // "top size"
  246. volume_params.setShear ( 0, 0 );
  247. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  248. volume_pcode = LL_PCODE_VOLUME;
  249. break;
  250. case LLViewerObject::LL_VO_TRIANGLE_TORUS:
  251. rotation.setQuat(90.f * DEG_TO_RAD, LLVector3::y_axis);
  252. volume_params.setType( LL_PCODE_PROFILE_EQUALTRI, LL_PCODE_PATH_CIRCLE );
  253. volume_params.setBeginAndEndS( 0.f, 1.f );
  254. volume_params.setBeginAndEndT( 0.f, 1.f );
  255. volume_params.setRatio ( 1.f, 0.25f ); // "top size"
  256. volume_params.setShear ( 0, 0 );
  257. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  258. volume_pcode = LL_PCODE_VOLUME;
  259. break;
  260. case LL_PCODE_SPHERE_HEMI:
  261. volume_params.setType( LL_PCODE_PROFILE_CIRCLE_HALF, LL_PCODE_PATH_CIRCLE );
  262. //volume_params.setBeginAndEndS( 0.5f, 1.f );
  263. volume_params.setBeginAndEndT( 0.f, 0.5f );
  264. volume_params.setRatio ( 1, 1 );
  265. volume_params.setShear ( 0, 0 );
  266. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  267. volume_pcode = LL_PCODE_VOLUME;
  268. break;
  269. case LL_PCODE_CUBE:
  270. volume_params.setType( LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_LINE );
  271. volume_params.setBeginAndEndS( 0.f, 1.f );
  272. volume_params.setBeginAndEndT( 0.f, 1.f );
  273. volume_params.setRatio ( 1, 1 );
  274. volume_params.setShear ( 0, 0 );
  275. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  276. volume_pcode = LL_PCODE_VOLUME;
  277. break;
  278. case LL_PCODE_PRISM:
  279. volume_params.setType( LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_LINE );
  280. volume_params.setBeginAndEndS( 0.f, 1.f );
  281. volume_params.setBeginAndEndT( 0.f, 1.f );
  282. volume_params.setRatio ( 0, 1 );
  283. volume_params.setShear ( -0.5f, 0 );
  284. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  285. volume_pcode = LL_PCODE_VOLUME;
  286. break;
  287. case LL_PCODE_PYRAMID:
  288. volume_params.setType( LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_LINE );
  289. volume_params.setBeginAndEndS( 0.f, 1.f );
  290. volume_params.setBeginAndEndT( 0.f, 1.f );
  291. volume_params.setRatio ( 0, 0 );
  292. volume_params.setShear ( 0, 0 );
  293. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  294. volume_pcode = LL_PCODE_VOLUME;
  295. break;
  296. case LL_PCODE_TETRAHEDRON:
  297. volume_params.setType( LL_PCODE_PROFILE_EQUALTRI, LL_PCODE_PATH_LINE );
  298. volume_params.setBeginAndEndS( 0.f, 1.f );
  299. volume_params.setBeginAndEndT( 0.f, 1.f );
  300. volume_params.setRatio ( 0, 0 );
  301. volume_params.setShear ( 0, 0 );
  302. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  303. volume_pcode = LL_PCODE_VOLUME;
  304. break;
  305. case LL_PCODE_CYLINDER:
  306. volume_params.setType( LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_LINE );
  307. volume_params.setBeginAndEndS( 0.f, 1.f );
  308. volume_params.setBeginAndEndT( 0.f, 1.f );
  309. volume_params.setRatio ( 1, 1 );
  310. volume_params.setShear ( 0, 0 );
  311. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  312. volume_pcode = LL_PCODE_VOLUME;
  313. break;
  314. case LL_PCODE_CYLINDER_HEMI:
  315. volume_params.setType( LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_LINE );
  316. volume_params.setBeginAndEndS( 0.25f, 0.75f );
  317. volume_params.setBeginAndEndT( 0.f, 1.f );
  318. volume_params.setRatio ( 1, 1 );
  319. volume_params.setShear ( 0, 0 );
  320. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  321. volume_pcode = LL_PCODE_VOLUME;
  322. break;
  323. case LL_PCODE_CONE:
  324. volume_params.setType( LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_LINE );
  325. volume_params.setBeginAndEndS( 0.f, 1.f );
  326. volume_params.setBeginAndEndT( 0.f, 1.f );
  327. volume_params.setRatio ( 0, 0 );
  328. volume_params.setShear ( 0, 0 );
  329. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  330. volume_pcode = LL_PCODE_VOLUME;
  331. break;
  332. case LL_PCODE_CONE_HEMI:
  333. volume_params.setType( LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_LINE );
  334. volume_params.setBeginAndEndS( 0.25f, 0.75f );
  335. volume_params.setBeginAndEndT( 0.f, 1.f );
  336. volume_params.setRatio ( 0, 0 );
  337. volume_params.setShear ( 0, 0 );
  338. LLVolumeMessage::packVolumeParams(&volume_params, gMessageSystem);
  339. volume_pcode = LL_PCODE_VOLUME;
  340. break;
  341. default:
  342. LLVolumeMessage::packVolumeParams(0, gMessageSystem);
  343. volume_pcode = pcode;
  344. break;
  345. }
  346. gMessageSystem->addU8Fast(_PREHASH_PCode, volume_pcode);
  347. gMessageSystem->addVector3Fast(_PREHASH_Scale, scale );
  348. gMessageSystem->addQuatFast(_PREHASH_Rotation, rotation );
  349. gMessageSystem->addVector3Fast(_PREHASH_RayStart, ray_start_region );
  350. gMessageSystem->addVector3Fast(_PREHASH_RayEnd, ray_end_region );
  351. gMessageSystem->addU8Fast(_PREHASH_BypassRaycast, (U8)b_hit_land );
  352. gMessageSystem->addU8Fast(_PREHASH_RayEndIsIntersection, (U8)FALSE );
  353. gMessageSystem->addU8Fast(_PREHASH_State, state);
  354. // Limit raycast to a single object.
  355. // Speeds up server raycast + avoid problems with server ray hitting objects
  356. // that were clipped by the near plane or culled on the viewer.
  357. LLUUID ray_target_id;
  358. if( hit_obj )
  359. {
  360. ray_target_id = hit_obj->getID();
  361. }
  362. else
  363. {
  364. ray_target_id.setNull();
  365. }
  366. gMessageSystem->addUUIDFast(_PREHASH_RayTargetID, ray_target_id );
  367. // Pack in name value pairs
  368. gMessageSystem->sendReliable(regionp->getHost());
  369. // Spawns a message, so must be after above send
  370. if (create_selected)
  371. {
  372. LLSelectMgr::getInstance()->deselectAll();
  373. gViewerWindow->getWindow()->incBusyCount();
  374. }
  375. // VEFFECT: AddObject
  376. LLHUDEffectSpiral *effectp = (LLHUDEffectSpiral *)LLHUDManager::getInstance()->createViewerEffect(LLHUDObject::LL_HUD_EFFECT_BEAM, TRUE);
  377. effectp->setSourceObject((LLViewerObject*)gAgentAvatarp);
  378. effectp->setPositionGlobal(regionp->getPosGlobalFromRegion(ray_end_region));
  379. effectp->setDuration(LL_HUD_DUR_SHORT);
  380. effectp->setColor(LLColor4U(gAgent.getEffectColor()));
  381. LLViewerStats::getInstance()->incStat(LLViewerStats::ST_CREATE_COUNT);
  382. return TRUE;
  383. }
  384. // Used by the placer tool to add copies of the current selection.
  385. // Inspired by add_object(). JC
  386. BOOL LLToolPlacer::addDuplicate(S32 x, S32 y)
  387. {
  388. LLVector3 ray_start_region;
  389. LLVector3 ray_end_region;
  390. LLViewerRegion* regionp = NULL;
  391. BOOL b_hit_land = FALSE;
  392. S32 hit_face = -1;
  393. LLViewerObject* hit_obj = NULL;
  394. BOOL success = raycastForNewObjPos( x, y, &hit_obj, &hit_face, &b_hit_land, &ray_start_region, &ray_end_region, &regionp );
  395. if( !success )
  396. {
  397. make_ui_sound("UISndInvalidOp");
  398. return FALSE;
  399. }
  400. if( hit_obj && (hit_obj->isAvatar() || hit_obj->isAttachment()) )
  401. {
  402. // Can't create objects on avatars or attachments
  403. make_ui_sound("UISndInvalidOp");
  404. return FALSE;
  405. }
  406. // Limit raycast to a single object.
  407. // Speeds up server raycast + avoid problems with server ray hitting objects
  408. // that were clipped by the near plane or culled on the viewer.
  409. LLUUID ray_target_id;
  410. if( hit_obj )
  411. {
  412. ray_target_id = hit_obj->getID();
  413. }
  414. else
  415. {
  416. ray_target_id.setNull();
  417. }
  418. LLSelectMgr::getInstance()->selectDuplicateOnRay(ray_start_region,
  419. ray_end_region,
  420. b_hit_land, // suppress raycast
  421. FALSE, // intersection
  422. ray_target_id,
  423. gSavedSettings.getBOOL("CreateToolCopyCenters"),
  424. gSavedSettings.getBOOL("CreateToolCopyRotates"),
  425. FALSE); // select copy
  426. if (regionp
  427. && (regionp->getRegionFlags() & REGION_FLAGS_SANDBOX))
  428. {
  429. //LLFirstUse::useSandbox();
  430. }
  431. return TRUE;
  432. }
  433. BOOL LLToolPlacer::placeObject(S32 x, S32 y, MASK mask)
  434. {
  435. BOOL added = TRUE;
  436. if (gSavedSettings.getBOOL("CreateToolCopySelection"))
  437. {
  438. added = addDuplicate(x, y);
  439. }
  440. else
  441. {
  442. added = addObject( sObjectType, x, y, FALSE );
  443. }
  444. // ...and go back to the default tool
  445. if (added && !gSavedSettings.getBOOL("CreateToolKeepSelected"))
  446. {
  447. LLToolMgr::getInstance()->getCurrentToolset()->selectTool( LLToolCompTranslate::getInstance() );
  448. }
  449. return added;
  450. }
  451. BOOL LLToolPlacer::handleHover(S32 x, S32 y, MASK mask)
  452. {
  453. lldebugst(LLERR_USER_INPUT) << "hover handled by LLToolPlacer" << llendl;
  454. gViewerWindow->setCursor(UI_CURSOR_TOOLCREATE);
  455. return TRUE;
  456. }
  457. void LLToolPlacer::handleSelect()
  458. {
  459. gFloaterTools->setStatusText("place");
  460. }
  461. void LLToolPlacer::handleDeselect()
  462. {
  463. }