llvoclouds.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * @file llvoclouds.cpp
  3. * @brief Implementation of LLVOClouds class which is a derivation fo LLViewerObject
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-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 "llvoclouds.h"
  34. #include "imageids.h"
  35. #include "llfasttimer.h"
  36. #include "llprimitive.h"
  37. #include "llagent.h" // to get camera position
  38. #include "lldrawable.h"
  39. #include "lldrawpoolalpha.h"
  40. #include "llenvironment.h"
  41. #include "llface.h"
  42. #include "llpipeline.h"
  43. #include "llsky.h"
  44. #include "llspatialpartition.h"
  45. #include "llviewercamera.h"
  46. #include "llviewercontrol.h"
  47. #include "llviewerobjectlist.h"
  48. #include "llviewerregion.h"
  49. #include "llviewertexturelist.h"
  50. #include "llvosky.h"
  51. #include "llworld.h"
  52. LLUUID gCloudTextureID = IMG_CLOUD_POOF;
  53. ///////////////////////////////////////////////////////////////////////////////
  54. // LLVOClouds class
  55. ///////////////////////////////////////////////////////////////////////////////
  56. LLVOClouds::LLVOClouds(const LLUUID& id, LLViewerRegion* regionp)
  57. : LLAlphaObject(id, LL_VO_CLOUDS, regionp)
  58. {
  59. mCloudGroupp = NULL;
  60. mCanSelect = false;
  61. setNumTEs(1);
  62. LLViewerTexture* image;
  63. if (gCloudTextureID != IMG_CLOUD_POOF ||
  64. LLViewerFetchedTexture::sDefaultCloudsImagep.isNull())
  65. {
  66. image =
  67. LLViewerTextureManager::getFetchedTexture(gCloudTextureID,
  68. FTT_DEFAULT, true,
  69. LLGLTexture::BOOST_CLOUDS);
  70. #if !LL_IMPLICIT_SETNODELETE
  71. image->setNoDelete();
  72. #endif
  73. }
  74. else
  75. {
  76. image = LLViewerFetchedTexture::sDefaultCloudsImagep.get();
  77. }
  78. setTEImage(0, image);
  79. }
  80. void LLVOClouds::idleUpdate(F64 time)
  81. {
  82. if (mDrawable && gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_CLOUDS))
  83. {
  84. // Set rebuild flag (so that the renderer will rebuild the primitive)
  85. gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_VOLUME);
  86. // Compute and cache clouds color for this frame.
  87. const LLSettingsSky::ptr_t& skyp = gEnvironment.getCurrentSky();
  88. if (!skyp) return; // Paranoia
  89. #if 1 // With PBR, LL published totally hacked EE midday settings (and they
  90. // changed them at least half a dozen of times already, as I am writing
  91. // this) to try and fix the ugly blue hue on shinies and other lighting
  92. // issues. One of the hack they use is to set the ambient light to
  93. // black, meaning both getTotalAmbient() and getLightDiffuse() return a
  94. // black color, leading to black clouds !... So, here is my workaround
  95. // (not as good as the original formula for clouds color, thus why I
  96. // use a threshold for the workaround). HB
  97. static LLCachedControl<F32> min_ambient(gSavedSettings,
  98. "CloudsMinAmbientThreshold");
  99. // Since the diffuse color is less bright than the total ambient color,
  100. // let's also adjust it to be brighter to try and match what we get
  101. // with non ruined/hacked EE settings... HB
  102. static LLCachedControl<F32> adjustment(gSavedSettings,
  103. "CloudsDiffuseLightAdjustment");
  104. LLColor3 total_ambient(skyp->getTotalAmbient());
  105. if (total_ambient.brightness() < (F32)min_ambient)
  106. {
  107. bool sun_up = gPipeline.mIsSunUp;
  108. total_ambient = LLColor3(sun_up ? gPipeline.mSunLightColor
  109. : gPipeline.mMoonLightColor);
  110. const LLColor3& diffuse =
  111. sun_up ? skyp->getSunDiffuse() : skyp->getMoonDiffuse();
  112. mCloudsColor = diffuse + total_ambient;
  113. mCloudsColor.adjust(adjustment);
  114. }
  115. else // If the EE settings are not ruined, keep the old formula. HB
  116. #endif
  117. {
  118. mCloudsColor = skyp->getLightDiffuse() +
  119. LLColor3(skyp->getTotalAmbient());
  120. }
  121. }
  122. }
  123. void LLVOClouds::setPixelAreaAndAngle()
  124. {
  125. mAppAngle = 50;
  126. mPixelArea = 1500 * 100;
  127. }
  128. void LLVOClouds::updateTextures()
  129. {
  130. getTEImage(0)->addTextureStats(mPixelArea);
  131. }
  132. LLDrawable* LLVOClouds::createDrawable()
  133. {
  134. gPipeline.allocDrawable(this);
  135. mDrawable->setLit(false);
  136. mDrawable->setRenderType(LLPipeline::RENDER_TYPE_CLOUDS);
  137. return mDrawable;
  138. }
  139. bool LLVOClouds::updateGeometry(LLDrawable* drawable)
  140. {
  141. LL_FAST_TIMER(FTM_UPDATE_CLOUDS);
  142. S32 num_parts = mCloudGroupp->getNumPuffs();
  143. LLSpatialGroup* group = drawable->getSpatialGroup();
  144. if (!group && num_parts)
  145. {
  146. drawable->movePartition();
  147. group = drawable->getSpatialGroup();
  148. }
  149. if (group && group->isVisible())
  150. {
  151. dirtySpatialGroup();
  152. }
  153. if (!num_parts)
  154. {
  155. if (group && drawable->getNumFaces())
  156. {
  157. group->setState(LLSpatialGroup::GEOM_DIRTY);
  158. }
  159. drawable->setNumFaces(0, NULL, getTEImage(0));
  160. return true;
  161. }
  162. if (!gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_CLOUDS))
  163. {
  164. return true;
  165. }
  166. if (num_parts > drawable->getNumFaces())
  167. {
  168. drawable->setNumFacesFast(num_parts + num_parts / 4, NULL,
  169. getTEImage(0));
  170. }
  171. mDepth = (getPositionAgent() - gViewerCamera.getOrigin()) *
  172. gViewerCamera.getAtAxis();
  173. LLFace* facep;
  174. S32 face_indx = 0;
  175. for ( ; face_indx < num_parts; face_indx++)
  176. {
  177. facep = drawable->getFace(face_indx);
  178. if (!facep)
  179. {
  180. continue;
  181. }
  182. facep->setTEOffset(face_indx);
  183. facep->setSize(4, 6);
  184. facep->setViewerObject(this);
  185. const LLCloudPuff& puff = mCloudGroupp->getPuff(face_indx);
  186. facep->mCenterLocal =
  187. gAgent.getPosAgentFromGlobal(puff.getPositionGlobal());
  188. facep->setFaceColor(LLColor4(mCloudsColor, puff.getAlpha()));
  189. facep->setDiffuseMap(getTEImage(0));
  190. }
  191. for (S32 count = drawable->getNumFaces(); face_indx < count; ++face_indx)
  192. {
  193. facep = drawable->getFace(face_indx);
  194. if (facep)
  195. {
  196. facep->setTEOffset(face_indx);
  197. facep->setSize(0, 0);
  198. }
  199. }
  200. drawable->movePartition();
  201. return true;
  202. }
  203. F32 LLVOClouds::getPartSize(S32 idx)
  204. {
  205. return (CLOUD_PUFF_HEIGHT + CLOUD_PUFF_WIDTH) * 0.5f;
  206. }
  207. void LLVOClouds::getGeometry(S32 idx,
  208. LLStrider<LLVector4a>& verticesp,
  209. LLStrider<LLVector3>& normalsp,
  210. LLStrider<LLVector2>& texcoordsp,
  211. LLStrider<LLColor4U>& colorsp,
  212. LLStrider<LLColor4U>& emissivep,
  213. LLStrider<U16>& indicesp)
  214. {
  215. if (idx >= mCloudGroupp->getNumPuffs())
  216. {
  217. return;
  218. }
  219. LLDrawable* drawable = mDrawable;
  220. LLFace* facep = drawable->getFace(idx);
  221. if (!facep || !facep->hasGeometry())
  222. {
  223. return;
  224. }
  225. const LLCloudPuff& puff = mCloudGroupp->getPuff(idx);
  226. LLColor4 float_color(mCloudsColor, puff.getAlpha());
  227. facep->setFaceColor(float_color);
  228. LLVector4a part_pos_agent;
  229. part_pos_agent.load3(facep->mCenterLocal.mV);
  230. LLVector4a at;
  231. at.load3(gViewerCamera.getAtAxis().mV);
  232. LLVector4a up(0.f, 0.f, 1.f);
  233. LLVector4a right;
  234. right.setCross3(at, up);
  235. right.normalize3fast();
  236. up.setCross3(right, at);
  237. up.normalize3fast();
  238. right.mul(0.5f * CLOUD_PUFF_WIDTH);
  239. up.mul(0.5f * CLOUD_PUFF_HEIGHT);
  240. LLVector3 normal(0.f, 0.f, -1.f);
  241. // *HACK: the verticesp->mV[3] = 0.f here are to set the texture index to 0
  242. // (particles do not use texture batching, maybe they should) this works
  243. // because there is actually a 4th float stored after the vertex position
  244. // which is used as a texture index.
  245. LLVector4a ppapu;
  246. LLVector4a ppamu;
  247. ppapu.setAdd(part_pos_agent, up);
  248. ppamu.setSub(part_pos_agent, up);
  249. verticesp->setSub(ppapu, right);
  250. (*verticesp++).getF32ptr()[3] = 0.f;
  251. verticesp->setSub(ppamu, right);
  252. (*verticesp++).getF32ptr()[3] = 0.f;
  253. verticesp->setAdd(ppapu, right);
  254. (*verticesp++).getF32ptr()[3] = 0.f;
  255. verticesp->setAdd(ppamu, right);
  256. (*verticesp++).getF32ptr()[3] = 0.f;
  257. LLColor4U color;
  258. color.set(float_color);
  259. *colorsp++ = color;
  260. *colorsp++ = color;
  261. *colorsp++ = color;
  262. *colorsp++ = color;
  263. *normalsp++ = normal;
  264. *normalsp++ = normal;
  265. *normalsp++ = normal;
  266. *normalsp++ = normal;
  267. }
  268. U32 LLVOClouds::getPartitionType() const
  269. {
  270. return LLViewerRegion::PARTITION_CLOUD;
  271. }
  272. // virtual
  273. void LLVOClouds::updateDrawable(bool force_damped)
  274. {
  275. // Force an immediate rebuild on any update
  276. if (mDrawable.notNull())
  277. {
  278. mDrawable->updateXform(true);
  279. gPipeline.markRebuild(mDrawable);
  280. }
  281. clearChanged(SHIFTED);
  282. }
  283. ///////////////////////////////////////////////////////////////////////////////
  284. // LLCloudPartition class (declared in llspatialpartition.h)
  285. ///////////////////////////////////////////////////////////////////////////////
  286. LLCloudPartition::LLCloudPartition(LLViewerRegion* regionp)
  287. : LLParticlePartition(regionp)
  288. {
  289. mDrawableType = LLPipeline::RENDER_TYPE_CLOUDS;
  290. mPartitionType = LLViewerRegion::PARTITION_CLOUD;
  291. }