PageRenderTime 100ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llviewerjoint.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 619 lines | 280 code | 107 blank | 232 comment | 40 complexity | 255b6f6b2f1918b3075c474bdc641e07 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llviewerjoint.cpp
  3. * @brief Implementation of LLViewerJoint class
  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. //-----------------------------------------------------------------------------
  27. // Header Files
  28. //-----------------------------------------------------------------------------
  29. #include "llviewerprecompiledheaders.h"
  30. #include "llviewerjoint.h"
  31. #include "llgl.h"
  32. #include "llrender.h"
  33. #include "llmath.h"
  34. #include "llglheaders.h"
  35. #include "llrendersphere.h"
  36. #include "llvoavatar.h"
  37. #include "pipeline.h"
  38. #define DEFAULT_LOD 0.0f
  39. const S32 MIN_PIXEL_AREA_3PASS_HAIR = 64*64;
  40. //-----------------------------------------------------------------------------
  41. // Static Data
  42. //-----------------------------------------------------------------------------
  43. BOOL LLViewerJoint::sDisableLOD = FALSE;
  44. //-----------------------------------------------------------------------------
  45. // LLViewerJoint()
  46. // Class Constructor
  47. //-----------------------------------------------------------------------------
  48. LLViewerJoint::LLViewerJoint()
  49. : LLJoint()
  50. {
  51. init();
  52. }
  53. //-----------------------------------------------------------------------------
  54. // LLViewerJoint()
  55. // Class Constructor
  56. //-----------------------------------------------------------------------------
  57. LLViewerJoint::LLViewerJoint(const std::string &name, LLJoint *parent)
  58. : LLJoint(name, parent)
  59. {
  60. init();
  61. }
  62. void LLViewerJoint::init()
  63. {
  64. mValid = FALSE;
  65. mComponents = SC_JOINT | SC_BONE | SC_AXES;
  66. mMinPixelArea = DEFAULT_LOD;
  67. mPickName = PN_DEFAULT;
  68. mVisible = TRUE;
  69. mMeshID = 0;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // ~LLViewerJoint()
  73. // Class Destructor
  74. //-----------------------------------------------------------------------------
  75. LLViewerJoint::~LLViewerJoint()
  76. {
  77. }
  78. //--------------------------------------------------------------------
  79. // setValid()
  80. //--------------------------------------------------------------------
  81. void LLViewerJoint::setValid( BOOL valid, BOOL recursive )
  82. {
  83. //----------------------------------------------------------------
  84. // set visibility for this joint
  85. //----------------------------------------------------------------
  86. mValid = valid;
  87. //----------------------------------------------------------------
  88. // set visibility for children
  89. //----------------------------------------------------------------
  90. if (recursive)
  91. {
  92. for (child_list_t::iterator iter = mChildren.begin();
  93. iter != mChildren.end(); ++iter)
  94. {
  95. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  96. joint->setValid(valid, TRUE);
  97. }
  98. }
  99. }
  100. //--------------------------------------------------------------------
  101. // renderSkeleton()
  102. // DEBUG (UNUSED)
  103. //--------------------------------------------------------------------
  104. // void LLViewerJoint::renderSkeleton(BOOL recursive)
  105. // {
  106. // F32 nc = 0.57735f;
  107. // //----------------------------------------------------------------
  108. // // push matrix stack
  109. // //----------------------------------------------------------------
  110. // gGL.pushMatrix();
  111. // //----------------------------------------------------------------
  112. // // render the bone to my parent
  113. // //----------------------------------------------------------------
  114. // if (mComponents & SC_BONE)
  115. // {
  116. // drawBone();
  117. // }
  118. // //----------------------------------------------------------------
  119. // // offset to joint position and
  120. // // rotate to our orientation
  121. // //----------------------------------------------------------------
  122. // gGL.loadIdentity();
  123. // gGL.multMatrix( &getWorldMatrix().mMatrix[0][0] );
  124. // //----------------------------------------------------------------
  125. // // render joint axes
  126. // //----------------------------------------------------------------
  127. // if (mComponents & SC_AXES)
  128. // {
  129. // gGL.begin(LLRender::LINES);
  130. // gGL.color3f( 1.0f, 0.0f, 0.0f );
  131. // gGL.vertex3f( 0.0f, 0.0f, 0.0f );
  132. // gGL.vertex3f( 0.1f, 0.0f, 0.0f );
  133. // gGL.color3f( 0.0f, 1.0f, 0.0f );
  134. // gGL.vertex3f( 0.0f, 0.0f, 0.0f );
  135. // gGL.vertex3f( 0.0f, 0.1f, 0.0f );
  136. // gGL.color3f( 0.0f, 0.0f, 1.0f );
  137. // gGL.vertex3f( 0.0f, 0.0f, 0.0f );
  138. // gGL.vertex3f( 0.0f, 0.0f, 0.1f );
  139. // gGL.end();
  140. // }
  141. // //----------------------------------------------------------------
  142. // // render the joint graphic
  143. // //----------------------------------------------------------------
  144. // if (mComponents & SC_JOINT)
  145. // {
  146. // gGL.color3f( 1.0f, 1.0f, 0.0f );
  147. // gGL.begin(LLRender::TRIANGLES);
  148. // // joint top half
  149. // glNormal3f(nc, nc, nc);
  150. // gGL.vertex3f(0.0f, 0.0f, 0.05f);
  151. // gGL.vertex3f(0.05f, 0.0f, 0.0f);
  152. // gGL.vertex3f(0.0f, 0.05f, 0.0f);
  153. // glNormal3f(-nc, nc, nc);
  154. // gGL.vertex3f(0.0f, 0.0f, 0.05f);
  155. // gGL.vertex3f(0.0f, 0.05f, 0.0f);
  156. // gGL.vertex3f(-0.05f, 0.0f, 0.0f);
  157. // glNormal3f(-nc, -nc, nc);
  158. // gGL.vertex3f(0.0f, 0.0f, 0.05f);
  159. // gGL.vertex3f(-0.05f, 0.0f, 0.0f);
  160. // gGL.vertex3f(0.0f, -0.05f, 0.0f);
  161. // glNormal3f(nc, -nc, nc);
  162. // gGL.vertex3f(0.0f, 0.0f, 0.05f);
  163. // gGL.vertex3f(0.0f, -0.05f, 0.0f);
  164. // gGL.vertex3f(0.05f, 0.0f, 0.0f);
  165. // // joint bottom half
  166. // glNormal3f(nc, nc, -nc);
  167. // gGL.vertex3f(0.0f, 0.0f, -0.05f);
  168. // gGL.vertex3f(0.0f, 0.05f, 0.0f);
  169. // gGL.vertex3f(0.05f, 0.0f, 0.0f);
  170. // glNormal3f(-nc, nc, -nc);
  171. // gGL.vertex3f(0.0f, 0.0f, -0.05f);
  172. // gGL.vertex3f(-0.05f, 0.0f, 0.0f);
  173. // gGL.vertex3f(0.0f, 0.05f, 0.0f);
  174. // glNormal3f(-nc, -nc, -nc);
  175. // gGL.vertex3f(0.0f, 0.0f, -0.05f);
  176. // gGL.vertex3f(0.0f, -0.05f, 0.0f);
  177. // gGL.vertex3f(-0.05f, 0.0f, 0.0f);
  178. // glNormal3f(nc, -nc, -nc);
  179. // gGL.vertex3f(0.0f, 0.0f, -0.05f);
  180. // gGL.vertex3f(0.05f, 0.0f, 0.0f);
  181. // gGL.vertex3f(0.0f, -0.05f, 0.0f);
  182. // gGL.end();
  183. // }
  184. // //----------------------------------------------------------------
  185. // // render children
  186. // //----------------------------------------------------------------
  187. // if (recursive)
  188. // {
  189. // for (child_list_t::iterator iter = mChildren.begin();
  190. // iter != mChildren.end(); ++iter)
  191. // {
  192. // LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  193. // joint->renderSkeleton();
  194. // }
  195. // }
  196. // //----------------------------------------------------------------
  197. // // pop matrix stack
  198. // //----------------------------------------------------------------
  199. // gGL.popMatrix();
  200. // }
  201. //--------------------------------------------------------------------
  202. // render()
  203. //--------------------------------------------------------------------
  204. U32 LLViewerJoint::render( F32 pixelArea, BOOL first_pass, BOOL is_dummy )
  205. {
  206. stop_glerror();
  207. U32 triangle_count = 0;
  208. //----------------------------------------------------------------
  209. // ignore invisible objects
  210. //----------------------------------------------------------------
  211. if ( mValid )
  212. {
  213. //----------------------------------------------------------------
  214. // if object is transparent, defer it, otherwise
  215. // give the joint subclass a chance to draw itself
  216. //----------------------------------------------------------------
  217. if ( is_dummy )
  218. {
  219. triangle_count += drawShape( pixelArea, first_pass, is_dummy );
  220. }
  221. else if (LLPipeline::sShadowRender)
  222. {
  223. triangle_count += drawShape(pixelArea, first_pass, is_dummy );
  224. }
  225. else if ( isTransparent() && !LLPipeline::sReflectionRender)
  226. {
  227. // Hair and Skirt
  228. if ((pixelArea > MIN_PIXEL_AREA_3PASS_HAIR))
  229. {
  230. // render all three passes
  231. LLGLDisable cull(GL_CULL_FACE);
  232. // first pass renders without writing to the z buffer
  233. {
  234. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
  235. triangle_count += drawShape( pixelArea, first_pass, is_dummy );
  236. }
  237. // second pass writes to z buffer only
  238. gGL.setColorMask(false, false);
  239. {
  240. triangle_count += drawShape( pixelArea, FALSE, is_dummy );
  241. }
  242. // third past respects z buffer and writes color
  243. gGL.setColorMask(true, false);
  244. {
  245. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
  246. triangle_count += drawShape( pixelArea, FALSE, is_dummy );
  247. }
  248. }
  249. else
  250. {
  251. // Render Inside (no Z buffer write)
  252. glCullFace(GL_FRONT);
  253. {
  254. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
  255. triangle_count += drawShape( pixelArea, first_pass, is_dummy );
  256. }
  257. // Render Outside (write to the Z buffer)
  258. glCullFace(GL_BACK);
  259. {
  260. triangle_count += drawShape( pixelArea, FALSE, is_dummy );
  261. }
  262. }
  263. }
  264. else
  265. {
  266. // set up render state
  267. triangle_count += drawShape( pixelArea, first_pass );
  268. }
  269. }
  270. //----------------------------------------------------------------
  271. // render children
  272. //----------------------------------------------------------------
  273. for (child_list_t::iterator iter = mChildren.begin();
  274. iter != mChildren.end(); ++iter)
  275. {
  276. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  277. F32 jointLOD = joint->getLOD();
  278. if (pixelArea >= jointLOD || sDisableLOD)
  279. {
  280. triangle_count += joint->render( pixelArea, TRUE, is_dummy );
  281. if (jointLOD != DEFAULT_LOD)
  282. {
  283. break;
  284. }
  285. }
  286. }
  287. return triangle_count;
  288. }
  289. //--------------------------------------------------------------------
  290. // drawBone()
  291. // DEBUG (UNUSED)
  292. //--------------------------------------------------------------------
  293. // void LLViewerJoint::drawBone()
  294. // {
  295. // if ( mParent == NULL )
  296. // return;
  297. // F32 boneSize = 0.02f;
  298. // // rotate to point to child (bone direction)
  299. // gGL.pushMatrix();
  300. // LLVector3 boneX = getPosition();
  301. // F32 length = boneX.normVec();
  302. // LLVector3 boneZ(1.0f, 0.0f, 1.0f);
  303. // LLVector3 boneY = boneZ % boneX;
  304. // boneY.normVec();
  305. // boneZ = boneX % boneY;
  306. // LLMatrix4 rotateMat;
  307. // rotateMat.setFwdRow( boneX );
  308. // rotateMat.setLeftRow( boneY );
  309. // rotateMat.setUpRow( boneZ );
  310. // gGL.multMatrix( &rotateMat.mMatrix[0][0] );
  311. // // render the bone
  312. // gGL.color3f( 0.5f, 0.5f, 0.0f );
  313. // gGL.begin(LLRender::TRIANGLES);
  314. // gGL.vertex3f( length, 0.0f, 0.0f);
  315. // gGL.vertex3f( 0.0f, boneSize, 0.0f);
  316. // gGL.vertex3f( 0.0f, 0.0f, boneSize);
  317. // gGL.vertex3f( length, 0.0f, 0.0f);
  318. // gGL.vertex3f( 0.0f, 0.0f, -boneSize);
  319. // gGL.vertex3f( 0.0f, boneSize, 0.0f);
  320. // gGL.vertex3f( length, 0.0f, 0.0f);
  321. // gGL.vertex3f( 0.0f, -boneSize, 0.0f);
  322. // gGL.vertex3f( 0.0f, 0.0f, -boneSize);
  323. // gGL.vertex3f( length, 0.0f, 0.0f);
  324. // gGL.vertex3f( 0.0f, 0.0f, boneSize);
  325. // gGL.vertex3f( 0.0f, -boneSize, 0.0f);
  326. // gGL.end();
  327. // // restore matrix
  328. // gGL.popMatrix();
  329. // }
  330. //--------------------------------------------------------------------
  331. // isTransparent()
  332. //--------------------------------------------------------------------
  333. BOOL LLViewerJoint::isTransparent()
  334. {
  335. return FALSE;
  336. }
  337. //--------------------------------------------------------------------
  338. // drawShape()
  339. //--------------------------------------------------------------------
  340. U32 LLViewerJoint::drawShape( F32 pixelArea, BOOL first_pass, BOOL is_dummy )
  341. {
  342. return 0;
  343. }
  344. //--------------------------------------------------------------------
  345. // setSkeletonComponents()
  346. //--------------------------------------------------------------------
  347. void LLViewerJoint::setSkeletonComponents( U32 comp, BOOL recursive )
  348. {
  349. mComponents = comp;
  350. if (recursive)
  351. {
  352. for (child_list_t::iterator iter = mChildren.begin();
  353. iter != mChildren.end(); ++iter)
  354. {
  355. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  356. joint->setSkeletonComponents(comp, recursive);
  357. }
  358. }
  359. }
  360. void LLViewerJoint::updateFaceSizes(U32 &num_vertices, U32& num_indices, F32 pixel_area)
  361. {
  362. for (child_list_t::iterator iter = mChildren.begin();
  363. iter != mChildren.end(); ++iter)
  364. {
  365. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  366. joint->updateFaceSizes(num_vertices, num_indices, pixel_area);
  367. }
  368. }
  369. void LLViewerJoint::updateFaceData(LLFace *face, F32 pixel_area, BOOL damp_wind, bool terse_update)
  370. {
  371. for (child_list_t::iterator iter = mChildren.begin();
  372. iter != mChildren.end(); ++iter)
  373. {
  374. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  375. joint->updateFaceData(face, pixel_area, damp_wind, terse_update);
  376. }
  377. }
  378. void LLViewerJoint::updateJointGeometry()
  379. {
  380. for (child_list_t::iterator iter = mChildren.begin();
  381. iter != mChildren.end(); ++iter)
  382. {
  383. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  384. joint->updateJointGeometry();
  385. }
  386. }
  387. BOOL LLViewerJoint::updateLOD(F32 pixel_area, BOOL activate)
  388. {
  389. BOOL lod_changed = FALSE;
  390. BOOL found_lod = FALSE;
  391. for (child_list_t::iterator iter = mChildren.begin();
  392. iter != mChildren.end(); ++iter)
  393. {
  394. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  395. F32 jointLOD = joint->getLOD();
  396. if (found_lod || jointLOD == DEFAULT_LOD)
  397. {
  398. // we've already found a joint to enable, so enable the rest as alternatives
  399. lod_changed |= joint->updateLOD(pixel_area, TRUE);
  400. }
  401. else
  402. {
  403. if (pixel_area >= jointLOD || sDisableLOD)
  404. {
  405. lod_changed |= joint->updateLOD(pixel_area, TRUE);
  406. found_lod = TRUE;
  407. }
  408. else
  409. {
  410. lod_changed |= joint->updateLOD(pixel_area, FALSE);
  411. }
  412. }
  413. }
  414. return lod_changed;
  415. }
  416. void LLViewerJoint::dump()
  417. {
  418. for (child_list_t::iterator iter = mChildren.begin();
  419. iter != mChildren.end(); ++iter)
  420. {
  421. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  422. joint->dump();
  423. }
  424. }
  425. void LLViewerJoint::setVisible(BOOL visible, BOOL recursive)
  426. {
  427. mVisible = visible;
  428. if (recursive)
  429. {
  430. for (child_list_t::iterator iter = mChildren.begin();
  431. iter != mChildren.end(); ++iter)
  432. {
  433. LLViewerJoint* joint = (LLViewerJoint*)(*iter);
  434. joint->setVisible(visible, recursive);
  435. }
  436. }
  437. }
  438. void LLViewerJoint::setMeshesToChildren()
  439. {
  440. removeAllChildren();
  441. for (std::vector<LLViewerJointMesh*>::iterator iter = mMeshParts.begin();
  442. iter != mMeshParts.end(); iter++)
  443. {
  444. addChild((LLViewerJointMesh *) *iter);
  445. }
  446. }
  447. //-----------------------------------------------------------------------------
  448. // LLViewerJointCollisionVolume()
  449. //-----------------------------------------------------------------------------
  450. LLViewerJointCollisionVolume::LLViewerJointCollisionVolume()
  451. {
  452. mUpdateXform = FALSE;
  453. }
  454. LLViewerJointCollisionVolume::LLViewerJointCollisionVolume(const std::string &name, LLJoint *parent) : LLViewerJoint(name, parent)
  455. {
  456. }
  457. void LLViewerJointCollisionVolume::renderCollision()
  458. {
  459. updateWorldMatrix();
  460. gGL.pushMatrix();
  461. gGL.multMatrix( &mXform.getWorldMatrix().mMatrix[0][0] );
  462. gGL.diffuseColor3f( 0.f, 0.f, 1.f );
  463. gGL.begin(LLRender::LINES);
  464. LLVector3 v[] =
  465. {
  466. LLVector3(1,0,0),
  467. LLVector3(-1,0,0),
  468. LLVector3(0,1,0),
  469. LLVector3(0,-1,0),
  470. LLVector3(0,0,-1),
  471. LLVector3(0,0,1),
  472. };
  473. //sides
  474. gGL.vertex3fv(v[0].mV);
  475. gGL.vertex3fv(v[2].mV);
  476. gGL.vertex3fv(v[0].mV);
  477. gGL.vertex3fv(v[3].mV);
  478. gGL.vertex3fv(v[1].mV);
  479. gGL.vertex3fv(v[2].mV);
  480. gGL.vertex3fv(v[1].mV);
  481. gGL.vertex3fv(v[3].mV);
  482. //top
  483. gGL.vertex3fv(v[0].mV);
  484. gGL.vertex3fv(v[4].mV);
  485. gGL.vertex3fv(v[1].mV);
  486. gGL.vertex3fv(v[4].mV);
  487. gGL.vertex3fv(v[2].mV);
  488. gGL.vertex3fv(v[4].mV);
  489. gGL.vertex3fv(v[3].mV);
  490. gGL.vertex3fv(v[4].mV);
  491. //bottom
  492. gGL.vertex3fv(v[0].mV);
  493. gGL.vertex3fv(v[5].mV);
  494. gGL.vertex3fv(v[1].mV);
  495. gGL.vertex3fv(v[5].mV);
  496. gGL.vertex3fv(v[2].mV);
  497. gGL.vertex3fv(v[5].mV);
  498. gGL.vertex3fv(v[3].mV);
  499. gGL.vertex3fv(v[5].mV);
  500. gGL.end();
  501. gGL.popMatrix();
  502. }
  503. LLVector3 LLViewerJointCollisionVolume::getVolumePos(LLVector3 &offset)
  504. {
  505. mUpdateXform = TRUE;
  506. LLVector3 result = offset;
  507. result.scaleVec(getScale());
  508. result.rotVec(getWorldRotation());
  509. result += getWorldPosition();
  510. return result;
  511. }
  512. // End