summaryrefslogtreecommitdiff
path: root/indra/newview/llvopartgroup.cpp
blob: caba9e3aef147aa647e7dfef69706ad3146fa1a9 (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
/** 
 * @file llvopartgroup.cpp
 * @brief Group of particle systems
 *
 * Copyright (c) 2001-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include "llviewerprecompiledheaders.h"

#include "llvopartgroup.h"

#include "llfasttimer.h"
#include "message.h"
#include "v2math.h"

#include "llagent.h"
#include "lldrawable.h"
#include "llface.h"
#include "llsky.h"
#include "llviewercamera.h"
#include "llviewerpartsim.h"
#include "llviewerregion.h"
#include "pipeline.h"

const F32 MAX_PART_LIFETIME = 120.f;

extern U64 gFrameTime;

LLVOPartGroup::LLVOPartGroup(const LLUUID &id, const LLPCode pcode, LLViewerRegion *regionp)
	:	LLViewerObject(id, pcode, regionp),
		mViewerPartGroupp(NULL)
{
	setNumTEs(1);
	setTETexture(0, LLUUID::null);
	mbCanSelect = FALSE;			// users can't select particle systems
	mDebugColor = LLColor4(frand(1.f), frand(1.f), frand(1.f), 1.f);
}


LLVOPartGroup::~LLVOPartGroup()
{
}

BOOL LLVOPartGroup::isActive() const
{
	return TRUE;
}

BOOL LLVOPartGroup::idleUpdate(LLAgent &agent, LLWorld &world, const F64 &time)
{
	if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_PARTICLES))
	{
		gPipeline.markRebuild(mDrawable, LLDrawable::REBUILD_VOLUME, TRUE);
	}

	// Particle groups don't need any of default idleUpdate velocity interpolation stuff.
	//LLViewerObject::idleUpdate(agent, world, time);
	return TRUE;
}


void LLVOPartGroup::setPixelAreaAndAngle(LLAgent &agent)
{
	// mPixelArea is calculated during render

	LLVector3 viewer_pos_agent = agent.getCameraPositionAgent();
	LLVector3 pos_agent = getRenderPosition();

	F32 dx = viewer_pos_agent.mV[VX] - pos_agent.mV[VX];
	F32 dy = viewer_pos_agent.mV[VY] - pos_agent.mV[VY];
	F32 dz = viewer_pos_agent.mV[VZ] - pos_agent.mV[VZ];

	F32 mid_scale = getMidScale();
	F32 range = sqrt(dx*dx + dy*dy + dz*dz);

	if (range < 0.001f || isHUDAttachment())		// range == zero
	{
		mAppAngle = 180.f;
	}
	else
	{
		mAppAngle = (F32) atan2( mid_scale, range) * RAD_TO_DEG;
	}
}

void LLVOPartGroup::updateTextures(LLAgent &agent)
{
	// Texture stats for particles will need to be updated in a different way...
}


LLDrawable* LLVOPartGroup::createDrawable(LLPipeline *pipeline)
{
	pipeline->allocDrawable(this);
	mDrawable->setLit(FALSE);
	mDrawable->setRenderType(LLPipeline::RENDER_TYPE_PARTICLES);

	LLDrawPool *pool = gPipeline.getPool(LLDrawPool::POOL_ALPHA);
	mDrawable->setNumFaces(mViewerPartGroupp->getCount(), pool, getTEImage(0));
	return mDrawable;
}

 const F32 MAX_PARTICLE_AREA_SCALE = 0.02f; // some tuned constant, limits on how much particle area to draw

BOOL LLVOPartGroup::updateGeometry(LLDrawable *drawable)
{
 	LLFastTimer t(LLFastTimer::FTM_UPDATE_PARTICLES);
	
	LLVector3 at;
	LLVector3 up;
	LLVector3 right;
	LLVector3 position_agent;
	LLVector3 camera_agent = gAgent.getCameraPositionAgent();
	LLVector2 uvs[4];

	uvs[0].setVec(0.f, 1.f);
	uvs[1].setVec(0.f, 0.f);
	uvs[2].setVec(1.f, 1.f);
	uvs[3].setVec(1.f, 0.f);

	LLDrawPool *drawpool = gPipeline.getPool(LLDrawPool::POOL_ALPHA);
	S32 num_parts = mViewerPartGroupp->getCount();
	LLFace *facep;
	
	if (!num_parts)
	{
		drawable->setNumFaces(0, drawpool, getTEImage(0));
		LLPipeline::sCompiles++;
		return TRUE;
	}

 	if (!(gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_PARTICLES)))
	{
		return TRUE;
	}

// 	drawable->setNumFaces(num_parts, drawpool, getTEImage(0));
	drawable->setNumFacesFast(num_parts, drawpool, getTEImage(0));
	
	LLVector3 normal(-gCamera->getXAxis()); // make point agent face camera
	
	LLStrider<LLVector3> verticesp;
	LLStrider<LLVector3> normalsp;
	LLStrider<LLVector2> texCoordsp;
	U32 *indicesp = 0;

	S32 vert_offset;
	F32 tot_area = 0;
	F32 max_area = LLViewerPartSim::getMaxPartCount() * MAX_PARTICLE_AREA_SCALE; 
	
	S32 count=0;
	for (S32 i = 0; i < num_parts; i++)
	{
		const LLViewerPart &part = mViewerPartGroupp->mParticles[i];

		LLVector3 part_pos_agent(part.mPosAgent);
		at = part_pos_agent - camera_agent;

		//F32 invcamdist = 1.0f / at.magVec();
		//area += (part.mScale.mV[0]*invcamdist)*(part.mScale.mV[1]*invcamdist);
		F32 camera_dist_squared = at.magVecSquared();
		F32 inv_camera_dist_squared;
		if (camera_dist_squared > 1.f)
			inv_camera_dist_squared = 1.f / camera_dist_squared;
		else
			inv_camera_dist_squared = 1.f;
		F32 area = part.mScale.mV[0] * part.mScale.mV[1] * inv_camera_dist_squared;
		tot_area += area;
 		if (tot_area > max_area)
		{
			break;
		}
		
		count++;

		facep = drawable->getFace(i);
		const F32 NEAR_PART_DIST_SQ = 5.f*5.f;  // Only discard particles > 5 m from the camera
		const F32 MIN_PART_AREA = .005f*.005f;  // only less than 5 mm x 5 mm at 1 m from camera
		if (camera_dist_squared > NEAR_PART_DIST_SQ && area < MIN_PART_AREA)
		{
			facep->setSize(0, 0);
			continue;
		}
		facep->setSize(4, 6);
		facep->mCenterAgent = part_pos_agent;
		vert_offset = facep->getGeometry(verticesp,normalsp,texCoordsp, indicesp);
		if (-1 == vert_offset)
		{
			return TRUE;
		}
		
		right = at % LLVector3(0.f, 0.f, 1.f);
		right.normVec();
		up = right % at;
		up.normVec();

		if (part.mFlags & LLPartData::LL_PART_FOLLOW_VELOCITY_MASK)
		{
			LLVector3 normvel = part.mVelocity;
			normvel.normVec();
			LLVector2 up_fracs;
			up_fracs.mV[0] = normvel*right;
			up_fracs.mV[1] = normvel*up;
			up_fracs.normVec();

			LLVector3 new_up;
			LLVector3 new_right;
			new_up = up_fracs.mV[0] * right + up_fracs.mV[1]*up;
			new_right = up_fracs.mV[1] * right - up_fracs.mV[0]*up;
			up = new_up;
			right = new_right;
			up.normVec();
			right.normVec();
		}

		right *= 0.5f*part.mScale.mV[0];
		up *= 0.5f*part.mScale.mV[1];

		*verticesp++ = part_pos_agent + up - right;
		*verticesp++ = part_pos_agent - up - right;
		*verticesp++ = part_pos_agent + up + right;
		*verticesp++ = part_pos_agent - up + right;

		*texCoordsp++ = uvs[0];
		*texCoordsp++ = uvs[1];
		*texCoordsp++ = uvs[2];
		*texCoordsp++ = uvs[3];

		*normalsp++   = normal;
		*normalsp++   = normal;
		*normalsp++   = normal;
		*normalsp++   = normal;

		if (part.mFlags & LLPartData::LL_PART_EMISSIVE_MASK)
		{
			facep->setState(LLFace::FULLBRIGHT);
		}
		else
		{
			facep->clearState(LLFace::FULLBRIGHT);
		}
		facep->setFaceColor(part.mColor);
		
		*indicesp++ = vert_offset + 0;
		*indicesp++ = vert_offset + 1;
		*indicesp++ = vert_offset + 2;

		*indicesp++ = vert_offset + 1;
		*indicesp++ = vert_offset + 3;
		*indicesp++ = vert_offset + 2;
	}
	for (S32 j = count; j < drawable->getNumFaces(); j++) 
	{
		drawable->getFace(j)->setSize(0,0);
	}
	
	mPixelArea = tot_area * gCamera->getPixelMeterRatio() * gCamera->getPixelMeterRatio();
	const F32 area_scale = 10.f; // scale area to increase priority a bit
	getTEImage(0)->addTextureStats(mPixelArea * area_scale);

	LLPipeline::sCompiles++;

	return TRUE;
}