llviewerjoint.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:17k
源码类别:

游戏引擎

开发平台:

C++ Builder

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