PageRenderTime 87ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llvowlsky.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 836 lines | 597 code | 168 blank | 71 comment | 58 complexity | c73ec6f34fc31b56b113846aad25d543 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llvowlsky.cpp
  3. * @brief LLVOWLSky class implementation
  4. *
  5. * $LicenseInfo:firstyear=2007&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 "pipeline.h"
  28. #include "llvowlsky.h"
  29. #include "llsky.h"
  30. #include "lldrawpoolwlsky.h"
  31. #include "llface.h"
  32. #include "llwlparammanager.h"
  33. #include "llviewercontrol.h"
  34. #define DOME_SLICES 1
  35. const F32 LLVOWLSky::DISTANCE_TO_STARS = (HORIZON_DIST - 10.f)*0.25f;
  36. const U32 LLVOWLSky::MIN_SKY_DETAIL = 3;
  37. const U32 LLVOWLSky::MAX_SKY_DETAIL = 180;
  38. inline U32 LLVOWLSky::getNumStacks(void)
  39. {
  40. return llmin(MAX_SKY_DETAIL, llmax(MIN_SKY_DETAIL, gSavedSettings.getU32("WLSkyDetail")));
  41. }
  42. inline U32 LLVOWLSky::getNumSlices(void)
  43. {
  44. return 2 * llmin(MAX_SKY_DETAIL, llmax(MIN_SKY_DETAIL, gSavedSettings.getU32("WLSkyDetail")));
  45. }
  46. inline U32 LLVOWLSky::getFanNumVerts(void)
  47. {
  48. return getNumSlices() + 1;
  49. }
  50. inline U32 LLVOWLSky::getFanNumIndices(void)
  51. {
  52. return getNumSlices() * 3;
  53. }
  54. inline U32 LLVOWLSky::getStripsNumVerts(void)
  55. {
  56. return (getNumStacks() - 1) * getNumSlices();
  57. }
  58. inline U32 LLVOWLSky::getStripsNumIndices(void)
  59. {
  60. return 2 * ((getNumStacks() - 2) * (getNumSlices() + 1)) + 1 ;
  61. }
  62. inline U32 LLVOWLSky::getStarsNumVerts(void)
  63. {
  64. return 1000;
  65. }
  66. inline U32 LLVOWLSky::getStarsNumIndices(void)
  67. {
  68. return 1000;
  69. }
  70. LLVOWLSky::LLVOWLSky(const LLUUID &id, const LLPCode pcode, LLViewerRegion *regionp)
  71. : LLStaticViewerObject(id, pcode, regionp, TRUE)
  72. {
  73. initStars();
  74. }
  75. void LLVOWLSky::initSunDirection(LLVector3 const & sun_direction,
  76. LLVector3 const & sun_angular_velocity)
  77. {
  78. }
  79. BOOL LLVOWLSky::idleUpdate(LLAgent &agent, LLWorld &world, const F64 &time)
  80. {
  81. return TRUE;
  82. }
  83. BOOL LLVOWLSky::isActive(void) const
  84. {
  85. return FALSE;
  86. }
  87. LLDrawable * LLVOWLSky::createDrawable(LLPipeline * pipeline)
  88. {
  89. pipeline->allocDrawable(this);
  90. //LLDrawPoolWLSky *poolp = static_cast<LLDrawPoolWLSky *>(
  91. gPipeline.getPool(LLDrawPool::POOL_WL_SKY);
  92. mDrawable->setRenderType(LLPipeline::RENDER_TYPE_WL_SKY);
  93. return mDrawable;
  94. }
  95. inline F32 LLVOWLSky::calcPhi(U32 i)
  96. {
  97. // i should range from [0..SKY_STACKS] so t will range from [0.f .. 1.f]
  98. F32 t = float(i) / float(getNumStacks());
  99. // ^4 the parameter of the tesselation to bias things toward 0 (the dome's apex)
  100. t = t*t*t*t;
  101. // invert and square the parameter of the tesselation to bias things toward 1 (the horizon)
  102. t = 1.f - t;
  103. t = t*t;
  104. t = 1.f - t;
  105. return (F_PI / 8.f) * t;
  106. }
  107. #if !DOME_SLICES
  108. static const F32 Q = (1.f + sqrtf(5.f))/2.f; //golden ratio
  109. //icosahedron verts (based on asset b0c7b76e-28c6-1f87-a1de-752d5e3cd264, contact Runitai Linden for a copy)
  110. static const LLVector3 icosahedron_vert[] =
  111. {
  112. LLVector3(0,1.f,Q),
  113. LLVector3(0,-1.f,Q),
  114. LLVector3(0,-1.f,-Q),
  115. LLVector3(0,1.f,-Q),
  116. LLVector3(Q,0,1.f),
  117. LLVector3(-Q,0,1.f),
  118. LLVector3(-Q,0,-1.f),
  119. LLVector3(Q,0,-1.f),
  120. LLVector3(1,-Q,0.f),
  121. LLVector3(-1,-Q,0.f),
  122. LLVector3(-1,Q,0.f),
  123. LLVector3(1,Q,0.f),
  124. };
  125. //indices
  126. static const U32 icosahedron_ind[] =
  127. {
  128. 5,0,1,
  129. 10,0,5,
  130. 5,1,9,
  131. 10,5,6,
  132. 6,5,9,
  133. 11,0,10,
  134. 3,11,10,
  135. 3,10,6,
  136. 3,6,2,
  137. 7,3,2,
  138. 8,7,2,
  139. 4,7,8,
  140. 1,4,8,
  141. 9,8,2,
  142. 9,2,6,
  143. 11,3,7,
  144. 4,0,11,
  145. 4,11,7,
  146. 1,0,4,
  147. 1,8,9,
  148. };
  149. //split every triangle in LLVertexBuffer into even fourths (assumes index triangle lists)
  150. void subdivide(LLVertexBuffer& in, LLVertexBuffer* ret)
  151. {
  152. S32 tri_in = in.getNumIndices()/3;
  153. ret->allocateBuffer(tri_in*4*3, tri_in*4*3, TRUE);
  154. LLStrider<LLVector3> vin, vout;
  155. LLStrider<U16> indin, indout;
  156. ret->getVertexStrider(vout);
  157. in.getVertexStrider(vin);
  158. ret->getIndexStrider(indout);
  159. in.getIndexStrider(indin);
  160. for (S32 i = 0; i < tri_in; i++)
  161. {
  162. LLVector3 v0 = vin[*indin++];
  163. LLVector3 v1 = vin[*indin++];
  164. LLVector3 v2 = vin[*indin++];
  165. LLVector3 v3 = (v0 + v1) * 0.5f;
  166. LLVector3 v4 = (v1 + v2) * 0.5f;
  167. LLVector3 v5 = (v2 + v0) * 0.5f;
  168. *vout++ = v0;
  169. *vout++ = v3;
  170. *vout++ = v5;
  171. *vout++ = v3;
  172. *vout++ = v4;
  173. *vout++ = v5;
  174. *vout++ = v3;
  175. *vout++ = v1;
  176. *vout++ = v4;
  177. *vout++ = v5;
  178. *vout++ = v4;
  179. *vout++ = v2;
  180. }
  181. for (S32 i = 0; i < ret->getNumIndices(); i++)
  182. {
  183. *indout++ = i;
  184. }
  185. }
  186. void chop(LLVertexBuffer& in, LLVertexBuffer* out)
  187. {
  188. //chop off all triangles below horizon
  189. F32 d = LLWLParamManager::sParamMgr->getDomeOffset() * LLWLParamManager::sParamMgr->getDomeRadius();
  190. std::vector<LLVector3> vert;
  191. LLStrider<LLVector3> vin;
  192. LLStrider<U16> index;
  193. in.getVertexStrider(vin);
  194. in.getIndexStrider(index);
  195. U32 tri_count = in.getNumIndices()/3;
  196. for (U32 i = 0; i < tri_count; i++)
  197. {
  198. LLVector3 &v1 = vin[index[i*3+0]];
  199. LLVector3 &v2 = vin[index[i*3+1]];
  200. LLVector3 &v3 = vin[index[i*3+2]];
  201. if (v1.mV[1] > d ||
  202. v2.mV[1] > d ||
  203. v3.mV[1] > d)
  204. {
  205. v1.mV[1] = llmax(v1.mV[1], d);
  206. v2.mV[1] = llmax(v1.mV[1], d);
  207. v3.mV[1] = llmax(v1.mV[1], d);
  208. vert.push_back(v1);
  209. vert.push_back(v2);
  210. vert.push_back(v3);
  211. }
  212. }
  213. out->allocateBuffer(vert.size(), vert.size(), TRUE);
  214. LLStrider<LLVector3> vout;
  215. out->getVertexStrider(vout);
  216. out->getIndexStrider(index);
  217. for (U32 i = 0; i < vert.size(); i++)
  218. {
  219. *vout++ = vert[i];
  220. *index++ = i;
  221. }
  222. }
  223. #endif // !DOME_SLICES
  224. void LLVOWLSky::resetVertexBuffers()
  225. {
  226. mFanVerts = NULL;
  227. mStripsVerts.clear();
  228. mStarsVerts = NULL;
  229. gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_ALL, TRUE);
  230. }
  231. void LLVOWLSky::cleanupGL()
  232. {
  233. mFanVerts = NULL;
  234. mStripsVerts.clear();
  235. mStarsVerts = NULL;
  236. LLDrawPoolWLSky::cleanupGL();
  237. }
  238. void LLVOWLSky::restoreGL()
  239. {
  240. LLDrawPoolWLSky::restoreGL();
  241. gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_ALL, TRUE);
  242. }
  243. static LLFastTimer::DeclareTimer FTM_GEO_SKY("Sky Geometry");
  244. BOOL LLVOWLSky::updateGeometry(LLDrawable * drawable)
  245. {
  246. LLFastTimer ftm(FTM_GEO_SKY);
  247. LLStrider<LLVector3> vertices;
  248. LLStrider<LLVector2> texCoords;
  249. LLStrider<U16> indices;
  250. #if DOME_SLICES
  251. {
  252. mFanVerts = new LLVertexBuffer(LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK, GL_STATIC_DRAW_ARB);
  253. mFanVerts->allocateBuffer(getFanNumVerts(), getFanNumIndices(), TRUE);
  254. BOOL success = mFanVerts->getVertexStrider(vertices)
  255. && mFanVerts->getTexCoord0Strider(texCoords)
  256. && mFanVerts->getIndexStrider(indices);
  257. if(!success)
  258. {
  259. llerrs << "Failed updating WindLight sky geometry." << llendl;
  260. }
  261. buildFanBuffer(vertices, texCoords, indices);
  262. mFanVerts->flush();
  263. }
  264. {
  265. const U32 max_buffer_bytes = gSavedSettings.getS32("RenderMaxVBOSize")*1024;
  266. const U32 data_mask = LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK;
  267. const U32 max_verts = max_buffer_bytes / LLVertexBuffer::calcVertexSize(data_mask);
  268. const U32 total_stacks = getNumStacks();
  269. const U32 verts_per_stack = getNumSlices();
  270. // each seg has to have one more row of verts than it has stacks
  271. // then round down
  272. const U32 stacks_per_seg = (max_verts - verts_per_stack) / verts_per_stack;
  273. // round up to a whole number of segments
  274. const U32 strips_segments = (total_stacks+stacks_per_seg-1) / stacks_per_seg;
  275. llinfos << "WL Skydome strips in " << strips_segments << " batches." << llendl;
  276. mStripsVerts.resize(strips_segments, NULL);
  277. LLTimer timer;
  278. timer.start();
  279. for (U32 i = 0; i < strips_segments ;++i)
  280. {
  281. LLVertexBuffer * segment = new LLVertexBuffer(LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK, GL_STATIC_DRAW_ARB);
  282. mStripsVerts[i] = segment;
  283. U32 num_stacks_this_seg = stacks_per_seg;
  284. if ((i == strips_segments - 1) && (total_stacks % stacks_per_seg) != 0)
  285. {
  286. // for the last buffer only allocate what we'll use
  287. num_stacks_this_seg = total_stacks % stacks_per_seg;
  288. }
  289. // figure out what range of the sky we're filling
  290. const U32 begin_stack = i * stacks_per_seg;
  291. const U32 end_stack = begin_stack + num_stacks_this_seg;
  292. llassert(end_stack <= total_stacks);
  293. const U32 num_verts_this_seg = verts_per_stack * (num_stacks_this_seg+1);
  294. llassert(num_verts_this_seg <= max_verts);
  295. const U32 num_indices_this_seg = 1+num_stacks_this_seg*(2+2*verts_per_stack);
  296. llassert(num_indices_this_seg * sizeof(U16) <= max_buffer_bytes);
  297. segment->allocateBuffer(num_verts_this_seg, num_indices_this_seg, TRUE);
  298. // lock the buffer
  299. BOOL success = segment->getVertexStrider(vertices)
  300. && segment->getTexCoord0Strider(texCoords)
  301. && segment->getIndexStrider(indices);
  302. if(!success)
  303. {
  304. llerrs << "Failed updating WindLight sky geometry." << llendl;
  305. }
  306. // fill it
  307. buildStripsBuffer(begin_stack, end_stack, vertices, texCoords, indices);
  308. // and unlock the buffer
  309. segment->flush();
  310. }
  311. llinfos << "completed in " << llformat("%.2f", timer.getElapsedTimeF32()) << "seconds" << llendl;
  312. }
  313. #else
  314. mStripsVerts = new LLVertexBuffer(LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK, GL_STATIC_DRAW_ARB);
  315. const F32 RADIUS = LLWLParamManager::sParamMgr->getDomeRadius();
  316. LLPointer<LLVertexBuffer> temp = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX, 0);
  317. temp->allocateBuffer(12, 60, TRUE);
  318. BOOL success = temp->getVertexStrider(vertices)
  319. && temp->getIndexStrider(indices);
  320. if (success)
  321. {
  322. for (U32 i = 0; i < 12; i++)
  323. {
  324. *vertices++ = icosahedron_vert[i];
  325. }
  326. for (U32 i = 0; i < 60; i++)
  327. {
  328. *indices++ = icosahedron_ind[i];
  329. }
  330. }
  331. LLPointer<LLVertexBuffer> temp2;
  332. for (U32 i = 0; i < 8; i++)
  333. {
  334. temp2 = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX, 0);
  335. subdivide(*temp, temp2);
  336. temp = temp2;
  337. }
  338. temp->getVertexStrider(vertices);
  339. for (S32 i = 0; i < temp->getNumVerts(); i++)
  340. {
  341. LLVector3 v = vertices[i];
  342. v.normVec();
  343. vertices[i] = v*RADIUS;
  344. }
  345. temp2 = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX, 0);
  346. chop(*temp, temp2);
  347. mStripsVerts->allocateBuffer(temp2->getNumVerts(), temp2->getNumIndices(), TRUE);
  348. success = mStripsVerts->getVertexStrider(vertices)
  349. && mStripsVerts->getTexCoordStrider(texCoords)
  350. && mStripsVerts->getIndexStrider(indices);
  351. LLStrider<LLVector3> v;
  352. temp2->getVertexStrider(v);
  353. LLStrider<U16> ind;
  354. temp2->getIndexStrider(ind);
  355. if (success)
  356. {
  357. for (S32 i = 0; i < temp2->getNumVerts(); ++i)
  358. {
  359. LLVector3 vert = *v++;
  360. vert.normVec();
  361. F32 z0 = vert.mV[2];
  362. F32 x0 = vert.mV[0];
  363. vert *= RADIUS;
  364. *vertices++ = vert;
  365. *texCoords++ = LLVector2((-z0 + 1.f) / 2.f, (-x0 + 1.f) / 2.f);
  366. }
  367. for (S32 i = 0; i < temp2->getNumIndices(); ++i)
  368. {
  369. *indices++ = *ind++;
  370. }
  371. }
  372. mStripsVerts->flush();
  373. #endif
  374. updateStarColors();
  375. updateStarGeometry(drawable);
  376. LLPipeline::sCompiles++;
  377. return TRUE;
  378. }
  379. void LLVOWLSky::drawStars(void)
  380. {
  381. // render the stars as a sphere centered at viewer camera
  382. if (mStarsVerts.notNull())
  383. {
  384. mStarsVerts->setBuffer(LLDrawPoolWLSky::STAR_VERTEX_DATA_MASK);
  385. mStarsVerts->drawArrays(LLRender::TRIANGLES, 0, getStarsNumVerts()*4);
  386. }
  387. }
  388. void LLVOWLSky::drawDome(void)
  389. {
  390. if (mStripsVerts.empty())
  391. {
  392. updateGeometry(mDrawable);
  393. }
  394. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
  395. const U32 data_mask = LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK;
  396. #if DOME_SLICES
  397. std::vector< LLPointer<LLVertexBuffer> >::const_iterator strips_vbo_iter, end_strips;
  398. end_strips = mStripsVerts.end();
  399. for(strips_vbo_iter = mStripsVerts.begin(); strips_vbo_iter != end_strips; ++strips_vbo_iter)
  400. {
  401. LLVertexBuffer * strips_segment = strips_vbo_iter->get();
  402. strips_segment->setBuffer(data_mask);
  403. strips_segment->drawRange(
  404. LLRender::TRIANGLE_STRIP,
  405. 0, strips_segment->getNumVerts()-1, strips_segment->getNumIndices(),
  406. 0);
  407. gPipeline.addTrianglesDrawn(strips_segment->getNumIndices(), LLRender::TRIANGLE_STRIP);
  408. }
  409. #else
  410. mStripsVerts->setBuffer(data_mask);
  411. gGL.syncMatrices();
  412. glDrawRangeElements(
  413. GL_TRIANGLES,
  414. 0, mStripsVerts->getNumVerts()-1, mStripsVerts->getNumIndices(),
  415. GL_UNSIGNED_SHORT,
  416. mStripsVerts->getIndicesPointer());
  417. #endif
  418. LLVertexBuffer::unbind();
  419. }
  420. void LLVOWLSky::initStars()
  421. {
  422. // Initialize star map
  423. mStarVertices.resize(getStarsNumVerts());
  424. mStarColors.resize(getStarsNumVerts());
  425. mStarIntensities.resize(getStarsNumVerts());
  426. std::vector<LLVector3>::iterator v_p = mStarVertices.begin();
  427. std::vector<LLColor4>::iterator v_c = mStarColors.begin();
  428. std::vector<F32>::iterator v_i = mStarIntensities.begin();
  429. U32 i;
  430. for (i = 0; i < getStarsNumVerts(); ++i)
  431. {
  432. v_p->mV[VX] = ll_frand() - 0.5f;
  433. v_p->mV[VY] = ll_frand() - 0.5f;
  434. // we only want stars on the top half of the dome!
  435. v_p->mV[VZ] = ll_frand()/2.f;
  436. v_p->normVec();
  437. *v_p *= DISTANCE_TO_STARS;
  438. *v_i = llmin((F32)pow(ll_frand(),2.f) + 0.1f, 1.f);
  439. v_c->mV[VRED] = 0.75f + ll_frand() * 0.25f ;
  440. v_c->mV[VGREEN] = 1.f ;
  441. v_c->mV[VBLUE] = 0.75f + ll_frand() * 0.25f ;
  442. v_c->mV[VALPHA] = 1.f;
  443. v_c->clamp();
  444. v_p++;
  445. v_c++;
  446. v_i++;
  447. }
  448. }
  449. void LLVOWLSky::buildFanBuffer(LLStrider<LLVector3> & vertices,
  450. LLStrider<LLVector2> & texCoords,
  451. LLStrider<U16> & indices)
  452. {
  453. const F32 RADIUS = LLWLParamManager::getInstance()->getDomeRadius();
  454. U32 i, num_slices;
  455. F32 phi0, theta, x0, y0, z0;
  456. // paranoia checking for SL-55986/SL-55833
  457. U32 count_verts = 0;
  458. U32 count_indices = 0;
  459. // apex
  460. *vertices++ = LLVector3(0.f, RADIUS, 0.f);
  461. *texCoords++ = LLVector2(0.5f, 0.5f);
  462. ++count_verts;
  463. num_slices = getNumSlices();
  464. // and fan in a circle around the apex
  465. phi0 = calcPhi(1);
  466. for(i = 0; i < num_slices; ++i) {
  467. theta = 2.f * F_PI * float(i) / float(num_slices);
  468. // standard transformation from spherical to
  469. // rectangular coordinates
  470. x0 = sin(phi0) * cos(theta);
  471. y0 = cos(phi0);
  472. z0 = sin(phi0) * sin(theta);
  473. *vertices++ = LLVector3(x0 * RADIUS, y0 * RADIUS, z0 * RADIUS);
  474. // generate planar uv coordinates
  475. // note: x and z are transposed in order for things to animate
  476. // correctly in the global coordinate system where +x is east and
  477. // +y is north
  478. *texCoords++ = LLVector2((-z0 + 1.f) / 2.f, (-x0 + 1.f) / 2.f);
  479. ++count_verts;
  480. if (i > 0)
  481. {
  482. *indices++ = 0;
  483. *indices++ = i;
  484. *indices++ = i+1;
  485. count_indices += 3;
  486. }
  487. }
  488. // the last vertex of the last triangle should wrap around to
  489. // the beginning
  490. *indices++ = 0;
  491. *indices++ = num_slices;
  492. *indices++ = 1;
  493. count_indices += 3;
  494. // paranoia checking for SL-55986/SL-55833
  495. llassert(getFanNumVerts() == count_verts);
  496. llassert(getFanNumIndices() == count_indices);
  497. }
  498. void LLVOWLSky::buildStripsBuffer(U32 begin_stack, U32 end_stack,
  499. LLStrider<LLVector3> & vertices,
  500. LLStrider<LLVector2> & texCoords,
  501. LLStrider<U16> & indices)
  502. {
  503. const F32 RADIUS = LLWLParamManager::getInstance()->getDomeRadius();
  504. U32 i, j, num_slices, num_stacks;
  505. F32 phi0, theta, x0, y0, z0;
  506. // paranoia checking for SL-55986/SL-55833
  507. U32 count_verts = 0;
  508. U32 count_indices = 0;
  509. num_slices = getNumSlices();
  510. num_stacks = getNumStacks();
  511. llassert(end_stack <= num_stacks);
  512. // stacks are iterated one-indexed since phi(0) was handled by the fan above
  513. for(i = begin_stack + 1; i <= end_stack+1; ++i)
  514. {
  515. phi0 = calcPhi(i);
  516. for(j = 0; j < num_slices; ++j)
  517. {
  518. theta = F_TWO_PI * (float(j) / float(num_slices));
  519. // standard transformation from spherical to
  520. // rectangular coordinates
  521. x0 = sin(phi0) * cos(theta);
  522. y0 = cos(phi0);
  523. z0 = sin(phi0) * sin(theta);
  524. if (i == num_stacks-2)
  525. {
  526. *vertices++ = LLVector3(x0*RADIUS, y0*RADIUS-1024.f*2.f, z0*RADIUS);
  527. }
  528. else if (i == num_stacks-1)
  529. {
  530. *vertices++ = LLVector3(0, y0*RADIUS-1024.f*2.f, 0);
  531. }
  532. else
  533. {
  534. *vertices++ = LLVector3(x0 * RADIUS, y0 * RADIUS, z0 * RADIUS);
  535. }
  536. ++count_verts;
  537. // generate planar uv coordinates
  538. // note: x and z are transposed in order for things to animate
  539. // correctly in the global coordinate system where +x is east and
  540. // +y is north
  541. *texCoords++ = LLVector2((-z0 + 1.f) / 2.f, (-x0 + 1.f) / 2.f);
  542. }
  543. }
  544. //build triangle strip...
  545. *indices++ = 0 ;
  546. count_indices++ ;
  547. S32 k = 0 ;
  548. for(i = 1; i <= end_stack - begin_stack; ++i)
  549. {
  550. *indices++ = i * num_slices + k ;
  551. count_indices++ ;
  552. k = (k+1) % num_slices ;
  553. for(j = 0; j < num_slices ; ++j)
  554. {
  555. *indices++ = (i-1) * num_slices + k ;
  556. *indices++ = i * num_slices + k ;
  557. count_indices += 2 ;
  558. k = (k+1) % num_slices ;
  559. }
  560. if((--k) < 0)
  561. {
  562. k = num_slices - 1 ;
  563. }
  564. *indices++ = i * num_slices + k ;
  565. count_indices++ ;
  566. }
  567. }
  568. void LLVOWLSky::updateStarColors()
  569. {
  570. std::vector<LLColor4>::iterator v_c = mStarColors.begin();
  571. std::vector<F32>::iterator v_i = mStarIntensities.begin();
  572. std::vector<LLVector3>::iterator v_p = mStarVertices.begin();
  573. const F32 var = 0.15f;
  574. const F32 min = 0.5f; //0.75f;
  575. const F32 sunclose_max = 0.6f;
  576. const F32 sunclose_range = 1 - sunclose_max;
  577. //F32 below_horizon = - llmin(0.0f, gSky.mVOSkyp->getToSunLast().mV[2]);
  578. //F32 brightness_factor = llmin(1.0f, below_horizon * 20);
  579. static S32 swap = 0;
  580. swap++;
  581. if ((swap % 2) == 1)
  582. {
  583. F32 intensity; // max intensity of each star
  584. U32 x;
  585. for (x = 0; x < getStarsNumVerts(); ++x)
  586. {
  587. F32 sundir_factor = 1;
  588. LLVector3 tostar = *v_p;
  589. tostar.normVec();
  590. const F32 how_close_to_sun = tostar * gSky.mVOSkyp->getToSunLast();
  591. if (how_close_to_sun > sunclose_max)
  592. {
  593. sundir_factor = (1 - how_close_to_sun) / sunclose_range;
  594. }
  595. intensity = *(v_i);
  596. F32 alpha = v_c->mV[VALPHA] + (ll_frand() - 0.5f) * var * intensity;
  597. if (alpha < min * intensity)
  598. {
  599. alpha = min * intensity;
  600. }
  601. if (alpha > intensity)
  602. {
  603. alpha = intensity;
  604. }
  605. //alpha *= brightness_factor * sundir_factor;
  606. alpha = llclamp(alpha, 0.f, 1.f);
  607. v_c->mV[VALPHA] = alpha;
  608. v_c++;
  609. v_i++;
  610. v_p++;
  611. }
  612. }
  613. }
  614. BOOL LLVOWLSky::updateStarGeometry(LLDrawable *drawable)
  615. {
  616. LLStrider<LLVector3> verticesp;
  617. LLStrider<LLColor4U> colorsp;
  618. LLStrider<LLVector2> texcoordsp;
  619. if (mStarsVerts.isNull())
  620. {
  621. mStarsVerts = new LLVertexBuffer(LLDrawPoolWLSky::STAR_VERTEX_DATA_MASK, GL_DYNAMIC_DRAW);
  622. mStarsVerts->allocateBuffer(getStarsNumVerts()*6, 0, TRUE);
  623. }
  624. BOOL success = mStarsVerts->getVertexStrider(verticesp)
  625. && mStarsVerts->getColorStrider(colorsp)
  626. && mStarsVerts->getTexCoord0Strider(texcoordsp);
  627. if(!success)
  628. {
  629. llerrs << "Failed updating star geometry." << llendl;
  630. }
  631. // *TODO: fix LLStrider with a real prefix increment operator so it can be
  632. // used as a model of OutputIterator. -Brad
  633. // std::copy(mStarVertices.begin(), mStarVertices.end(), verticesp);
  634. if (mStarVertices.size() < getStarsNumVerts())
  635. {
  636. llerrs << "Star reference geometry insufficient." << llendl;
  637. }
  638. for (U32 vtx = 0; vtx < getStarsNumVerts(); ++vtx)
  639. {
  640. LLVector3 at = mStarVertices[vtx];
  641. at.normVec();
  642. LLVector3 left = at%LLVector3(0,0,1);
  643. LLVector3 up = at%left;
  644. F32 sc = 0.5f+ll_frand()*1.25f;
  645. left *= sc;
  646. up *= sc;
  647. *(verticesp++) = mStarVertices[vtx];
  648. *(verticesp++) = mStarVertices[vtx]+left;
  649. *(verticesp++) = mStarVertices[vtx]+left+up;
  650. *(verticesp++) = mStarVertices[vtx]+left;
  651. *(verticesp++) = mStarVertices[vtx]+left+up;
  652. *(verticesp++) = mStarVertices[vtx]+up;
  653. *(texcoordsp++) = LLVector2(0,0);
  654. *(texcoordsp++) = LLVector2(0,1);
  655. *(texcoordsp++) = LLVector2(1,1);
  656. *(texcoordsp++) = LLVector2(0,1);
  657. *(texcoordsp++) = LLVector2(1,1);
  658. *(texcoordsp++) = LLVector2(1,0);
  659. *(colorsp++) = LLColor4U(mStarColors[vtx]);
  660. *(colorsp++) = LLColor4U(mStarColors[vtx]);
  661. *(colorsp++) = LLColor4U(mStarColors[vtx]);
  662. *(colorsp++) = LLColor4U(mStarColors[vtx]);
  663. *(colorsp++) = LLColor4U(mStarColors[vtx]);
  664. *(colorsp++) = LLColor4U(mStarColors[vtx]);
  665. }
  666. mStarsVerts->flush();
  667. return TRUE;
  668. }