summaryrefslogtreecommitdiff
path: root/indra/newview/llhudicon.cpp
blob: 040027c70d6e509d59b3b9ebcf20f48a5d773a18 (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
/** 
 * @file llhudicon.cpp
 * @brief LLHUDIcon class implementation
 *
 * $LicenseInfo:firstyear=2006&license=viewergpl$
 * 
 * Copyright (c) 2006-2009, 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://secondlifegrid.net/programs/open_source/licensing/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://secondlifegrid.net/programs/open_source/licensing/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 "llhudicon.h"

#include "llgl.h"
#include "llrender.h"

#include "llviewerobject.h"
#include "lldrawable.h"
#include "llviewercamera.h"
#include "llviewertexture.h"
#include "llviewerwindow.h"

//-----------------------------------------------------------------------------
// Local consts
//-----------------------------------------------------------------------------
const F32 ANIM_TIME = 0.4f;
const F32 DIST_START_FADE = 15.f;
const F32 DIST_END_FADE = 30.f;
const F32 MAX_VISIBLE_TIME = 15.f;
const F32 FADE_OUT_TIME = 1.f;

//-----------------------------------------------------------------------------
// Utility functions
//-----------------------------------------------------------------------------
static F32 calc_bouncy_animation(F32 x)
{
	return -(cosf(x * F_PI * 2.5f - F_PI_BY_TWO))*(0.4f + x * -0.1f) + x * 1.3f;
}


//-----------------------------------------------------------------------------
// static declarations
//-----------------------------------------------------------------------------
LLHUDIcon::icon_instance_t LLHUDIcon::sIconInstances;

LLHUDIcon::LLHUDIcon(const U8 type) :
			LLHUDObject(type),
			mImagep(NULL),
			mPickID(0),
			mScale(0.1f),
			mHidden(FALSE)
{
	sIconInstances.push_back(this);
}

LLHUDIcon::~LLHUDIcon()
{
	mImagep = NULL;
}

void LLHUDIcon::renderIcon(BOOL for_select)
{
	LLGLSUIDefault texture_state;
	LLGLDepthTest gls_depth(GL_TRUE);
	if (for_select)
	{
		gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
	}
	
	if (mHidden)
		return;

	if (mSourceObject.isNull() || mImagep.isNull())
	{
		markDead();
		return;
	}

	LLVector3 obj_position = mSourceObject->getRenderPosition();

	// put icon above object, and in front
	// RN: don't use drawable radius, it's fricking HUGE
	LLVector3 icon_relative_pos = (LLViewerCamera::getInstance()->getUpAxis() * ~mSourceObject->getRenderRotation());
	icon_relative_pos.abs();

	F32 distance_scale = llmin(mSourceObject->getScale().mV[VX] / icon_relative_pos.mV[VX], 
		mSourceObject->getScale().mV[VY] / icon_relative_pos.mV[VY], 
		mSourceObject->getScale().mV[VZ] / icon_relative_pos.mV[VZ]);
	F32 up_distance = 0.5f * distance_scale;
	LLVector3 icon_position = obj_position + (up_distance * LLViewerCamera::getInstance()->getUpAxis()) * 1.2f;

	LLVector3 icon_to_cam = LLViewerCamera::getInstance()->getOrigin() - icon_position;
	icon_to_cam.normVec();

	icon_position += icon_to_cam * mSourceObject->mDrawable->getRadius() * 1.1f;

	mDistance = dist_vec(icon_position, LLViewerCamera::getInstance()->getOrigin());

	F32 alpha_factor = for_select ? 1.f : clamp_rescale(mDistance, DIST_START_FADE, DIST_END_FADE, 1.f, 0.f);

	LLVector3 x_pixel_vec;
	LLVector3 y_pixel_vec;
	
	LLViewerCamera::getInstance()->getPixelVectors(icon_position, y_pixel_vec, x_pixel_vec);

	F32 scale_factor = 1.f;
	if (mAnimTimer.getElapsedTimeF32() < ANIM_TIME)
	{
		scale_factor = llmax(0.f, calc_bouncy_animation(mAnimTimer.getElapsedTimeF32() / ANIM_TIME));
	}

	F32 time_elapsed = mLifeTimer.getElapsedTimeF32();
	if (time_elapsed > MAX_VISIBLE_TIME)
	{
		markDead();
		return;
	}
	
	if (time_elapsed > MAX_VISIBLE_TIME - FADE_OUT_TIME)
	{
		alpha_factor *= clamp_rescale(time_elapsed, MAX_VISIBLE_TIME - FADE_OUT_TIME, MAX_VISIBLE_TIME, 1.f, 0.f);
	}

	F32 image_aspect = (F32)mImagep->getFullWidth() / (F32)mImagep->getFullHeight() ;
	LLVector3 x_scale = image_aspect * (F32)gViewerWindow->getWindowHeightScaled() * mScale * scale_factor * x_pixel_vec;
	LLVector3 y_scale = (F32)gViewerWindow->getWindowHeightScaled() * mScale * scale_factor * y_pixel_vec;

	LLVector3 lower_left = icon_position - (x_scale * 0.5f);
	LLVector3 lower_right = icon_position + (x_scale * 0.5f);
	LLVector3 upper_left = icon_position - (x_scale * 0.5f) + y_scale;
	LLVector3 upper_right = icon_position + (x_scale * 0.5f) + y_scale;

	if (for_select)
	{
		// set color to unique color id for picking
		LLColor4U coloru((U8)(mPickID >> 16), (U8)(mPickID >> 8), (U8)mPickID);
		gGL.color4ubv(coloru.mV);
	}
	else
	{
		LLColor4 icon_color = LLColor4::white;
		icon_color.mV[VALPHA] = alpha_factor;
		gGL.color4fv(icon_color.mV);
		gGL.getTexUnit(0)->bind(mImagep);
	}

	gGL.begin(LLRender::QUADS);
	{
		gGL.texCoord2f(0.f, 1.f);
		gGL.vertex3fv(upper_left.mV);
		gGL.texCoord2f(0.f, 0.f);
		gGL.vertex3fv(lower_left.mV);
		gGL.texCoord2f(1.f, 0.f);
		gGL.vertex3fv(lower_right.mV);
		gGL.texCoord2f(1.f, 1.f);
		gGL.vertex3fv(upper_right.mV);
	}
	gGL.end();
}

void LLHUDIcon::setImage(LLViewerTexture* imagep)
{
	mImagep = imagep;
	mImagep->setAddressMode(LLTexUnit::TAM_CLAMP);
}

void LLHUDIcon::setScale(F32 fraction_of_fov)
{
	mScale = fraction_of_fov;
}

void LLHUDIcon::markDead()
{
	if (mSourceObject)
	{
		mSourceObject->clearIcon();
	}
	LLHUDObject::markDead();
}

void LLHUDIcon::render()
{
	renderIcon(FALSE);
}

void LLHUDIcon::renderForSelect()
{
	renderIcon(TRUE);
}

BOOL LLHUDIcon::lineSegmentIntersect(const LLVector3& start, const LLVector3& end, LLVector3* intersection)
{
	if (mHidden)
		return FALSE;

	if (mSourceObject.isNull() || mImagep.isNull())
	{
		markDead();
		return FALSE;
	}

	LLVector3 obj_position = mSourceObject->getRenderPosition();

	// put icon above object, and in front
	// RN: don't use drawable radius, it's fricking HUGE
	LLVector3 icon_relative_pos = (LLViewerCamera::getInstance()->getUpAxis() * ~mSourceObject->getRenderRotation());
	icon_relative_pos.abs();

	F32 distance_scale = llmin(mSourceObject->getScale().mV[VX] / icon_relative_pos.mV[VX], 
		mSourceObject->getScale().mV[VY] / icon_relative_pos.mV[VY], 
		mSourceObject->getScale().mV[VZ] / icon_relative_pos.mV[VZ]);
	F32 up_distance = 0.5f * distance_scale;
	LLVector3 icon_position = obj_position + (up_distance * LLViewerCamera::getInstance()->getUpAxis()) * 1.2f;

	LLVector3 icon_to_cam = LLViewerCamera::getInstance()->getOrigin() - icon_position;
	icon_to_cam.normVec();

	icon_position += icon_to_cam * mSourceObject->mDrawable->getRadius() * 1.1f;

	mDistance = dist_vec(icon_position, LLViewerCamera::getInstance()->getOrigin());

	LLVector3 x_pixel_vec;
	LLVector3 y_pixel_vec;
	
	LLViewerCamera::getInstance()->getPixelVectors(icon_position, y_pixel_vec, x_pixel_vec);

	F32 scale_factor = 1.f;
	if (mAnimTimer.getElapsedTimeF32() < ANIM_TIME)
	{
		scale_factor = llmax(0.f, calc_bouncy_animation(mAnimTimer.getElapsedTimeF32() / ANIM_TIME));
	}

	F32 time_elapsed = mLifeTimer.getElapsedTimeF32();
	if (time_elapsed > MAX_VISIBLE_TIME)
	{
		markDead();
		return FALSE;
	}
	
	F32 image_aspect = (F32)mImagep->getFullWidth() / (F32)mImagep->getFullHeight() ;
	LLVector3 x_scale = image_aspect * (F32)gViewerWindow->getWindowHeightScaled() * mScale * scale_factor * x_pixel_vec;
	LLVector3 y_scale = (F32)gViewerWindow->getWindowHeightScaled() * mScale * scale_factor * y_pixel_vec;

	LLVector3 lower_left = icon_position - (x_scale * 0.5f);
	LLVector3 lower_right = icon_position + (x_scale * 0.5f);
	LLVector3 upper_left = icon_position - (x_scale * 0.5f) + y_scale;
	LLVector3 upper_right = icon_position + (x_scale * 0.5f) + y_scale;

	
	F32 t = 0.f;
	LLVector3 dir = end-start;

	if (LLTriangleRayIntersect(upper_right, upper_left, lower_right, start, dir, NULL, NULL, &t, FALSE) ||
		LLTriangleRayIntersect(upper_left, lower_left, lower_right, start, dir, NULL, NULL, &t, FALSE))
	{
		if (intersection)
		{
			*intersection = start + dir*t;
		}
		return TRUE;
	}

	return FALSE;
}

//static
S32 LLHUDIcon::generatePickIDs(S32 start_id, S32 step_size)
{
	S32 cur_id = start_id;
	icon_instance_t::iterator icon_it;

	for(icon_it = sIconInstances.begin(); icon_it != sIconInstances.end(); ++icon_it)
	{
		(*icon_it)->mPickID = cur_id;
		cur_id += step_size;
	}

	return cur_id;
}

//static
LLHUDIcon* LLHUDIcon::handlePick(S32 pick_id)
{
	icon_instance_t::iterator icon_it;

	for(icon_it = sIconInstances.begin(); icon_it != sIconInstances.end(); ++icon_it)
	{
		if (pick_id == (*icon_it)->mPickID)
		{
			return *icon_it;
		}
	}

	return NULL;
}

//static
LLHUDIcon* LLHUDIcon::lineSegmentIntersectAll(const LLVector3& start, const LLVector3& end, LLVector3* intersection)
{
	icon_instance_t::iterator icon_it;

	LLVector3 local_end = end;
	LLVector3 position;

	LLHUDIcon* ret = NULL;
	for(icon_it = sIconInstances.begin(); icon_it != sIconInstances.end(); ++icon_it)
	{
		LLHUDIcon* icon = *icon_it;
		if (icon->lineSegmentIntersect(start, local_end, &position))
		{
			ret = icon;
			if (intersection)
			{
				*intersection = position;
			}
			local_end = position;
		}
	}

	return ret;
}


 //static
void LLHUDIcon::updateAll()
{
	cleanupDeadIcons();
}

//static
BOOL LLHUDIcon::iconsNearby()
{
	return !sIconInstances.empty();
}

//static
void LLHUDIcon::cleanupDeadIcons()
{
	icon_instance_t::iterator icon_it;

	icon_instance_t icons_to_erase;
	for(icon_it = sIconInstances.begin(); icon_it != sIconInstances.end(); ++icon_it)
	{
		if ((*icon_it)->mDead)
		{
			icons_to_erase.push_back(*icon_it);
		}
	}

	for(icon_it = icons_to_erase.begin(); icon_it != icons_to_erase.end(); ++icon_it)
	{
		icon_instance_t::iterator found_it = std::find(sIconInstances.begin(), sIconInstances.end(), *icon_it);
		if (found_it != sIconInstances.end())
		{
			sIconInstances.erase(found_it);
		}
	}
}

//static
S32 LLHUDIcon::getNumInstances()
{
	return (S32)sIconInstances.size();
}