summaryrefslogtreecommitdiff
path: root/indra/newview/llhudeffectbeam.cpp
blob: b3e98ba5612abab690a591492ae1066eef6a9897 (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
/**
 * @file llhudeffectbeam.cpp
 * @brief LLHUDEffectBeam class implementation
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llhudeffectbeam.h"
#include "message.h"

#include "llviewerobjectlist.h"

#include "llagent.h"
#include "lldrawable.h"
#include "llfontgl.h"
#include "llgl.h"
#include "llglheaders.h"
#include "llhudrender.h"
#include "llrendersphere.h"
#include "llviewercamera.h"
#include "llvoavatar.h"
#include "llviewercontrol.h"

const F32 BEAM_SPACING = 0.075f;

LLHUDEffectBeam::LLHUDEffectBeam(const U8 type) : LLHUDEffect(type)
{
    mKillTime = mDuration;

    // Initialize all of these to defaults
    S32 i;
    for (i = 0; i < NUM_POINTS; i++)
    {
        mInterp[i].setStartTime(BEAM_SPACING*i);
        mInterp[i].setEndTime(BEAM_SPACING*NUM_POINTS + BEAM_SPACING*i);
        mInterp[i].start();
        mInterpFade[i].setStartTime(BEAM_SPACING*NUM_POINTS + BEAM_SPACING*i - 0.5f*NUM_POINTS*BEAM_SPACING);
        mInterpFade[i].setEndTime(BEAM_SPACING*NUM_POINTS + BEAM_SPACING*i);
        mInterpFade[i].setStartVal(1.f);
        mInterpFade[i].setEndVal(0.f);
    }

    // Setup default timeouts and fade animations.
    F32 fade_length;
    fade_length = llmin(0.5f, mDuration);
    mFadeInterp.setStartTime(mKillTime - fade_length);
    mFadeInterp.setEndTime(mKillTime);
    mFadeInterp.setStartVal(1.f);
    mFadeInterp.setEndVal(0.f);
}

LLHUDEffectBeam::~LLHUDEffectBeam()
{
}

void LLHUDEffectBeam::packData(LLMessageSystem *mesgsys)
{
    if (!mSourceObject)
    {
        LL_WARNS() << "Missing source object!" << LL_ENDL;
    }

    // Pack the default data
    LLHUDEffect::packData(mesgsys);

    // Pack the type-specific data.  Uses a fun packed binary format.  Whee!
    // 16 + 24 + 1 = 41
    U8 packed_data[41];
    memset(packed_data, 0, 41);
    if (mSourceObject)
    {
        htolememcpy(packed_data, mSourceObject->mID.mData, MVT_LLUUID, 16);
    }

    if (mTargetObject)
    {
        packed_data[16] = 1;
    }
    else
    {
        packed_data[16] = 0;
    }

    if (mTargetObject)
    {
        htolememcpy(&(packed_data[17]), mTargetObject->mID.mData, MVT_LLUUID, 16);
    }
    else
    {
        htolememcpy(&(packed_data[17]), mTargetPos.mdV, MVT_LLVector3d, 24);
    }
    mesgsys->addBinaryDataFast(_PREHASH_TypeData, packed_data, 41);
}

void LLHUDEffectBeam::unpackData(LLMessageSystem *mesgsys, S32 blocknum)
{
    LL_ERRS() << "Got beam!" << LL_ENDL;
    BOOL use_target_object;
    LLVector3d new_target;
    U8 packed_data[41];

    LLHUDEffect::unpackData(mesgsys, blocknum);
    LLUUID source_id;
    LLUUID target_id;
    S32 size = mesgsys->getSizeFast(_PREHASH_Effect, blocknum, _PREHASH_TypeData);
    if (size != 41)
    {
        LL_WARNS() << "Beam effect with bad size " << size << LL_ENDL;
        return;
    }
    mesgsys->getBinaryDataFast(_PREHASH_Effect, _PREHASH_TypeData, packed_data, 41, blocknum);

    htolememcpy(source_id.mData, packed_data, MVT_LLUUID, 16);

    LLViewerObject *objp = gObjectList.findObject(source_id);
    if (objp)
    {
        setSourceObject(objp);
    }

    use_target_object = packed_data[16];

    if (use_target_object)
    {
        htolememcpy(target_id.mData, &packed_data[17], MVT_LLUUID, 16);

        LLViewerObject *objp = gObjectList.findObject(target_id);
        if (objp)
        {
            setTargetObject(objp);
        }
    }
    else
    {
        htolememcpy(new_target.mdV, &(packed_data[17]), MVT_LLVector3d, 24);
        setTargetPos(new_target);
    }

    // We've received an update for the effect, update the various timeouts
    // and fade animations.
    mKillTime = mTimer.getElapsedTimeF32() + mDuration;
    F32 fade_length;
    fade_length = llmin(0.5f, mDuration);
    mFadeInterp.setStartTime(mKillTime - fade_length);
    mFadeInterp.setEndTime(mKillTime);
    mFadeInterp.setStartVal(1.f);
    mFadeInterp.setEndVal(0.f);
}

void LLHUDEffectBeam::setSourceObject(LLViewerObject *objp)
{
    if (objp->isDead())
    {
        LL_WARNS() << "HUDEffectBeam: Source object is dead!" << LL_ENDL;
        mSourceObject = NULL;
        return;
    }

    if (mSourceObject == objp)
    {
        return;
    }

    mSourceObject = objp;
    if (mSourceObject)
    {
        S32 i;
        for (i = 0; i < NUM_POINTS; i++)
        {
            if (mSourceObject->isAvatar())
            {
                LLViewerObject *objp = mSourceObject;
                LLVOAvatar *avatarp = (LLVOAvatar *)objp;
                LLVector3d hand_pos_global = gAgent.getPosGlobalFromAgent(avatarp->mWristLeftp->getWorldPosition());
                mInterp[i].setStartVal(hand_pos_global);
                mInterp[i].start();
            }
            else
            {
                mInterp[i].setStartVal(mSourceObject->getPositionGlobal());
                mInterp[i].start();
            }
        }
    }
}


void LLHUDEffectBeam::setTargetObject(LLViewerObject *objp)
{
    if (mTargetObject->isDead())
    {
        LL_WARNS() << "HUDEffectBeam: Target object is dead!" << LL_ENDL;
    }

    mTargetObject = objp;
}

void LLHUDEffectBeam::setTargetPos(const LLVector3d &pos_global)
{
    mTargetPos = pos_global;
    mTargetObject = NULL;
}

void LLHUDEffectBeam::render()
{
    if (!mSourceObject)
    {
        markDead();
        return;
    }
    if (mSourceObject->isDead())
    {
        markDead();
        return;
    }

    F32 time = mTimer.getElapsedTimeF32();

    // Kill us if our time is over...
    if (mKillTime < time)
    {
        markDead();
        return;
    }

    LLGLSPipelineAlpha gls_pipeline_alpha;
    gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);


    // Interpolate the global fade alpha
    mFadeInterp.update(time);

    if (mTargetObject.notNull() && mTargetObject->mDrawable.notNull())
    {
        // use viewer object position on freshly created objects
        if (mTargetObject->mDrawable->getGeneration() == -1)
        {
            mTargetPos = mTargetObject->getPositionGlobal();
        }
        // otherwise use drawable
        else
        {
            mTargetPos = gAgent.getPosGlobalFromAgent(mTargetObject->mDrawable->getPositionAgent());
        }
    }


    // Init the color of the particles
    LLColor4U coloru = mColor;

    // Draw the particles
    S32 i;
    for (i = 0; i < NUM_POINTS; i++)
    {
        mInterp[i].update(time);
        if (!mInterp[i].isActive())
        {
            continue;
        }
        mInterpFade[i].update(time);

        if (mInterp[i].isDone())
        {
            // Reinitialize the particle when the particle has finished its animation.
            setupParticle(i);
        }

        F32 frac = mInterp[i].getCurFrac();
        F32 scale = 0.025f + fabs(0.05f*sin(2.f*F_PI*(frac - time)));
        scale *= mInterpFade[i].getCurVal();

        LLVector3 pos_agent = gAgent.getPosAgentFromGlobal(mInterp[i].getCurVal());

        F32 alpha = mFadeInterp.getCurVal()*mColor.mV[3];
        alpha *= mInterpFade[i].getCurVal();
        coloru.mV[3] = (U8)alpha;
        gGL.color4ubv(coloru.mV);

        gGL.pushMatrix();
        gGL.translatef(pos_agent.mV[0], pos_agent.mV[1], pos_agent.mV[2]);
        gGL.scalef(scale, scale, scale);
        gSphere.render();
        gGL.popMatrix();
    }
}

void LLHUDEffectBeam::renderForTimer()
{
    render();
}

void LLHUDEffectBeam::setupParticle(const S32 i)
{
    LLVector3d start_pos_global;
    if (mSourceObject->getPCode() == LL_PCODE_LEGACY_AVATAR)
    {
        LLViewerObject *objp = mSourceObject;
        LLVOAvatar *avatarp = (LLVOAvatar *)objp;
        start_pos_global = gAgent.getPosGlobalFromAgent(avatarp->mWristLeftp->getWorldPosition());
    }
    else
    {
        start_pos_global = mSourceObject->getPositionGlobal();
    }

    // Generate a random offset for the target point.
    const F32 SCALE = 0.5f;
    F32 x, y, z;
    x = ll_frand(SCALE) - 0.5f*SCALE;
    y = ll_frand(SCALE) - 0.5f*SCALE;
    z = ll_frand(SCALE) - 0.5f*SCALE;

    LLVector3d target_pos_global(mTargetPos);
    target_pos_global += LLVector3d(x, y, z);

    mInterp[i].setStartTime(mInterp[i].getEndTime());
    mInterp[i].setEndTime(mInterp[i].getStartTime() + BEAM_SPACING*NUM_POINTS);
    mInterp[i].setStartVal(start_pos_global);
    mInterp[i].setEndVal(target_pos_global);
    mInterp[i].start();


    // Setup the interpolator that fades out the alpha.
    mInterpFade[i].setStartTime(mInterp[i].getStartTime() + BEAM_SPACING*NUM_POINTS - 0.5f*NUM_POINTS*BEAM_SPACING);
    mInterpFade[i].setEndTime(mInterp[i].getStartTime() + BEAM_SPACING*NUM_POINTS - 0.05f);
    mInterpFade[i].start();
}