llsky.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * @file llsky.cpp
  3. * @brief IndraWorld sky class
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-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. // Ideas:
  33. // -haze should be controlled by global query from sims
  34. // -need secondary optical effects on sun (flare)
  35. // -stars should be brought down from sims
  36. // -star intensity should be driven by global ambient level from sims,
  37. // so that eclipses, etc can be easily done.
  38. //
  39. #include "llviewerprecompiledheaders.h"
  40. #include "llsky.h"
  41. #include "llcubemap.h"
  42. #include "llrenderutils.h"
  43. #include "lldrawpool.h"
  44. #include "llpipeline.h"
  45. #include "llviewercamera.h"
  46. #include "llviewercontrol.h"
  47. #include "llviewerobjectlist.h"
  48. #include "llvoavatarself.h"
  49. #include "llvowlsky.h"
  50. LLSky gSky;
  51. LLSky::LLSky()
  52. : mLightingGeneration(0),
  53. mUpdatedThisFrame(true),
  54. mOverrideSimSunPosition(false)
  55. {
  56. }
  57. void LLSky::cleanup()
  58. {
  59. mVOSkyp = NULL;
  60. mVOWLSkyp = NULL;
  61. }
  62. void LLSky::destroyGL()
  63. {
  64. if (mVOSkyp.notNull() && mVOSkyp->getCubeMap())
  65. {
  66. mVOSkyp->cleanupGL();
  67. }
  68. if (mVOWLSkyp.notNull())
  69. {
  70. mVOWLSkyp->cleanupGL();
  71. }
  72. }
  73. void LLSky::restoreGL()
  74. {
  75. if (mVOSkyp.notNull())
  76. {
  77. mVOSkyp->restoreGL();
  78. }
  79. if (mVOWLSkyp.notNull())
  80. {
  81. mVOWLSkyp->restoreGL();
  82. }
  83. }
  84. void LLSky::resetVertexBuffers()
  85. {
  86. if (gSky.mVOWLSkyp.notNull())
  87. {
  88. gSky.mVOWLSkyp->resetVertexBuffers();
  89. gPipeline.resetVertexBuffers(gSky.mVOWLSkyp->mDrawable);
  90. gPipeline.markRebuild(gSky.mVOWLSkyp->mDrawable);
  91. }
  92. if (gSky.mVOSkyp.notNull())
  93. {
  94. gPipeline.resetVertexBuffers(gSky.mVOSkyp->mDrawable);
  95. gPipeline.markRebuild(gSky.mVOSkyp->mDrawable);
  96. }
  97. }
  98. void LLSky::init()
  99. {
  100. mVOWLSkyp =
  101. (LLVOWLSky*)gObjectList.createObjectViewer(LLViewerObject::LL_VO_WL_SKY,
  102. NULL);
  103. gPipeline.createObject(mVOWLSkyp.get());
  104. mVOSkyp =
  105. (LLVOSky*)gObjectList.createObjectViewer(LLViewerObject::LL_VO_SKY,
  106. NULL);
  107. mVOSkyp->initSunDirection(LLVector3::x_axis);
  108. gPipeline.createObject((LLViewerObject*)mVOSkyp);
  109. setSunDirection(LLVector3::x_axis, LLVector3::zero);
  110. mUpdatedThisFrame = true;
  111. }
  112. void LLSky::setCloudDensityAtAgent(F32 cloud_density)
  113. {
  114. if (mVOSkyp.notNull())
  115. {
  116. mVOSkyp->setCloudDensity(cloud_density);
  117. }
  118. }
  119. void LLSky::setWind(const LLVector3& average_wind)
  120. {
  121. if (mVOSkyp.notNull())
  122. {
  123. mVOSkyp->setWind(average_wind);
  124. }
  125. }
  126. void LLSky::updateFog(F32 distance)
  127. {
  128. if (mVOSkyp.notNull())
  129. {
  130. mVOSkyp->updateFog(distance);
  131. }
  132. }
  133. void LLSky::updateSky()
  134. {
  135. if (mVOSkyp.notNull() &&
  136. gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  137. {
  138. mVOSkyp->updateSky();
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. // Windlight renderer specific methods
  143. //-----------------------------------------------------------------------------
  144. void LLSky::setOverrideSun(bool override_sun)
  145. {
  146. if (!mOverrideSimSunPosition && override_sun)
  147. {
  148. mLastSunDirection = getSunDirection();
  149. }
  150. else if (mOverrideSimSunPosition && !override_sun)
  151. {
  152. setSunDirection(mLastSunDirection, LLVector3::zero);
  153. }
  154. mOverrideSimSunPosition = override_sun;
  155. }
  156. void LLSky::setSunDirection(const LLVector3& sun_direction,
  157. const LLVector3& sun_ang_velocity)
  158. {
  159. if (mVOSkyp.notNull())
  160. {
  161. mVOSkyp->setSunDirection(sun_direction, sun_ang_velocity);
  162. }
  163. }
  164. LLVector3 LLSky::getSunDirection() const
  165. {
  166. return mVOSkyp.notNull() ? mVOSkyp->getToSun() : LLVector3::z_axis;
  167. }
  168. LLVector3 LLSky::getMoonDirection() const
  169. {
  170. return mVOSkyp.notNull() ? mVOSkyp->getToMoon() : LLVector3::z_axis;
  171. }
  172. bool LLSky::sunUp() const
  173. {
  174. return getSunDirection().mV[2] >= NIGHTTIME_ELEVATION_COS;
  175. }
  176. void LLSky::propagateHeavenlyBodies(F32 dt)
  177. {
  178. if (!mOverrideSimSunPosition)
  179. {
  180. LLVector3 curr_dir = getSunDirection();
  181. LLVector3 diff = mSunTargDir - curr_dir;
  182. F32 dist = diff.normalize();
  183. if (dist > 0.f)
  184. {
  185. F32 step = llmin(dist, 0.00005f);
  186. diff *= step;
  187. curr_dir += diff;
  188. curr_dir.normalize();
  189. if (mVOSkyp.notNull())
  190. {
  191. mVOSkyp->setSunDirection(curr_dir, LLVector3::zero);
  192. }
  193. }
  194. }
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Extended environment specific methods
  198. //-----------------------------------------------------------------------------
  199. void LLSky::setSunScale(F32 sun_scale)
  200. {
  201. if (mVOSkyp.notNull())
  202. {
  203. mVOSkyp->setSunScale(sun_scale);
  204. }
  205. }
  206. void LLSky::setMoonScale(F32 moon_scale)
  207. {
  208. if (mVOSkyp.notNull())
  209. {
  210. mVOSkyp->setMoonScale(moon_scale);
  211. }
  212. }
  213. void LLSky::setSunTextures(const LLUUID& sun_tex1, const LLUUID& sun_tex2)
  214. {
  215. if (mVOSkyp.notNull())
  216. {
  217. mVOSkyp->setSunTextures(sun_tex1, sun_tex2);
  218. }
  219. }
  220. void LLSky::setMoonTextures(const LLUUID& moon_tex1, const LLUUID& moon_tex2)
  221. {
  222. if (mVOSkyp.notNull())
  223. {
  224. mVOSkyp->setMoonTextures(moon_tex1, moon_tex2);
  225. }
  226. }
  227. void LLSky::setCloudNoiseTextures(const LLUUID& noise_tex1,
  228. const LLUUID& noise_tex2)
  229. {
  230. if (mVOSkyp.notNull())
  231. {
  232. mVOSkyp->setCloudNoiseTextures(noise_tex1, noise_tex2);
  233. }
  234. }
  235. void LLSky::setBloomTextures(const LLUUID& bloom_tex1,
  236. const LLUUID& bloom_tex2)
  237. {
  238. if (mVOSkyp.notNull())
  239. {
  240. mVOSkyp->setBloomTextures(bloom_tex1, bloom_tex2);
  241. }
  242. }
  243. void LLSky::setSunAndMoonDirectionsCFR(const LLVector3& sun_direction,
  244. const LLVector3& moon_direction)
  245. {
  246. if (mVOSkyp.notNull())
  247. {
  248. mVOSkyp->setSunAndMoonDirectionsCFR(sun_direction, moon_direction);
  249. }
  250. }
  251. void LLSky::setSunDirectionCFR(const LLVector3& sun_direction)
  252. {
  253. if (mVOSkyp.notNull())
  254. {
  255. mVOSkyp->setSunDirectionCFR(sun_direction);
  256. }
  257. }
  258. void LLSky::setMoonDirectionCFR(const LLVector3& moon_direction)
  259. {
  260. if (mVOSkyp.notNull())
  261. {
  262. mVOSkyp->setMoonDirectionCFR(moon_direction);
  263. }
  264. }
  265. //-----------------------------------------------------------------------------
  266. static void render_sun_moon_beacons(const LLVector3& pos_agent,
  267. const LLVector3& direction,
  268. const LLColor4& color)
  269. {
  270. LLGLSUIDefault gls_ui;
  271. gUIProgram.bind();
  272. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  273. LLVector3 pos_end;
  274. for (S32 i = 0; i < 3; ++i)
  275. {
  276. pos_end.mV[i] = pos_agent.mV[i] + (50 * direction.mV[i]);
  277. }
  278. gGL.lineWidth(LLPipeline::DebugBeaconLineWidth);
  279. gGL.begin(LLRender::LINES);
  280. gGL.color4fv(color.mV);
  281. gl_draw_3d_cross_lines(pos_agent, 0.5f, 0.5f, 0.5f);
  282. gl_draw_3d_cross_lines(pos_end, 2.f, 2.f, 2.f);
  283. gGL.vertex3fv(pos_agent.mV);
  284. gGL.vertex3fv(pos_end.mV);
  285. gGL.end(true);
  286. gGL.lineWidth(1.f);
  287. gUIProgram.unbind();
  288. stop_glerror();
  289. }
  290. void LLSky::addSunMoonBeacons()
  291. {
  292. if (!isAgentAvatarValid() || mVOSkyp.isNull()) return;
  293. static LLCachedControl<bool> show_sun(gSavedSettings, "sunbeacon");
  294. if (show_sun)
  295. {
  296. static LLColor4 sun_beacon_color(1.f, 0.5f, 0.f, 0.5f);
  297. render_sun_moon_beacons(gAgentAvatarp->getPositionAgent(),
  298. mVOSkyp->getSun().getDirection(),
  299. sun_beacon_color);
  300. }
  301. static LLCachedControl<bool> show_moon(gSavedSettings, "moonbeacon");
  302. if (show_moon)
  303. {
  304. static LLColor4 moon_beacon_color(1.f, 0.f, 0.8f, 0.5f);
  305. render_sun_moon_beacons(gAgentAvatarp->getPositionAgent(),
  306. mVOSkyp->getMoon().getDirection(),
  307. moon_beacon_color);
  308. }
  309. }