summaryrefslogtreecommitdiff
path: root/indra/llprimitive/llmaterial.cpp
blob: fa22145972207a8a81e794d71d08cc7bc99ec650 (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
/**
 * @file llmaterial.cpp
 * @brief Material definition
 *
 * $LicenseInfo:firstyear=2006&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 "linden_common.h"

#include "llmaterial.h"

/**
 * Materials cap parameters
 */
#define MATERIALS_CAP_NORMAL_MAP_FIELD            "NormMap"
#define MATERIALS_CAP_NORMAL_MAP_OFFSET_X_FIELD   "NormOffsetX"
#define MATERIALS_CAP_NORMAL_MAP_OFFSET_Y_FIELD   "NormOffsetY"
#define MATERIALS_CAP_NORMAL_MAP_REPEAT_X_FIELD   "NormRepeatX"
#define MATERIALS_CAP_NORMAL_MAP_REPEAT_Y_FIELD   "NormRepeatY"
#define MATERIALS_CAP_NORMAL_MAP_ROTATION_FIELD   "NormRotation"

#define MATERIALS_CAP_SPECULAR_MAP_FIELD          "SpecMap"
#define MATERIALS_CAP_SPECULAR_MAP_OFFSET_X_FIELD "SpecOffsetX"
#define MATERIALS_CAP_SPECULAR_MAP_OFFSET_Y_FIELD "SpecOffsetY"
#define MATERIALS_CAP_SPECULAR_MAP_REPEAT_X_FIELD "SpecRepeatX"
#define MATERIALS_CAP_SPECULAR_MAP_REPEAT_Y_FIELD "SpecRepeatY"
#define MATERIALS_CAP_SPECULAR_MAP_ROTATION_FIELD "SpecRotation"

#define MATERIALS_CAP_SPECULAR_COLOR_FIELD        "SpecColor"
#define MATERIALS_CAP_SPECULAR_EXP_FIELD          "SpecExp"
#define MATERIALS_CAP_ENV_INTENSITY_FIELD         "EnvIntensity"
#define MATERIALS_CAP_ALPHA_MASK_CUTOFF_FIELD     "AlphaMaskCutoff"
#define MATERIALS_CAP_DIFFUSE_ALPHA_MODE_FIELD    "DiffuseAlphaMode"

const LLColor4U LLMaterial::DEFAULT_SPECULAR_LIGHT_COLOR(255,255,255,255);

/**
 * Materials constants
 */

const F32 MATERIALS_MULTIPLIER                   = 10000.f;

/**
 * Helper functions
 */

template<typename T> T getMaterialField(const LLSD& data, const std::string& field, const LLSD::Type field_type)
{
    if ( (data.has(field)) && (field_type == data[field].type()) )
    {
        return (T)data[field];
    }
    LL_ERRS() << "Missing or mistyped field '" << field << "' in material definition" << LL_ENDL;
    return (T)LLSD();
}

// GCC didn't like the generic form above for some reason
template<> LLUUID getMaterialField(const LLSD& data, const std::string& field, const LLSD::Type field_type)
{
    if ( (data.has(field)) && (field_type == data[field].type()) )
    {
        return data[field].asUUID();
    }
    LL_ERRS() << "Missing or mistyped field '" << field << "' in material definition" << LL_ENDL;
    return LLUUID::null;
}

/**
 * LLMaterial class
 */

const LLMaterial LLMaterial::null;

LLMaterial::LLMaterial()
    : mNormalOffsetX(0.0f)
    , mNormalOffsetY(0.0f)
    , mNormalRepeatX(1.0f)
    , mNormalRepeatY(1.0f)
    , mNormalRotation(0.0f)
    , mSpecularOffsetX(0.0f)
    , mSpecularOffsetY(0.0f)
    , mSpecularRepeatX(1.0f)
    , mSpecularRepeatY(1.0f)
    , mSpecularRotation(0.0f)
    , mSpecularLightColor(LLMaterial::DEFAULT_SPECULAR_LIGHT_COLOR)
    , mSpecularLightExponent(LLMaterial::DEFAULT_SPECULAR_LIGHT_EXPONENT)
    , mEnvironmentIntensity(LLMaterial::DEFAULT_ENV_INTENSITY)
    , mDiffuseAlphaMode(LLMaterial::DIFFUSE_ALPHA_MODE_BLEND)
    , mAlphaMaskCutoff(0)
{
}

LLMaterial::LLMaterial(const LLSD& material_data)
{
    fromLLSD(material_data);
}

const LLUUID& LLMaterial::getNormalID() const
{
    return mNormalID;
}

void LLMaterial::setNormalID(const LLUUID& normal_id)
{
    mNormalID = normal_id;
}

void LLMaterial::getNormalOffset(F32& offset_x, F32& offset_y) const
{
    offset_x = mNormalOffsetX;
    offset_y = mNormalOffsetY;
}

F32 LLMaterial::getNormalOffsetX() const
{
    return mNormalOffsetX;
}

F32 LLMaterial::getNormalOffsetY() const
{
    return mNormalOffsetY;
}

void LLMaterial::setNormalOffset(F32 offset_x, F32 offset_y)
{
    mNormalOffsetX = offset_x;
    mNormalOffsetY = offset_y;
}

void LLMaterial::setNormalOffsetX(F32 offset_x)
{
    mNormalOffsetX = offset_x;
}

void LLMaterial::setNormalOffsetY(F32 offset_y)
{
    mNormalOffsetY = offset_y;
}

void LLMaterial::getNormalRepeat(F32& repeat_x, F32& repeat_y) const
{
    repeat_x = mNormalRepeatX;
    repeat_y = mNormalRepeatY;
}

F32 LLMaterial::getNormalRepeatX() const
{
    return mNormalRepeatX;
}

F32 LLMaterial::getNormalRepeatY() const
{
    return mNormalRepeatY;
}

void LLMaterial::setNormalRepeat(F32 repeat_x, F32 repeat_y)
{
    mNormalRepeatX = repeat_x;
    mNormalRepeatY = repeat_y;
}

void LLMaterial::setNormalRepeatX(F32 repeat_x)
{
    mNormalRepeatX = repeat_x;
}

void LLMaterial::setNormalRepeatY(F32 repeat_y)
{
    mNormalRepeatY = repeat_y;
}

F32 LLMaterial::getNormalRotation() const
{
    return mNormalRotation;
}

void LLMaterial::setNormalRotation(F32 rot)
{
    mNormalRotation = rot;
}

const LLUUID& LLMaterial::getSpecularID() const
{
    return mSpecularID;
}

void LLMaterial::setSpecularID(const LLUUID& specular_id)
{
    mSpecularID = specular_id;
}

void LLMaterial::getSpecularOffset(F32& offset_x, F32& offset_y) const
{
    offset_x = mSpecularOffsetX;
    offset_y = mSpecularOffsetY;
}

F32 LLMaterial::getSpecularOffsetX() const
{
    return mSpecularOffsetX;
}

F32 LLMaterial::getSpecularOffsetY() const
{
    return mSpecularOffsetY;
}

void LLMaterial::setSpecularOffset(F32 offset_x, F32 offset_y)
{
    mSpecularOffsetX = offset_x;
    mSpecularOffsetY = offset_y;
}

void LLMaterial::setSpecularOffsetX(F32 offset_x)
{
    mSpecularOffsetX = offset_x;
}

void LLMaterial::setSpecularOffsetY(F32 offset_y)
{
    mSpecularOffsetY = offset_y;
}

void LLMaterial::getSpecularRepeat(F32& repeat_x, F32& repeat_y) const
{
    repeat_x = mSpecularRepeatX;
    repeat_y = mSpecularRepeatY;
}

F32 LLMaterial::getSpecularRepeatX() const
{
    return mSpecularRepeatX;
}

F32 LLMaterial::getSpecularRepeatY() const
{
    return mSpecularRepeatY;
}

void LLMaterial::setSpecularRepeat(F32 repeat_x, F32 repeat_y)
{
    mSpecularRepeatX = repeat_x; mSpecularRepeatY = repeat_y;
}

void LLMaterial::setSpecularRepeatX(F32 repeat_x)
{
    mSpecularRepeatX = repeat_x;
}

void LLMaterial::setSpecularRepeatY(F32 repeat_y)
{
    mSpecularRepeatY = repeat_y;
}

F32 LLMaterial::getSpecularRotation() const
{
    return mSpecularRotation;
}

void LLMaterial::setSpecularRotation(F32 rot)
{
    mSpecularRotation = rot;
}

const LLColor4U LLMaterial::getSpecularLightColor() const
{
    return mSpecularLightColor;
}

void LLMaterial::setSpecularLightColor(const LLColor4U& color)
{
    mSpecularLightColor = color;
}

U8 LLMaterial::getSpecularLightExponent() const
{
    return mSpecularLightExponent;
}

void LLMaterial::setSpecularLightExponent(U8 exponent)
{
    mSpecularLightExponent = exponent;
}

U8 LLMaterial::getEnvironmentIntensity() const
{
    return mEnvironmentIntensity;
}

void LLMaterial::setEnvironmentIntensity(U8 intensity)
{
    mEnvironmentIntensity = intensity;
}

U8 LLMaterial::getDiffuseAlphaMode() const
{
    return mDiffuseAlphaMode;
}

void LLMaterial::setDiffuseAlphaMode(U8 alpha_mode)
{
    mDiffuseAlphaMode = alpha_mode;
}

U8 LLMaterial::getAlphaMaskCutoff() const
{
    return mAlphaMaskCutoff;
}

void LLMaterial::setAlphaMaskCutoff(U8 cutoff)
{
    mAlphaMaskCutoff = cutoff;
}

LLSD LLMaterial::asLLSD() const
{
    LLSD material_data;

    S32 normalOffsetXInt = ll_round(mNormalOffsetX  * MATERIALS_MULTIPLIER);
    S32 normalOffsetYInt = ll_round(mNormalOffsetY  * MATERIALS_MULTIPLIER);
    S32 normalRotInt     = ll_round(mNormalRotation * MATERIALS_MULTIPLIER);

    material_data[MATERIALS_CAP_NORMAL_MAP_FIELD] = mNormalID;
    material_data[MATERIALS_CAP_NORMAL_MAP_OFFSET_X_FIELD] = normalOffsetXInt;
    material_data[MATERIALS_CAP_NORMAL_MAP_OFFSET_Y_FIELD] = normalOffsetYInt;
    material_data[MATERIALS_CAP_NORMAL_MAP_REPEAT_X_FIELD] = ll_round(mNormalRepeatX * MATERIALS_MULTIPLIER);
    material_data[MATERIALS_CAP_NORMAL_MAP_REPEAT_Y_FIELD] = ll_round(mNormalRepeatY * MATERIALS_MULTIPLIER);
    material_data[MATERIALS_CAP_NORMAL_MAP_ROTATION_FIELD] = normalRotInt;

    material_data[MATERIALS_CAP_SPECULAR_MAP_FIELD] = mSpecularID;

    S32 specularOffsetXInt = ll_round(mSpecularOffsetX  * MATERIALS_MULTIPLIER);
    S32 specularOffsetYInt = ll_round(mSpecularOffsetY  * MATERIALS_MULTIPLIER);
    S32 specularRotInt     = ll_round(mSpecularRotation * MATERIALS_MULTIPLIER);

    material_data[MATERIALS_CAP_SPECULAR_MAP_OFFSET_X_FIELD] = specularOffsetXInt;
    material_data[MATERIALS_CAP_SPECULAR_MAP_OFFSET_Y_FIELD] = specularOffsetYInt;
    material_data[MATERIALS_CAP_SPECULAR_MAP_REPEAT_X_FIELD] = ll_round(mSpecularRepeatX * MATERIALS_MULTIPLIER);
    material_data[MATERIALS_CAP_SPECULAR_MAP_REPEAT_Y_FIELD] = ll_round(mSpecularRepeatY * MATERIALS_MULTIPLIER);
    material_data[MATERIALS_CAP_SPECULAR_MAP_ROTATION_FIELD] = specularRotInt;

    material_data[MATERIALS_CAP_SPECULAR_COLOR_FIELD]     = mSpecularLightColor.getValue();
    material_data[MATERIALS_CAP_SPECULAR_EXP_FIELD]       = mSpecularLightExponent;
    material_data[MATERIALS_CAP_ENV_INTENSITY_FIELD]      = mEnvironmentIntensity;
    material_data[MATERIALS_CAP_DIFFUSE_ALPHA_MODE_FIELD] = mDiffuseAlphaMode;
    material_data[MATERIALS_CAP_ALPHA_MASK_CUTOFF_FIELD]  = mAlphaMaskCutoff;

    return material_data;
}

void LLMaterial::fromLLSD(const LLSD& material_data)
{
    mNormalID = getMaterialField<LLSD::UUID>(material_data, MATERIALS_CAP_NORMAL_MAP_FIELD, LLSD::TypeUUID);

    S32 normalOffsetXInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_NORMAL_MAP_OFFSET_X_FIELD, LLSD::TypeInteger);
    S32 normalOffsetYInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_NORMAL_MAP_OFFSET_Y_FIELD, LLSD::TypeInteger);
    S32 normalRotInt     = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_NORMAL_MAP_ROTATION_FIELD, LLSD::TypeInteger);
    S32 normalRepeatXInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_NORMAL_MAP_REPEAT_X_FIELD, LLSD::TypeInteger);
    S32 normalRepeatYInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_NORMAL_MAP_REPEAT_Y_FIELD, LLSD::TypeInteger);

    mNormalOffsetX  = F32(normalOffsetXInt) / MATERIALS_MULTIPLIER;
    mNormalOffsetY  = F32(normalOffsetYInt) / MATERIALS_MULTIPLIER;
    mNormalRotation = F32(normalRotInt)     / MATERIALS_MULTIPLIER;
    mNormalRepeatX  = F32(normalRepeatXInt) / MATERIALS_MULTIPLIER;
    mNormalRepeatY  = F32(normalRepeatYInt) / MATERIALS_MULTIPLIER;

    mSpecularID = getMaterialField<LLSD::UUID>(material_data, MATERIALS_CAP_SPECULAR_MAP_FIELD, LLSD::TypeUUID);

    S32 specularOffsetXInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_SPECULAR_MAP_OFFSET_X_FIELD, LLSD::TypeInteger);
    S32 specularOffsetYInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_SPECULAR_MAP_OFFSET_Y_FIELD, LLSD::TypeInteger);
    S32 specularRotInt     = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_SPECULAR_MAP_ROTATION_FIELD, LLSD::TypeInteger);
    S32 specularRepeatXInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_SPECULAR_MAP_REPEAT_X_FIELD, LLSD::TypeInteger);
    S32 specularRepeatYInt = getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_SPECULAR_MAP_REPEAT_Y_FIELD, LLSD::TypeInteger);

    mSpecularOffsetX   = F32(specularOffsetXInt) / MATERIALS_MULTIPLIER;
    mSpecularOffsetY   = F32(specularOffsetYInt) / MATERIALS_MULTIPLIER;
    mSpecularRotation  = F32(specularRotInt)     / MATERIALS_MULTIPLIER;
    mSpecularRepeatX  = F32(specularRepeatXInt) / MATERIALS_MULTIPLIER;
    mSpecularRepeatY  = F32(specularRepeatYInt) / MATERIALS_MULTIPLIER;

    mSpecularLightColor.setValue(getMaterialField<LLSD>(material_data, MATERIALS_CAP_SPECULAR_COLOR_FIELD, LLSD::TypeArray));
    mSpecularLightExponent = (U8)getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_SPECULAR_EXP_FIELD,       LLSD::TypeInteger);
    mEnvironmentIntensity  = (U8)getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_ENV_INTENSITY_FIELD,      LLSD::TypeInteger);
    mDiffuseAlphaMode      = (U8)getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_DIFFUSE_ALPHA_MODE_FIELD, LLSD::TypeInteger);
    mAlphaMaskCutoff       = (U8)getMaterialField<LLSD::Integer>(material_data, MATERIALS_CAP_ALPHA_MASK_CUTOFF_FIELD,  LLSD::TypeInteger);
}

bool LLMaterial::isNull() const
{
    return (*this == null);
}

bool LLMaterial::operator == (const LLMaterial& rhs) const
{
    return 
        (mNormalID == rhs.mNormalID) && (mNormalOffsetX == rhs.mNormalOffsetX) && (mNormalOffsetY == rhs.mNormalOffsetY) &&
        (mNormalRepeatX == rhs.mNormalRepeatX) && (mNormalRepeatY == rhs.mNormalRepeatY) && (mNormalRotation == rhs.mNormalRotation) &&
        (mSpecularID == rhs.mSpecularID) && (mSpecularOffsetX == rhs.mSpecularOffsetX) && (mSpecularOffsetY == rhs.mSpecularOffsetY) &&
        (mSpecularRepeatX == rhs.mSpecularRepeatX) && (mSpecularRepeatY == rhs.mSpecularRepeatY) && (mSpecularRotation == rhs.mSpecularRotation) &&
        (mSpecularLightColor == rhs.mSpecularLightColor) && (mSpecularLightExponent == rhs.mSpecularLightExponent) &&
        (mEnvironmentIntensity == rhs.mEnvironmentIntensity) && (mDiffuseAlphaMode == rhs.mDiffuseAlphaMode) && (mAlphaMaskCutoff == rhs.mAlphaMaskCutoff);
}

bool LLMaterial::operator != (const LLMaterial& rhs) const
{
    return !(*this == rhs);
}


U32 LLMaterial::getShaderMask(U32 alpha_mode, BOOL is_alpha)
{ //NEVER incorporate this value into the message system -- this function will vary depending on viewer implementation

	//two least significant bits are "diffuse alpha mode"
	U32 ret = alpha_mode;
    if (ret == DIFFUSE_ALPHA_MODE_DEFAULT)
    {
		ret = getDiffuseAlphaMode();
		if (ret == DIFFUSE_ALPHA_MODE_BLEND && !is_alpha)
		{
			ret = DIFFUSE_ALPHA_MODE_NONE;
		}
    }

    llassert(ret < SHADER_COUNT);

    //next bit is whether or not specular map is present
    const U32 SPEC_BIT = 0x4;

    if (getSpecularID().notNull())
    {
        ret |= SPEC_BIT;
    }

    llassert(ret < SHADER_COUNT);
    
    //next bit is whether or not normal map is present
    const U32 NORM_BIT = 0x8;
    if (getNormalID().notNull())
    {
        ret |= NORM_BIT;
    }

    llassert(ret < SHADER_COUNT);

    return ret;
}