summaryrefslogtreecommitdiff
path: root/indra/newview/llcloud.cpp
blob: b3b6b411ab2b1272f6670960b78a1b7ef7e0c2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/** 
 * @file llcloud.cpp
 * @brief Implementation of viewer LLCloudLayer class
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llmath.h"
//#include "vmath.h"
#include "v3math.h"
#include "v4math.h"
#include "llquaternion.h"
#include "v4color.h"

#include "llwind.h"
#include "llcloud.h"
#include "llgl.h"
#include "llviewerobjectlist.h"
#include "llvoclouds.h"
#include "llvosky.h"
#include "llsky.h"
#include "llviewerregion.h"
#include "patch_dct.h"
#include "patch_code.h"
#include "llglheaders.h"
#include "pipeline.h"
#include "lldrawpool.h"
#include "llworld.h"

extern LLPipeline gPipeline;

const F32 CLOUD_UPDATE_RATE = 1.0f;  // Global time dilation for clouds
const F32 CLOUD_GROW_RATE = 0.05f;
const F32 CLOUD_DECAY_RATE = -0.05f;
const F32 CLOUD_VELOCITY_SCALE = 0.01f;
const F32 CLOUD_DENSITY = 25.f;
const S32 CLOUD_COUNT_MAX = 20;
const F32 CLOUD_HEIGHT_RANGE = 48.f;
const F32 CLOUD_HEIGHT_MEAN = 192.f;

enum
{
	LL_PUFF_GROWING = 0,
	LL_PUFF_DYING = 1
};

// Used for patch decoder
S32 gBuffer[16*16];


//static
S32 LLCloudPuff::sPuffCount = 0;

LLCloudPuff::LLCloudPuff() :
	mAlpha(0.01f),
	mRate(CLOUD_GROW_RATE*CLOUD_UPDATE_RATE),
	mLifeState(LL_PUFF_GROWING)
{
}

LLCloudGroup::LLCloudGroup() :
	mCloudLayerp(NULL),
	mDensity(0.f),
	mTargetPuffCount(0),
	mVOCloudsp(NULL)
{
}

void LLCloudGroup::cleanup()
{
	if (mVOCloudsp)
	{
		if (!mVOCloudsp->isDead())
		{
			gObjectList.killObject(mVOCloudsp);
		}
		mVOCloudsp = NULL;
	}
}

void LLCloudGroup::setCenterRegion(const LLVector3 &center)
{
	mCenterRegion = center;
}

void LLCloudGroup::updatePuffs(const F32 dt)
{
	mDensity = mCloudLayerp->getDensityRegion(mCenterRegion);

	if (!mVOCloudsp)
	{
		mVOCloudsp = (LLVOClouds *)gObjectList.createObjectViewer(LLViewerObject::LL_VO_CLOUDS, mCloudLayerp->getRegion());
		mVOCloudsp->setCloudGroup(this);
		mVOCloudsp->setPositionRegion(mCenterRegion);
		mVOCloudsp->setScale(LLVector3(256.f/CLOUD_GROUPS_PER_EDGE + CLOUD_PUFF_WIDTH,
										 256.f/CLOUD_GROUPS_PER_EDGE + CLOUD_PUFF_WIDTH,
										 CLOUD_HEIGHT_RANGE + CLOUD_PUFF_HEIGHT)*0.5f);
		gPipeline.addObject(mVOCloudsp);
	}

	LLVector3 velocity;
	LLVector3d vel_d;
	// Update the positions of all of the clouds
	for (U32 i = 0; i < mCloudPuffs.size(); i++)
	{
		LLCloudPuff &puff = mCloudPuffs[i];
		velocity = mCloudLayerp->getRegion()->mWind.getCloudVelocity(mCloudLayerp->getRegion()->getPosRegionFromGlobal(puff.mPositionGlobal));
		velocity *= CLOUD_VELOCITY_SCALE*CLOUD_UPDATE_RATE;
		vel_d.setVec(velocity);
		mCloudPuffs[i].mPositionGlobal += vel_d;
		mCloudPuffs[i].mAlpha += mCloudPuffs[i].mRate * dt;
		mCloudPuffs[i].mAlpha = llmin(1.f, mCloudPuffs[i].mAlpha);
		mCloudPuffs[i].mAlpha = llmax(0.f, mCloudPuffs[i].mAlpha);
	}
}

void LLCloudGroup::updatePuffOwnership()
{
	U32 i = 0;
	while (i < mCloudPuffs.size())
	{
		if (mCloudPuffs[i].getLifeState() == LL_PUFF_DYING)
		{
			i++;
			continue;
		}
		if (inGroup(mCloudPuffs[i]))
		{
			i++;
			continue;
		}

		//llinfos << "Cloud moving to new group" << llendl;
		LLCloudGroup *new_cgp = gWorldPointer->findCloudGroup(mCloudPuffs[i]);
		if (!new_cgp)
		{
			//llinfos << "Killing puff not in group" << llendl;
			mCloudPuffs[i].setLifeState(LL_PUFF_DYING);
			mCloudPuffs[i].mRate = CLOUD_DECAY_RATE*CLOUD_UPDATE_RATE;
			i++;
			continue;
		}
		//llinfos << "Puff handed off!" << llendl;
		LLCloudPuff puff;
		puff.mPositionGlobal = mCloudPuffs[i].mPositionGlobal;
		puff.mAlpha = mCloudPuffs[i].mAlpha;
		mCloudPuffs.erase(mCloudPuffs.begin() + i);
		new_cgp->mCloudPuffs.push_back(puff);
	}

	//llinfos << "Puff count: " << LLCloudPuff::sPuffCount << llendl;
}

void LLCloudGroup::updatePuffCount()
{
	if (!mVOCloudsp)
	{
		return;
	}
	S32 i;
	S32 target_puff_count = llround(CLOUD_DENSITY * mDensity);
	target_puff_count = llmax(0, target_puff_count);
	target_puff_count = llmin(CLOUD_COUNT_MAX, target_puff_count);
	S32 current_puff_count = (S32) mCloudPuffs.size();
	// Create a new cloud if we need one
	if (current_puff_count < target_puff_count)
	{
		LLVector3d puff_pos_global;
		mCloudPuffs.resize(target_puff_count);
		for (i = current_puff_count; i < target_puff_count; i++)
		{
			puff_pos_global = mVOCloudsp->getPositionGlobal();
			F32 x = ll_frand(256.f/CLOUD_GROUPS_PER_EDGE) - 128.f/CLOUD_GROUPS_PER_EDGE;
			F32 y = ll_frand(256.f/CLOUD_GROUPS_PER_EDGE) - 128.f/CLOUD_GROUPS_PER_EDGE;
			F32 z = ll_frand(CLOUD_HEIGHT_RANGE) - 0.5f*CLOUD_HEIGHT_RANGE;
			puff_pos_global += LLVector3d(x, y, z);
			mCloudPuffs[i].mPositionGlobal = puff_pos_global;
			mCloudPuffs[i].mAlpha = 0.01f;
			LLCloudPuff::sPuffCount++;
		}
	}

	// Count the number of live puffs
	S32 live_puff_count = 0;
	for (i = 0; i < (S32) mCloudPuffs.size(); i++)
	{
		if (mCloudPuffs[i].getLifeState() != LL_PUFF_DYING)
		{
			live_puff_count++;
		}
	}


	// Start killing enough puffs so the live puff count == target puff count
	S32 new_dying_count = llmax(0, live_puff_count - target_puff_count);
	i = 0;
	while (new_dying_count > 0)
	{
		if (mCloudPuffs[i].getLifeState() != LL_PUFF_DYING)
		{
			//llinfos << "Killing extra live cloud" << llendl;
			mCloudPuffs[i].setLifeState(LL_PUFF_DYING);
			mCloudPuffs[i].mRate = CLOUD_DECAY_RATE*CLOUD_UPDATE_RATE;
			new_dying_count--;
		}
		i++;
	}

	// Remove fully dead puffs
	i = 0;
	while (i < (S32) mCloudPuffs.size())
	{
		if (mCloudPuffs[i].isDead())
		{
			//llinfos << "Removing dead puff!" << llendl;
			mCloudPuffs.erase(mCloudPuffs.begin() + i);
			LLCloudPuff::sPuffCount--;
		}
		else
		{
			i++;
		}
	}
}

BOOL LLCloudGroup::inGroup(const LLCloudPuff &puff) const
{
	// Do min/max check on center of the cloud puff
	F32 min_x, min_y, max_x, max_y;
	F32 delta = 128.f/CLOUD_GROUPS_PER_EDGE;
	min_x = mCenterRegion.mV[VX] - delta;
	min_y = mCenterRegion.mV[VY] - delta;
	max_x = mCenterRegion.mV[VX] + delta;
	max_y = mCenterRegion.mV[VY] + delta;

	LLVector3 pos_region = mCloudLayerp->getRegion()->getPosRegionFromGlobal(puff.getPositionGlobal());

	if ((pos_region.mV[VX] < min_x)
		|| (pos_region.mV[VY] < min_y)
		|| (pos_region.mV[VX] > max_x)
		|| (pos_region.mV[VY] > max_y))
	{
		return FALSE;
	}
	return TRUE;
}

LLCloudLayer::LLCloudLayer()
: 	mOriginGlobal(0.0f, 0.0f, 0.0f),
	mMetersPerEdge(1.0f),
	mMetersPerGrid(1.0f),
	mWindp(NULL),
	mDensityp(NULL)
{
	S32 i, j;
	for (i = 0; i < 4; i++)
	{
		mNeighbors[i] = NULL;
	}

	F32 x, y;
	for (i = 0; i < CLOUD_GROUPS_PER_EDGE; i++)
	{
		y = (0.5f + i)*(256.f/CLOUD_GROUPS_PER_EDGE);
		for (j = 0; j < CLOUD_GROUPS_PER_EDGE; j++)
		{
			x = (0.5f + j)*(256.f/CLOUD_GROUPS_PER_EDGE);

			mCloudGroups[i][j].setCloudLayerp(this);
			mCloudGroups[i][j].setCenterRegion(LLVector3(x, y, CLOUD_HEIGHT_MEAN));
		}
	}
}



LLCloudLayer::~LLCloudLayer()
{
	destroy();
}


void LLCloudLayer::create(LLViewerRegion *regionp)
{
	llassert(regionp);

	mRegionp = regionp;
	mDensityp = new F32 [CLOUD_GRIDS_PER_EDGE * CLOUD_GRIDS_PER_EDGE];

	U32 i;
	for (i = 0; i < CLOUD_GRIDS_PER_EDGE*CLOUD_GRIDS_PER_EDGE; i++)
	{
		mDensityp[i] = 0.f;
	}
}

void LLCloudLayer::setRegion(LLViewerRegion *regionp)
{
	mRegionp = regionp;
}

void LLCloudLayer::destroy()
{
	// Kill all of the existing puffs
	S32 i, j;
	
	for (i = 0; i < CLOUD_GROUPS_PER_EDGE; i++)
	{
		for (j = 0; j < CLOUD_GROUPS_PER_EDGE; j++)
		{
			mCloudGroups[i][j].cleanup();
		}
	}

	delete [] mDensityp;
	mDensityp = NULL;
	mWindp = NULL;
}


void LLCloudLayer::reset()
{
}


void LLCloudLayer::setWindPointer(LLWind *windp)
{
	if (mWindp)
	{
		mWindp->setCloudDensityPointer(NULL);
	}
	mWindp = windp;
	if (mWindp)
	{
		mWindp->setCloudDensityPointer(mDensityp);
	}
}


void LLCloudLayer::setWidth(F32 width)
{
	mMetersPerEdge = width;
	mMetersPerGrid = width / CLOUD_GRIDS_PER_EDGE;
}


F32 LLCloudLayer::getDensityRegion(const LLVector3 &pos_region)
{	
	// "position" is region-local
	S32 i, j, ii, jj;

	i = lltrunc(pos_region.mV[VX] / mMetersPerGrid);
	j = lltrunc(pos_region.mV[VY] / mMetersPerGrid);
	ii = i + 1;
	jj = j + 1;


	// clamp 
	if (i >= (S32)CLOUD_GRIDS_PER_EDGE)
	{
		i = CLOUD_GRIDS_PER_EDGE - 1;
		ii = i;
	}
	else if (i < 0)
	{
		i = 0;
		ii = i;
	}
	else if (ii >= (S32)CLOUD_GRIDS_PER_EDGE || ii < 0)
	{
		ii = i;
	}

	if (j >= (S32)CLOUD_GRIDS_PER_EDGE)
	{
		j = CLOUD_GRIDS_PER_EDGE - 1;
		jj = j;
	}
	else if (j < 0)
	{
		j = 0;
		jj = j;
	}
	else if (jj >= (S32)CLOUD_GRIDS_PER_EDGE || jj < 0)
	{
		jj = j;
	}

	F32 dx = (pos_region.mV[VX] - (F32) i * mMetersPerGrid) / mMetersPerGrid;
	F32 dy = (pos_region.mV[VY] - (F32) j * mMetersPerGrid) / mMetersPerGrid;
	F32 omdx = 1.0f - dx;
	F32 omdy = 1.0f - dy;

	F32 density = dx * dy * *(mDensityp + ii + jj * CLOUD_GRIDS_PER_EDGE) + 
	   			  dx * omdy * *(mDensityp + i + jj * CLOUD_GRIDS_PER_EDGE) +
				  omdx * dy * *(mDensityp + ii + j * CLOUD_GRIDS_PER_EDGE) +
				  omdx * omdy * *(mDensityp + i + j * CLOUD_GRIDS_PER_EDGE);	  

	return density;
}

// a debug method that may yet be useful
void LLCloudLayer::renderDensityField()
{
// 	F32 x, y, z;
// 	U32 i, j, k;
// 	LLGLSNoTexture gls_ui_no_texture;
// 	// Render a bunch of triangles to represent the cloud density field
// 	glBegin(GL_TRIANGLES);
// 	for(j=0; j<CLOUD_GRIDS_PER_EDGE-1; j++)
// 	{
// 		y = j * mMetersPerGrid;

// 		for(i=0; i<CLOUD_GRIDS_PER_EDGE; i++)
// 		{
// 			k = i + j*CLOUD_GRIDS_PER_EDGE;
// 			x = i * mMetersPerGrid;

// 			z = 0.5f * CLOUD_MAX_HEIGHT + 40.0f * *(mDensityp + k + CLOUD_GRIDS_PER_EDGE);
// 			glColor3f(1.0f - *(mDensityp + k + CLOUD_GRIDS_PER_EDGE), *(mDensityp + k + CLOUD_GRIDS_PER_EDGE), 0.0f);
// 			glVertex3f(x, y+mMetersPerGrid, z);

// 			z = 0.5f * CLOUD_MAX_HEIGHT + 40.0f * *(mDensityp + k);
// 			glColor3f(1.0f - *(mDensityp + k), *(mDensityp + k), 0.0f);
// 			glVertex3f(x, y, z);

// 			z = 0.5f * CLOUD_MAX_HEIGHT + 40.0f * *(mDensityp + k + 1);
// 			glColor3f(1.0f - *(mDensityp + k + 1), *(mDensityp + k + 1), 0.0f);
// 			glVertex3f(x+mMetersPerGrid, y, z);

// 		}
// 	}
// 	glEnd();
}


void LLCloudLayer::decompress(LLBitPack &bitpack, LLGroupHeader *group_headerp)
{
	LLPatchHeader  patch_header;

	init_patch_decompressor(group_headerp->patch_size);

	// Don't use the packed group_header stride because the strides used on
	// simulator and viewer are not equal.
	group_headerp->stride = group_headerp->patch_size; 		// offset required to step up one row
	set_group_of_patch_header(group_headerp);

	decode_patch_header(bitpack, &patch_header);
	decode_patch(bitpack, gBuffer);
	decompress_patch(mDensityp, gBuffer, &patch_header); 
}

void LLCloudLayer::updatePuffs(const F32 dt)
{
	// We want to iterate through all of the cloud groups
	// and update their density targets

	S32 i, j;
	
	for (i = 0; i < CLOUD_GROUPS_PER_EDGE; i++)
	{
		for (j = 0; j < CLOUD_GROUPS_PER_EDGE; j++)
		{
			mCloudGroups[i][j].updatePuffs(dt);
		}
	}
}

void LLCloudLayer::updatePuffOwnership()
{
	S32 i, j;
	
	for (i = 0; i < CLOUD_GROUPS_PER_EDGE; i++)
	{
		for (j = 0; j < CLOUD_GROUPS_PER_EDGE; j++)
		{
			mCloudGroups[i][j].updatePuffOwnership();
		}
	}
}

void LLCloudLayer::updatePuffCount()
{
	S32 i, j;
	
	for (i = 0; i < CLOUD_GROUPS_PER_EDGE; i++)
	{
		for (j = 0; j < CLOUD_GROUPS_PER_EDGE; j++)
		{
			mCloudGroups[i][j].updatePuffCount();
		}
	}
}

LLCloudGroup *LLCloudLayer::findCloudGroup(const LLCloudPuff &puff)
{
	S32 i, j;
	
	for (i = 0; i < CLOUD_GROUPS_PER_EDGE; i++)
	{
		for (j = 0; j < CLOUD_GROUPS_PER_EDGE; j++)
		{
			if (mCloudGroups[i][j].inGroup(puff))
			{
				return &(mCloudGroups[i][j]);
			}
		}
	}
	return NULL;
}



void LLCloudLayer::connectNeighbor(LLCloudLayer *cloudp, U32 direction)
{
	if (direction >= 4)
	{
		// Only care about cardinal 4 directions.
		return;
	}

	mNeighbors[direction] = cloudp;
	if (cloudp)
		mNeighbors[direction]->mNeighbors[gDirOpposite[direction]] = this;
}


void LLCloudLayer::disconnectNeighbor(U32 direction)
{
	if (direction >= 4)
	{
		// Only care about cardinal 4 directions.
		return;
	}

	if (mNeighbors[direction])
	{
		mNeighbors[direction]->mNeighbors[gDirOpposite[direction]] = NULL;
		mNeighbors[direction] = NULL;
	}
}


void LLCloudLayer::disconnectAllNeighbors()
{
	S32 i;
	for (i = 0; i < 4; i++)
	{
		disconnectNeighbor(i);
	}
}