summaryrefslogtreecommitdiff
path: root/indra/llprimitive/lltextureentry.cpp
blob: b534939dfc436d741731d62f4784df083ce0dc93 (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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/** 
 * @file lltextureentry.cpp
 * @brief LLTextureEntry base class
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-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 "linden_common.h"

#include "lluuid.h"
#include "llmediaentry.h"
#include "lltextureentry.h"
#include "llsdutil.h"
#include "v4color.h"

const U8 DEFAULT_BUMP_CODE = 0;  // no bump or shininess

const LLTextureEntry LLTextureEntry::null;

// Some LLSD keys.  Do not change these!
#define OBJECT_ID_KEY_STR "object_id"
#define TEXTURE_INDEX_KEY_STR "texture_index"
#define OBJECT_MEDIA_VERSION_KEY_STR "object_media_version"
#define OBJECT_MEDIA_DATA_KEY_STR "object_media_data"
#define TEXTURE_MEDIA_DATA_KEY_STR "media_data"

/*static*/ const char* LLTextureEntry::OBJECT_ID_KEY = OBJECT_ID_KEY_STR;
/*static*/ const char* LLTextureEntry::OBJECT_MEDIA_DATA_KEY = OBJECT_MEDIA_DATA_KEY_STR;
/*static*/ const char* LLTextureEntry::MEDIA_VERSION_KEY = OBJECT_MEDIA_VERSION_KEY_STR;
/*static*/ const char* LLTextureEntry::TEXTURE_INDEX_KEY = TEXTURE_INDEX_KEY_STR;
/*static*/ const char* LLTextureEntry::TEXTURE_MEDIA_DATA_KEY = TEXTURE_MEDIA_DATA_KEY_STR;

static const std::string MEDIA_VERSION_STRING_PREFIX = "x-mv:";

// static 
LLTextureEntry* LLTextureEntry::newTextureEntry()
{
	return new LLTextureEntry();
}

//===============================================================
LLTextureEntry::LLTextureEntry()
  : mMediaEntry(NULL)
{
	init(LLUUID::null,1.f,1.f,0.f,0.f,0.f,DEFAULT_BUMP_CODE);
}

LLTextureEntry::LLTextureEntry(const LLUUID& tex_id)
  : mMediaEntry(NULL)
{
	init(tex_id,1.f,1.f,0.f,0.f,0.f,DEFAULT_BUMP_CODE);
}

LLTextureEntry::LLTextureEntry(const LLTextureEntry &rhs)
  : mMediaEntry(NULL)
{
	mID = rhs.mID;
	mScaleS = rhs.mScaleS;
	mScaleT = rhs.mScaleT;
	mOffsetS = rhs.mOffsetS;
	mOffsetT = rhs.mOffsetT;
	mRotation = rhs.mRotation;
	mColor = rhs.mColor;
	mBump = rhs.mBump;
	mMediaFlags = rhs.mMediaFlags;
	mGlow = rhs.mGlow;
	if (rhs.mMediaEntry != NULL) {
		// Make a copy
		mMediaEntry = new LLMediaEntry(*rhs.mMediaEntry);
	}
}

LLTextureEntry &LLTextureEntry::operator=(const LLTextureEntry &rhs)
{
	if (this != &rhs)
	{
		mID = rhs.mID;
		mScaleS = rhs.mScaleS;
		mScaleT = rhs.mScaleT;
		mOffsetS = rhs.mOffsetS;
		mOffsetT = rhs.mOffsetT;
		mRotation = rhs.mRotation;
		mColor = rhs.mColor;
		mBump = rhs.mBump;
		mMediaFlags = rhs.mMediaFlags;
		mGlow = rhs.mGlow;
		if (mMediaEntry != NULL) {
			delete mMediaEntry;
		}
		if (rhs.mMediaEntry != NULL) {
			// Make a copy
			mMediaEntry = new LLMediaEntry(*rhs.mMediaEntry);
		}
		else {
			mMediaEntry = NULL;
		}
	}

	return *this;
}

void LLTextureEntry::init(const LLUUID& tex_id, F32 scale_s, F32 scale_t, F32 offset_s, F32 offset_t, F32 rotation, U8 bump)
{
	setID(tex_id);

	mScaleS = scale_s;
	mScaleT = scale_t;
	mOffsetS = offset_s;
	mOffsetT = offset_t;
	mRotation = rotation;
	mBump = bump;
	mMediaFlags = 0x0;
    mGlow = 0;
	
	setColor(LLColor4(1.f, 1.f, 1.f, 1.f));
	if (mMediaEntry != NULL) {
	    delete mMediaEntry;
	}
	mMediaEntry = NULL;
}

LLTextureEntry::~LLTextureEntry()
{
	if(mMediaEntry)
	{
		delete mMediaEntry;
		mMediaEntry = NULL;
	}
}

bool LLTextureEntry::operator!=(const LLTextureEntry &rhs) const
{
	if (mID != rhs.mID) return(true);
	if (mScaleS != rhs.mScaleS) return(true);
	if (mScaleT != rhs.mScaleT) return(true);
	if (mOffsetS != rhs.mOffsetS) return(true);
	if (mOffsetT != rhs.mOffsetT) return(true);
	if (mRotation != rhs.mRotation) return(true);
	if (mColor != rhs.mColor) return (true);
	if (mBump != rhs.mBump) return (true);
	if (mMediaFlags != rhs.mMediaFlags) return (true);
	if (mGlow != rhs.mGlow) return (true);
	return(false);
}

bool LLTextureEntry::operator==(const LLTextureEntry &rhs) const
{
	if (mID != rhs.mID) return(false);
	if (mScaleS != rhs.mScaleS) return(false);
	if (mScaleT != rhs.mScaleT) return(false);
	if (mOffsetS != rhs.mOffsetS) return(false);
	if (mOffsetT != rhs.mOffsetT) return(false);
	if (mRotation != rhs.mRotation) return(false);
	if (mColor != rhs.mColor) return (false);
	if (mBump != rhs.mBump) return (false);
	if (mMediaFlags != rhs.mMediaFlags) return false;
	if (mGlow != rhs.mGlow) return false;
	return(true);
}

LLSD LLTextureEntry::asLLSD() const
{
	LLSD sd;
	asLLSD(sd);
	return sd;
}

void LLTextureEntry::asLLSD(LLSD& sd) const
{
	sd["imageid"] = mID;
	sd["colors"] = ll_sd_from_color4(mColor);
	sd["scales"] = mScaleS;
	sd["scalet"] = mScaleT;
	sd["offsets"] = mOffsetS;
	sd["offsett"] = mOffsetT;
	sd["imagerot"] = mRotation;
	sd["bump"] = getBumpShiny();
	sd["fullbright"] = getFullbright();
	sd["media_flags"] = mMediaFlags;
	if (hasMedia()) {
		LLSD mediaData;
        if (NULL != getMediaData()) {
            getMediaData()->asLLSD(mediaData);
        }
		sd[TEXTURE_MEDIA_DATA_KEY] = mediaData;
	}
	sd["glow"] = mGlow;
}

bool LLTextureEntry::fromLLSD(const LLSD& sd)
{
	const char *w, *x;
	w = "imageid";
	if (sd.has(w))
	{
		setID( sd[w] );
	} else goto fail;
	w = "colors";
	if (sd.has(w))
	{
		setColor( ll_color4_from_sd(sd["colors"]) );
	} else goto fail;
	w = "scales";
	x = "scalet";
	if (sd.has(w) && sd.has(x))
	{
		setScale( (F32)sd[w].asReal(), (F32)sd[x].asReal() );
	} else goto fail;
	w = "offsets";
	x = "offsett";
	if (sd.has(w) && sd.has(x))
	{
		setOffset( (F32)sd[w].asReal(), (F32)sd[x].asReal() );
	} else goto fail;
	w = "imagerot";
	if (sd.has(w))
	{
		setRotation( (F32)sd[w].asReal() );
	} else goto fail;
	w = "bump";
	if (sd.has(w))
	{
		setBumpShiny( sd[w].asInteger() );
	} else goto fail;
	w = "fullbright";
	if (sd.has(w))
	{
		setFullbright( sd[w].asInteger() );
	} else goto fail;
	w = "media_flags";
	if (sd.has(w))
	{
		setMediaTexGen( sd[w].asInteger() );
	} else goto fail;
	// If the "has media" flag doesn't match the fact that 
	// media data exists, updateMediaData will "fix" it
	// by either clearing or setting the flag
	w = TEXTURE_MEDIA_DATA_KEY;
	if (hasMedia() != sd.has(w))
	{
		llwarns << "LLTextureEntry::fromLLSD: media_flags (" << hasMedia() <<
			") does not match presence of media_data (" << sd.has(w) << ").  Fixing." << llendl;
	}
	updateMediaData(sd[w]);

	w = "glow";
	if (sd.has(w))
	{
		setGlow((F32)sd[w].asReal() );
	}

	return true;
fail:
	return false;
}

// virtual 
// override this method for each derived class
LLTextureEntry* LLTextureEntry::newBlank() const
{
	return new LLTextureEntry();
}

// virtual 
LLTextureEntry* LLTextureEntry::newCopy() const
{
	return new LLTextureEntry(*this);
}

S32 LLTextureEntry::setID(const LLUUID &tex_id)
{
	if (mID != tex_id)
	{
		mID = tex_id;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setScale(F32 s, F32 t)
{
	S32 retval = 0;

	if (  (mScaleS != s)
		||(mScaleT != t))
	{
		mScaleS = s;
		mScaleT = t;

		retval = TEM_CHANGE_TEXTURE;
	}
	return retval;
}

S32 LLTextureEntry::setScaleS(F32 s)
{
	S32 retval = TEM_CHANGE_NONE;
	if (mScaleS != s)
	{
		mScaleS = s;
		retval = TEM_CHANGE_TEXTURE;
	}
	return retval;
}

S32 LLTextureEntry::setScaleT(F32 t)
{
	S32 retval = TEM_CHANGE_NONE;
	if (mScaleT != t)
	{
		mScaleT = t;
		retval = TEM_CHANGE_TEXTURE;
	}
	return retval;
}

S32 LLTextureEntry::setColor(const LLColor4 &color)
{
	if (mColor != color)
	{
		mColor = color;
		return TEM_CHANGE_COLOR;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setColor(const LLColor3 &color)
{
	if (mColor != color)
	{
		// This preserves alpha.
		mColor.setVec(color);
		return TEM_CHANGE_COLOR;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setAlpha(const F32 alpha)
{
	if (mColor.mV[VW] != alpha)
	{
		mColor.mV[VW] = alpha;
		return TEM_CHANGE_COLOR;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setOffset(F32 s, F32 t)
{
	S32 retval = 0;

	if (  (mOffsetS != s)
		||(mOffsetT != t))
	{
		mOffsetS = s;
		mOffsetT = t;

		retval = TEM_CHANGE_TEXTURE;
	}
	return retval;
}

S32 LLTextureEntry::setOffsetS(F32 s)
{
	S32 retval = 0;
	if (mOffsetS != s)
	{
		mOffsetS = s;
		retval = TEM_CHANGE_TEXTURE;
	}
	return retval;
}

S32 LLTextureEntry::setOffsetT(F32 t)
{
	S32 retval = 0;
	if (mOffsetT != t)
	{
		mOffsetT = t;
		retval = TEM_CHANGE_TEXTURE;
	}
	return retval;
}

S32 LLTextureEntry::setRotation(F32 theta)
{
	if (mRotation != theta)
	{
		mRotation = theta;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setBumpShinyFullbright(U8 bump)
{
	if (mBump != bump)
	{
		mBump = bump;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setMediaTexGen(U8 media)
{
	if (mMediaFlags != media)
	{
		mMediaFlags = media;

		// Special code for media handling
		if( hasMedia() && mMediaEntry == NULL)
		{
			mMediaEntry = new LLMediaEntry;
		}
        else if ( ! hasMedia() && mMediaEntry != NULL)
        {
            delete mMediaEntry;
            mMediaEntry = NULL;
        }

		return TEM_CHANGE_MEDIA;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setBumpmap(U8 bump)
{
	bump &= TEM_BUMP_MASK;
	if (getBumpmap() != bump)
	{
		mBump &= ~TEM_BUMP_MASK;
		mBump |= bump;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setFullbright(U8 fullbright)
{
	fullbright &= TEM_FULLBRIGHT_MASK;
	if (getFullbright() != fullbright)
	{
		mBump &= ~(TEM_FULLBRIGHT_MASK<<TEM_FULLBRIGHT_SHIFT);
		mBump |= fullbright << TEM_FULLBRIGHT_SHIFT;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setShiny(U8 shiny)
{
	shiny &= TEM_SHINY_MASK;
	if (getShiny() != shiny)
	{
		mBump &= ~(TEM_SHINY_MASK<<TEM_SHINY_SHIFT);
		mBump |= shiny << TEM_SHINY_SHIFT;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setBumpShiny(U8 bump_shiny)
{
	bump_shiny &= TEM_BUMP_SHINY_MASK;
	if (getBumpShiny() != bump_shiny)
	{
		mBump &= ~TEM_BUMP_SHINY_MASK;
		mBump |= bump_shiny;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setMediaFlags(U8 media_flags)
{
	media_flags &= TEM_MEDIA_MASK;
	if (getMediaFlags() != media_flags)
	{
		mMediaFlags &= ~TEM_MEDIA_MASK;
		mMediaFlags |= media_flags;
        
		// Special code for media handling
		if( hasMedia() && mMediaEntry == NULL)
		{
			mMediaEntry = new LLMediaEntry;
		}
        else if ( ! hasMedia() && mMediaEntry != NULL)
        {
            delete mMediaEntry;
            mMediaEntry = NULL;
        }
        
		return TEM_CHANGE_MEDIA;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setTexGen(U8 tex_gen)
{
	tex_gen &= TEM_TEX_GEN_MASK;
	if (getTexGen() != tex_gen)
	{
		mMediaFlags &= ~TEM_TEX_GEN_MASK;
		mMediaFlags |= tex_gen;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

S32 LLTextureEntry::setGlow(F32 glow)
{
	if (mGlow != glow)
	{
		mGlow = glow;
		return TEM_CHANGE_TEXTURE;
	}
	return TEM_CHANGE_NONE;
}

void LLTextureEntry::setMediaData(const LLMediaEntry &media_entry)
{
    mMediaFlags |= MF_HAS_MEDIA;
    if (NULL != mMediaEntry)
    {
        delete mMediaEntry;
    }
    mMediaEntry = new LLMediaEntry(media_entry);
}

bool LLTextureEntry::updateMediaData(const LLSD& media_data)
{
	if (media_data.isUndefined())
	{
		// clear the media data
        clearMediaData();
		return false;
	}
	else {
		mMediaFlags |= MF_HAS_MEDIA;
		if (mMediaEntry == NULL)
		{
			mMediaEntry = new LLMediaEntry;
		}
        // *NOTE: this will *clobber* all of the fields in mMediaEntry 
        // with whatever fields are present (or not present) in media_data!
 		mMediaEntry->fromLLSD(media_data);
		return true;
	}
}

void LLTextureEntry::clearMediaData()
{
    mMediaFlags &= ~MF_HAS_MEDIA;
    if (mMediaEntry != NULL) {
        delete mMediaEntry;
    }
    mMediaEntry = NULL;
}    

void LLTextureEntry::mergeIntoMediaData(const LLSD& media_fields)
{
    mMediaFlags |= MF_HAS_MEDIA;
    if (mMediaEntry == NULL)
    {
        mMediaEntry = new LLMediaEntry;
    }
    // *NOTE: this will *merge* the data in media_fields
    // with the data in our media entry
    mMediaEntry->mergeFromLLSD(media_fields);
}

//static
std::string LLTextureEntry::touchMediaVersionString(const std::string &in_version, const LLUUID &agent_id)
{
    // XXX TODO: make media version string binary (base64-encoded?)
    // Media "URL" is a representation of a version and the last-touched agent
    // x-mv:nnnnn/agent-id
    // where "nnnnn" is version number
    // *NOTE: not the most efficient code in the world...
    U32 current_version = getVersionFromMediaVersionString(in_version) + 1;
    const size_t MAX_VERSION_LEN = 10; // 2^32 fits in 10 decimal digits
    char buf[MAX_VERSION_LEN+1];
    snprintf(buf, (int)MAX_VERSION_LEN+1, "%0*u", (int)MAX_VERSION_LEN, current_version);  // added int cast to fix warning/breakage on mac.
    return MEDIA_VERSION_STRING_PREFIX + buf + "/" + agent_id.asString();
}

//static
U32 LLTextureEntry::getVersionFromMediaVersionString(const std::string &version_string)
{
    U32 version = 0;
    if (!version_string.empty()) 
    {
        size_t found = version_string.find(MEDIA_VERSION_STRING_PREFIX);
        if (found != std::string::npos) 
        {
            found = version_string.find_first_of("/", found);
            std::string v = version_string.substr(MEDIA_VERSION_STRING_PREFIX.length(), found);
            version = strtoul(v.c_str(),NULL,10);
        }
    }
    return version;
}

//static
LLUUID LLTextureEntry::getAgentIDFromMediaVersionString(const std::string &version_string)
{
    LLUUID id;
    if (!version_string.empty()) 
    {
        size_t found = version_string.find(MEDIA_VERSION_STRING_PREFIX);
        if (found != std::string::npos) 
        {
            found = version_string.find_first_of("/", found);
            if (found != std::string::npos) 
            {
                std::string v = version_string.substr(found + 1);
                id.set(v);
            }
        }
    }
    return id;
}