PageRenderTime 104ms CodeModel.GetById 40ms app.highlight 24ms RepoModel.GetById 37ms app.codeStats 0ms

/indra/newview/llwind.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 267 lines | 173 code | 45 blank | 49 comment | 27 complexity | 6f551aae20bea230dec57744c1a213bf MD5 | raw file
  1/** 
  2 * @file llwind.cpp
  3 * @brief LLWind class implementation
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27// Wind is a lattice.  It is computed on the simulator, and transmitted to the viewer.
 28// It drives special effects like smoke blowing, trees bending, and grass wiggling.
 29//
 30// Currently wind lattice does not interpolate correctly to neighbors.  This will need 
 31// work.
 32
 33#include "llviewerprecompiledheaders.h"
 34#include "indra_constants.h"
 35
 36#include "llwind.h"
 37
 38// linden libraries
 39#include "llgl.h"
 40#include "patch_dct.h"
 41#include "patch_code.h"
 42
 43// viewer
 44#include "noise.h"
 45#include "v4color.h"
 46#include "llworld.h"
 47
 48
 49//////////////////////////////////////////////////////////////////////
 50// Construction/Destruction
 51//////////////////////////////////////////////////////////////////////
 52
 53LLWind::LLWind()
 54:	mSize(16)
 55{
 56	init();
 57}
 58
 59
 60LLWind::~LLWind()
 61{
 62	delete [] mVelX;
 63	delete [] mVelY;
 64}
 65
 66
 67//////////////////////////////////////////////////////////////////////
 68// Public Methods
 69//////////////////////////////////////////////////////////////////////
 70
 71
 72void LLWind::init()
 73{
 74	LL_DEBUGS("Wind") << "initializing wind size: "<< mSize << LL_ENDL;
 75	
 76	// Initialize vector data
 77	mVelX = new F32[mSize*mSize];
 78	mVelY = new F32[mSize*mSize];
 79
 80	S32 i;
 81	for (i = 0; i < mSize*mSize; i++)
 82	{
 83		mVelX[i] = 0.5f;
 84		mVelY[i] = 0.5f;
 85	}
 86}
 87
 88
 89void LLWind::decompress(LLBitPack &bitpack, LLGroupHeader *group_headerp)
 90{
 91	LLPatchHeader  patch_header;
 92	S32 buffer[16*16];
 93
 94	init_patch_decompressor(group_headerp->patch_size);
 95
 96	// Don't use the packed group_header stride because the strides used on
 97	// simulator and viewer are not equal.
 98	group_headerp->stride = group_headerp->patch_size;	
 99	set_group_of_patch_header(group_headerp);
100
101	// X component
102	decode_patch_header(bitpack, &patch_header);
103	decode_patch(bitpack, buffer);
104	decompress_patch(mVelX, buffer, &patch_header);
105
106	// Y component
107	decode_patch_header(bitpack, &patch_header);
108	decode_patch(bitpack, buffer);
109	decompress_patch(mVelY, buffer, &patch_header);
110
111	S32 i, j, k;
112
113	for (j=1; j<mSize-1; j++)
114	{
115		for (i=1; i<mSize-1; i++)
116		{
117			k = i + j * mSize;
118			*(mVelX + k) = *(mVelX + k);
119			*(mVelY + k) = *(mVelY + k);
120		}
121	}
122
123	i = mSize - 1;
124	for (j=1; j<mSize-1; j++)
125	{
126		k = i + j * mSize;
127		*(mVelX + k) = *(mVelX + k);
128		*(mVelY + k) = *(mVelY + k);
129	}
130	i = 0;
131	for (j=1; j<mSize-1; j++)
132	{
133		k = i + j * mSize;
134		*(mVelX + k) = *(mVelX + k);
135		*(mVelY + k) = *(mVelY + k);
136	}
137	j = mSize - 1;
138	for (i=1; i<mSize-1; i++)
139	{
140		k = i + j * mSize;
141		*(mVelX + k) = *(mVelX + k);
142		*(mVelY + k) = *(mVelY + k);
143	}
144	j = 0;
145	for (i=1; i<mSize-1; i++)
146	{
147		k = i + j * mSize;
148		*(mVelX + k) = *(mVelX + k);
149		*(mVelY + k) = *(mVelY + k);
150	}
151}
152
153
154LLVector3 LLWind::getAverage()
155{
156	//  Returns in average_wind the average wind velocity 
157	LLVector3 average(0.0f, 0.0f, 0.0f);	
158	S32 i, grid_count;
159	grid_count = mSize * mSize;
160	for (i = 0; i < grid_count; i++)
161	{
162		average.mV[VX] += mVelX[i];
163		average.mV[VY] += mVelY[i];
164	}
165
166	average *= 1.f/((F32)(grid_count)) * WIND_SCALE_HACK;
167	return average;
168}
169
170
171LLVector3 LLWind::getVelocityNoisy(const LLVector3 &pos_region, const F32 dim)
172{
173	//  Resolve a value, using fractal summing to perturb the returned value 
174	LLVector3 r_val(0,0,0);
175	F32 norm = 1.0f;
176	if (dim == 8)
177	{
178		norm = 1.875;
179	}
180	else if (dim == 4)
181	{
182		norm = 1.75;
183	}
184	else if (dim == 2)
185	{
186		norm = 1.5;
187	}
188
189	F32 temp_dim = dim;
190	while (temp_dim >= 1.0)
191	{
192		LLVector3 pos_region_scaled(pos_region * temp_dim);
193		r_val += getVelocity(pos_region_scaled) * (1.0f/temp_dim);
194		temp_dim /= 2.0;
195	}
196	
197	return r_val * (1.0f/norm) * WIND_SCALE_HACK;
198}
199
200
201LLVector3 LLWind::getVelocity(const LLVector3 &pos_region)
202{
203	llassert(mSize == 16);
204	// Resolves value of wind at a location relative to SW corner of region
205	//  
206	// Returns wind magnitude in X,Y components of vector3
207	LLVector3 r_val;
208	F32 dx,dy;
209	S32 k;
210
211	LLVector3 pos_clamped_region(pos_region);
212	
213	F32 region_width_meters = LLWorld::getInstance()->getRegionWidthInMeters();
214
215	if (pos_clamped_region.mV[VX] < 0.f)
216	{
217		pos_clamped_region.mV[VX] = 0.f;
218	}
219	else if (pos_clamped_region.mV[VX] >= region_width_meters)
220	{
221		pos_clamped_region.mV[VX] = (F32) fmod(pos_clamped_region.mV[VX], region_width_meters);
222	}
223
224	if (pos_clamped_region.mV[VY] < 0.f)
225	{
226		pos_clamped_region.mV[VY] = 0.f;
227	}
228	else if (pos_clamped_region.mV[VY] >= region_width_meters)
229	{
230		pos_clamped_region.mV[VY] = (F32) fmod(pos_clamped_region.mV[VY], region_width_meters);
231	}
232	
233	
234	S32 i = llfloor(pos_clamped_region.mV[VX] * mSize / region_width_meters);
235	S32 j = llfloor(pos_clamped_region.mV[VY] * mSize / region_width_meters);
236	k = i + j*mSize;
237	dx = ((pos_clamped_region.mV[VX] * mSize / region_width_meters) - (F32) i);
238	dy = ((pos_clamped_region.mV[VY] * mSize / region_width_meters) - (F32) j);
239
240	if ((i < mSize-1) && (j < mSize-1))
241	{
242		//  Interior points, no edges
243		r_val.mV[VX] =  mVelX[k]*(1.0f - dx)*(1.0f - dy) + 
244						mVelX[k + 1]*dx*(1.0f - dy) + 
245						mVelX[k + mSize]*dy*(1.0f - dx) + 
246						mVelX[k + mSize + 1]*dx*dy;
247		r_val.mV[VY] =  mVelY[k]*(1.0f - dx)*(1.0f - dy) + 
248						mVelY[k + 1]*dx*(1.0f - dy) + 
249						mVelY[k + mSize]*dy*(1.0f - dx) + 
250						mVelY[k + mSize + 1]*dx*dy;
251	}
252	else 
253	{
254		r_val.mV[VX] = mVelX[k];
255		r_val.mV[VY] = mVelY[k];
256	}
257
258	r_val.mV[VZ] = 0.f;
259	return r_val * WIND_SCALE_HACK;
260}
261
262void LLWind::setOriginGlobal(const LLVector3d &origin_global)
263{
264	mOriginGlobal = origin_global;
265}
266
267