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
|
/**
* @file llcubemap.cpp
* @brief LLCubeMap class implementation
*
* Copyright (c) 2002-2007, Linden Research, Inc.
*
* 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://secondlife.com/developers/opensource/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://secondlife.com/developers/opensource/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.
*/
#include "llviewerprecompiledheaders.h"
#include "llworkerthread.h"
#include "llcubemap.h"
#include "v4coloru.h"
#include "v3math.h"
#include "llviewercamera.h"
#include "llviewerimage.h"
#include "llviewerimagelist.h"
#include "llglheaders.h"
const F32 epsilon = 1e-7f;
const U16 RESOLUTION = 64;
#if LL_DARWIN
// mipmap generation on cubemap textures seems to be broken on the Mac for at least some cards.
// Since the cubemap is small (64x64 per face) and doesn't have any fine detail, turning off mipmaps is a usable workaround.
const BOOL use_cube_mipmaps = FALSE;
#else
const BOOL use_cube_mipmaps = FALSE; //current build works best without cube mipmaps
#endif
LLCubeMap::LLCubeMap()
: mTextureStage(0),
mMatrixStage(0)
{
}
LLCubeMap::~LLCubeMap()
{
}
void LLCubeMap::initGL()
{
llassert(gGLManager.mInited);
if (gGLManager.mHasCubeMap)
{
mTargets[0] = GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB;
mTargets[1] = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB;
mTargets[2] = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB;
mTargets[3] = GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB;
mTargets[4] = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB;
mTargets[5] = GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB;
// Not initialized, do stuff.
if (mImages[0].isNull())
{
GLuint texname = 0;
glGenTextures(1, &texname);
for (int i = 0; i < 6; i++)
{
mImages[i] = new LLImageGL(64, 64, 4, (use_cube_mipmaps? TRUE : FALSE));
mImages[i]->setTarget(mTargets[i], GL_TEXTURE_CUBE_MAP_ARB);
mRawImages[i] = new LLImageRaw(64, 64, 4);
mImages[i]->createGLTexture(0, mRawImages[i], texname);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, texname);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
stop_glerror();
}
}
disable();
}
else
{
llwarns << "Using cube map without extension!" << llendl
}
}
void LLCubeMap::initRawData(const std::vector<LLPointer<LLImageRaw> >& rawimages)
{
bool flip_x[6] = { false, true, false, false, true, false };
bool flip_y[6] = { true, true, true, false, true, true };
bool transpose[6] = { false, false, false, false, true, true };
// Yes, I know that this is inefficient! - djs 08/08/02
for (int i = 0; i < 6; i++)
{
const U8 *sd = rawimages[i]->getData();
U8 *td = mRawImages[i]->getData();
S32 offset = 0;
S32 sx, sy, so;
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
sx = x;
sy = y;
if (flip_y[i])
{
sy = 63 - y;
}
if (flip_x[i])
{
sx = 63 - x;
}
if (transpose[i])
{
S32 temp = sx;
sx = sy;
sy = temp;
}
so = 64*sy + sx;
so *= 4;
*(td + offset++) = *(sd + so++);
*(td + offset++) = *(sd + so++);
*(td + offset++) = *(sd + so++);
*(td + offset++) = *(sd + so++);
}
}
}
}
void LLCubeMap::initGLData()
{
for (int i = 0; i < 6; i++)
{
mImages[i]->setSubImage(mRawImages[i], 0, 0, 64, 64);
}
}
void LLCubeMap::init(const std::vector<LLPointer<LLImageRaw> >& rawimages)
{
if (!gGLManager.mIsDisabled)
{
initGL();
initRawData(rawimages);
initGLData();
}
}
void LLCubeMap::bind()
{
if (gGLManager.mHasCubeMap)
{
// We assume that if they have cube mapping, they have multitexturing.
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, mImages[0]->getTexName());
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, (use_cube_mipmaps? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
}
else
{
llwarns << "Using cube map without extension!" << llendl
}
}
void LLCubeMap::enable(S32 stage)
{
mTextureStage = stage;
if (gGLManager.mHasCubeMap && stage >= 0)
{
glActiveTextureARB(GL_TEXTURE0_ARB + stage); // NOTE: leaves texture stage set
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
}
}
void LLCubeMap::disable()
{
if (gGLManager.mHasCubeMap && mTextureStage >= 0)
{
glActiveTextureARB(GL_TEXTURE0_ARB + mTextureStage);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
}
}
void LLCubeMap::setMatrix(S32 stage)
{
mMatrixStage = stage;
glActiveTextureARB(GL_TEXTURE0_ARB+stage);
F32 mat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
LLVector3 x(mat);
LLVector3 y(mat+4);
LLVector3 z(mat+8);
LLMatrix3 mat3;
mat3.setRows(x,y,z);
LLMatrix4 trans(mat3);
trans.transpose();
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadMatrixf((F32 *)trans.mMatrix);
glMatrixMode(GL_MODELVIEW);
}
void LLCubeMap::restoreMatrix()
{
glActiveTextureARB(GL_TEXTURE0_ARB+mMatrixStage);
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
LLVector3 LLCubeMap::map(U8 side, U16 v_val, U16 h_val) const
{
LLVector3 dir;
const U8 curr_coef = side >> 1; // 0/1 = X axis, 2/3 = Y, 4/5 = Z
const S8 side_dir = (((side & 1) << 1) - 1); // even = -1, odd = 1
const U8 i_coef = (curr_coef + 1) % 3;
const U8 j_coef = (i_coef + 1) % 3;
dir.mV[curr_coef] = side_dir;
switch (side)
{
case 0: // negative X
dir.mV[i_coef] = -F32((v_val<<1) + 1) / RESOLUTION + 1;
dir.mV[j_coef] = F32((h_val<<1) + 1) / RESOLUTION - 1;
break;
case 1: // positive X
dir.mV[i_coef] = -F32((v_val<<1) + 1) / RESOLUTION + 1;
dir.mV[j_coef] = -F32((h_val<<1) + 1) / RESOLUTION + 1;
break;
case 2: // negative Y
dir.mV[i_coef] = -F32((v_val<<1) + 1) / RESOLUTION + 1;
dir.mV[j_coef] = F32((h_val<<1) + 1) / RESOLUTION - 1;
break;
case 3: // positive Y
dir.mV[i_coef] = F32((v_val<<1) + 1) / RESOLUTION - 1;
dir.mV[j_coef] = F32((h_val<<1) + 1) / RESOLUTION - 1;
break;
case 4: // negative Z
dir.mV[i_coef] = -F32((h_val<<1) + 1) / RESOLUTION + 1;
dir.mV[j_coef] = -F32((v_val<<1) + 1) / RESOLUTION + 1;
break;
case 5: // positive Z
dir.mV[i_coef] = -F32((h_val<<1) + 1) / RESOLUTION + 1;
dir.mV[j_coef] = F32((v_val<<1) + 1) / RESOLUTION - 1;
break;
default:
dir.mV[i_coef] = F32((v_val<<1) + 1) / RESOLUTION - 1;
dir.mV[j_coef] = F32((h_val<<1) + 1) / RESOLUTION - 1;
}
dir.normVec();
return dir;
}
BOOL LLCubeMap::project(F32& v_val, F32& h_val, BOOL& outside,
U8 side, const LLVector3& dir) const
{
const U8 curr_coef = side >> 1; // 0/1 = X axis, 2/3 = Y, 4/5 = Z
const S8 side_dir = (((side & 1) << 1) - 1); // even = -1, odd = 1
const U8 i_coef = (curr_coef + 1) % 3;
const U8 j_coef = (i_coef + 1) % 3;
outside = TRUE;
if (side_dir * dir.mV[curr_coef] < 0)
return FALSE;
LLVector3 ray;
F32 norm_val = fabs(dir.mV[curr_coef]);
if (norm_val < epsilon)
norm_val = 1e-5f;
ray.mV[curr_coef] = side_dir;
ray.mV[i_coef] = dir.mV[i_coef] / norm_val;
ray.mV[j_coef] = dir.mV[j_coef] / norm_val;
const F32 i_val = (ray.mV[i_coef] + 1) * 0.5f * RESOLUTION;
const F32 j_val = (ray.mV[j_coef] + 1) * 0.5f * RESOLUTION;
switch (side)
{
case 0: // negative X
v_val = RESOLUTION - i_val;
h_val = j_val;
break;
case 1: // positive X
v_val = RESOLUTION - i_val;
h_val = RESOLUTION - j_val;
break;
case 2: // negative Y
v_val = RESOLUTION - i_val;
h_val = j_val;
break;
case 3: // positive Y
v_val = i_val;
h_val = j_val;
break;
case 4: // negative Z
v_val = RESOLUTION - j_val;
h_val = RESOLUTION - i_val;
break;
case 5: // positive Z
v_val = RESOLUTION - j_val;
h_val = i_val;
break;
default:
v_val = i_val;
h_val = j_val;
}
outside = ((v_val < 0) || (v_val > RESOLUTION) ||
(h_val < 0) || (h_val > RESOLUTION));
return TRUE;
}
BOOL LLCubeMap::project(F32& v_min, F32& v_max, F32& h_min, F32& h_max,
U8 side, LLVector3 dir[4]) const
{
v_min = h_min = RESOLUTION;
v_max = h_max = 0;
BOOL fully_outside = TRUE;
for (U8 vtx = 0; vtx < 4; ++vtx)
{
F32 v_val, h_val;
BOOL outside;
BOOL consider = project(v_val, h_val, outside, side, dir[vtx]);
if (!outside)
fully_outside = FALSE;
if (consider)
{
if (v_val < v_min) v_min = v_val;
if (v_val > v_max) v_max = v_val;
if (h_val < h_min) h_min = h_val;
if (h_val > h_max) h_max = h_val;
}
}
v_min = llmax(0.0f, v_min);
v_max = llmin(RESOLUTION - epsilon, v_max);
h_min = llmax(0.0f, h_min);
h_max = llmin(RESOLUTION - epsilon, h_max);
return !fully_outside;
}
void LLCubeMap::paintIn(LLVector3 dir[4], const LLColor4U& col)
{
F32 v_min, v_max, h_min, h_max;
LLVector3 center = dir[0] + dir[1] + dir[2] + dir[3];
center.normVec();
for (U8 side = 0; side < 6; ++side)
{
if (!project(v_min, v_max, h_min, h_max, side, dir))
continue;
U8 *td = mRawImages[side]->getData();
U16 v_minu = (U16) v_min;
U16 v_maxu = (U16) (ceil(v_max) + 0.5);
U16 h_minu = (U16) h_min;
U16 h_maxu = (U16) (ceil(h_max) + 0.5);
for (U16 v = v_minu; v < v_maxu; ++v)
for (U16 h = h_minu; h < h_maxu; ++h)
//for (U16 v = 0; v < RESOLUTION; ++v)
// for (U16 h = 0; h < RESOLUTION; ++h)
{
const LLVector3 ray = map(side, v, h);
if (ray * center > 0.999)
{
const U32 offset = (RESOLUTION * v + h) * 4;
for (U8 cc = 0; cc < 3; ++cc)
td[offset + cc] = U8((td[offset + cc] + col.mV[cc]) * 0.5);
}
}
mImages[side]->setSubImage(mRawImages[side], 0, 0, 64, 64);
}
}
void LLCubeMap::destroyGL()
{
for (S32 i = 0; i < 6; i++)
{
mImages[i] = NULL;
}
}
|