PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llface.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2472 lines | 1890 code | 378 blank | 204 comment | 310 complexity | 5eb7f045053bb5b3bac2320a7edd0b1d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llface.cpp
  3. * @brief LLFace class implementation
  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. #include "lldrawable.h" // lldrawable needs to be included before llface
  28. #include "llface.h"
  29. #include "llviewertextureanim.h"
  30. #include "llviewercontrol.h"
  31. #include "llvolume.h"
  32. #include "m3math.h"
  33. #include "llmatrix4a.h"
  34. #include "v3color.h"
  35. #include "lldrawpoolavatar.h"
  36. #include "lldrawpoolbump.h"
  37. #include "llgl.h"
  38. #include "llrender.h"
  39. #include "lllightconstants.h"
  40. #include "llsky.h"
  41. #include "llviewercamera.h"
  42. #include "llviewertexturelist.h"
  43. #include "llvosky.h"
  44. #include "llvovolume.h"
  45. #include "pipeline.h"
  46. #include "llviewerregion.h"
  47. #include "llviewerwindow.h"
  48. #define LL_MAX_INDICES_COUNT 1000000
  49. BOOL LLFace::sSafeRenderSelect = TRUE; // FALSE
  50. #define DOTVEC(a,b) (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1] + a.mV[2]*b.mV[2])
  51. /*
  52. For each vertex, given:
  53. B - binormal
  54. T - tangent
  55. N - normal
  56. P - position
  57. The resulting texture coordinate <u,v> is:
  58. u = 2(B dot P)
  59. v = 2(T dot P)
  60. */
  61. void planarProjection(LLVector2 &tc, const LLVector4a& normal,
  62. const LLVector4a &center, const LLVector4a& vec)
  63. {
  64. LLVector4a binormal;
  65. F32 d = normal[0];
  66. if (d >= 0.5f || d <= -0.5f)
  67. {
  68. if (d < 0)
  69. {
  70. binormal.set(0,-1,0);
  71. }
  72. else
  73. {
  74. binormal.set(0, 1, 0);
  75. }
  76. }
  77. else
  78. {
  79. if (normal[1] > 0)
  80. {
  81. binormal.set(-1,0,0);
  82. }
  83. else
  84. {
  85. binormal.set(1,0,0);
  86. }
  87. }
  88. LLVector4a tangent;
  89. tangent.setCross3(binormal,normal);
  90. tc.mV[1] = -((tangent.dot3(vec).getF32())*2 - 0.5f);
  91. tc.mV[0] = 1.0f+((binormal.dot3(vec).getF32())*2 - 0.5f);
  92. }
  93. void sphericalProjection(LLVector2 &tc, const LLVector4a& normal,
  94. const LLVector4a &mCenter, const LLVector4a& vec)
  95. { //BROKEN
  96. /*tc.mV[0] = acosf(vd.mNormal * LLVector3(1,0,0))/3.14159f;
  97. tc.mV[1] = acosf(vd.mNormal * LLVector3(0,0,1))/6.284f;
  98. if (vd.mNormal.mV[1] > 0)
  99. {
  100. tc.mV[1] = 1.0f-tc.mV[1];
  101. }*/
  102. }
  103. void cylindricalProjection(LLVector2 &tc, const LLVector4a& normal, const LLVector4a &mCenter, const LLVector4a& vec)
  104. { //BROKEN
  105. /*LLVector3 binormal;
  106. float d = vd.mNormal * LLVector3(1,0,0);
  107. if (d >= 0.5f || d <= -0.5f)
  108. {
  109. binormal = LLVector3(0,1,0);
  110. }
  111. else{
  112. binormal = LLVector3(1,0,0);
  113. }
  114. LLVector3 tangent = binormal % vd.mNormal;
  115. tc.mV[1] = -((tangent*vec)*2 - 0.5f);
  116. tc.mV[0] = acosf(vd.mNormal * LLVector3(1,0,0))/6.284f;
  117. if (vd.mNormal.mV[1] < 0)
  118. {
  119. tc.mV[0] = 1.0f-tc.mV[0];
  120. }*/
  121. }
  122. ////////////////////
  123. //
  124. // LLFace implementation
  125. //
  126. void LLFace::init(LLDrawable* drawablep, LLViewerObject* objp)
  127. {
  128. mLastUpdateTime = gFrameTimeSeconds;
  129. mLastMoveTime = 0.f;
  130. mLastSkinTime = gFrameTimeSeconds;
  131. mVSize = 0.f;
  132. mPixelArea = 16.f;
  133. mState = GLOBAL;
  134. mDrawPoolp = NULL;
  135. mPoolType = 0;
  136. mCenterLocal = objp->getPosition();
  137. mCenterAgent = drawablep->getPositionAgent();
  138. mDistance = 0.f;
  139. mGeomCount = 0;
  140. mGeomIndex = 0;
  141. mIndicesCount = 0;
  142. mIndicesIndex = 0;
  143. mIndexInTex = 0;
  144. mTexture = NULL;
  145. mTEOffset = -1;
  146. mTextureIndex = 255;
  147. setDrawable(drawablep);
  148. mVObjp = objp;
  149. mReferenceIndex = -1;
  150. mTextureMatrix = NULL;
  151. mDrawInfo = NULL;
  152. mFaceColor = LLColor4(1,0,0,1);
  153. mLastVertexBuffer = mVertexBuffer;
  154. mLastGeomCount = mGeomCount;
  155. mLastGeomIndex = mGeomIndex;
  156. mLastIndicesCount = mIndicesCount;
  157. mLastIndicesIndex = mIndicesIndex;
  158. mImportanceToCamera = 0.f ;
  159. mBoundingSphereRadius = 0.0f ;
  160. mAtlasInfop = NULL ;
  161. mUsingAtlas = FALSE ;
  162. mHasMedia = FALSE ;
  163. }
  164. void LLFace::destroy()
  165. {
  166. if (gDebugGL)
  167. {
  168. gPipeline.checkReferences(this);
  169. }
  170. if(mTexture.notNull())
  171. {
  172. mTexture->removeFace(this) ;
  173. }
  174. if (mDrawPoolp)
  175. {
  176. if (this->isState(LLFace::RIGGED) && mDrawPoolp->getType() == LLDrawPool::POOL_AVATAR)
  177. {
  178. ((LLDrawPoolAvatar*) mDrawPoolp)->removeRiggedFace(this);
  179. }
  180. else
  181. {
  182. mDrawPoolp->removeFace(this);
  183. }
  184. mDrawPoolp = NULL;
  185. }
  186. if (mTextureMatrix)
  187. {
  188. delete mTextureMatrix;
  189. mTextureMatrix = NULL;
  190. if (mDrawablep.notNull())
  191. {
  192. LLSpatialGroup* group = mDrawablep->getSpatialGroup();
  193. if (group)
  194. {
  195. group->dirtyGeom();
  196. gPipeline.markRebuild(group, TRUE);
  197. }
  198. }
  199. }
  200. setDrawInfo(NULL);
  201. removeAtlas();
  202. mDrawablep = NULL;
  203. mVObjp = NULL;
  204. }
  205. // static
  206. void LLFace::initClass()
  207. {
  208. }
  209. void LLFace::setWorldMatrix(const LLMatrix4 &mat)
  210. {
  211. llerrs << "Faces on this drawable are not independently modifiable\n" << llendl;
  212. }
  213. void LLFace::setPool(LLFacePool* pool)
  214. {
  215. mDrawPoolp = pool;
  216. }
  217. void LLFace::setPool(LLFacePool* new_pool, LLViewerTexture *texturep)
  218. {
  219. LLMemType mt1(LLMemType::MTYPE_DRAWABLE);
  220. if (!new_pool)
  221. {
  222. llerrs << "Setting pool to null!" << llendl;
  223. }
  224. if (new_pool != mDrawPoolp)
  225. {
  226. // Remove from old pool
  227. if (mDrawPoolp)
  228. {
  229. mDrawPoolp->removeFace(this);
  230. if (mDrawablep)
  231. {
  232. gPipeline.markRebuild(mDrawablep, LLDrawable::REBUILD_ALL, TRUE);
  233. }
  234. }
  235. mGeomIndex = 0;
  236. // Add to new pool
  237. if (new_pool)
  238. {
  239. new_pool->addFace(this);
  240. }
  241. mDrawPoolp = new_pool;
  242. }
  243. setTexture(texturep) ;
  244. }
  245. void LLFace::setTexture(LLViewerTexture* tex)
  246. {
  247. if(mTexture == tex)
  248. {
  249. return ;
  250. }
  251. if(mTexture.notNull())
  252. {
  253. mTexture->removeFace(this) ;
  254. removeAtlas() ;
  255. }
  256. if(tex)
  257. {
  258. tex->addFace(this) ;
  259. }
  260. mTexture = tex ;
  261. }
  262. void LLFace::dirtyTexture()
  263. {
  264. gPipeline.markTextured(getDrawable());
  265. }
  266. void LLFace::switchTexture(LLViewerTexture* new_texture)
  267. {
  268. if(mTexture == new_texture)
  269. {
  270. return ;
  271. }
  272. if(!new_texture)
  273. {
  274. llerrs << "Can not switch to a null texture." << llendl;
  275. return;
  276. }
  277. new_texture->addTextureStats(mTexture->getMaxVirtualSize()) ;
  278. getViewerObject()->changeTEImage(mTEOffset, new_texture) ;
  279. setTexture(new_texture) ;
  280. dirtyTexture();
  281. }
  282. void LLFace::setTEOffset(const S32 te_offset)
  283. {
  284. mTEOffset = te_offset;
  285. }
  286. void LLFace::setFaceColor(const LLColor4& color)
  287. {
  288. mFaceColor = color;
  289. setState(USE_FACE_COLOR);
  290. }
  291. void LLFace::unsetFaceColor()
  292. {
  293. clearState(USE_FACE_COLOR);
  294. }
  295. void LLFace::setDrawable(LLDrawable *drawable)
  296. {
  297. mDrawablep = drawable;
  298. mXform = &drawable->mXform;
  299. }
  300. void LLFace::setSize(S32 num_vertices, S32 num_indices, bool align)
  301. {
  302. if (align)
  303. {
  304. //allocate vertices in blocks of 4 for alignment
  305. num_vertices = (num_vertices + 0x3) & ~0x3;
  306. }
  307. if (mGeomCount != num_vertices ||
  308. mIndicesCount != num_indices)
  309. {
  310. mGeomCount = num_vertices;
  311. mIndicesCount = num_indices;
  312. mVertexBuffer = NULL;
  313. mLastVertexBuffer = NULL;
  314. }
  315. llassert(verify());
  316. }
  317. void LLFace::setGeomIndex(U16 idx)
  318. {
  319. if (mGeomIndex != idx)
  320. {
  321. mGeomIndex = idx;
  322. mVertexBuffer = NULL;
  323. }
  324. }
  325. void LLFace::setTextureIndex(U8 index)
  326. {
  327. if (index != mTextureIndex)
  328. {
  329. mTextureIndex = index;
  330. if (mTextureIndex != 255)
  331. {
  332. mDrawablep->setState(LLDrawable::REBUILD_POSITION);
  333. }
  334. else
  335. {
  336. if (mDrawInfo && !mDrawInfo->mTextureList.empty())
  337. {
  338. llerrs << "Face with no texture index references indexed texture draw info." << llendl;
  339. }
  340. }
  341. }
  342. }
  343. void LLFace::setIndicesIndex(S32 idx)
  344. {
  345. if (mIndicesIndex != idx)
  346. {
  347. mIndicesIndex = idx;
  348. mVertexBuffer = NULL;
  349. }
  350. }
  351. //============================================================================
  352. U16 LLFace::getGeometryAvatar(
  353. LLStrider<LLVector3> &vertices,
  354. LLStrider<LLVector3> &normals,
  355. LLStrider<LLVector2> &tex_coords,
  356. LLStrider<F32> &vertex_weights,
  357. LLStrider<LLVector4> &clothing_weights)
  358. {
  359. LLMemType mt1(LLMemType::MTYPE_DRAWABLE);
  360. if (mVertexBuffer.notNull())
  361. {
  362. mVertexBuffer->getVertexStrider (vertices, mGeomIndex, mGeomCount);
  363. mVertexBuffer->getNormalStrider (normals, mGeomIndex, mGeomCount);
  364. mVertexBuffer->getTexCoord0Strider (tex_coords, mGeomIndex, mGeomCount);
  365. mVertexBuffer->getWeightStrider(vertex_weights, mGeomIndex, mGeomCount);
  366. mVertexBuffer->getClothWeightStrider(clothing_weights, mGeomIndex, mGeomCount);
  367. }
  368. return mGeomIndex;
  369. }
  370. U16 LLFace::getGeometry(LLStrider<LLVector3> &vertices, LLStrider<LLVector3> &normals,
  371. LLStrider<LLVector2> &tex_coords, LLStrider<U16> &indicesp)
  372. {
  373. LLMemType mt1(LLMemType::MTYPE_DRAWABLE);
  374. if (mVertexBuffer.notNull())
  375. {
  376. mVertexBuffer->getVertexStrider(vertices, mGeomIndex, mGeomCount);
  377. if (mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_NORMAL))
  378. {
  379. mVertexBuffer->getNormalStrider(normals, mGeomIndex, mGeomCount);
  380. }
  381. if (mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD0))
  382. {
  383. mVertexBuffer->getTexCoord0Strider(tex_coords, mGeomIndex, mGeomCount);
  384. }
  385. mVertexBuffer->getIndexStrider(indicesp, mIndicesIndex, mIndicesCount);
  386. }
  387. return mGeomIndex;
  388. }
  389. void LLFace::updateCenterAgent()
  390. {
  391. if (mDrawablep->isActive())
  392. {
  393. mCenterAgent = mCenterLocal * getRenderMatrix();
  394. }
  395. else
  396. {
  397. mCenterAgent = mCenterLocal;
  398. }
  399. }
  400. void LLFace::renderSelected(LLViewerTexture *imagep, const LLColor4& color)
  401. {
  402. if (mDrawablep->getSpatialGroup() == NULL)
  403. {
  404. return;
  405. }
  406. mDrawablep->getSpatialGroup()->rebuildGeom();
  407. mDrawablep->getSpatialGroup()->rebuildMesh();
  408. if(mDrawablep.isNull() || mVertexBuffer.isNull())
  409. {
  410. return;
  411. }
  412. if (mGeomCount > 0 && mIndicesCount > 0)
  413. {
  414. gGL.getTexUnit(0)->bind(imagep);
  415. gGL.pushMatrix();
  416. if (mDrawablep->isActive())
  417. {
  418. gGL.multMatrix((GLfloat*)mDrawablep->getRenderMatrix().mMatrix);
  419. }
  420. else
  421. {
  422. gGL.multMatrix((GLfloat*)mDrawablep->getRegion()->mRenderMatrix.mMatrix);
  423. }
  424. gGL.diffuseColor4fv(color.mV);
  425. if (mDrawablep->isState(LLDrawable::RIGGED))
  426. {
  427. LLVOVolume* volume = mDrawablep->getVOVolume();
  428. if (volume)
  429. {
  430. LLRiggedVolume* rigged = volume->getRiggedVolume();
  431. if (rigged)
  432. {
  433. LLGLEnable offset(GL_POLYGON_OFFSET_FILL);
  434. glPolygonOffset(-1.f, -1.f);
  435. gGL.multMatrix((F32*) volume->getRelativeXform().mMatrix);
  436. const LLVolumeFace& vol_face = rigged->getVolumeFace(getTEOffset());
  437. LLVertexBuffer::unbind();
  438. glVertexPointer(3, GL_FLOAT, 16, vol_face.mPositions);
  439. if (vol_face.mTexCoords)
  440. {
  441. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  442. glTexCoordPointer(2, GL_FLOAT, 8, vol_face.mTexCoords);
  443. }
  444. gGL.syncMatrices();
  445. glDrawElements(GL_TRIANGLES, vol_face.mNumIndices, GL_UNSIGNED_SHORT, vol_face.mIndices);
  446. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  447. }
  448. }
  449. }
  450. else
  451. {
  452. mVertexBuffer->setBuffer(LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_TEXCOORD0);
  453. mVertexBuffer->draw(LLRender::TRIANGLES, mIndicesCount, mIndicesIndex);
  454. }
  455. gGL.popMatrix();
  456. }
  457. }
  458. /* removed in lieu of raycast uv detection
  459. void LLFace::renderSelectedUV()
  460. {
  461. LLViewerTexture* red_blue_imagep = LLViewerTextureManager::getFetchedTextureFromFile("uv_test1.j2c", TRUE, LLViewerTexture::BOOST_UI);
  462. LLViewerTexture* green_imagep = LLViewerTextureManager::getFetchedTextureFromFile("uv_test2.tga", TRUE, LLViewerTexture::BOOST_UI);
  463. LLGLSUVSelect object_select;
  464. // use red/blue gradient to get coarse UV coordinates
  465. renderSelected(red_blue_imagep, LLColor4::white);
  466. static F32 bias = 0.f;
  467. static F32 factor = -10.f;
  468. glPolygonOffset(factor, bias);
  469. // add green dither pattern on top of red/blue gradient
  470. gGL.blendFunc(LLRender::BF_ONE, LLRender::BF_ONE);
  471. gGL.matrixMode(LLRender::MM_TEXTURE);
  472. gGL.pushMatrix();
  473. // make green pattern repeat once per texel in red/blue texture
  474. gGL.scalef(256.f, 256.f, 1.f);
  475. gGL.matrixMode(LLRender::MM_MODELVIEW);
  476. renderSelected(green_imagep, LLColor4::white);
  477. gGL.matrixMode(LLRender::MM_TEXTURE);
  478. gGL.popMatrix();
  479. gGL.matrixMode(LLRender::MM_MODELVIEW);
  480. gGL.blendFunc(LLRender::BF_SOURCE_ALPHA, LLRender::BF_ONE_MINUS_SOURCE_ALPHA);
  481. }
  482. */
  483. void LLFace::setDrawInfo(LLDrawInfo* draw_info)
  484. {
  485. if (draw_info)
  486. {
  487. if (draw_info->mFace)
  488. {
  489. draw_info->mFace->setDrawInfo(NULL);
  490. }
  491. draw_info->mFace = this;
  492. }
  493. if (mDrawInfo)
  494. {
  495. mDrawInfo->mFace = NULL;
  496. }
  497. mDrawInfo = draw_info;
  498. }
  499. void LLFace::printDebugInfo() const
  500. {
  501. LLFacePool *poolp = getPool();
  502. llinfos << "Object: " << getViewerObject()->mID << llendl;
  503. if (getDrawable())
  504. {
  505. llinfos << "Type: " << LLPrimitive::pCodeToString(getDrawable()->getVObj()->getPCode()) << llendl;
  506. }
  507. if (getTexture())
  508. {
  509. llinfos << "Texture: " << getTexture() << " Comps: " << (U32)getTexture()->getComponents() << llendl;
  510. }
  511. else
  512. {
  513. llinfos << "No texture: " << llendl;
  514. }
  515. llinfos << "Face: " << this << llendl;
  516. llinfos << "State: " << getState() << llendl;
  517. llinfos << "Geom Index Data:" << llendl;
  518. llinfos << "--------------------" << llendl;
  519. llinfos << "GI: " << mGeomIndex << " Count:" << mGeomCount << llendl;
  520. llinfos << "Face Index Data:" << llendl;
  521. llinfos << "--------------------" << llendl;
  522. llinfos << "II: " << mIndicesIndex << " Count:" << mIndicesCount << llendl;
  523. llinfos << llendl;
  524. if (poolp)
  525. {
  526. poolp->printDebugInfo();
  527. S32 pool_references = 0;
  528. for (std::vector<LLFace*>::iterator iter = poolp->mReferences.begin();
  529. iter != poolp->mReferences.end(); iter++)
  530. {
  531. LLFace *facep = *iter;
  532. if (facep == this)
  533. {
  534. llinfos << "Pool reference: " << pool_references << llendl;
  535. pool_references++;
  536. }
  537. }
  538. if (pool_references != 1)
  539. {
  540. llinfos << "Incorrect number of pool references!" << llendl;
  541. }
  542. }
  543. #if 0
  544. llinfos << "Indices:" << llendl;
  545. llinfos << "--------------------" << llendl;
  546. const U32 *indicesp = getRawIndices();
  547. S32 indices_count = getIndicesCount();
  548. S32 geom_start = getGeomStart();
  549. for (S32 i = 0; i < indices_count; i++)
  550. {
  551. llinfos << i << ":" << indicesp[i] << ":" << (S32)(indicesp[i] - geom_start) << llendl;
  552. }
  553. llinfos << llendl;
  554. llinfos << "Vertices:" << llendl;
  555. llinfos << "--------------------" << llendl;
  556. for (S32 i = 0; i < mGeomCount; i++)
  557. {
  558. llinfos << mGeomIndex + i << ":" << poolp->getVertex(mGeomIndex + i) << llendl;
  559. }
  560. llinfos << llendl;
  561. #endif
  562. }
  563. // Transform the texture coordinates for this face.
  564. static void xform(LLVector2 &tex_coord, F32 cosAng, F32 sinAng, F32 offS, F32 offT, F32 magS, F32 magT)
  565. {
  566. // New, good way
  567. F32 s = tex_coord.mV[0];
  568. F32 t = tex_coord.mV[1];
  569. // Texture transforms are done about the center of the face.
  570. s -= 0.5;
  571. t -= 0.5;
  572. // Handle rotation
  573. F32 temp = s;
  574. s = s * cosAng + t * sinAng;
  575. t = -temp * sinAng + t * cosAng;
  576. // Then scale
  577. s *= magS;
  578. t *= magT;
  579. // Then offset
  580. s += offS + 0.5f;
  581. t += offT + 0.5f;
  582. tex_coord.mV[0] = s;
  583. tex_coord.mV[1] = t;
  584. }
  585. bool less_than_max_mag(const LLVector4a& vec)
  586. {
  587. LLVector4a MAX_MAG;
  588. MAX_MAG.splat(1024.f*1024.f);
  589. LLVector4a val;
  590. val.setAbs(vec);
  591. S32 lt = val.lessThan(MAX_MAG).getGatheredBits() & 0x7;
  592. return lt == 0x7;
  593. }
  594. BOOL LLFace::genVolumeBBoxes(const LLVolume &volume, S32 f,
  595. const LLMatrix4& mat_vert_in, const LLMatrix3& mat_normal_in, BOOL global_volume)
  596. {
  597. LLMemType mt1(LLMemType::MTYPE_DRAWABLE);
  598. //get bounding box
  599. if (mDrawablep->isState(LLDrawable::REBUILD_VOLUME | LLDrawable::REBUILD_POSITION | LLDrawable::REBUILD_RIGGED))
  600. {
  601. //VECTORIZE THIS
  602. LLMatrix4a mat_vert;
  603. mat_vert.loadu(mat_vert_in);
  604. LLMatrix4a mat_normal;
  605. mat_normal.loadu(mat_normal_in);
  606. //if (mDrawablep->isState(LLDrawable::REBUILD_VOLUME))
  607. //{ //vertex buffer no longer valid
  608. // mVertexBuffer = NULL;
  609. // mLastVertexBuffer = NULL;
  610. //}
  611. //VECTORIZE THIS
  612. LLVector4a min,max;
  613. if (f >= volume.getNumVolumeFaces())
  614. {
  615. llwarns << "Generating bounding box for invalid face index!" << llendl;
  616. f = 0;
  617. }
  618. const LLVolumeFace &face = volume.getVolumeFace(f);
  619. min = face.mExtents[0];
  620. max = face.mExtents[1];
  621. llassert(less_than_max_mag(min));
  622. llassert(less_than_max_mag(max));
  623. //min, max are in volume space, convert to drawable render space
  624. LLVector4a center;
  625. LLVector4a t;
  626. t.setAdd(min, max);
  627. t.mul(0.5f);
  628. mat_vert.affineTransform(t, center);
  629. LLVector4a size;
  630. size.setSub(max, min);
  631. size.mul(0.5f);
  632. llassert(less_than_max_mag(min));
  633. llassert(less_than_max_mag(max));
  634. if (!global_volume)
  635. {
  636. //VECTORIZE THIS
  637. LLVector4a scale;
  638. scale.load3(mDrawablep->getVObj()->getScale().mV);
  639. size.mul(scale);
  640. }
  641. mat_normal.mMatrix[0].normalize3fast();
  642. mat_normal.mMatrix[1].normalize3fast();
  643. mat_normal.mMatrix[2].normalize3fast();
  644. LLVector4a v[4];
  645. //get 4 corners of bounding box
  646. mat_normal.rotate(size,v[0]);
  647. //VECTORIZE THIS
  648. LLVector4a scale;
  649. scale.set(-1.f, -1.f, 1.f);
  650. scale.mul(size);
  651. mat_normal.rotate(scale, v[1]);
  652. scale.set(1.f, -1.f, -1.f);
  653. scale.mul(size);
  654. mat_normal.rotate(scale, v[2]);
  655. scale.set(-1.f, 1.f, -1.f);
  656. scale.mul(size);
  657. mat_normal.rotate(scale, v[3]);
  658. LLVector4a& newMin = mExtents[0];
  659. LLVector4a& newMax = mExtents[1];
  660. newMin = newMax = center;
  661. llassert(less_than_max_mag(center));
  662. for (U32 i = 0; i < 4; i++)
  663. {
  664. LLVector4a delta;
  665. delta.setAbs(v[i]);
  666. LLVector4a min;
  667. min.setSub(center, delta);
  668. LLVector4a max;
  669. max.setAdd(center, delta);
  670. newMin.setMin(newMin,min);
  671. newMax.setMax(newMax,max);
  672. llassert(less_than_max_mag(newMin));
  673. llassert(less_than_max_mag(newMax));
  674. }
  675. if (!mDrawablep->isActive())
  676. {
  677. LLVector4a offset;
  678. offset.load3(mDrawablep->getRegion()->getOriginAgent().mV);
  679. newMin.add(offset);
  680. newMax.add(offset);
  681. llassert(less_than_max_mag(newMin));
  682. llassert(less_than_max_mag(newMax));
  683. }
  684. t.setAdd(newMin, newMax);
  685. t.mul(0.5f);
  686. llassert(less_than_max_mag(t));
  687. //VECTORIZE THIS
  688. mCenterLocal.set(t.getF32ptr());
  689. llassert(less_than_max_mag(newMin));
  690. llassert(less_than_max_mag(newMax));
  691. t.setSub(newMax,newMin);
  692. mBoundingSphereRadius = t.getLength3().getF32()*0.5f;
  693. updateCenterAgent();
  694. }
  695. return TRUE;
  696. }
  697. // convert surface coordinates to texture coordinates, based on
  698. // the values in the texture entry. probably should be
  699. // integrated with getGeometryVolume() for its texture coordinate
  700. // generation - but i'll leave that to someone more familiar
  701. // with the implications.
  702. LLVector2 LLFace::surfaceToTexture(LLVector2 surface_coord, LLVector3 position, LLVector3 normal)
  703. {
  704. LLVector2 tc = surface_coord;
  705. const LLTextureEntry *tep = getTextureEntry();
  706. if (tep == NULL)
  707. {
  708. // can't do much without the texture entry
  709. return surface_coord;
  710. }
  711. //VECTORIZE THIS
  712. // see if we have a non-default mapping
  713. U8 texgen = getTextureEntry()->getTexGen();
  714. if (texgen != LLTextureEntry::TEX_GEN_DEFAULT)
  715. {
  716. LLVector4a& center = *(mDrawablep->getVOVolume()->getVolume()->getVolumeFace(mTEOffset).mCenter);
  717. LLVector4a volume_position;
  718. volume_position.load3(mDrawablep->getVOVolume()->agentPositionToVolume(position).mV);
  719. if (!mDrawablep->getVOVolume()->isVolumeGlobal())
  720. {
  721. LLVector4a scale;
  722. scale.load3(mVObjp->getScale().mV);
  723. volume_position.mul(scale);
  724. }
  725. LLVector4a volume_normal;
  726. volume_normal.load3(mDrawablep->getVOVolume()->agentDirectionToVolume(normal).mV);
  727. volume_normal.normalize3fast();
  728. switch (texgen)
  729. {
  730. case LLTextureEntry::TEX_GEN_PLANAR:
  731. planarProjection(tc, volume_normal, center, volume_position);
  732. break;
  733. case LLTextureEntry::TEX_GEN_SPHERICAL:
  734. sphericalProjection(tc, volume_normal, center, volume_position);
  735. break;
  736. case LLTextureEntry::TEX_GEN_CYLINDRICAL:
  737. cylindricalProjection(tc, volume_normal, center, volume_position);
  738. break;
  739. default:
  740. break;
  741. }
  742. }
  743. if (mTextureMatrix) // if we have a texture matrix, use it
  744. {
  745. LLVector3 tc3(tc);
  746. tc3 = tc3 * *mTextureMatrix;
  747. tc = LLVector2(tc3);
  748. }
  749. else // otherwise use the texture entry parameters
  750. {
  751. xform(tc, cos(tep->getRotation()), sin(tep->getRotation()),
  752. tep->mOffsetS, tep->mOffsetT, tep->mScaleS, tep->mScaleT);
  753. }
  754. return tc;
  755. }
  756. // Returns scale compared to default texgen, and face orientation as calculated
  757. // by planarProjection(). This is needed to match planar texgen parameters.
  758. void LLFace::getPlanarProjectedParams(LLQuaternion* face_rot, LLVector3* face_pos, F32* scale) const
  759. {
  760. const LLMatrix4& vol_mat = getWorldMatrix();
  761. const LLVolumeFace& vf = getViewerObject()->getVolume()->getVolumeFace(mTEOffset);
  762. const LLVector4a& normal4a = vf.mNormals[0];
  763. const LLVector4a& binormal4a = vf.mBinormals[0];
  764. LLVector2 projected_binormal;
  765. planarProjection(projected_binormal, normal4a, *vf.mCenter, binormal4a);
  766. projected_binormal -= LLVector2(0.5f, 0.5f); // this normally happens in xform()
  767. *scale = projected_binormal.length();
  768. // rotate binormal to match what planarProjection() thinks it is,
  769. // then find rotation from that:
  770. projected_binormal.normalize();
  771. F32 ang = acos(projected_binormal.mV[VY]);
  772. ang = (projected_binormal.mV[VX] < 0.f) ? -ang : ang;
  773. //VECTORIZE THIS
  774. LLVector3 binormal(binormal4a.getF32ptr());
  775. LLVector3 normal(normal4a.getF32ptr());
  776. binormal.rotVec(ang, normal);
  777. LLQuaternion local_rot( binormal % normal, binormal, normal );
  778. *face_rot = local_rot * vol_mat.quaternion();
  779. *face_pos = vol_mat.getTranslation();
  780. }
  781. // Returns the necessary texture transform to align this face's TE to align_to's TE
  782. bool LLFace::calcAlignedPlanarTE(const LLFace* align_to, LLVector2* res_st_offset,
  783. LLVector2* res_st_scale, F32* res_st_rot) const
  784. {
  785. if (!align_to)
  786. {
  787. return false;
  788. }
  789. const LLTextureEntry *orig_tep = align_to->getTextureEntry();
  790. if ((orig_tep->getTexGen() != LLTextureEntry::TEX_GEN_PLANAR) ||
  791. (getTextureEntry()->getTexGen() != LLTextureEntry::TEX_GEN_PLANAR))
  792. {
  793. return false;
  794. }
  795. LLVector3 orig_pos, this_pos;
  796. LLQuaternion orig_face_rot, this_face_rot;
  797. F32 orig_proj_scale, this_proj_scale;
  798. align_to->getPlanarProjectedParams(&orig_face_rot, &orig_pos, &orig_proj_scale);
  799. getPlanarProjectedParams(&this_face_rot, &this_pos, &this_proj_scale);
  800. // The rotation of "this face's" texture:
  801. LLQuaternion orig_st_rot = LLQuaternion(orig_tep->getRotation(), LLVector3::z_axis) * orig_face_rot;
  802. LLQuaternion this_st_rot = orig_st_rot * ~this_face_rot;
  803. F32 x_ang, y_ang, z_ang;
  804. this_st_rot.getEulerAngles(&x_ang, &y_ang, &z_ang);
  805. *res_st_rot = z_ang;
  806. // Offset and scale of "this face's" texture:
  807. LLVector3 centers_dist = (this_pos - orig_pos) * ~orig_st_rot;
  808. LLVector3 st_scale(orig_tep->mScaleS, orig_tep->mScaleT, 1.f);
  809. st_scale *= orig_proj_scale;
  810. centers_dist.scaleVec(st_scale);
  811. LLVector2 orig_st_offset(orig_tep->mOffsetS, orig_tep->mOffsetT);
  812. *res_st_offset = orig_st_offset + (LLVector2)centers_dist;
  813. res_st_offset->mV[VX] -= (S32)res_st_offset->mV[VX];
  814. res_st_offset->mV[VY] -= (S32)res_st_offset->mV[VY];
  815. st_scale /= this_proj_scale;
  816. *res_st_scale = (LLVector2)st_scale;
  817. return true;
  818. }
  819. void LLFace::updateRebuildFlags()
  820. {
  821. if (!mDrawablep->isState(LLDrawable::REBUILD_VOLUME))
  822. {
  823. BOOL moved = TRUE;
  824. if (mLastVertexBuffer == mVertexBuffer &&
  825. !mVertexBuffer->isEmpty())
  826. { //this face really doesn't need to be regenerated, try real hard not to do so
  827. if (mLastGeomCount == mGeomCount &&
  828. mLastGeomIndex == mGeomIndex &&
  829. mLastIndicesCount == mIndicesCount &&
  830. mLastIndicesIndex == mIndicesIndex)
  831. { //data is in same location in vertex buffer
  832. moved = FALSE;
  833. }
  834. }
  835. mLastMoveTime = gFrameTimeSeconds;
  836. if (moved)
  837. {
  838. mDrawablep->setState(LLDrawable::REBUILD_VOLUME);
  839. }
  840. }
  841. else
  842. {
  843. mLastUpdateTime = gFrameTimeSeconds;
  844. }
  845. }
  846. bool LLFace::canRenderAsMask()
  847. {
  848. if (LLPipeline::sNoAlpha)
  849. {
  850. return true;
  851. }
  852. const LLTextureEntry* te = getTextureEntry();
  853. if ((te->getColor().mV[3] == 1.0f) && // can't treat as mask if we have face alpha
  854. (te->getGlow() == 0.f) && // glowing masks are hard to implement - don't mask
  855. getTexture()->getIsAlphaMask()) // texture actually qualifies for masking (lazily recalculated but expensive)
  856. {
  857. if (LLPipeline::sRenderDeferred)
  858. {
  859. if (getViewerObject()->isHUDAttachment() || te->getFullbright())
  860. { //hud attachments and fullbright objects are NOT subject to the deferred rendering pipe
  861. return LLPipeline::sAutoMaskAlphaNonDeferred;
  862. }
  863. else
  864. {
  865. return LLPipeline::sAutoMaskAlphaDeferred;
  866. }
  867. }
  868. else
  869. {
  870. return LLPipeline::sAutoMaskAlphaNonDeferred;
  871. }
  872. }
  873. return false;
  874. }
  875. static LLFastTimer::DeclareTimer FTM_FACE_GET_GEOM("Face Geom");
  876. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_POSITION("Position");
  877. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_NORMAL("Normal");
  878. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_TEXTURE("Texture");
  879. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_COLOR("Color");
  880. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_EMISSIVE("Emissive");
  881. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_WEIGHTS("Weights");
  882. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_BINORMAL("Binormal");
  883. static LLFastTimer::DeclareTimer FTM_FACE_GEOM_INDEX("Index");
  884. BOOL LLFace::getGeometryVolume(const LLVolume& volume,
  885. const S32 &f,
  886. const LLMatrix4& mat_vert_in, const LLMatrix3& mat_norm_in,
  887. const U16 &index_offset,
  888. bool force_rebuild)
  889. {
  890. LLFastTimer t(FTM_FACE_GET_GEOM);
  891. llassert(verify());
  892. const LLVolumeFace &vf = volume.getVolumeFace(f);
  893. S32 num_vertices = (S32)vf.mNumVertices;
  894. S32 num_indices = (S32) vf.mNumIndices;
  895. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCTREE))
  896. {
  897. updateRebuildFlags();
  898. }
  899. bool map_range = gGLManager.mHasMapBufferRange || gGLManager.mHasFlushBufferRange;
  900. if (mVertexBuffer.notNull())
  901. {
  902. if (num_indices + (S32) mIndicesIndex > mVertexBuffer->getNumIndices())
  903. {
  904. llwarns << "Index buffer overflow!" << llendl;
  905. llwarns << "Indices Count: " << mIndicesCount
  906. << " VF Num Indices: " << num_indices
  907. << " Indices Index: " << mIndicesIndex
  908. << " VB Num Indices: " << mVertexBuffer->getNumIndices() << llendl;
  909. llwarns << "Last Indices Count: " << mLastIndicesCount
  910. << " Last Indices Index: " << mLastIndicesIndex
  911. << " Face Index: " << f
  912. << " Pool Type: " << mPoolType << llendl;
  913. return FALSE;
  914. }
  915. if (num_vertices + mGeomIndex > mVertexBuffer->getNumVerts())
  916. {
  917. llwarns << "Vertex buffer overflow!" << llendl;
  918. return FALSE;
  919. }
  920. }
  921. LLStrider<LLVector3> vert;
  922. LLVector4a* vertices = NULL;
  923. LLStrider<LLVector2> tex_coords;
  924. LLStrider<LLVector2> tex_coords2;
  925. LLVector4a* normals = NULL;
  926. LLStrider<LLVector3> norm;
  927. LLStrider<LLColor4U> colors;
  928. LLVector4a* binormals = NULL;
  929. LLStrider<LLVector3> binorm;
  930. LLStrider<U16> indicesp;
  931. LLVector4a* weights = NULL;
  932. LLStrider<LLVector4> wght;
  933. BOOL full_rebuild = force_rebuild || mDrawablep->isState(LLDrawable::REBUILD_VOLUME);
  934. BOOL global_volume = mDrawablep->getVOVolume()->isVolumeGlobal();
  935. LLVector3 scale;
  936. if (global_volume)
  937. {
  938. scale.setVec(1,1,1);
  939. }
  940. else
  941. {
  942. scale = mVObjp->getScale();
  943. }
  944. bool rebuild_pos = full_rebuild || mDrawablep->isState(LLDrawable::REBUILD_POSITION);
  945. bool rebuild_color = full_rebuild || mDrawablep->isState(LLDrawable::REBUILD_COLOR);
  946. bool rebuild_emissive = rebuild_color && mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_EMISSIVE);
  947. bool rebuild_tcoord = full_rebuild || mDrawablep->isState(LLDrawable::REBUILD_TCOORD);
  948. bool rebuild_normal = rebuild_pos && mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_NORMAL);
  949. bool rebuild_binormal = rebuild_pos && mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_BINORMAL);
  950. bool rebuild_weights = rebuild_pos && mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_WEIGHT4);
  951. const LLTextureEntry *tep = mVObjp->getTE(f);
  952. const U8 bump_code = tep ? tep->getBumpmap() : 0;
  953. F32 tcoord_xoffset = 0.f ;
  954. F32 tcoord_yoffset = 0.f ;
  955. F32 tcoord_xscale = 1.f ;
  956. F32 tcoord_yscale = 1.f ;
  957. BOOL in_atlas = FALSE ;
  958. if (rebuild_tcoord)
  959. {
  960. in_atlas = isAtlasInUse() ;
  961. if(in_atlas)
  962. {
  963. const LLVector2* tmp = getTexCoordOffset() ;
  964. tcoord_xoffset = tmp->mV[0] ;
  965. tcoord_yoffset = tmp->mV[1] ;
  966. tmp = getTexCoordScale() ;
  967. tcoord_xscale = tmp->mV[0] ;
  968. tcoord_yscale = tmp->mV[1] ;
  969. }
  970. }
  971. BOOL is_static = mDrawablep->isStatic();
  972. BOOL is_global = is_static;
  973. LLVector3 center_sum(0.f, 0.f, 0.f);
  974. if (is_global)
  975. {
  976. setState(GLOBAL);
  977. }
  978. else
  979. {
  980. clearState(GLOBAL);
  981. }
  982. LLColor4U color = tep->getColor();
  983. if (rebuild_color)
  984. {
  985. if (tep)
  986. {
  987. GLfloat alpha[4] =
  988. {
  989. 0.00f,
  990. 0.25f,
  991. 0.5f,
  992. 0.75f
  993. };
  994. if (getPoolType() != LLDrawPool::POOL_ALPHA && (LLPipeline::sRenderDeferred || (LLPipeline::sRenderBump && tep->getShiny())))
  995. {
  996. color.mV[3] = U8 (alpha[tep->getShiny()] * 255);
  997. }
  998. }
  999. }
  1000. // INDICES
  1001. if (full_rebuild)
  1002. {
  1003. LLFastTimer t(FTM_FACE_GEOM_INDEX);
  1004. mVertexBuffer->getIndexStrider(indicesp, mIndicesIndex, mIndicesCount, map_range);
  1005. __m128i* dst = (__m128i*) indicesp.get();
  1006. __m128i* src = (__m128i*) vf.mIndices;
  1007. __m128i offset = _mm_set1_epi16(index_offset);
  1008. S32 end = num_indices/8;
  1009. for (S32 i = 0; i < end; i++)
  1010. {
  1011. __m128i res = _mm_add_epi16(src[i], offset);
  1012. _mm_storeu_si128(dst+i, res);
  1013. }
  1014. for (S32 i = end*8; i < num_indices; ++i)
  1015. {
  1016. indicesp[i] = vf.mIndices[i]+index_offset;
  1017. }
  1018. if (map_range)
  1019. {
  1020. mVertexBuffer->flush();
  1021. }
  1022. }
  1023. LLMatrix4a mat_normal;
  1024. mat_normal.loadu(mat_norm_in);
  1025. //if it's not fullbright and has no normals, bake sunlight based on face normal
  1026. //bool bake_sunlight = !getTextureEntry()->getFullbright() &&
  1027. // !mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_NORMAL);
  1028. F32 r = 0, os = 0, ot = 0, ms = 0, mt = 0, cos_ang = 0, sin_ang = 0;
  1029. if (rebuild_tcoord)
  1030. {
  1031. LLFastTimer t(FTM_FACE_GEOM_TEXTURE);
  1032. bool do_xform;
  1033. if (tep)
  1034. {
  1035. r = tep->getRotation();
  1036. os = tep->mOffsetS;
  1037. ot = tep->mOffsetT;
  1038. ms = tep->mScaleS;
  1039. mt = tep->mScaleT;
  1040. cos_ang = cos(r);
  1041. sin_ang = sin(r);
  1042. if (cos_ang != 1.f ||
  1043. sin_ang != 0.f ||
  1044. os != 0.f ||
  1045. ot != 0.f ||
  1046. ms != 1.f ||
  1047. mt != 1.f)
  1048. {
  1049. do_xform = true;
  1050. }
  1051. else
  1052. {
  1053. do_xform = false;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. do_xform = false;
  1059. }
  1060. //bump setup
  1061. LLVector4a binormal_dir( -sin_ang, cos_ang, 0.f );
  1062. LLVector4a bump_s_primary_light_ray(0.f, 0.f, 0.f);
  1063. LLVector4a bump_t_primary_light_ray(0.f, 0.f, 0.f);
  1064. LLQuaternion bump_quat;
  1065. if (mDrawablep->isActive())
  1066. {
  1067. bump_quat = LLQuaternion(mDrawablep->getRenderMatrix());
  1068. }
  1069. if (bump_code)
  1070. {
  1071. mVObjp->getVolume()->genBinormals(f);
  1072. F32 offset_multiple;
  1073. switch( bump_code )
  1074. {
  1075. case BE_NO_BUMP:
  1076. offset_multiple = 0.f;
  1077. break;
  1078. case BE_BRIGHTNESS:
  1079. case BE_DARKNESS:
  1080. if( mTexture.notNull() && mTexture->hasGLTexture())
  1081. {
  1082. // Offset by approximately one texel
  1083. S32 cur_discard = mTexture->getDiscardLevel();
  1084. S32 max_size = llmax( mTexture->getWidth(), mTexture->getHeight() );
  1085. max_size <<= cur_discard;
  1086. const F32 ARTIFICIAL_OFFSET = 2.f;
  1087. offset_multiple = ARTIFICIAL_OFFSET / (F32)max_size;
  1088. }
  1089. else
  1090. {
  1091. offset_multiple = 1.f/256;
  1092. }
  1093. break;
  1094. default: // Standard bumpmap textures. Assumed to be 256x256
  1095. offset_multiple = 1.f / 256;
  1096. break;
  1097. }
  1098. F32 s_scale = 1.f;
  1099. F32 t_scale = 1.f;
  1100. if( tep )
  1101. {
  1102. tep->getScale( &s_scale, &t_scale );
  1103. }
  1104. // Use the nudged south when coming from above sun angle, such
  1105. // that emboss mapping always shows up on the upward faces of cubes when
  1106. // it's noon (since a lot of builders build with the sun forced to noon).
  1107. LLVector3 sun_ray = gSky.mVOSkyp->mBumpSunDir;
  1108. LLVector3 moon_ray = gSky.getMoonDirection();
  1109. LLVector3& primary_light_ray = (sun_ray.mV[VZ] > 0) ? sun_ray : moon_ray;
  1110. bump_s_primary_light_ray.load3((offset_multiple * s_scale * primary_light_ray).mV);
  1111. bump_t_primary_light_ray.load3((offset_multiple * t_scale * primary_light_ray).mV);
  1112. }
  1113. U8 texgen = getTextureEntry()->getTexGen();
  1114. if (rebuild_tcoord && texgen != LLTextureEntry::TEX_GEN_DEFAULT)
  1115. { //planar texgen needs binormals
  1116. mVObjp->getVolume()->genBinormals(f);
  1117. }
  1118. U8 tex_mode = 0;
  1119. if (isState(TEXTURE_ANIM))
  1120. {
  1121. LLVOVolume* vobj = (LLVOVolume*) (LLViewerObject*) mVObjp;
  1122. tex_mode = vobj->mTexAnimMode;
  1123. if (!tex_mode)
  1124. {
  1125. clearState(TEXTURE_ANIM);
  1126. }
  1127. else
  1128. {
  1129. os = ot = 0.f;
  1130. r = 0.f;
  1131. cos_ang = 1.f;
  1132. sin_ang = 0.f;
  1133. ms = mt = 1.f;
  1134. do_xform = false;
  1135. }
  1136. if (getVirtualSize() >= MIN_TEX_ANIM_SIZE)
  1137. { //don't override texture transform during tc bake
  1138. tex_mode = 0;
  1139. }
  1140. }
  1141. LLVector4a scalea;
  1142. scalea.load3(scale.mV);
  1143. bool do_bump = bump_code && mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD1);
  1144. bool do_tex_mat = tex_mode && mTextureMatrix;
  1145. if (!in_atlas && !do_bump)
  1146. { //not in atlas or not bump mapped, might be able to do a cheap update
  1147. mVertexBuffer->getTexCoord0Strider(tex_coords, mGeomIndex, mGeomCount);
  1148. if (texgen != LLTextureEntry::TEX_GEN_PLANAR)
  1149. {
  1150. if (!do_tex_mat)
  1151. {
  1152. if (!do_xform)
  1153. {
  1154. LLVector4a::memcpyNonAliased16((F32*) tex_coords.get(), (F32*) vf.mTexCoords, num_vertices*2*sizeof(F32));
  1155. }
  1156. else
  1157. {
  1158. for (S32 i = 0; i < num_vertices; i++)
  1159. {
  1160. LLVector2 tc(vf.mTexCoords[i]);
  1161. xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
  1162. *tex_coords++ = tc;
  1163. }
  1164. }
  1165. }
  1166. else
  1167. { //do tex mat, no texgen, no atlas, no bump
  1168. for (S32 i = 0; i < num_vertices; i++)
  1169. {
  1170. LLVector2 tc(vf.mTexCoords[i]);
  1171. //LLVector4a& norm = vf.mNormals[i];
  1172. //LLVector4a& center = *(vf.mCenter);
  1173. LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
  1174. tmp = tmp * *mTextureMatrix;
  1175. tc.mV[0] = tmp.mV[0];
  1176. tc.mV[1] = tmp.mV[1];
  1177. *tex_coords++ = tc;
  1178. }
  1179. }
  1180. }
  1181. else
  1182. { //no bump, no atlas, tex gen planar
  1183. if (do_tex_mat)
  1184. {
  1185. for (S32 i = 0; i < num_vertices; i++)
  1186. {
  1187. LLVector2 tc(vf.mTexCoords[i]);
  1188. LLVector4a& norm = vf.mNormals[i];
  1189. LLVector4a& center = *(vf.mCenter);
  1190. LLVector4a vec = vf.mPositions[i];
  1191. vec.mul(scalea);
  1192. planarProjection(tc, norm, center, vec);
  1193. LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
  1194. tmp = tmp * *mTextureMatrix;
  1195. tc.mV[0] = tmp.mV[0];
  1196. tc.mV[1] = tmp.mV[1];
  1197. *tex_coords++ = tc;
  1198. }
  1199. }
  1200. else
  1201. {
  1202. for (S32 i = 0; i < num_vertices; i++)
  1203. {
  1204. LLVector2 tc(vf.mTexCoords[i]);
  1205. LLVector4a& norm = vf.mNormals[i];
  1206. LLVector4a& center = *(vf.mCenter);
  1207. LLVector4a vec = vf.mPositions[i];
  1208. vec.mul(scalea);
  1209. planarProjection(tc, norm, center, vec);
  1210. xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
  1211. *tex_coords++ = tc;
  1212. }
  1213. }
  1214. }
  1215. if (map_range)
  1216. {
  1217. mVertexBuffer->flush();
  1218. }
  1219. }
  1220. else
  1221. { //either bump mapped or in atlas, just do the whole expensive loop
  1222. mVertexBuffer->getTexCoord0Strider(tex_coords, mGeomIndex, mGeomCount, map_range);
  1223. std::vector<LLVector2> bump_tc;
  1224. for (S32 i = 0; i < num_vertices; i++)
  1225. {
  1226. LLVector2 tc(vf.mTexCoords[i]);
  1227. LLVector4a& norm = vf.mNormals[i];
  1228. LLVector4a& center = *(vf.mCenter);
  1229. if (texgen != LLTextureEntry::TEX_GEN_DEFAULT)
  1230. {
  1231. LLVector4a vec = vf.mPositions[i];
  1232. vec.mul(scalea);
  1233. switch (texgen)
  1234. {
  1235. case LLTextureEntry::TEX_GEN_PLANAR:
  1236. planarProjection(tc, norm, center, vec);
  1237. break;
  1238. case LLTextureEntry::TEX_GEN_SPHERICAL:
  1239. sphericalProjection(tc, norm, center, vec);
  1240. break;
  1241. case LLTextureEntry::TEX_GEN_CYLINDRICAL:
  1242. cylindricalProjection(tc, norm, center, vec);
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. }
  1248. if (tex_mode && mTextureMatrix)
  1249. {
  1250. LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
  1251. tmp = tmp * *mTextureMatrix;
  1252. tc.mV[0] = tmp.mV[0];
  1253. tc.mV[1] = tmp.mV[1];
  1254. }
  1255. else
  1256. {
  1257. xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
  1258. }
  1259. if(in_atlas)
  1260. {
  1261. //
  1262. //manually calculate tex-coord per vertex for varying address modes.
  1263. //should be removed if shader can handle this.
  1264. //
  1265. S32 int_part = 0 ;
  1266. switch(mTexture->getAddressMode())
  1267. {
  1268. case LLTexUnit::TAM_CLAMP:
  1269. if(tc.mV[0] < 0.f)
  1270. {
  1271. tc.mV[0] = 0.f ;
  1272. }
  1273. else if(tc.mV[0] > 1.f)
  1274. {
  1275. tc.mV[0] = 1.f;
  1276. }
  1277. if(tc.mV[1] < 0.f)
  1278. {
  1279. tc.mV[1] = 0.f ;
  1280. }
  1281. else if(tc.mV[1] > 1.f)
  1282. {
  1283. tc.mV[1] = 1.f;
  1284. }
  1285. break;
  1286. case LLTexUnit::TAM_MIRROR:
  1287. if(tc.mV[0] < 0.f)
  1288. {
  1289. tc.mV[0] = -tc.mV[0] ;
  1290. }
  1291. int_part = (S32)tc.mV[0] ;
  1292. if(int_part & 1) //odd number
  1293. {
  1294. tc.mV[0] = int_part + 1 - tc.mV[0] ;
  1295. }
  1296. else //even number
  1297. {
  1298. tc.mV[0] -= int_part ;
  1299. }
  1300. if(tc.mV[1] < 0.f)
  1301. {
  1302. tc.mV[1] = -tc.mV[1] ;
  1303. }
  1304. int_part = (S32)tc.mV[1] ;
  1305. if(int_part & 1) //odd number
  1306. {
  1307. tc.mV[1] = int_part + 1 - tc.mV[1] ;
  1308. }
  1309. else //even number
  1310. {
  1311. tc.mV[1] -= int_part ;
  1312. }
  1313. break;
  1314. case LLTexUnit::TAM_WRAP:
  1315. if(tc.mV[0] > 1.f)
  1316. tc.mV[0] -= (S32)(tc.mV[0] - 0.00001f) ;
  1317. else if(tc.mV[0] < -1.f)
  1318. tc.mV[0] -= (S32)(tc.mV[0] + 0.00001f) ;
  1319. if(tc.mV[1] > 1.f)
  1320. tc.mV[1] -= (S32)(tc.mV[1] - 0.00001f) ;
  1321. else if(tc.mV[1] < -1.f)
  1322. tc.mV[1] -= (S32)(tc.mV[1] + 0.00001f) ;
  1323. if(tc.mV[0] < 0.f)
  1324. {
  1325. tc.mV[0] = 1.0f + tc.mV[0] ;
  1326. }
  1327. if(tc.mV[1] < 0.f)
  1328. {
  1329. tc.mV[1] = 1.0f + tc.mV[1] ;
  1330. }
  1331. break;
  1332. default:
  1333. break;
  1334. }
  1335. tc.mV[0] = tcoord_xoffset + tcoord_xscale * tc.mV[0] ;
  1336. tc.mV[1] = tcoord_yoffset + tcoord_yscale * tc.mV[1] ;
  1337. }
  1338. *tex_coords++ = tc;
  1339. if (do_bump)
  1340. {
  1341. bump_tc.push_back(tc);
  1342. }
  1343. }
  1344. if (map_range)
  1345. {
  1346. mVertexBuffer->flush();
  1347. }
  1348. if (do_bump)
  1349. {
  1350. mVertexBuffer->getTexCoord1Strider(tex_coords2, mGeomIndex, mGeomCount, map_range);
  1351. for (S32 i = 0; i < num_vertices; i++)
  1352. {
  1353. LLVector4a tangent;
  1354. tangent.setCross3(vf.mBinormals[i], vf.mNormals[i]);
  1355. LLMatrix4a tangent_to_object;
  1356. tangent_to_object.setRows(tangent, vf.mBinormals[i], vf.mNormals[i]);
  1357. LLVector4a t;
  1358. tangent_to_object.rotate(binormal_dir, t);
  1359. LLVector4a binormal;
  1360. mat_normal.rotate(t, binormal);
  1361. //VECTORIZE THIS
  1362. if (mDrawablep->isActive())
  1363. {
  1364. LLVector3 t;
  1365. t.set(binormal.getF32ptr());
  1366. t *= bump_quat;
  1367. binormal.load3(t.mV);
  1368. }
  1369. binormal.normalize3fast();
  1370. LLVector2 tc = bump_tc[i];
  1371. tc += LLVector2( bump_s_primary_light_ray.dot3(tangent).getF32(), bump_t_primary_light_ray.dot3(binormal).getF32() );
  1372. *tex_coords2++ = tc;
  1373. }
  1374. if (map_range)
  1375. {
  1376. mVertexBuffer->flush();
  1377. }
  1378. }
  1379. }
  1380. }
  1381. if (rebuild_pos)
  1382. {
  1383. LLFastTimer t(FTM_FACE_GEOM_POSITION);
  1384. llassert(num_vertices > 0);
  1385. mVertexBuffer->getVertexStrider(vert, mGeomIndex, mGeomCount, map_range);
  1386. vertices = (LLVector4a*) vert.get();
  1387. LLMatrix4a mat_vert;
  1388. mat_vert.loadu(mat_vert_in);
  1389. LLVector4a* src = vf.mPositions;
  1390. LLVector4a* dst = vertices;
  1391. LLVector4a* end = dst+num_vertices;
  1392. do
  1393. {
  1394. mat_vert.affineTransform(*src++, *dst++);
  1395. }
  1396. while(dst < end);
  1397. F32 index = (F32) (mTextureIndex < 255 ? mTextureIndex : 0);
  1398. llassert(index <= LLGLSLShader::sIndexedTextureChannels-1);
  1399. F32 *index_dst = (F32*) vertices;
  1400. F32 *index_end = (F32*) end;
  1401. index_dst += 3;
  1402. index_end += 3;
  1403. do
  1404. {
  1405. *index_dst = index;
  1406. index_dst += 4;
  1407. }
  1408. while (index_dst < index_end);
  1409. S32 aligned_pad_vertices = mGeomCount - num_vertices;
  1410. LLVector4a* last_vec = end - 1;
  1411. while (aligned_pad_vertices > 0)
  1412. {
  1413. --aligned_pad_vertices;
  1414. *dst++ = *last_vec;
  1415. }
  1416. if (map_range)
  1417. {
  1418. mVertexBuffer->flush();
  1419. }
  1420. }
  1421. if (rebuild_normal)
  1422. {
  1423. LLFastTimer t(FTM_FACE_GEOM_NORMAL);
  1424. mVertexBuffer->getNormalStrider(norm, mGeomIndex, mGeomCount, map_range);
  1425. normals = (LLVector4a*) norm.get();
  1426. for (S32 i = 0; i < num_vertices; i++)
  1427. {
  1428. LLVector4a normal;
  1429. mat_normal.rotate(vf.mNormals[i], normal);
  1430. normal.normalize3fast();
  1431. normals[i] = normal;
  1432. }
  1433. if (map_range)
  1434. {
  1435. mVertexBuffer->flush();
  1436. }
  1437. }
  1438. if (rebuild_binormal)
  1439. {
  1440. LLFastTimer t(FTM_FACE_GEOM_BINORMAL);
  1441. mVertexBuffer->getBinormalStrider(binorm, mGeomIndex, mGeomCount, map_range);
  1442. binormals = (LLVector4a*) binorm.get();
  1443. for (S32 i = 0; i < num_vertices; i++)
  1444. {
  1445. LLVector4a binormal;
  1446. mat_normal.rotate(vf.mBinormals[i], binormal);
  1447. binormal.normalize3fast();
  1448. binormals[i] = binormal;
  1449. }
  1450. if (map_range)
  1451. {
  1452. mVertexBuffer->flush();
  1453. }
  1454. }
  1455. if (rebuild_weights && vf.mWeights)
  1456. {
  1457. LLFastTimer t(FTM_FACE_GEOM_WEIGHTS);
  1458. mVertexBuffer->getWeight4Strider(wght, mGeomIndex, mGeomCount, map_range);
  1459. weights = (LLVector4a*) wght.get();
  1460. LLVector4a::memcpyNonAliased16((F32*) weights, (F32*) vf.mWeights, num_vertices*4*sizeof(F32));
  1461. if (map_range)
  1462. {
  1463. mVertexBuffer->flush();
  1464. }
  1465. }
  1466. if (rebuild_color)
  1467. {
  1468. LLFastTimer t(FTM_FACE_GEOM_COLOR);
  1469. mVertexBuffer->getColorStrider(colors, mGeomIndex, mGeomCount, map_range);
  1470. LLVector4a src;
  1471. U32 vec[4];
  1472. vec[0] = vec[1] = vec[2] = vec[3] = color.mAll;
  1473. src.loadua((F32*) vec);
  1474. LLVector4a* dst = (LLVector4a*) colors.get();
  1475. S32 num_vecs = num_vertices/4;
  1476. if (num_vertices%4 > 0)
  1477. {
  1478. ++num_vecs;
  1479. }
  1480. for (S32 i = 0; i < num_vecs; i++)
  1481. {
  1482. dst[i] = src;
  1483. }
  1484. if (map_range)
  1485. {
  1486. mVertexBuffer->flush();
  1487. }
  1488. }
  1489. if (rebuild_emissive)
  1490. {
  1491. LLFastTimer t(FTM_FACE_GEOM_EMISSIVE);
  1492. LLStrider<LLColor4U> emissive;
  1493. mVertexBuffer->getEmissiveStrider(emissive, mGeomIndex, mGeomCount, map_range);
  1494. U8 glow = (U8) llclamp((S32) (getTextureEntry()->getGlow()*255), 0, 255);
  1495. LLVector4a src;
  1496. U32 glow32 = glow |
  1497. (glow << 8) |
  1498. (glow << 16) |
  1499. (glow << 24);
  1500. U32 vec[4];
  1501. vec[0] = vec[1] = vec[2] = vec[3] = glow32;
  1502. src.loadua((F32*) vec);
  1503. LLVector4a* dst = (LLVector4a*) emissive.get();
  1504. S32 num_vecs = num_vertices/4;
  1505. if (num_vertices%4 > 0)
  1506. {
  1507. ++num_vecs;
  1508. }
  1509. for (S32 i = 0; i < num_vecs; i++)
  1510. {
  1511. dst[i] = src;
  1512. }
  1513. if (map_range)
  1514. {
  1515. mVertexBuffer->flush();
  1516. }
  1517. }
  1518. if (rebuild_tcoord)
  1519. {
  1520. mTexExtents[0].setVec(0,0);
  1521. mTexExtents[1].setVec(1,1);
  1522. xform(mTexExtents[0], cos_ang, sin_ang, os, ot, ms, mt);
  1523. xform(mTexExtents[1], cos_ang, sin_ang, os, ot, ms, mt);
  1524. F32 es = vf.mTexCoordExtents[1].mV[0] - vf.mTexCoordExtents[0].mV[0] ;
  1525. F32 et = vf.mTexCoordExtents[1].mV[1] - vf.mTexCoordExtents[0].mV[1] ;
  1526. mTexExtents[0][0] *= es ;
  1527. mTexExtents[1][0] *= es ;
  1528. mTexExtents[0][1] *= et ;
  1529. mTexExtents[1][1] *= et ;
  1530. }
  1531. mLastVertexBuffer = mVertexBuffer;
  1532. mLastGeomCount = mGeomCount;
  1533. mLastGeomIndex = mGeomIndex;
  1534. mLastIndicesCount = mIndicesCount;
  1535. mLastIndicesIndex = mIndicesIndex;
  1536. return TRUE;
  1537. }
  1538. //check if the face has a media
  1539. BOOL LLFace::hasMedia() const
  1540. {
  1541. if(mHasMedia)
  1542. {
  1543. return TRUE ;
  1544. }
  1545. if(mTexture.notNull())
  1546. {
  1547. return mTexture->hasParcelMedia() ; //if has a parcel media
  1548. }
  1549. return FALSE ; //no media.
  1550. }
  1551. const F32 LEAST_IMPORTANCE = 0.05f ;
  1552. const F32 LEAST_IMPORTANCE_FOR_LARGE_IMAGE = 0.3f ;
  1553. F32 LLFace::getTextureVirtualSize()
  1554. {
  1555. F32 radius;
  1556. F32 cos_angle_to_view_dir;
  1557. BOOL in_frustum = calcPixelArea(cos_angle_to_view_dir, radius);
  1558. if (mPixelArea < F_ALMOST_ZERO || !in_frustum)
  1559. {
  1560. setVirtualSize(0.f) ;
  1561. return 0.f;
  1562. }
  1563. //get area of circle in texture space
  1564. LLVector2 tdim = mTexExtents[1] - mTexExtents[0];
  1565. F32 texel_area = (tdim * 0.5f).lengthSquared()*3.14159f;
  1566. if (texel_area <= 0)
  1567. {
  1568. // Probably animated, use default
  1569. texel_area = 1.f;
  1570. }
  1571. F32 face_area;
  1572. if (mVObjp->isSculpted() && texel_area > 1.f)
  1573. {
  1574. //sculpts can break assumptions about texel area
  1575. face_area = mPixelArea;
  1576. }
  1577. else
  1578. {
  1579. //apply texel area to face area to get accurate ratio
  1580. //face_area /= llclamp(texel_area, 1.f/64.f, 16.f);
  1581. face_area = mPixelArea / llclamp(texel_area, 0.015625f, 128.f);
  1582. }
  1583. face_area = LLFace::adjustPixelArea(mImportanceToCamera, face_area) ;
  1584. if(face_area > LLViewerTexture::sMinLargeImageSize) //if is large image, shrink face_area by considering the partial overlapping.
  1585. {
  1586. if(mImportanceToCamera > LEAST_IMPORTANCE_FOR_LARGE_IMAGE && mTexture.notNull() && mTexture->isLargeImage())
  1587. {
  1588. face_area *= adjustPartialOverlapPixelArea(cos_angle_to_view_dir, radius );
  1589. }
  1590. }
  1591. setVirtualSize(face_area) ;
  1592. return face_area;
  1593. }
  1594. BOOL LLFace::calcPixelArea(F32& cos_angle_to_view_dir, F32& radius)
  1595. {
  1596. //VECTORIZE THIS
  1597. //get area of circle around face
  1598. LLVector4a center;
  1599. center.load3(getPositionAgent().mV);
  1600. LLVector4a size;
  1601. size.setSub(mExtents[1], mExtents[0]);
  1602. size.mul(0.5f);
  1603. LLViewerCamera* camera = LLViewerCamera::getInstance();
  1604. F32 size_squared = size.dot3(size).getF32();
  1605. LLVector4a lookAt;
  1606. LLVector4a t;
  1607. t.load3(camera->getOrigin().mV);
  1608. lookAt.setSub(center, t);
  1609. F32 dist = lookAt.getLength3().getF32();
  1610. dist = llmax(dist-size.getLength3().getF32(), 0.f);
  1611. lookAt.normalize3fast() ;
  1612. //get area of circle around node
  1613. F32 app_angle = atanf((F32) sqrt(size_squared) / dist);
  1614. radius = app_angle*LLDrawable::sCurPixelAngle;
  1615. mPixelArea = radius*radius * 3.14159f;
  1616. LLVector4a x_axis;
  1617. x_axis.load3(camera->getXAxis().mV);
  1618. cos_angle_to_view_dir = lookAt.dot3(x_axis).getF32();
  1619. //if has media, check if the face is out of the view frustum.
  1620. if(hasMedia())
  1621. {
  1622. if(!camera->AABBInFrustum(center, size))
  1623. {
  1624. mImportanceToCamera = 0.f ;
  1625. return false ;
  1626. }
  1627. if(cos_angle_to_view_dir > camera->getCosHalfFov()) //the center is within the view frustum
  1628. {
  1629. cos_angle_to_view_dir = 1.0f ;
  1630. }
  1631. else
  1632. {
  1633. LLVector4a d;
  1634. d.setSub(lookAt, x_axis);
  1635. if(dist * dist * d.dot3(d) < size_squared)
  1636. {
  1637. cos_angle_to_view_dir = 1.0f ;
  1638. }
  1639. }
  1640. }
  1641. if(dist < mBoundingSphereRadius) //camera is very close
  1642. {
  1643. cos_angle_to_view_dir = 1.0f ;
  1644. mImportanceToCamera = 1.0f ;
  1645. }
  1646. else
  1647. {
  1648. mImportanceToCamera = LLFace::calcImportanceToCamera(cos_angle_to_view_dir, dist) ;
  1649. }
  1650. return true ;
  1651. }
  1652. //the projection of the face partially overlaps with the screen
  1653. F32 LLFace::adjustPartialOverlapPixelArea(F32 cos_angle_to_view_dir, F32 radius )
  1654. {
  1655. F32 screen_radius = (F32)llmax(gViewerWindow->getWindowWidthRaw(), gViewerWindow->getWindowHeightRaw()) ;
  1656. F32 center_angle = acosf(cos_angle_to_view_dir) ;
  1657. F32 d = center_angle * LLDrawable::sCurPixelAngle ;
  1658. if(d + radius > screen_radius + 5.f)
  1659. {
  1660. //----------------------------------------------
  1661. //calculate the intersection area of two circles
  1662. //F32 radius_square = radius * radius ;
  1663. //F32 d_square = d * d ;
  1664. //F32 screen_radius_square = screen_radius * screen_radius ;
  1665. //face_area =
  1666. // radius_square * acosf((d_square + radius_square - screen_radius_square)/(2 * d * radius)) +
  1667. // screen_radius_square * acosf((d_square + screen_radius_square - radius_square)/(2 * d * screen_radius)) -
  1668. // 0.5f * sqrtf((-d + radius + screen_radius) * (d + radius - screen_radius) * (d - radius + screen_radius) * (d + radius + screen_radius)) ;
  1669. //----------------------------------------------
  1670. //the above calculation is too expensive
  1671. //the below is a good estimation: bounding box of the bounding sphere:
  1672. F32 alpha = 0.5f * (radius + screen_radius - d) / radius ;
  1673. alpha = llclamp(alpha, 0.f, 1.f) ;
  1674. return alpha * alpha ;
  1675. }
  1676. return 1.0f ;
  1677. }
  1678. const S8 FACE_IMPORTANCE_LEVEL = 4 ;
  1679. const F32 FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[FACE_IMPORTANCE_LEVEL][2] = //{distance, importance_weight}
  1680. {{16.1f, 1.0f}, {32.1f, 0.5f}, {48.1f, 0.2f}, {96.1f, 0.05f} } ;
  1681. const F32 FACE_IMPORTANCE_TO_CAMERA_OVER_ANGLE[FACE_IMPORTANCE_LEVEL][2] = //{cos(angle), importance_weight}
  1682. {{0.985f /*cos(10 degrees)*/, 1.0f}, {0.94f /*cos(20 degrees)*/, 0.8f}, {0.866f /*cos(30 degrees)*/, 0.64f}, {0.0f, 0.36f}} ;
  1683. //static
  1684. F32 LLFace::calcImportanceToCamera(F32 cos_angle_to_view_dir, F32 dist)
  1685. {
  1686. F32 importance = 0.f ;
  1687. if(cos_angle_to_view_dir > LLViewerCamera::getInstance()->getCosHalfFov() &&
  1688. dist < FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[FACE_IMPORTANCE_LEVEL - 1][0])
  1689. {
  1690. LLViewerCamera* camera = LLViewerCamera::getInstance();
  1691. F32 camera_moving_speed = camera->getAverageSpeed() ;
  1692. F32 camera_angular_speed = camera->getAverageAngularSpeed();
  1693. if(camera_moving_speed > 10.0f || camera_angular_speed > 1.0f)
  1694. {
  1695. //if camera moves or rotates too fast, ignore the importance factor
  1696. return 0.f ;
  1697. }
  1698. //F32 camera_relative_speed = camera_moving_speed * (lookAt * LLViewerCamera::getInstance()->getVelocityDir()) ;
  1699. S32 i = 0 ;
  1700. for(i = 0; i < FACE_IMPORTANCE_LEVEL && dist > FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[i][0]; ++i);
  1701. i = llmin(i, FACE_IMPORTANCE_LEVEL - 1) ;
  1702. F32 dist_factor = FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[i][1] ;
  1703. for(i = 0; i < FACE_IMPORTANCE_LEVEL && cos_angle_to_view_dir < FACE_IMPORTANCE_TO_CAMERA_OVER_ANGLE[i][0] ; ++i) ;
  1704. i = llmin(i, FACE_IMPORTANCE_LEVEL - 1) ;
  1705. importance = dist_factor * FACE_IMPORTANCE_TO_CAMERA_OVER_ANGLE[i][1] ;
  1706. }
  1707. return importance ;
  1708. }
  1709. //static
  1710. F32 LLFace::adjustPixelArea(F32 importance, F32 pixel_area)
  1711. {
  1712. if(pixel_area > LLViewerTexture::sMaxSmallImageSize)
  1713. {
  1714. if(importance < LEAST_IMPORTANCE) //if the face is not important, do not load hi-res.
  1715. {
  1716. static const F32 MAX_LEAST_IMPORTANCE_IMAGE_SIZE = 128.0f * 128.0f ;
  1717. pixel_area = llmin(pixel_area * 0.5f, MAX_LEAST_IMPORTANCE_IMAGE_SIZE) ;
  1718. }
  1719. else if(pixel_area > LLViewerTexture::sMinLargeImageSize) //if is large image, shrink face_area by considering the partial overlapping.
  1720. {
  1721. if(importance < LEAST_IMPORTANCE_FOR_LARGE_IMAGE)//if the face is not important, do not load hi-res.
  1722. {
  1723. pixel_area = LLViewerTexture::sMinLargeImageSize ;
  1724. }
  1725. }
  1726. }
  1727. return pixel_area ;
  1728. }
  1729. BOOL LLFace::verify(const U32* indices_array) const
  1730. {
  1731. BOOL ok = TRUE;
  1732. if( mVertexBuffer.isNull() )
  1733. { //no vertex buffer, face is implicitly valid
  1734. return TRUE;
  1735. }
  1736. // First, check whether the face data fits within the pool's range.
  1737. if ((mGeomIndex + mGeomCount) > mVertexBuffer->getNumVerts())
  1738. {
  1739. ok = FALSE;
  1740. llinfos << "Face references invalid vertices!" << llendl;
  1741. }
  1742. S32 indices_count = (S32)getIndicesCount();
  1743. if (!indices_count)
  1744. {
  1745. return TRUE;
  1746. }
  1747. if (indices_count > LL_MAX_INDICES_COUNT)
  1748. {
  1749. ok = FALSE;
  1750. llinfos << "Face has bogus indices count" << llendl;
  1751. }
  1752. if (mIndicesIndex + mIndicesCount > mVertexBuffer->getNumIndices())
  1753. {
  1754. ok = FALSE;
  1755. llinfos << "Face references invalid indices!" << llendl;
  1756. }
  1757. #if 0
  1758. S32 geom_start = getGeomStart();
  1759. S32 geom_count = mGeomCount;
  1760. const U32 *indicesp = indices_array ? indices_array + mIndicesIndex : getRawIndices();
  1761. for (S32 i = 0; i < indices_count; i++)
  1762. {
  1763. S32 delta = indicesp[i] - geom_start;
  1764. if (0 > delta)
  1765. {
  1766. llwarns << "Face index too low!" << llendl;
  1767. llinfos << "i:" << i << " Index:" << indicesp[i] << " GStart: " << geom_start << llendl;
  1768. ok = FALSE;
  1769. }
  1770. else if (delta >= geom_count)
  1771. {
  1772. llwarns << "Face index too high!" << llendl;
  1773. llinfos << "i:" << i << " Index:" << indicesp[i] << " GEnd: " << geom_start + geom_count << llendl;
  1774. ok = FALSE;
  1775. }
  1776. }
  1777. #endif
  1778. if (!ok)
  1779. {
  1780. printDebugInfo();
  1781. }
  1782. return ok;
  1783. }
  1784. void LLFace::setViewerObject(LLViewerObject* objp)
  1785. {
  1786. mVObjp = objp;
  1787. }
  1788. const LLColor4& LLFace::getRenderColor() const
  1789. {
  1790. if (isState(USE_FACE_COLOR))
  1791. {
  1792. return mFaceColor; // Face Color
  1793. }
  1794. else
  1795. {
  1796. const LLTextureEntry* tep = getTextureEntry();
  1797. return (tep ? tep->getColor() : LLColor4::white);
  1798. }
  1799. }
  1800. void LLFace::renderSetColor() const
  1801. {
  1802. if (!LLFacePool::LLOverrideFaceColor::sOverrideFaceColor)
  1803. {
  1804. const LLColor4* color = &(getRenderColor());
  1805. gGL.diffuseColor4fv(color->mV);
  1806. }
  1807. }
  1808. S32 LLFace::pushVertices(const U16* index_array) const
  1809. {
  1810. if (mIndicesCount)
  1811. {
  1812. U32 render_type = LLRender::TRIANGLES;
  1813. if (mDrawInfo)
  1814. {
  1815. render_type = mDrawInfo->mDrawMode;
  1816. }
  1817. mVertexBuffer->drawRange(render_type, mGeomIndex, mGeomIndex+mGeomCount-1, mIndicesCount, mIndicesIndex);
  1818. gPipeline.addTrianglesDrawn(mIndicesCount, render_type);
  1819. }
  1820. return mIndicesCount;
  1821. }
  1822. const LLMatrix4& LLFace::getRenderMatrix() const
  1823. {
  1824. return mDrawablep->getRenderMatrix();
  1825. }
  1826. S32 LLFace::renderElements(const U16 *index_array) const
  1827. {
  1828. S32 ret = 0;
  1829. if (isState(GLOBAL))
  1830. {
  1831. ret = pushVertices(index_array);
  1832. }
  1833. else
  1834. {
  1835. gGL.pushMatrix();
  1836. gGL.multMatrix((float*)getRenderMatrix().mMatrix);
  1837. ret = pushVertices(index_array);
  1838. gGL.popMatrix();
  1839. }
  1840. return ret;
  1841. }
  1842. S32 LLFace::renderIndexed()
  1843. {
  1844. if(mDrawablep.isNull() || mDrawPoolp == NULL)
  1845. {
  1846. return 0;
  1847. }
  1848. return renderIndexed(mDrawPoolp->getVertexDataMask());
  1849. }
  1850. S32 LLFace::renderIndexed(U32 mask)
  1851. {
  1852. if (mVertexBuffer.isNull())
  1853. {
  1854. return 0;
  1855. }
  1856. mVertexBuffer->setBuffer(mask);
  1857. U16* index_array = (U16*) mVertexBuffer->getIndicesPointer();
  1858. return renderElements(index_array);
  1859. }
  1860. //============================================================================
  1861. // From llface.inl
  1862. S32 LLFace::getColors(LLStrider<LLColor4U> &colors)
  1863. {
  1864. if (!mGeomCount)
  1865. {
  1866. return -1;
  1867. }
  1868. // llassert(mGeomIndex >= 0);
  1869. mVertexBuffer->getColorStrider(colors, mGeomIndex, mGeomCount);
  1870. return mGeomIndex;
  1871. }
  1872. S32 LLFace::getIndices(LLStrider<U16> &indicesp)
  1873. {
  1874. mVertexBuffer->getIndexStrider(indicesp, mIndicesIndex, mIndicesCount);
  1875. llassert(indicesp[0] != indicesp[1]);
  1876. return mIndicesIndex;
  1877. }
  1878. LLVector3 LLFace::getPositionAgent() const
  1879. {
  1880. if (mDrawablep->isStatic())
  1881. {
  1882. return mCenterAgent;
  1883. }
  1884. else
  1885. {
  1886. return mCenterLocal * getRenderMatrix();
  1887. }
  1888. }
  1889. //
  1890. //atlas
  1891. //
  1892. void LLFace::removeAtlas()
  1893. {
  1894. setAtlasInUse(FALSE) ;
  1895. mAtlasInfop = NULL ;
  1896. }
  1897. const LLTextureAtlas* LLFace::getAtlas()const
  1898. {
  1899. if(mAtlasInfop)
  1900. {
  1901. return mAtlasInfop->getAtlas() ;
  1902. }
  1903. return NULL ;
  1904. }
  1905. const LLVector2* LLFace::getTexCoordOffset()const
  1906. {
  1907. if(isAtlasInUse())
  1908. {
  1909. return mAtlasInfop->getTexCoordOffset() ;
  1910. }
  1911. return NULL ;
  1912. }
  1913. const LLVector2* LLFace::getTexCoordScale() const
  1914. {
  1915. if(isAtlasInUse())
  1916. {
  1917. return mAtlasInfop->getTexCoordScale() ;
  1918. }
  1919. return NULL ;
  1920. }
  1921. BOOL LLFace::isAtlasInUse()const
  1922. {
  1923. return mUsingAtlas ;
  1924. }
  1925. BOOL LLFace::canUseAtlas()const
  1926. {
  1927. //no drawable or no spatial group, do not use atlas
  1928. if(!mDrawablep || !mDrawablep->getSpatialGroup())
  1929. {
  1930. return FALSE ;
  1931. }
  1932. //if bump face, do not use atlas
  1933. if(getTextureEntry() && getTextureEntry()->getBumpmap())
  1934. {
  1935. return FALSE ;
  1936. }
  1937. //if animated texture, do not use atlas
  1938. if(isState(TEXTURE_ANIM))
  1939. {
  1940. return FALSE ;
  1941. }
  1942. return TRUE ;
  1943. }
  1944. void LLFace::setAtlasInUse(BOOL flag)
  1945. {
  1946. //no valid atlas to use.
  1947. if(flag && (!mAtlasInfop || !mAtlasInfop->isValid()))
  1948. {
  1949. flag = FALSE ;
  1950. }
  1951. if(!flag && !mUsingAtlas)
  1952. {
  1953. return ;
  1954. }
  1955. //
  1956. //at this stage (flag || mUsingAtlas) is always true.
  1957. //
  1958. //rebuild the tex coords
  1959. if(mDrawablep)
  1960. {
  1961. gPipeline.markRebuild(mDrawablep, LLDrawable::REBUILD_TCOORD);
  1962. mUsingAtlas = flag ;
  1963. }
  1964. else
  1965. {
  1966. mUsingAtlas = FALSE ;
  1967. }
  1968. }
  1969. LLTextureAtlasSlot* LLFace::getAtlasInfo()
  1970. {
  1971. return mAtlasInfop ;
  1972. }
  1973. void LLFace::setAtlasInfo(LLTextureAtlasSlot* atlasp)
  1974. {
  1975. if(mAtlasInfop != atlasp)
  1976. {
  1977. if(mAtlasInfop)
  1978. {
  1979. //llerrs << "Atlas slot changed!" << llendl ;
  1980. }
  1981. mAtlasInfop = atlasp ;
  1982. }
  1983. }
  1984. LLViewerTexture* LLFace::getTexture() const
  1985. {
  1986. if(isAtlasInUse())
  1987. {
  1988. return (LLViewerTexture*)mAtlasInfop->getAtlas() ;
  1989. }
  1990. return mTexture ;
  1991. }
  1992. //switch to atlas or switch back to gl texture
  1993. //return TRUE if using atlas.
  1994. BOOL LLFace::switchTexture()
  1995. {
  1996. //no valid atlas or texture
  1997. if(!mAtlasInfop || !mAtlasInfop->isValid() || !mTexture)
  1998. {
  1999. return FALSE ;
  2000. }
  2001. if(mTexture->getTexelsInAtlas() >= (U32)mVSize ||
  2002. mTexture->getTexelsInAtlas() >= mTexture->getTexelsInGLTexture())
  2003. {
  2004. //switch to use atlas
  2005. //atlas resolution is qualified, use it.
  2006. if(!mUsingAtlas)
  2007. {
  2008. setAtlasInUse(TRUE) ;
  2009. }
  2010. }
  2011. else //if atlas not qualified.
  2012. {
  2013. //switch back to GL texture
  2014. if(mUsingAtlas && mTexture->isGLTextureCreated() &&
  2015. mTexture->getDiscardLevel() < mTexture->getDiscardLevelInAtlas())
  2016. {
  2017. setAtlasInUse(FALSE) ;
  2018. }
  2019. }
  2020. return mUsingAtlas ;
  2021. }
  2022. void LLFace::setVertexBuffer(LLVertexBuffer* buffer)
  2023. {
  2024. mVertexBuffer = buffer;
  2025. llassert(verify());
  2026. }
  2027. void LLFace::clearVertexBuffer()
  2028. {
  2029. mVertexBuffer = NULL;
  2030. mLastVertexBuffer = NULL;
  2031. }
  2032. //static
  2033. U32 LLFace::getRiggedDataMask(U32 type)
  2034. {
  2035. static const U32 rigged_data_mask[] = {
  2036. LLDrawPoolAvatar::RIGGED_SIMPLE_MASK,
  2037. LLDrawPoolAvatar::RIGGED_FULLBRIGHT_MASK,
  2038. LLDrawPoolAvatar::RIGGED_SHINY_MASK,
  2039. LLDrawPoolAvatar::RIGGED_FULLBRIGHT_SHINY_MASK,
  2040. LLDrawPoolAvatar::RIGGED_GLOW_MASK,
  2041. LLDrawPoolAvatar::RIGGED_ALPHA_MASK,
  2042. LLDrawPoolAvatar::RIGGED_FULLBRIGHT_ALPHA_MASK,
  2043. LLDrawPoolAvatar::RIGGED_DEFERRED_BUMP_MASK,
  2044. LLDrawPoolAvatar::RIGGED_DEFERRED_SIMPLE_MASK,
  2045. };
  2046. llassert(type < sizeof(rigged_data_mask)/sizeof(U32));
  2047. return rigged_data_mask[type];
  2048. }
  2049. U32 LLFace::getRiggedVertexBufferDataMask() const
  2050. {
  2051. U32 data_mask = 0;
  2052. for (U32 i = 0; i < mRiggedIndex.size(); ++i)
  2053. {
  2054. if (mRiggedIndex[i] > -1)
  2055. {
  2056. data_mask |= LLFace::getRiggedDataMask(i);
  2057. }
  2058. }
  2059. return data_mask;
  2060. }
  2061. S32 LLFace::getRiggedIndex(U32 type) const
  2062. {
  2063. if (mRiggedIndex.empty())
  2064. {
  2065. return -1;
  2066. }
  2067. llassert(type < mRiggedIndex.size());
  2068. return mRiggedIndex[type];
  2069. }
  2070. void LLFace::setRiggedIndex(U32 type, S32 index)
  2071. {
  2072. if (mRiggedIndex.empty())
  2073. {
  2074. mRiggedIndex.resize(LLDrawPoolAvatar::NUM_RIGGED_PASSES);
  2075. for (U32 i = 0; i < mRiggedIndex.size(); ++i)
  2076. {
  2077. mRiggedIndex[i] = -1;
  2078. }
  2079. }
  2080. llassert(type < mRiggedIndex.size());
  2081. mRiggedIndex[type] = index;
  2082. }