llvowlsky.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**
  2. * @file llvowlsky.cpp
  3. * @brief LLVOWLSky class implementation
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llvowlsky.h"
  34. #include "llfasttimer.h"
  35. #include "lldrawpoolwlsky.h"
  36. #include "llenvironment.h"
  37. #include "llface.h"
  38. #include "llpipeline.h"
  39. #include "llsky.h"
  40. #include "llviewercontrol.h"
  41. #include "llvovolume.h" // For LLVOVolume::sRenderMaxVBOSize
  42. #define STAR_NUM_VERTS 1000
  43. // Anything less than 3 makes it impossible to create a closed dome.
  44. constexpr U32 MIN_SKY_DETAIL = 3;
  45. // Anything bigger than about 180 will cause getStripsNumVerts() to exceed
  46. // 65535.
  47. constexpr U32 MAX_SKY_DETAIL = 180;
  48. U32 LLVOWLSky::sWLSkyDetail = 64;
  49. LLVOWLSky::LLVOWLSky(const LLUUID& id, LLViewerRegion* regionp)
  50. : LLStaticViewerObject(id, LL_VO_WL_SKY, regionp, true),
  51. mLastWLSkyDetail(sWLSkyDetail)
  52. {
  53. initStars();
  54. }
  55. //static
  56. void LLVOWLSky::initClass()
  57. {
  58. updateSettings();
  59. }
  60. //static
  61. void LLVOWLSky::updateSettings()
  62. {
  63. sWLSkyDetail = llclamp(gSavedSettings.getU32("WLSkyDetail"),
  64. MIN_SKY_DETAIL, MAX_SKY_DETAIL);
  65. constexpr U32 data_mask = LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK;
  66. const U32 max_verts = LLVOVolume::sRenderMaxVBOSize * 1024 /
  67. LLVertexBuffer::calcVertexSize(data_mask);
  68. bool max_vbo_clamp = false;
  69. do
  70. {
  71. U32 verts_per_stack = getNumSlices();
  72. U32 stacks_per_seg = (max_verts - verts_per_stack) / verts_per_stack;
  73. if (stacks_per_seg > 0)
  74. {
  75. break;
  76. }
  77. max_vbo_clamp = true;
  78. }
  79. while (--sWLSkyDetail > MIN_SKY_DETAIL);
  80. if (max_vbo_clamp)
  81. {
  82. llwarns << "Sky details clamped to " << sWLSkyDetail
  83. << ": increase RenderMaxVBOSize for more." << llendl;
  84. }
  85. // We need to rebuild our current sky geometry
  86. if (gSky.mVOWLSkyp.notNull())
  87. {
  88. gSky.mVOWLSkyp->updateGeometry(gSky.mVOWLSkyp->mDrawable);
  89. }
  90. }
  91. //static
  92. void LLVOWLSky::cleanupClass()
  93. {
  94. }
  95. LLDrawable* LLVOWLSky::createDrawable()
  96. {
  97. gPipeline.allocDrawable(this);
  98. //LLDrawPoolWLSky *poolp = static_cast<LLDrawPoolWLSky *>(
  99. gPipeline.getPool(LLDrawPool::POOL_WL_SKY);
  100. mDrawable->setRenderType(LLPipeline::RENDER_TYPE_WL_SKY);
  101. return mDrawable;
  102. }
  103. LL_INLINE F32 LLVOWLSky::calcPhi(U32 i)
  104. {
  105. // i should range from [0..SKY_STACKS] so t will range from [0.f .. 1.f]
  106. F32 t = (F32)i / (F32)getNumStacks();
  107. // ^4 the parameter of the tesselation to bias things toward 0 (the dome's
  108. // apex)
  109. t *= t;
  110. t *= t;
  111. // Invert and square the parameter of the tesselation to bias things toward
  112. // 1 (the horizon)
  113. t = 1.f - t;
  114. t *= t;
  115. t = 1.f - t;
  116. return (F_PI / 8.f) * t;
  117. }
  118. void LLVOWLSky::resetVertexBuffers()
  119. {
  120. mStripsVerts.clear();
  121. mStarsVerts = NULL;
  122. gPipeline.markRebuild(mDrawable);
  123. }
  124. void LLVOWLSky::cleanupGL()
  125. {
  126. mStripsVerts.clear();
  127. mStarsVerts = NULL;
  128. LLDrawPoolWLSky::cleanupGL();
  129. }
  130. void LLVOWLSky::restoreGL()
  131. {
  132. LLDrawPoolWLSky::restoreGL();
  133. gPipeline.markRebuild(mDrawable);
  134. }
  135. bool LLVOWLSky::updateGeometry(LLDrawable* drawable)
  136. {
  137. LL_FAST_TIMER(FTM_GEO_SKY);
  138. initStars();
  139. if (mLastWLSkyDetail != sWLSkyDetail)
  140. {
  141. // Sky detail settings changed so we need to rebuild our vertex buffers
  142. resetVertexBuffers();
  143. }
  144. const U32 max_buffer_bytes = LLVOVolume::sRenderMaxVBOSize * 1024;
  145. const U32 data_mask = LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK;
  146. const U32 max_verts = max_buffer_bytes /
  147. LLVertexBuffer::calcVertexSize(data_mask);
  148. const U32 total_stacks = getNumStacks();
  149. const U32 verts_per_stack = getNumSlices();
  150. // Each segment has to have one more row of verts than it has stacks then
  151. // round down
  152. const U32 stacks_per_seg = (max_verts - verts_per_stack) / verts_per_stack;
  153. if (stacks_per_seg == 0)
  154. {
  155. llwarns << "Failed updating WindLight sky geometry." << llendl;
  156. return false;
  157. }
  158. // Round up to a whole number of segments
  159. const U32 strips_segments = (total_stacks + stacks_per_seg - 1) /
  160. stacks_per_seg;
  161. llinfos << "WL Skydome strips in " << strips_segments << " batches."
  162. << llendl;
  163. mStripsVerts.resize(strips_segments, NULL);
  164. LLStrider<LLVector3> vertices;
  165. LLStrider<LLVector2> texcoords;
  166. LLStrider<U16> indices;
  167. for (U32 i = 0; i < strips_segments; ++i)
  168. {
  169. LLVertexBuffer* segment =
  170. new LLVertexBuffer(LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK);
  171. #if LL_DEBUG_VB_ALLOC
  172. segment->setOwner("LLVOWLSky segment");
  173. #endif
  174. mStripsVerts[i] = segment;
  175. U32 num_stacks_this_seg = stacks_per_seg;
  176. if (i == strips_segments - 1 && (total_stacks % stacks_per_seg) != 0)
  177. {
  178. // For the last buffer only allocate what we will use
  179. num_stacks_this_seg = total_stacks % stacks_per_seg;
  180. }
  181. // Figure out what range of the sky we are filling
  182. const U32 begin_stack = i * stacks_per_seg;
  183. const U32 end_stack = begin_stack + num_stacks_this_seg;
  184. llassert(end_stack <= total_stacks);
  185. const U32 num_verts_this_seg = verts_per_stack *
  186. (num_stacks_this_seg + 1);
  187. llassert(num_verts_this_seg <= max_verts);
  188. const U32 num_indices_this_seg = 1 + num_stacks_this_seg *
  189. (2 + 2 * verts_per_stack);
  190. llassert(num_indices_this_seg * sizeof(U16) <= max_buffer_bytes);
  191. if (!segment->allocateBuffer(num_verts_this_seg, num_indices_this_seg))
  192. {
  193. llwarns << "Failure to allocate a vertex buffer with "
  194. << num_verts_this_seg << " vertices and "
  195. << num_indices_this_seg << " indices" << llendl;
  196. return false;
  197. }
  198. // Lock the buffer
  199. bool success = segment->getVertexStrider(vertices) &&
  200. segment->getTexCoord0Strider(texcoords) &&
  201. segment->getIndexStrider(indices);
  202. if (!success)
  203. {
  204. llwarns << "Failed updating WindLight sky geometry." << llendl;
  205. return false;
  206. }
  207. // Fill it
  208. buildStripsBuffer(begin_stack, end_stack, vertices, texcoords,
  209. indices);
  210. // And unlock the buffer
  211. segment->unmapBuffer();
  212. }
  213. updateStarColors();
  214. updateStarGeometry(drawable);
  215. return true;
  216. }
  217. void LLVOWLSky::drawStars()
  218. {
  219. // Render the stars as a sphere centered at viewer camera
  220. if (mStarsVerts.notNull())
  221. {
  222. mStarsVerts->setBuffer(LLDrawPoolWLSky::STAR_VERTEX_DATA_MASK);
  223. mStarsVerts->drawArrays(LLRender::TRIANGLES, 0, STAR_NUM_VERTS * 4);
  224. }
  225. }
  226. void LLVOWLSky::drawDome()
  227. {
  228. if (mStripsVerts.empty())
  229. {
  230. updateGeometry(mDrawable);
  231. }
  232. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
  233. U32 total_triangles = 0;
  234. for (std::vector<LLPointer<LLVertexBuffer> >::const_iterator
  235. it = mStripsVerts.begin(), end = mStripsVerts.end();
  236. it != end; ++it)
  237. {
  238. LLVertexBuffer* strips_segment = it->get();
  239. strips_segment->setBuffer(LLDrawPoolWLSky::SKY_VERTEX_DATA_MASK);
  240. strips_segment->drawRange(LLRender::TRIANGLE_STRIP, 0,
  241. strips_segment->getNumVerts() - 1,
  242. strips_segment->getNumIndices(), 0);
  243. total_triangles += (strips_segment->getNumIndices() + 2) * 3;
  244. }
  245. gPipeline.addTrianglesDrawn(total_triangles);
  246. LLVertexBuffer::unbind();
  247. }
  248. void LLVOWLSky::initStars()
  249. {
  250. const F32 distance_to_stars = getDomeRadius();
  251. // Initialize star map
  252. mStarVertices.resize(STAR_NUM_VERTS);
  253. mStarColors.resize(STAR_NUM_VERTS);
  254. mStarIntensities.resize(STAR_NUM_VERTS);
  255. std::vector<LLVector3>::iterator v_p = mStarVertices.begin();
  256. std::vector<LLColor4>::iterator v_c = mStarColors.begin();
  257. std::vector<F32>::iterator v_i = mStarIntensities.begin();
  258. for (U32 i = 0; i < STAR_NUM_VERTS; ++i)
  259. {
  260. v_p->mV[VX] = ll_frand() - 0.5f;
  261. v_p->mV[VY] = ll_frand() - 0.5f;
  262. // We only want stars on the top half of the dome !
  263. v_p->mV[VZ] = ll_frand() * 0.5f;
  264. v_p->normalize();
  265. *v_p++ *= distance_to_stars;
  266. *v_i++ = llmin(powf(ll_frand(), 2.f) + 0.1f, 1.f);
  267. v_c->mV[VRED] = 0.75f + ll_frand() * 0.25f;
  268. v_c->mV[VGREEN] = 1.f;
  269. v_c->mV[VBLUE] = 0.75f + ll_frand() * 0.25f;
  270. v_c->mV[VALPHA] = 1.f;
  271. (v_c++)->clamp();
  272. }
  273. }
  274. #if LL_VARIABLE_SKY_DOME_SIZE
  275. //static
  276. F32 LLVOWLSky::getDomeRadius()
  277. {
  278. // Corresponds as well to the Windlight constant, equal to 15000m
  279. F32 radius = SKY_DOME_RADIUS;
  280. const LLSettingsSky::ptr_t& skyp = gEnvironment.getCurrentSky();
  281. if (skyp)
  282. {
  283. // NOTE: this is for now a constant equal to SKY_DOME_RADIUS.
  284. radius = skyp->getDomeRadius();
  285. }
  286. return radius;
  287. }
  288. #endif
  289. void LLVOWLSky::buildStripsBuffer(U32 begin_stack, U32 end_stack,
  290. LLStrider<LLVector3>& vertices,
  291. LLStrider<LLVector2>& texcoords,
  292. LLStrider<U16>& indices)
  293. {
  294. const F32 radius = getDomeRadius();
  295. U32 num_slices = getNumSlices();
  296. U32 num_stacks = getNumStacks();
  297. llassert(end_stack <= num_stacks);
  298. // Stacks are iterated one-indexed since phi(0) was handled by the fan
  299. // above
  300. for (U32 i = begin_stack + 1; i <= end_stack + 1; ++i)
  301. {
  302. F32 phi0 = calcPhi(i);
  303. F32 sin_phi0 = sinf(phi0);
  304. F32 scaled_y0 = cosf(phi0) * radius;
  305. for (U32 j = 0; j < num_slices; ++j)
  306. {
  307. F32 theta = F_TWO_PI * (F32)j / (F32)num_slices;
  308. // Standard transformation from spherical to rectangular
  309. // coordinates
  310. F32 x0 = sin_phi0 * cosf(theta);
  311. F32 z0 = sin_phi0 * sinf(theta);
  312. if (i == num_stacks - 2)
  313. {
  314. *vertices++ = LLVector3(x0 * radius, scaled_y0 - 2048.f,
  315. z0 * radius);
  316. }
  317. else if (i == num_stacks - 1)
  318. {
  319. *vertices++ = LLVector3(0.f, scaled_y0 - 2048.f, 0.f);
  320. }
  321. else
  322. {
  323. *vertices++ = LLVector3(x0 * radius, scaled_y0, z0 * radius);
  324. }
  325. // Denerate planar UV coordinates. Note: x and z are transposed in
  326. // order for things to animate correctly in the global coordinate
  327. // system where +x is east and +y is north
  328. *texcoords++ = LLVector2((1.f - z0) * 0.5f, (1.f - x0) * 0.5f);
  329. }
  330. }
  331. // Build triangle strip...
  332. *indices++ = 0;
  333. S32 k = 0;
  334. for (U32 i = 1; i <= end_stack - begin_stack; ++i)
  335. {
  336. *indices++ = i * num_slices + k;
  337. k = (k + 1) % num_slices;
  338. for (U32 j = 0; j < num_slices; ++j)
  339. {
  340. *indices++ = (i - 1) * num_slices + k;
  341. *indices++ = i * num_slices + k;
  342. k = (k + 1) % num_slices;
  343. }
  344. if (--k < 0)
  345. {
  346. k = num_slices - 1;
  347. }
  348. *indices++ = i * num_slices + k;
  349. }
  350. }
  351. void LLVOWLSky::updateStarColors()
  352. {
  353. std::vector<LLColor4>::iterator v_c = mStarColors.begin();
  354. std::vector<F32>::iterator v_i = mStarIntensities.begin();
  355. std::vector<LLVector3>::iterator v_p = mStarVertices.begin();
  356. constexpr F32 var = 0.15f;
  357. constexpr F32 min = 0.5f; // 0.75f;
  358. static S32 swap = 0;
  359. if (++swap % 2 == 1)
  360. {
  361. for (U32 x = 0; x < STAR_NUM_VERTS; ++x)
  362. {
  363. LLVector3 tostar = *v_p++;
  364. tostar.normalize();
  365. F32 intensity = *v_i++;
  366. F32 alpha = v_c->mV[VALPHA] + (ll_frand() - 0.5f) * var * intensity;
  367. if (alpha < min * intensity)
  368. {
  369. alpha = min * intensity;
  370. }
  371. if (alpha > intensity)
  372. {
  373. alpha = intensity;
  374. }
  375. alpha = llclamp(alpha, 0.f, 1.f);
  376. (v_c++)->mV[VALPHA] = alpha;
  377. }
  378. }
  379. }
  380. bool LLVOWLSky::updateStarGeometry(LLDrawable* drawable)
  381. {
  382. if (mStarsVerts.isNull())
  383. {
  384. mStarsVerts =
  385. new LLVertexBuffer(LLDrawPoolWLSky::STAR_VERTEX_DATA_MASK);
  386. #if LL_DEBUG_VB_ALLOC
  387. mStarsVerts->setOwner("LLVOWLSky stars");
  388. #endif
  389. if (!mStarsVerts->allocateBuffer(STAR_NUM_VERTS * 6, 0))
  390. {
  391. llwarns << "Failure to resize a vertex buffer with "
  392. << STAR_NUM_VERTS * 6 << " vertices" << llendl;
  393. return false;
  394. }
  395. }
  396. LLStrider<LLVector3> verticesp;
  397. LLStrider<LLColor4U> colorsp;
  398. LLStrider<LLVector2> texcoordsp;
  399. bool success = mStarsVerts->getVertexStrider(verticesp) &&
  400. mStarsVerts->getColorStrider(colorsp) &&
  401. mStarsVerts->getTexCoord0Strider(texcoordsp);
  402. if (!success)
  403. {
  404. llwarns << "Failed updating star geometry." << llendl;
  405. return false;
  406. }
  407. // *TODO: fix LLStrider with a real prefix increment operator so it can be
  408. // used as a model of OutputIterator. -Brad
  409. // std::copy(mStarVertices.begin(), mStarVertices.end(), verticesp);
  410. if (mStarVertices.size() < STAR_NUM_VERTS)
  411. {
  412. llwarns_once << "Star reference geometry insufficient." << llendl;
  413. return false;
  414. }
  415. // Texture coordinates:
  416. static const LLVector2 TEX00 = LLVector2(0.f, 0.f);
  417. static const LLVector2 TEX01 = LLVector2(0.f, 1.f);
  418. static const LLVector2 TEX10 = LLVector2(1.f, 0.f);
  419. static const LLVector2 TEX11 = LLVector2(1.f, 1.f);
  420. LLVector3 at0, at, left, up;
  421. LLColor4U col4u;
  422. for (U32 vtx = 0; vtx < STAR_NUM_VERTS; ++vtx)
  423. {
  424. at0 = at = mStarVertices[vtx];
  425. at.normalize();
  426. left = at % LLVector3::z_axis;
  427. up = at % left;
  428. F32 sc;
  429. sc = 16.f + ll_frand() * 20.f;
  430. left *= sc;
  431. up *= sc;
  432. *verticesp++ = at0;
  433. *verticesp++ = at0 + up;
  434. *verticesp++ = at0 + left + up;
  435. *verticesp++ = at0;
  436. *verticesp++ = at0 + left + up;
  437. *verticesp++ = at0 + left;
  438. *texcoordsp++ = TEX10;
  439. *texcoordsp++ = TEX11;
  440. *texcoordsp++ = TEX01;
  441. *texcoordsp++ = TEX10;
  442. *texcoordsp++ = TEX01;
  443. *texcoordsp++ = TEX00;
  444. col4u = LLColor4U(mStarColors[vtx]);
  445. *colorsp++ = col4u;
  446. *colorsp++ = col4u;
  447. *colorsp++ = col4u;
  448. *colorsp++ = col4u;
  449. *colorsp++ = col4u;
  450. *colorsp++ = col4u;
  451. }
  452. mStarsVerts->unmapBuffer();
  453. return true;
  454. }