llwind.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * @file llwind.cpp
  3. * @brief LLWind class implementation
  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. // Wind is a lattice. It is computed on the simulator, and transmitted to the
  33. // viewer. It drives special effects like smoke blowing, trees bending, and
  34. // grass wiggling.
  35. //
  36. // Currently wind lattice does not interpolate correctly to neighbors. This
  37. // will need work.
  38. #include "llviewerprecompiledheaders.h"
  39. #include "llwind.h"
  40. #include "llgl.h"
  41. #include "llpatch_code.h"
  42. #include "llagent.h"
  43. constexpr F32 CLOUD_DIVERGENCE_COEF = 0.5f;
  44. constexpr F32 WIND_RELATIVE_ALTITUDE = 25.f;
  45. static S32 PatchBuffer[256];
  46. //////////////////////////////////////////////////////////////////////
  47. // Construction/Destruction
  48. //////////////////////////////////////////////////////////////////////
  49. LLWind::LLWind()
  50. : mSize(16),
  51. mCloudDensityp(NULL)
  52. {
  53. init();
  54. }
  55. LLWind::~LLWind()
  56. {
  57. delete[] mVelX;
  58. delete[] mVelY;
  59. delete[] mCloudVelX;
  60. delete[] mCloudVelY;
  61. }
  62. //////////////////////////////////////////////////////////////////////
  63. // Public Methods
  64. //////////////////////////////////////////////////////////////////////
  65. void LLWind::init()
  66. {
  67. // Initialize vector data
  68. mVelX = new F32[mSize * mSize];
  69. mVelY = new F32[mSize * mSize];
  70. mCloudVelX = new F32[mSize * mSize];
  71. mCloudVelY = new F32[mSize * mSize];
  72. for (S32 i = 0, count = mSize * mSize; i < count; ++i)
  73. {
  74. mVelX[i] = 0.5f;
  75. mVelY[i] = 0.5f;
  76. mCloudVelX[i] = 0.0f;
  77. mCloudVelY[i] = 0.0f;
  78. }
  79. }
  80. void LLWind::decompress(LLBitPack& bitpack, LLGroupHeader* group_headerp)
  81. {
  82. if (!mCloudDensityp)
  83. {
  84. return;
  85. }
  86. LLPatchHeader patch_header;
  87. init_patch_decompressor(group_headerp->patch_size);
  88. // Don't use the packed group_header stride because the strides used on
  89. // simulator and viewer are not equal.
  90. group_headerp->stride = group_headerp->patch_size;
  91. set_group_of_patch_header(group_headerp);
  92. // X component
  93. decode_patch_header(bitpack, &patch_header);
  94. decode_patch(bitpack, PatchBuffer);
  95. decompress_patch(mVelX, PatchBuffer, &patch_header);
  96. // Y component
  97. decode_patch_header(bitpack, &patch_header);
  98. decode_patch(bitpack, PatchBuffer);
  99. decompress_patch(mVelY, PatchBuffer, &patch_header);
  100. // HACK -- mCloudVelXY is the same as mVelXY, except we add a divergence
  101. // that is proportional to the gradient of the cloud density ==> this helps
  102. // to clump clouds together.
  103. // NOTE ASSUMPTION: cloud density has the same dimensions as the wind field
  104. // This needs to be fixed... causes discrepency at region boundaries
  105. S32 i, j, k;
  106. for (j = 1; j < mSize - 1; ++j)
  107. {
  108. for (i = 1; i < mSize - 1; ++i)
  109. {
  110. k = i + j * mSize;
  111. *(mCloudVelX + k) = *(mVelX + k) +
  112. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + 1) -
  113. *(mCloudDensityp + k - 1));
  114. *(mCloudVelY + k) = *(mVelY + k) +
  115. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + mSize) -
  116. *(mCloudDensityp + k - mSize));
  117. }
  118. }
  119. i = mSize - 1;
  120. for (j = 1; j < mSize - 1; ++j)
  121. {
  122. k = i + j * mSize;
  123. *(mCloudVelX + k) = *(mVelX + k) +
  124. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k) -
  125. *(mCloudDensityp + k - 2));
  126. *(mCloudVelY + k) = *(mVelY + k) +
  127. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + mSize) -
  128. *(mCloudDensityp + k - mSize));
  129. }
  130. i = 0;
  131. for (j = 1; j < mSize - 1; ++j)
  132. {
  133. k = i + j * mSize;
  134. *(mCloudVelX + k) = *(mVelX + k) +
  135. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + 2) -
  136. *(mCloudDensityp + k));
  137. *(mCloudVelY + k) = *(mVelY + k) +
  138. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + mSize) -
  139. *(mCloudDensityp + k + mSize));
  140. }
  141. j = mSize - 1;
  142. for (i = 1; i < mSize - 1; ++i)
  143. {
  144. k = i + j * mSize;
  145. *(mCloudVelX + k) = *(mVelX + k) +
  146. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + 1) -
  147. *(mCloudDensityp + k - 1));
  148. *(mCloudVelY + k) = *(mVelY + k) +
  149. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k) -
  150. *(mCloudDensityp + k - 2 * mSize));
  151. }
  152. j = 0;
  153. for (i = 1; i < mSize - 1; ++i)
  154. {
  155. k = i + j * mSize;
  156. *(mCloudVelX + k) = *(mVelX + k) +
  157. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + 1) -
  158. *(mCloudDensityp + k -1));
  159. *(mCloudVelY + k) = *(mVelY + k) +
  160. CLOUD_DIVERGENCE_COEF * (*(mCloudDensityp + k + 2 * mSize) -
  161. *(mCloudDensityp + k));
  162. }
  163. }
  164. // Returns in average_wind the average wind velocity
  165. LLVector3 LLWind::getAverage()
  166. {
  167. LLVector3 average(0.0f, 0.0f, 0.0f);
  168. S32 grid_count = mSize * mSize;
  169. for (S32 i = 0; i < grid_count; ++i)
  170. {
  171. average.mV[VX] += mVelX[i];
  172. average.mV[VY] += mVelY[i];
  173. }
  174. average *= WIND_SCALE_HACK / (F32)grid_count;
  175. return average;
  176. }
  177. // Resolve a value, using fractal summing to perturb the returned value
  178. LLVector3 LLWind::getVelocityNoisy(const LLVector3& pos_region, F32 dim)
  179. {
  180. LLVector3 r_val(0.f, 0.f, 0.f);
  181. F32 norm = 1.0f;
  182. if (dim == 8)
  183. {
  184. norm = 1.875f;
  185. }
  186. else if (dim == 4)
  187. {
  188. norm = 1.75f;
  189. }
  190. else if (dim == 2)
  191. {
  192. norm = 1.5f;
  193. }
  194. F32 temp_dim = dim;
  195. while (temp_dim >= 1.f)
  196. {
  197. LLVector3 pos_region_scaled(pos_region * temp_dim);
  198. r_val += getVelocity(pos_region_scaled) / temp_dim;
  199. temp_dim /= 2.f;
  200. }
  201. return r_val / norm * WIND_SCALE_HACK;
  202. }
  203. // Resolves value of wind at a location relative to SW corner of region.
  204. // Returns wind magnitude in X,Y components of vector3.
  205. LLVector3 LLWind::getVelocity(const LLVector3& pos_region)
  206. {
  207. llassert(mSize == 16);
  208. LLVector3 pos_clamped_region(pos_region);
  209. if (pos_clamped_region.mV[VX] < 0.f)
  210. {
  211. pos_clamped_region.mV[VX] = 0.f;
  212. }
  213. else if (pos_clamped_region.mV[VX] >= mRegionWidth)
  214. {
  215. pos_clamped_region.mV[VX] = (F32)fmod(pos_clamped_region.mV[VX],
  216. mRegionWidth);
  217. }
  218. if (pos_clamped_region.mV[VY] < 0.f)
  219. {
  220. pos_clamped_region.mV[VY] = 0.f;
  221. }
  222. else if (pos_clamped_region.mV[VY] >= mRegionWidth)
  223. {
  224. pos_clamped_region.mV[VY] = (F32)fmod(pos_clamped_region.mV[VY],
  225. mRegionWidth);
  226. }
  227. S32 i = llfloor(pos_clamped_region.mV[VX] * mSize / mRegionWidth);
  228. S32 j = llfloor(pos_clamped_region.mV[VY] * mSize / mRegionWidth);
  229. S32 k = i + j * mSize;
  230. F32 dx = pos_clamped_region.mV[VX] * mSize / mRegionWidth - (F32)i;
  231. F32 dy = pos_clamped_region.mV[VY] * mSize / mRegionWidth - (F32)j;
  232. LLVector3 r_val;
  233. if (i < mSize - 1 && j < mSize - 1)
  234. {
  235. // Interior points, no edges
  236. r_val.mV[VX] = mVelX[k] * (1.0f - dx) * (1.0f - dy) +
  237. mVelX[k + 1] * dx * (1.0f - dy) +
  238. mVelX[k + mSize] * dy * (1.0f - dx) +
  239. mVelX[k + mSize + 1] * dx * dy;
  240. r_val.mV[VY] = mVelY[k] * (1.0f - dx) * (1.0f - dy) +
  241. mVelY[k + 1] * dx * (1.0f - dy) +
  242. mVelY[k + mSize] * dy * (1.0f - dx) +
  243. mVelY[k + mSize + 1] * dx * dy;
  244. }
  245. else
  246. {
  247. r_val.mV[VX] = mVelX[k];
  248. r_val.mV[VY] = mVelY[k];
  249. }
  250. r_val.mV[VZ] = 0.f;
  251. return r_val * WIND_SCALE_HACK;
  252. }
  253. // Resolves value of wind at a location relative to SW corner of region.
  254. // Returns wind magnitude in X,Y components of vector3.
  255. LLVector3 LLWind::getCloudVelocity(const LLVector3& pos_region)
  256. {
  257. llassert(mSize == 16);
  258. LLVector3 r_val;
  259. F32 dx, dy;
  260. S32 k;
  261. LLVector3 pos_clamped_region(pos_region);
  262. if (pos_clamped_region.mV[VX] < 0.f)
  263. {
  264. pos_clamped_region.mV[VX] = 0.f;
  265. }
  266. else if (pos_clamped_region.mV[VX] >= mRegionWidth)
  267. {
  268. pos_clamped_region.mV[VX] = (F32)fmod(pos_clamped_region.mV[VX],
  269. mRegionWidth);
  270. }
  271. if (pos_clamped_region.mV[VY] < 0.f)
  272. {
  273. pos_clamped_region.mV[VY] = 0.f;
  274. }
  275. else if (pos_clamped_region.mV[VY] >= mRegionWidth)
  276. {
  277. pos_clamped_region.mV[VY] = (F32)fmod(pos_clamped_region.mV[VY],
  278. mRegionWidth);
  279. }
  280. S32 i = llfloor(pos_clamped_region.mV[VX] * mSize / mRegionWidth);
  281. S32 j = llfloor(pos_clamped_region.mV[VY] * mSize / mRegionWidth);
  282. k = i + j * mSize;
  283. dx = pos_clamped_region.mV[VX] * mSize / mRegionWidth - (F32)i;
  284. dy = pos_clamped_region.mV[VY] * mSize / mRegionWidth - (F32)j;
  285. if (i < mSize - 1 && j < mSize - 1)
  286. {
  287. // Interior points, no edges
  288. r_val.mV[VX] = mCloudVelX[k] * (1.0f - dx) * (1.0f - dy) +
  289. mCloudVelX[k + 1] * dx * (1.0f - dy) +
  290. mCloudVelX[k + mSize] * dy * (1.0f - dx) +
  291. mCloudVelX[k + mSize + 1] * dx * dy;
  292. r_val.mV[VY] = mCloudVelY[k] * (1.0f - dx) * (1.0f - dy) +
  293. mCloudVelY[k + 1] * dx * (1.0f - dy) +
  294. mCloudVelY[k + mSize] * dy * (1.0f - dx) +
  295. mCloudVelY[k + mSize + 1] * dx * dy;
  296. }
  297. else
  298. {
  299. r_val.mV[VX] = mCloudVelX[k];
  300. r_val.mV[VY] = mCloudVelY[k];
  301. }
  302. r_val.mV[VZ] = 0.f;
  303. return r_val * WIND_SCALE_HACK;
  304. }
  305. // Renders the wind as vectors (used for debug - used to be in llglsandbox.cpp)
  306. void LLWind::renderVectors()
  307. {
  308. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  309. gGL.pushMatrix();
  310. LLVector3 origin_agent;
  311. origin_agent = gAgent.getPosAgentFromGlobal(mOriginGlobal);
  312. gGL.translatef(origin_agent.mV[VX], origin_agent.mV[VY],
  313. gAgent.getPositionAgent().mV[VZ] + WIND_RELATIVE_ALTITUDE);
  314. for (S32 j = 0; j < mSize; ++j)
  315. {
  316. for (S32 i = 0; i < mSize; ++i)
  317. {
  318. F32 x = mCloudVelX[i + j * mSize] * WIND_SCALE_HACK;
  319. F32 y = mCloudVelY[i + j * mSize] * WIND_SCALE_HACK;
  320. gGL.pushMatrix();
  321. gGL.translatef((F32)i * mRegionWidth / (F32)mSize,
  322. (F32)j * mRegionWidth / (F32)mSize, 0.f);
  323. gGL.color3f(0, 1, 0);
  324. gGL.begin(LLRender::POINTS);
  325. gGL.vertex3f(0, 0, 0);
  326. gGL.end();
  327. gGL.color3f(1, 0, 0);
  328. gGL.begin(LLRender::LINES);
  329. gGL.vertex3f(x * 0.1f, y * 0.1f, 0.f);
  330. gGL.vertex3f(x, y, 0.f);
  331. gGL.end();
  332. gGL.popMatrix();
  333. }
  334. }
  335. gGL.popMatrix();
  336. stop_glerror();
  337. }