llwlskyparammgr.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /**
  2. * @file llwlskyparammgr.h
  3. * @brief Implementation for the LLWLSkyParamMgr class.
  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. #ifndef LL_WLSKYPARAMMGR_H
  33. #define LL_WLSKYPARAMMGR_H
  34. #include <map>
  35. #include "llcolor4.h"
  36. #include "llstringtable.h"
  37. #include "llvector4.h"
  38. class LLMessageSystem;
  39. // A class representing a set of parameter values for the WindLight sky
  40. class LLWLParamSet
  41. {
  42. friend class LLWLSkyParamMgr;
  43. public:
  44. LLWLParamSet();
  45. // Sets the total LLSD
  46. void setAll(const LLSD& val);
  47. // Gets the total LLSD
  48. LL_INLINE const LLSD& getAll() const { return mParamValues; }
  49. // Sets a float parameter.
  50. // - param param_name The name of the parameter to set.
  51. // - param x The float value to set.
  52. void set(const std::string& param_name, F32 x);
  53. // Sets a float2 parameter.
  54. // - param param_name The name of the parameter to set.
  55. // - param x The x component's value to set.
  56. // - param y The y component's value to set.
  57. void set(const std::string& param_name, F32 x, F32 y);
  58. // Sets a float3 parameter.
  59. // - param param_name The name of the parameter to set.
  60. // - param x The x component's value to set.
  61. // - param y The y component's value to set.
  62. // - param z The z component's value to set.
  63. void set(const std::string& param_name, F32 x, F32 y, F32 z);
  64. // Sets a float4 parameter.
  65. // - param param_name The name of the parameter to set.
  66. // - param x The x component's value to set.
  67. // - param y The y component's value to set.
  68. // - param z The z component's value to set.
  69. // - param w The w component's value to set.
  70. void set(const std::string& param_name, F32 x, F32 y, F32 z, F32 w);
  71. // Sets a float4 parameter.
  72. // - param param_name The name of the parameter to set.
  73. // - param val An array of the 4 float values to set the parameter
  74. // to.
  75. void set(const std::string& param_name, const F32* val);
  76. // Sets a float4 parameter.
  77. // - param param_name The name of the parameter to set.
  78. // - param val A struct of the 4 float values to set the parameter
  79. // to.
  80. void set(const std::string& param_name, const LLVector4& val);
  81. // Sets a float4 parameter.
  82. // - param param_name The name of the parameter to set.
  83. // - param val A struct of the 4 float values to set the parameter
  84. // to.
  85. void set(const std::string& param_name, const LLColor4& val);
  86. // Gets a float4 parameter.
  87. // - param param_name The name of the parameter to set.
  88. // - param error A flag to set if it's not the proper return type
  89. LLVector4 getVector(const std::string& param_name, bool& error);
  90. // Gets an integer parameter
  91. // - param param_name The name of the parameter to set.
  92. // - param error A flag to set if it's not the proper return type
  93. F32 getFloat(const std::string& param_name, bool& error);
  94. // Sets the star's brightness
  95. void setStarBrightness(F32 val);
  96. // Gets the star brightness value
  97. LL_INLINE F32 getStarBrightness() { return mStartBrightness; }
  98. void setSunAngle(F32 val);
  99. LL_INLINE F32 getSunAngle() { return mSunAngle; }
  100. void setEastAngle(F32 val);
  101. LL_INLINE F32 getEastAngle() { return mEastAngle; }
  102. // Sets the cloud scroll x enable value
  103. void setEnableCloudScrollX(bool val);
  104. // Gets the scroll x enable value;
  105. LL_INLINE bool getEnableCloudScrollX() { return mCloudScrollEnableX; }
  106. // Sets the cloud scroll y enable value
  107. void setEnableCloudScrollY(bool val);
  108. // Gets the scroll enable y value
  109. LL_INLINE bool getEnableCloudScrollY() { return mCloudScrollEnableY; }
  110. // Sets the scroll x value
  111. void setCloudScrollX(F32 val);
  112. // Gets the scroll x enable value
  113. LL_INLINE F32 getCloudScrollX() { return mCloudScrollRateX; }
  114. // Sets the scroll y enable value
  115. void setCloudScrollY(F32 val);
  116. // Gets the scroll enable y value
  117. LL_INLINE F32 getCloudScrollY() { return mCloudScrollRateY; }
  118. // Interpolate two parameter sets
  119. // - param src The parameter set to start with
  120. // - param dest The parameter set to end with
  121. // - param weight The amount to interpolate
  122. void mix(LLWLParamSet& src, LLWLParamSet& dest, F32 weight);
  123. void updateCloudScrolling();
  124. private:
  125. void updateHashedNames();
  126. public:
  127. std::string mName;
  128. private:
  129. LLSD mParamValues;
  130. typedef std::vector<LLStaticHashedString> hash_vector_t;
  131. hash_vector_t mParamHashedNames;
  132. F32 mSunAngle;
  133. F32 mEastAngle;
  134. F32 mStartBrightness;
  135. F32 mCloudScrollXOffset;
  136. F32 mCloudScrollYOffset;
  137. F32 mCloudScrollRateX;
  138. F32 mCloudScrollRateY;
  139. bool mCloudScrollEnableX;
  140. bool mCloudScrollEnableY;
  141. };
  142. // Color control
  143. struct WLColorControl
  144. {
  145. F32 r, g, b, i; // The values
  146. std::string mName; // Name to use to dereference params
  147. std::string mSliderName; // Name of the slider in menu
  148. bool hasSliderName; // Only set slider name for true color types
  149. // Flag for if it is the Sun or ambient color controller
  150. bool isSunOrAmbientColor;
  151. // Flag for if it is the Blue Horizon or Density color controller
  152. bool isBlueHorizonOrDensity;
  153. LL_INLINE WLColorControl(F32 red, F32 green, F32 blue, F32 intensity,
  154. const std::string& name,
  155. const std::string& sld_name = LLStringUtil::null)
  156. : r(red),
  157. g(green),
  158. b(blue),
  159. i(intensity),
  160. mName(name),
  161. mSliderName(sld_name)
  162. {
  163. hasSliderName = !sld_name.empty();
  164. isSunOrAmbientColor = sld_name == "WLSunlight" ||
  165. sld_name == "WLAmbient";
  166. isBlueHorizonOrDensity = sld_name == "WLBlueHorizon" ||
  167. sld_name == "WLBlueDensity";
  168. }
  169. LL_INLINE WLColorControl& operator=(const LLVector4& val)
  170. {
  171. r = val.mV[0];
  172. g = val.mV[1];
  173. b = val.mV[2];
  174. i = val.mV[3];
  175. return *this;
  176. }
  177. LL_INLINE operator LLVector4() const
  178. {
  179. return LLVector4(r, g, b, i);
  180. }
  181. LL_INLINE operator LLVector3() const
  182. {
  183. return LLVector3(r, g, b);
  184. }
  185. LL_INLINE void update(LLWLParamSet& params) const
  186. {
  187. params.set(mName, r, g, b, i);
  188. }
  189. };
  190. // Float slider control
  191. struct WLFloatControl
  192. {
  193. F32 x;
  194. std::string mName;
  195. F32 mult;
  196. LL_INLINE WLFloatControl(F32 val, const std::string& n, F32 m = 1.f)
  197. : x(val),
  198. mName(n),
  199. mult(m)
  200. {
  201. }
  202. LL_INLINE WLFloatControl & operator=(const LLVector4& val)
  203. {
  204. x = val.mV[0];
  205. return *this;
  206. }
  207. LL_INLINE operator F32 () const
  208. {
  209. return x;
  210. }
  211. LL_INLINE void update(LLWLParamSet & params) const
  212. {
  213. params.set(mName, x);
  214. }
  215. };
  216. // LLWLAnimator class
  217. class LLWLAnimator
  218. {
  219. public:
  220. LLWLAnimator();
  221. // Update the parameters
  222. void update(LLWLParamSet& curParams);
  223. // Returns time of day which is either the region time (based on sSunPhase)
  224. // or the Windlight time (based on mDayOffset and mDayLenth), as a [0.0-1.0]
  225. // double float.
  226. F64 getDayTime();
  227. // Sets a long float 0.0 - 1.0 saying what time of day it is
  228. void setDayTime(F64 day_time);
  229. // Sets day rate and offset
  230. void setDayRateAndOffset(S32 day_length, S32 day_offset);
  231. // Sets an animation track
  232. LL_INLINE void setTrack(const std::map<F32, std::string>& track,
  233. S32 day_length, F64 day_time = 0.0,
  234. bool run = true)
  235. {
  236. mTimeTrack = track;
  237. mDayLenth = day_length;
  238. setDayTime(day_time);
  239. mIsRunning = run;
  240. }
  241. // Returns the estate/region time (AKA "Linden time", based on sSunPhase),
  242. // as a [0.0-1.0] double float.
  243. static F64 getEstateTime();
  244. public:
  245. F64 mDayTime;
  246. S32 mDayOffset;
  247. S32 mDayLenth;
  248. // Track to play
  249. typedef std::map<F32, std::string> time_track_t;
  250. time_track_t mTimeTrack;
  251. typedef std::map<F32, std::string>::iterator time_track_it_t;
  252. time_track_it_t mFirstIt;
  253. time_track_it_t mSecondIt;
  254. bool mIsRunning;
  255. static F32 sSunPhase;
  256. };
  257. // LLWLDayCycle class
  258. class LLWLDayCycle
  259. {
  260. protected:
  261. LOG_CLASS(LLWLDayCycle);
  262. public:
  263. LLWLDayCycle();
  264. // Finds available day cycle presets by name
  265. static void findPresets();
  266. // Removes a day cycle preset (by name or file name).
  267. static void removeDayCycle(const std::string& name);
  268. // Loads a day cycle (by name or file name). Returns true on success.
  269. // When 'alert' is true, pop ups alerts when a sky track is missing.
  270. bool loadDayCycle(const std::string& name, bool alert = true);
  271. // Saves a day cycle (by name or file name).
  272. void saveDayCycle(const std::string& name);
  273. // Clears keys
  274. void clearKeys();
  275. // Adds a new key frame to the day cycle. Returns true if successful. No
  276. // negative time allowed.
  277. bool addKey(F32 new_time, const std::string& param_name);
  278. // Adjusts a key placement in the day cycle. Returns true if successful.
  279. bool changeKeyTime(F32 old_time, F32 new_time);
  280. // Adjusts a key parameter used. Returns true if successful.
  281. bool changeKeyParam(F32 time, const std::string& param_name);
  282. // Remove a key from the day cycle. Returns true if successful.
  283. bool removeKey(F32 time);
  284. // Gets the first key time for a parameter. Returns false if not there.
  285. bool getKey(const std::string& name, F32& key);
  286. // Gets the param set at a given time. Returns true if it found one.
  287. bool getKeyedParam(F32 time, LLWLParamSet& param);
  288. // Gets the name. Returns true if it found one.
  289. bool getKeyedParamName(F32 time, std::string& name);
  290. // These methods return (respectively) the viewer installation directory
  291. // and the user settings path for Windlight presets files. 'subdir' may
  292. // either be an empty string (in which case the base Windlight presets
  293. // directory is returned), or contain "days", "skies" or "water".
  294. static std::string getSysDir(const std::string& subdir);
  295. static std::string getUserDir(const std::string& subdir);
  296. // URI-escapes 'name', with or without dash ('-') characters escaping, and
  297. // adds ".xml" to the name if not already there.
  298. static std::string makeFileName(const std::string& name,
  299. bool escape_dash = true);
  300. // Used to find a matching preset file for 'name', either in the base_path,
  301. // the user settings, or the viewer installation directory, in this order
  302. // of priority. 'name' is turned into an XML file name with URI escaping
  303. // and possibly with dash escaping (an attempt is first made to find a file
  304. // with dash escaping, then another attempt is made without it).
  305. // 'subdir' shall contain "days", "skies" or "water", to specify which
  306. // setting is sought for. 'base_path' may be empty (in which case the
  307. // search is done only in the user settings and viewer installation
  308. // directories). Returns true on success to find the preset file, with
  309. // 'filename' and 'path' set accordingly to the findings. On failure, false
  310. // is returned, with a properly escaped file name in 'filename', and 'path'
  311. // set to 'base_path'. 'path' contains a trailing directory separator so
  312. // that the full path for the file is path + filename.
  313. static bool findPresetFile(const std::string& name,
  314. const std::string& subdir,
  315. const std::string& base_path,
  316. std::string& filename, std::string& path);
  317. static std::vector<std::string> getLoadedPresetsList();
  318. public:
  319. // Lists what param sets are used and when during the day
  320. typedef std::map<F32, std::string> time_map_t;
  321. time_map_t mTimeMap;
  322. // How long is my day, in seconds
  323. S32 mDayLenth;
  324. // Lists the available day cycle presets in both the user and system
  325. // directories.
  326. typedef std::set<std::string> names_list_t;
  327. static names_list_t sPresetNames;
  328. };
  329. // Parameter manager class for the Windlight sky
  330. class LLWLSkyParamMgr
  331. {
  332. friend class LLWLAnimator;
  333. protected:
  334. LOG_CLASS(LLWLSkyParamMgr);
  335. public:
  336. LLWLSkyParamMgr();
  337. // This method is called in llappviewer.cpp only
  338. void initClass();
  339. // Loads all preset files
  340. void loadPresets();
  341. // Loads an individual preset into the sky. Returns true if successful.
  342. bool loadPreset(const std::string& name, bool propogate = true);
  343. // Saves the parameter presets to file
  344. void savePreset(const std::string& name);
  345. // Propagates the parameters to the environment
  346. void propagateParameters();
  347. // Animate the Windlight sky or not.
  348. void animate(bool enable = true);
  349. // Setups the animator to run
  350. void resetAnimator(F32 cur_time, bool run);
  351. // Gets where the light is pointing
  352. LL_INLINE LLVector4 getLightDir() const { return mLightDir; }
  353. // Gets where the light is pointing
  354. LL_INLINE LLVector4 getClampedLightDir() const { return mClampedLightDir; }
  355. // Adds a param to the list
  356. bool addParamSet(const std::string& name, LLWLParamSet& param);
  357. // Adds a param to the list
  358. bool addParamSet(const std::string& name, const LLSD& param);
  359. // Gets a param from the list
  360. bool getParamSet(const std::string& name, LLWLParamSet& param);
  361. // Sets the param in the list with a new param
  362. LL_INLINE bool setParamSet(const std::string& name, LLWLParamSet& param)
  363. {
  364. mParamList[name] = param;
  365. return true;
  366. }
  367. // Sets the param in the list with a new param
  368. bool setParamSet(const std::string& name, const LLSD& param);
  369. // Gets rid of a parameter and any references to it. Returns true if
  370. // successful
  371. bool removeParamSet(const std::string& name, bool delete_from_disk);
  372. // Lightshare support
  373. void processLightshareMessage(LLMessageSystem* msg);
  374. void processLightshareReset(bool force = false);
  375. LL_INLINE void setDirty() { mCurrentParamsDirty = true; }
  376. static std::vector<std::string> getLoadedPresetsList();
  377. static bool findPresetFile(const std::string& name,
  378. const std::string& base_path,
  379. std::string& filename, std::string& path);
  380. public:
  381. LLWLAnimator mAnimator;
  382. // Actual direction of the sun
  383. LLVector4 mLightDir;
  384. // Clamped light norm for shaders that are adversely affected when the sun
  385. // goes below the horizon
  386. LLVector4 mClampedLightDir;
  387. // List of params and how they are cycled for days
  388. LLWLDayCycle mDay;
  389. LLWLParamSet mCurParams;
  390. WLFloatControl mWLGamma;
  391. F32 mSceneLightStrength;
  392. // Atmospherics
  393. WLColorControl mBlueHorizon;
  394. WLColorControl mHazeDensity;
  395. WLColorControl mBlueDensity;
  396. WLFloatControl mDensityMult;
  397. WLColorControl mHazeHorizon;
  398. WLFloatControl mMaxAlt;
  399. // Lighting
  400. WLColorControl mLightnorm;
  401. WLColorControl mSunlight;
  402. WLColorControl mAmbient;
  403. WLColorControl mGlow;
  404. // Clouds
  405. WLColorControl mCloudColor;
  406. WLColorControl mCloudMain;
  407. WLFloatControl mCloudCoverage;
  408. WLColorControl mCloudDetail;
  409. WLFloatControl mDistanceMult;
  410. WLFloatControl mCloudScale;
  411. // List of all the parameter sets, indexed by name
  412. typedef std::map<std::string, LLWLParamSet> paramset_map_t;
  413. paramset_map_t mParamList;
  414. bool mHasLightshareOverride;
  415. bool mCurrentParamsDirty;
  416. };
  417. extern LLWLSkyParamMgr gWLSkyParamMgr;
  418. #endif