Debian Codegraph @

code/renderergl2/tr_image.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // tr_image.c
23 #include "tr_local.h"
24
25 #include "tr_dsa.h"
26
27 static byte s_intensitytable[256];
28 static unsigned char s_gammatable[256];
29
30 int gl_filter_min = GL_LINEAR_MIPMAP_NEAREST;
31 int gl_filter_max = GL_LINEAR;
32
33 #define FILE_HASH_SIZE 1024
34 static image_t* hashTable[FILE_HASH_SIZE];
35
36 /*
37 ** R_GammaCorrect
38 */
39 void R_GammaCorrect( byte *buffer, int bufSize ) {
40 int i;
41
42 for ( i = 0; i < bufSize; i++ ) {
43 buffer[i] = s_gammatable[buffer[i]];
44 }
45 }
46
47 typedef struct {
48 char *name;
49 int minimize, maximize;
50 } textureMode_t;
51
52 textureMode_t modes[] = {
53 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
54 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
55 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
56 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
57 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
58 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
59 };
60
61 /*
62 ================
63 return a hash value for the filename
64 ================
65 */
66 static long generateHashValue( const char *fname ) {
67 int i;
68 long hash;
69 char letter;
70
71 hash = 0;
72 i = 0;
73 while (fname[i] != '\0') {
74 letter = tolower(fname[i]);
75 if (letter =='.') break; // don't include extension
76 if (letter =='\\') letter = '/'; // damn path names
77 hash+=(long)(letter)*(i+119);
78 i++;
79 }
80 hash &= (FILE_HASH_SIZE-1);
81 return hash;
82 }
83
84 /*
85 ===============
86 GL_TextureMode
87 ===============
88 */
89 void GL_TextureMode( const char *string ) {
90 int i;
91 image_t *glt;
92
93 for ( i=0 ; i< 6 ; i++ ) {
94 if ( !Q_stricmp( modes[i].name, string ) ) {
95 break;
96 }
97 }
98
99 // hack to prevent trilinear from being set on voodoo,
100 // because their driver freaks...
101 if ( i == 5 && glConfig.hardwareType == GLHW_3DFX_2D3D ) {
102 ri.Printf( PRINT_ALL, "Refusing to set trilinear on a voodoo.\n" );
103 i = 3;
104 }
105
106
107 if ( i == 6 ) {
108 ri.Printf (PRINT_ALL, "bad filter name\n");
109 return;
110 }
111
112 gl_filter_min = modes[i].minimize;
113 gl_filter_max = modes[i].maximize;
114
115 // change all the existing mipmap texture objects
116 for ( i = 0 ; i < tr.numImages ; i++ ) {
117 glt = tr.images[ i ];
118 if ( glt->flags & IMGFLAG_MIPMAP && !(glt->flags & IMGFLAG_CUBEMAP)) {
119 qglTextureParameterfEXT(glt->texnum, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
120 qglTextureParameterfEXT(glt->texnum, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
121 }
122 }
123 }
124
125 /*
126 ===============
127 R_SumOfUsedImages
128 ===============
129 */
130 int R_SumOfUsedImages( void ) {
131 int total;
132 int i;
133
134 total = 0;
135 for ( i = 0; i < tr.numImages; i++ ) {
136 if ( tr.images[i]->frameUsed == tr.frameCount ) {
137 total += tr.images[i]->uploadWidth * tr.images[i]->uploadHeight;
138 }
139 }
140
141 return total;
142 }
143
144 /*
145 ===============
146 R_ImageList_f
147 ===============
148 */
149 void R_ImageList_f( void ) {
150 int i;
151 int estTotalSize = 0;
152
153 ri.Printf(PRINT_ALL, "\n -w-- -h-- -type-- -size- --name-------\n");
154
155 for ( i = 0 ; i < tr.numImages ; i++ )
156 {
157 image_t *image = tr.images[i];
158 char *format = "???? ";
159 char *sizeSuffix;
160 int estSize;
161 int displaySize;
162
163 estSize = image->uploadHeight * image->uploadWidth;
164
165 switch(image->internalFormat)
166 {
167 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
168 format = "sDXT1 ";
169 // 64 bits per 16 pixels, so 4 bits per pixel
170 estSize /= 2;
171 break;
172 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
173 format = "sDXT5 ";
174 // 128 bits per 16 pixels, so 1 byte per pixel
175 break;
176 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
177 format = "sBPTC ";
178 // 128 bits per 16 pixels, so 1 byte per pixel
179 break;
180 case GL_COMPRESSED_RG_RGTC2:
181 format = "RGTC2 ";
182 // 128 bits per 16 pixels, so 1 byte per pixel
183 break;
184 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
185 format = "DXT1 ";
186 // 64 bits per 16 pixels, so 4 bits per pixel
187 estSize /= 2;
188 break;
189 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
190 format = "DXT1a ";
191 // 64 bits per 16 pixels, so 4 bits per pixel
192 estSize /= 2;
193 break;
194 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
195 format = "DXT5 ";
196 // 128 bits per 16 pixels, so 1 byte per pixel
197 break;
198 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
199 format = "BPTC ";
200 // 128 bits per 16 pixels, so 1 byte per pixel
201 break;
202 case GL_RGB4_S3TC:
203 format = "S3TC ";
204 // same as DXT1?
205 estSize /= 2;
206 break;
207 case GL_RGBA16F:
208 format = "RGBA16F";
209 // 8 bytes per pixel
210 estSize *= 8;
211 break;
212 case GL_RGBA16:
213 format = "RGBA16 ";
214 // 8 bytes per pixel
215 estSize *= 8;
216 break;
217 case GL_RGBA4:
218 case GL_RGBA8:
219 case GL_RGBA:
220 format = "RGBA ";
221 // 4 bytes per pixel
222 estSize *= 4;
223 break;
224 case GL_LUMINANCE8:
225 case GL_LUMINANCE:
226 format = "L ";
227 // 1 byte per pixel?
228 break;
229 case GL_RGB5:
230 case GL_RGB8:
231 case GL_RGB:
232 format = "RGB ";
233 // 3 bytes per pixel?
234 estSize *= 3;
235 break;
236 case GL_LUMINANCE8_ALPHA8:
237 case GL_LUMINANCE_ALPHA:
238 format = "LA ";
239 // 2 bytes per pixel?
240 estSize *= 2;
241 break;
242 case GL_SRGB_EXT:
243 case GL_SRGB8_EXT:
244 format = "sRGB ";
245 // 3 bytes per pixel?
246 estSize *= 3;
247 break;
248 case GL_SRGB_ALPHA_EXT:
249 case GL_SRGB8_ALPHA8_EXT:
250 format = "sRGBA ";
251 // 4 bytes per pixel?
252 estSize *= 4;
253 break;
254 case GL_SLUMINANCE_EXT:
255 case GL_SLUMINANCE8_EXT:
256 format = "sL ";
257 // 1 byte per pixel?
258 break;
259 case GL_SLUMINANCE_ALPHA_EXT:
260 case GL_SLUMINANCE8_ALPHA8_EXT:
261 format = "sLA ";
262 // 2 byte per pixel?
263 estSize *= 2;
264 break;
265 case GL_DEPTH_COMPONENT16:
266 format = "Depth16";
267 // 2 bytes per pixel
268 estSize *= 2;
269 break;
270 case GL_DEPTH_COMPONENT24:
271 format = "Depth24";
272 // 3 bytes per pixel
273 estSize *= 3;
274 break;
275 case GL_DEPTH_COMPONENT:
276 case GL_DEPTH_COMPONENT32:
277 format = "Depth32";
278 // 4 bytes per pixel
279 estSize *= 4;
280 break;
281 }
282
283 // mipmap adds about 50%
284 if (image->flags & IMGFLAG_MIPMAP)
285 estSize += estSize / 2;
286
287 sizeSuffix = "b ";
288 displaySize = estSize;
289
290 if (displaySize > 1024)
291 {
292 displaySize /= 1024;
293 sizeSuffix = "kb";
294 }
295
296 if (displaySize > 1024)
297 {
298 displaySize /= 1024;
299 sizeSuffix = "Mb";
300 }
301
302 if (displaySize > 1024)
303 {
304 displaySize /= 1024;
305 sizeSuffix = "Gb";
306 }
307
308 ri.Printf(PRINT_ALL, "%4i: %4ix%4i %s %4i%s %s\n", i, image->uploadWidth, image->uploadHeight, format, displaySize, sizeSuffix, image->imgName);
309 estTotalSize += estSize;
310 }
311
312 ri.Printf (PRINT_ALL, " ---------\n");
313 ri.Printf (PRINT_ALL, " approx %i bytes\n", estTotalSize);
314 ri.Printf (PRINT_ALL, " %i total images\n\n", tr.numImages );
315 }
316
317 //=======================================================================
318
319 /*
320 ================
321 ResampleTexture
322
323 Used to resample images in a more general than quartering fashion.
324
325 This will only be filtered properly if the resampled size
326 is greater than half the original size.
327
328 If a larger shrinking is needed, use the mipmap function
329 before or after.
330 ================
331 */
332 static void ResampleTexture( byte *in, int inwidth, int inheight, byte *out,
333 int outwidth, int outheight ) {
334 int i, j;
335 byte *inrow, *inrow2;
336 int frac, fracstep;
337 int p1[2048], p2[2048];
338 byte *pix1, *pix2, *pix3, *pix4;
339
340 if (outwidth>2048)
341 ri.Error(ERR_DROP, "ResampleTexture: max width");
342
343 fracstep = inwidth*0x10000/outwidth;
344
345 frac = fracstep>>2;
346 for ( i=0 ; i<outwidth ; i++ ) {
347 p1[i] = 4*(frac>>16);
348 frac += fracstep;
349 }
350 frac = 3*(fracstep>>2);
351 for ( i=0 ; i<outwidth ; i++ ) {
352 p2[i] = 4*(frac>>16);
353 frac += fracstep;
354 }
355
356 for (i=0 ; i<outheight ; i++) {
357 inrow = in + 4*inwidth*(int)((i+0.25)*inheight/outheight);
358 inrow2 = in + 4*inwidth*(int)((i+0.75)*inheight/outheight);
359 for (j=0 ; j<outwidth ; j++) {
360 pix1 = inrow + p1[j];
361 pix2 = inrow + p2[j];
362 pix3 = inrow2 + p1[j];
363 pix4 = inrow2 + p2[j];
364 *out++ = (pix1[0] + pix2[0] + pix3[0] + pix4[0])>>2;
365 *out++ = (pix1[1] + pix2[1] + pix3[1] + pix4[1])>>2;
366 *out++ = (pix1[2] + pix2[2] + pix3[2] + pix4[2])>>2;
367 *out++ = (pix1[3] + pix2[3] + pix3[3] + pix4[3])>>2;
368 }
369 }
370 }
371
372 static void RGBAtoYCoCgA(const byte *in, byte *out, int width, int height)
373 {
374 int x, y;
375
376 for (y = 0; y < height; y++)
377 {
378 const byte *inbyte = in + y * width * 4;
379 byte *outbyte = out + y * width * 4;
380
381 for (x = 0; x < width; x++)
382 {
383 byte r, g, b, a, rb2;
384
385 r = *inbyte++;
386 g = *inbyte++;
387 b = *inbyte++;
388 a = *inbyte++;
389 rb2 = (r + b) >> 1;
390
391 *outbyte++ = (g + rb2) >> 1; // Y = R/4 + G/2 + B/4
392 *outbyte++ = (r - b + 256) >> 1; // Co = R/2 - B/2
393 *outbyte++ = (g - rb2 + 256) >> 1; // Cg = -R/4 + G/2 - B/4
394 *outbyte++ = a;
395 }
396 }
397 }
398
399 static void YCoCgAtoRGBA(const byte *in, byte *out, int width, int height)
400 {
401 int x, y;
402
403 for (y = 0; y < height; y++)
404 {
405 const byte *inbyte = in + y * width * 4;
406 byte *outbyte = out + y * width * 4;
407
408 for (x = 0; x < width; x++)
409 {
410 byte _Y, Co, Cg, a;
411
412 _Y = *inbyte++;
413 Co = *inbyte++;
414 Cg = *inbyte++;
415 a = *inbyte++;
416
417 *outbyte++ = CLAMP(_Y + Co - Cg, 0, 255); // R = Y + Co - Cg
418 *outbyte++ = CLAMP(_Y + Cg - 128, 0, 255); // G = Y + Cg
419 *outbyte++ = CLAMP(_Y - Co - Cg + 256, 0, 255); // B = Y - Co - Cg
420 *outbyte++ = a;
421 }
422 }
423 }
424
425
426 // uses a sobel filter to change a texture to a normal map
427 static void RGBAtoNormal(const byte *in, byte *out, int width, int height, qboolean clampToEdge)
428 {
429 int x, y, max;
430
431 // convert to heightmap, storing in alpha
432 // same as converting to Y in YCoCg
433 max = 1;
434 for (y = 0; y < height; y++)
435 {
436 const byte *inbyte = in + y * width * 4;
437 byte *outbyte = out + y * width * 4 + 3;
438
439 for (x = 0; x < width; x++)
440 {
441 byte result = (inbyte[0] >> 2) + (inbyte[1] >> 1) + (inbyte[2] >> 2);
442 result = result * result / 255; // Make linear
443 *outbyte = result;
444 max = MAX(max, *outbyte);
445 outbyte += 4;
446 inbyte += 4;
447 }
448 }
449
450 // level out heights
451 if (max < 255)
452 {
453 for (y = 0; y < height; y++)
454 {
455 byte *outbyte = out + y * width * 4 + 3;
456
457 for (x = 0; x < width; x++)
458 {
459 *outbyte = *outbyte + (255 - max);
460 outbyte += 4;
461 }
462 }
463 }
464
465
466 // now run sobel filter over height values to generate X and Y
467 // then normalize
468 for (y = 0; y < height; y++)
469 {
470 byte *outbyte = out + y * width * 4;
471
472 for (x = 0; x < width; x++)
473 {
474 // 0 1 2
475 // 3 4 5
476 // 6 7 8
477
478 byte s[9];
479 int x2, y2, i;
480 vec3_t normal;
481
482 i = 0;
483 for (y2 = -1; y2 <= 1; y2++)
484 {
485 int src_y = y + y2;
486
487 if (clampToEdge)
488 {
489 src_y = CLAMP(src_y, 0, height - 1);
490 }
491 else
492 {
493 src_y = (src_y + height) % height;
494 }
495
496
497 for (x2 = -1; x2 <= 1; x2++)
498 {
499 int src_x = x + x2;
500
501 if (clampToEdge)
502 {
503 src_x = CLAMP(src_x, 0, width - 1);
504 }
505 else
506 {
507 src_x = (src_x + width) % width;
508 }
509
510 s[i++] = *(out + (src_y * width + src_x) * 4 + 3);
511 }
512 }
513
514 normal[0] = s[0] - s[2]
515 + 2 * s[3] - 2 * s[5]
516 + s[6] - s[8];
517
518 normal[1] = s[0] + 2 * s[1] + s[2]
519
520 - s[6] - 2 * s[7] - s[8];
521
522 normal[2] = s[4] * 4;
523
524 if (!VectorNormalize2(normal, normal))
525 {
526 VectorSet(normal, 0, 0, 1);
527 }
528
529 *outbyte++ = FloatToOffsetByte(normal[0]);
530 *outbyte++ = FloatToOffsetByte(normal[1]);
531 *outbyte++ = FloatToOffsetByte(normal[2]);
532 outbyte++;
533 }
534 }
535 }
536
537 #define COPYSAMPLE(a,b) *(unsigned int *)(a) = *(unsigned int *)(b)
538
539 // based on Fast Curve Based Interpolation
540 // from Fast Artifacts-Free Image Interpolation (http://www.andreagiachetti.it/icbi/)
541 // assumes data has a 2 pixel thick border of clamped or wrapped data
542 // expects data to be a grid with even (0, 0), (2, 0), (0, 2), (2, 2) etc pixels filled
543 // only performs FCBI on specified component
544 static void DoFCBI(byte *in, byte *out, int width, int height, int component)
545 {
546 int x, y;
547 byte *outbyte, *inbyte;
548
549 // copy in to out
550 for (y = 2; y < height - 2; y += 2)
551 {
552 inbyte = in + (y * width + 2) * 4 + component;
553 outbyte = out + (y * width + 2) * 4 + component;
554
555 for (x = 2; x < width - 2; x += 2)
556 {
557 *outbyte = *inbyte;
558 outbyte += 8;
559 inbyte += 8;
560 }
561 }
562
563 for (y = 3; y < height - 3; y += 2)
564 {
565 // diagonals
566 //
567 // NWp - northwest interpolated pixel
568 // NEp - northeast interpolated pixel
569 // NWd - northwest first derivative
570 // NEd - northeast first derivative
571 // NWdd - northwest second derivative
572 // NEdd - northeast second derivative
573 //
574 // Uses these samples:
575 //
576 // 0
577 // - - a - b - -
578 // - - - - - - -
579 // c - d - e - f
580 // 0 - - - - - - -
581 // g - h - i - j
582 // - - - - - - -
583 // - - k - l - -
584 //
585 // x+2 uses these samples:
586 //
587 // 0
588 // - - - - a - b - -
589 // - - - - - - - - -
590 // - - c - d - e - f
591 // 0 - - - - - - - - -
592 // - - g - h - i - j
593 // - - - - - - - - -
594 // - - - - k - l - -
595 //
596 // so we can reuse 8 of them on next iteration
597 //
598 // a=b, c=d, d=e, e=f, g=h, h=i, i=j, k=l
599 //
600 // only b, f, j, and l need to be sampled on next iteration
601
602 byte sa, sb, sc, sd, se, sf, sg, sh, si, sj, sk, sl;
603 byte *line1, *line2, *line3, *line4;
604
605 x = 3;
606
607 // optimization one
608 // SAMPLE2(sa, x-1, y-3);
609 //SAMPLE2(sc, x-3, y-1); SAMPLE2(sd, x-1, y-1); SAMPLE2(se, x+1, y-1);
610 //SAMPLE2(sg, x-3, y+1); SAMPLE2(sh, x-1, y+1); SAMPLE2(si, x+1, y+1);
611 // SAMPLE2(sk, x-1, y+3);
612
613 // optimization two
614 line1 = in + ((y - 3) * width + (x - 1)) * 4 + component;
615 line2 = in + ((y - 1) * width + (x - 3)) * 4 + component;
616 line3 = in + ((y + 1) * width + (x - 3)) * 4 + component;
617 line4 = in + ((y + 3) * width + (x - 1)) * 4 + component;
618
619 // COPYSAMPLE(sa, line1); line1 += 8;
620 //COPYSAMPLE(sc, line2); line2 += 8; COPYSAMPLE(sd, line2); line2 += 8; COPYSAMPLE(se, line2); line2 += 8;
621 //COPYSAMPLE(sg, line3); line3 += 8; COPYSAMPLE(sh, line3); line3 += 8; COPYSAMPLE(si, line3); line3 += 8;
622 // COPYSAMPLE(sk, line4); line4 += 8;
623
624 sa = *line1; line1 += 8;
625 sc = *line2; line2 += 8; sd = *line2; line2 += 8; se = *line2; line2 += 8;
626 sg = *line3; line3 += 8; sh = *line3; line3 += 8; si = *line3; line3 += 8;
627 sk = *line4; line4 += 8;
628
629 outbyte = out + (y * width + x) * 4 + component;
630
631 for ( ; x < width - 3; x += 2)
632 {
633 int NWd, NEd, NWp, NEp;
634
635 // original
636 // SAMPLE2(sa, x-1, y-3); SAMPLE2(sb, x+1, y-3);
637 //SAMPLE2(sc, x-3, y-1); SAMPLE2(sd, x-1, y-1); SAMPLE2(se, x+1, y-1); SAMPLE2(sf, x+3, y-1);
638 //SAMPLE2(sg, x-3, y+1); SAMPLE2(sh, x-1, y+1); SAMPLE2(si, x+1, y+1); SAMPLE2(sj, x+3, y+1);
639 // SAMPLE2(sk, x-1, y+3); SAMPLE2(sl, x+1, y+3);
640
641 // optimization one
642 //SAMPLE2(sb, x+1, y-3);
643 //SAMPLE2(sf, x+3, y-1);
644 //SAMPLE2(sj, x+3, y+1);
645 //SAMPLE2(sl, x+1, y+3);
646
647 // optimization two
648 //COPYSAMPLE(sb, line1); line1 += 8;
649 //COPYSAMPLE(sf, line2); line2 += 8;
650 //COPYSAMPLE(sj, line3); line3 += 8;
651 //COPYSAMPLE(sl, line4); line4 += 8;
652
653 sb = *line1; line1 += 8;
654 sf = *line2; line2 += 8;
655 sj = *line3; line3 += 8;
656 sl = *line4; line4 += 8;
657
658 NWp = sd + si;
659 NEp = se + sh;
660 NWd = abs(sd - si);
661 NEd = abs(se - sh);
662
663 if (NWd > 100 || NEd > 100 || abs(NWp-NEp) > 200)
664 {
665 if (NWd < NEd)
666 *outbyte = NWp >> 1;
667 else
668 *outbyte = NEp >> 1;
669 }
670 else
671 {
672 int NWdd, NEdd;
673
674 //NEdd = abs(sg + sd + sb - 3 * (se + sh) + sk + si + sf);
675 //NWdd = abs(sa + se + sj - 3 * (sd + si) + sc + sh + sl);
676 NEdd = abs(sg + sb - 3 * NEp + sk + sf + NWp);
677 NWdd = abs(sa + sj - 3 * NWp + sc + sl + NEp);
678
679 if (NWdd > NEdd)
680 *outbyte = NWp >> 1;
681 else
682 *outbyte = NEp >> 1;
683 }
684
685 outbyte += 8;
686
687 // COPYSAMPLE(sa, sb);
688 //COPYSAMPLE(sc, sd); COPYSAMPLE(sd, se); COPYSAMPLE(se, sf);
689 //COPYSAMPLE(sg, sh); COPYSAMPLE(sh, si); COPYSAMPLE(si, sj);
690 // COPYSAMPLE(sk, sl);
691
692 sa = sb;
693 sc = sd; sd = se; se = sf;
694 sg = sh; sh = si; si = sj;
695 sk = sl;
696 }
697 }
698
699 // hack: copy out to in again
700 for (y = 3; y < height - 3; y += 2)
701 {
702 inbyte = out + (y * width + 3) * 4 + component;
703 outbyte = in + (y * width + 3) * 4 + component;
704
705 for (x = 3; x < width - 3; x += 2)
706 {
707 *outbyte = *inbyte;
708 outbyte += 8;
709 inbyte += 8;
710 }
711 }
712
713 for (y = 2; y < height - 3; y++)
714 {
715 // horizontal & vertical
716 //
717 // hp - horizontally interpolated pixel
718 // vp - vertically interpolated pixel
719 // hd - horizontal first derivative
720 // vd - vertical first derivative
721 // hdd - horizontal second derivative
722 // vdd - vertical second derivative
723 // Uses these samples:
724 //
725 // 0
726 // - a - b -
727 // c - d - e
728 // 0 - f - g -
729 // h - i - j
730 // - k - l -
731 //
732 // x+2 uses these samples:
733 //
734 // 0
735 // - - - a - b -
736 // - - c - d - e
737 // 0 - - - f - g -
738 // - - h - i - j
739 // - - - k - l -
740 //
741 // so we can reuse 7 of them on next iteration
742 //
743 // a=b, c=d, d=e, f=g, h=i, i=j, k=l
744 //
745 // only b, e, g, j, and l need to be sampled on next iteration
746
747 byte sa, sb, sc, sd, se, sf, sg, sh, si, sj, sk, sl;
748 byte *line1, *line2, *line3, *line4, *line5;
749
750 //x = (y + 1) % 2;
751 x = (y + 1) % 2 + 2;
752
753 // optimization one
754 // SAMPLE2(sa, x-1, y-2);
755 //SAMPLE2(sc, x-2, y-1); SAMPLE2(sd, x, y-1);
756 // SAMPLE2(sf, x-1, y );
757 //SAMPLE2(sh, x-2, y+1); SAMPLE2(si, x, y+1);
758 // SAMPLE2(sk, x-1, y+2);
759
760 line1 = in + ((y - 2) * width + (x - 1)) * 4 + component;
761 line2 = in + ((y - 1) * width + (x - 2)) * 4 + component;
762 line3 = in + ((y ) * width + (x - 1)) * 4 + component;
763 line4 = in + ((y + 1) * width + (x - 2)) * 4 + component;
764 line5 = in + ((y + 2) * width + (x - 1)) * 4 + component;
765
766 // COPYSAMPLE(sa, line1); line1 += 8;
767 //COPYSAMPLE(sc, line2); line2 += 8; COPYSAMPLE(sd, line2); line2 += 8;
768 // COPYSAMPLE(sf, line3); line3 += 8;
769 //COPYSAMPLE(sh, line4); line4 += 8; COPYSAMPLE(si, line4); line4 += 8;
770 // COPYSAMPLE(sk, line5); line5 += 8;
771
772 sa = *line1; line1 += 8;
773 sc = *line2; line2 += 8; sd = *line2; line2 += 8;
774 sf = *line3; line3 += 8;
775 sh = *line4; line4 += 8; si = *line4; line4 += 8;
776 sk = *line5; line5 += 8;
777
778 outbyte = out + (y * width + x) * 4 + component;
779
780 for ( ; x < width - 3; x+=2)
781 {
782 int hd, vd, hp, vp;
783
784 // SAMPLE2(sa, x-1, y-2); SAMPLE2(sb, x+1, y-2);
785 //SAMPLE2(sc, x-2, y-1); SAMPLE2(sd, x, y-1); SAMPLE2(se, x+2, y-1);
786 // SAMPLE2(sf, x-1, y ); SAMPLE2(sg, x+1, y );
787 //SAMPLE2(sh, x-2, y+1); SAMPLE2(si, x, y+1); SAMPLE2(sj, x+2, y+1);
788 // SAMPLE2(sk, x-1, y+2); SAMPLE2(sl, x+1, y+2);
789
790 // optimization one
791 //SAMPLE2(sb, x+1, y-2);
792 //SAMPLE2(se, x+2, y-1);
793 //SAMPLE2(sg, x+1, y );
794 //SAMPLE2(sj, x+2, y+1);
795 //SAMPLE2(sl, x+1, y+2);
796
797 //COPYSAMPLE(sb, line1); line1 += 8;
798 //COPYSAMPLE(se, line2); line2 += 8;
799 //COPYSAMPLE(sg, line3); line3 += 8;
800 //COPYSAMPLE(sj, line4); line4 += 8;
801 //COPYSAMPLE(sl, line5); line5 += 8;
802
803 sb = *line1; line1 += 8;
804 se = *line2; line2 += 8;
805 sg = *line3; line3 += 8;
806 sj = *line4; line4 += 8;
807 sl = *line5; line5 += 8;
808
809 hp = sf + sg;
810 vp = sd + si;
811 hd = abs(sf - sg);
812 vd = abs(sd - si);
813
814 if (hd > 100 || vd > 100 || abs(hp-vp) > 200)
815 {
816 if (hd < vd)
817 *outbyte = hp >> 1;
818 else
819 *outbyte = vp >> 1;
820 }
821 else
822 {
823 int hdd, vdd;
824
825 //hdd = abs(sc[i] + sd[i] + se[i] - 3 * (sf[i] + sg[i]) + sh[i] + si[i] + sj[i]);
826 //vdd = abs(sa[i] + sf[i] + sk[i] - 3 * (sd[i] + si[i]) + sb[i] + sg[i] + sl[i]);
827
828 hdd = abs(sc + se - 3 * hp + sh + sj + vp);
829 vdd = abs(sa + sk - 3 * vp + sb + sl + hp);
830
831 if (hdd > vdd)
832 *outbyte = hp >> 1;
833 else
834 *outbyte = vp >> 1;
835 }
836
837 outbyte += 8;
838
839 // COPYSAMPLE(sa, sb);
840 //COPYSAMPLE(sc, sd); COPYSAMPLE(sd, se);
841 // COPYSAMPLE(sf, sg);
842 //COPYSAMPLE(sh, si); COPYSAMPLE(si, sj);
843 // COPYSAMPLE(sk, sl);
844 sa = sb;
845 sc = sd; sd = se;
846 sf = sg;
847 sh = si; si = sj;
848 sk = sl;
849 }
850 }
851 }
852
853 // Similar to FCBI, but throws out the second order derivatives for speed
854 static void DoFCBIQuick(byte *in, byte *out, int width, int height, int component)
855 {
856 int x, y;
857 byte *outbyte, *inbyte;
858
859 // copy in to out
860 for (y = 2; y < height - 2; y += 2)
861 {
862 inbyte = in + (y * width + 2) * 4 + component;
863 outbyte = out + (y * width + 2) * 4 + component;
864
865 for (x = 2; x < width - 2; x += 2)
866 {
867 *outbyte = *inbyte;
868 outbyte += 8;
869 inbyte += 8;
870 }
871 }
872
873 for (y = 3; y < height - 4; y += 2)
874 {
875 byte sd, se, sh, si;
876 byte *line2, *line3;
877
878 x = 3;
879
880 line2 = in + ((y - 1) * width + (x - 1)) * 4 + component;
881 line3 = in + ((y + 1) * width + (x - 1)) * 4 + component;
882
883 sd = *line2; line2 += 8;
884 sh = *line3; line3 += 8;
885
886 outbyte = out + (y * width + x) * 4 + component;
887
888 for ( ; x < width - 4; x += 2)
889 {
890 int NWd, NEd, NWp, NEp;
891
892 se = *line2; line2 += 8;
893 si = *line3; line3 += 8;
894
895 NWp = sd + si;
896 NEp = se + sh;
897 NWd = abs(sd - si);
898 NEd = abs(se - sh);
899
900 if (NWd < NEd)
901 *outbyte = NWp >> 1;
902 else
903 *outbyte = NEp >> 1;
904
905 outbyte += 8;
906
907 sd = se;
908 sh = si;
909 }
910 }
911
912 // hack: copy out to in again
913 for (y = 3; y < height - 3; y += 2)
914 {
915 inbyte = out + (y * width + 3) * 4 + component;
916 outbyte = in + (y * width + 3) * 4 + component;
917
918 for (x = 3; x < width - 3; x += 2)
919 {
920 *outbyte = *inbyte;
921 outbyte += 8;
922 inbyte += 8;
923 }
924 }
925
926 for (y = 2; y < height - 3; y++)
927 {
928 byte sd, sf, sg, si;
929 byte *line2, *line3, *line4;
930
931 x = (y + 1) % 2 + 2;
932
933 line2 = in + ((y - 1) * width + (x )) * 4 + component;
934 line3 = in + ((y ) * width + (x - 1)) * 4 + component;
935 line4 = in + ((y + 1) * width + (x )) * 4 + component;
936
937 outbyte = out + (y * width + x) * 4 + component;
938
939 sf = *line3; line3 += 8;
940
941 for ( ; x < width - 3; x+=2)
942 {
943 int hd, vd, hp, vp;
944
945 sd = *line2; line2 += 8;
946 sg = *line3; line3 += 8;
947 si = *line4; line4 += 8;
948
949 hp = sf + sg;
950 vp = sd + si;
951 hd = abs(sf - sg);
952 vd = abs(sd - si);
953
954 if (hd < vd)
955 *outbyte = hp >> 1;
956 else
957 *outbyte = vp >> 1;
958
959 outbyte += 8;
960
961 sf = sg;
962 }
963 }
964 }
965
966 // Similar to DoFCBIQuick, but just takes the average instead of checking derivatives
967 // as well, this operates on all four components
968 static void DoLinear(byte *in, byte *out, int width, int height)
969 {
970 int x, y, i;
971 byte *outbyte, *inbyte;
972
973 // copy in to out
974 for (y = 2; y < height - 2; y += 2)
975 {
976 x = 2;
977
978 inbyte = in + (y * width + x) * 4;
979 outbyte = out + (y * width + x) * 4;
980
981 for ( ; x < width - 2; x += 2)
982 {
983 COPYSAMPLE(outbyte, inbyte);
984 outbyte += 8;
985 inbyte += 8;
986 }
987 }
988
989 for (y = 1; y < height - 1; y += 2)
990 {
991 byte sd[4] = {0}, se[4] = {0}, sh[4] = {0}, si[4] = {0};
992 byte *line2, *line3;
993
994 x = 1;
995
996 line2 = in + ((y - 1) * width + (x - 1)) * 4;
997 line3 = in + ((y + 1) * width + (x - 1)) * 4;
998
999 COPYSAMPLE(sd, line2); line2 += 8;
1000 COPYSAMPLE(sh, line3); line3 += 8;
1001
1002 outbyte = out + (y * width + x) * 4;
1003
1004 for ( ; x < width - 1; x += 2)
1005 {
1006 COPYSAMPLE(se, line2); line2 += 8;
1007 COPYSAMPLE(si, line3); line3 += 8;
1008
1009 for (i = 0; i < 4; i++)
1010 {
1011 *outbyte++ = (sd[i] + si[i] + se[i] + sh[i]) >> 2;
1012 }
1013
1014 outbyte += 4;
1015
1016 COPYSAMPLE(sd, se);
1017 COPYSAMPLE(sh, si);
1018 }
1019 }
1020
1021 // hack: copy out to in again
1022 for (y = 1; y < height - 1; y += 2)
1023 {
1024 x = 1;
1025
1026 inbyte = out + (y * width + x) * 4;
1027 outbyte = in + (y * width + x) * 4;
1028
1029 for ( ; x < width - 1; x += 2)
1030 {
1031 COPYSAMPLE(outbyte, inbyte);
1032 outbyte += 8;
1033 inbyte += 8;
1034 }
1035 }
1036
1037 for (y = 1; y < height - 1; y++)
1038 {
1039 byte sd[4], sf[4], sg[4], si[4];
1040 byte *line2, *line3, *line4;
1041
1042 x = y % 2 + 1;
1043
1044 line2 = in + ((y - 1) * width + (x )) * 4;
1045 line3 = in + ((y ) * width + (x - 1)) * 4;
1046 line4 = in + ((y + 1) * width + (x )) * 4;
1047
1048 COPYSAMPLE(sf, line3); line3 += 8;
1049
1050 outbyte = out + (y * width + x) * 4;
1051
1052 for ( ; x < width - 1; x += 2)
1053 {
1054 COPYSAMPLE(sd, line2); line2 += 8;
1055 COPYSAMPLE(sg, line3); line3 += 8;
1056 COPYSAMPLE(si, line4); line4 += 8;
1057
1058 for (i = 0; i < 4; i++)
1059 {
1060 *outbyte++ = (sf[i] + sg[i] + sd[i] + si[i]) >> 2;
1061 }
1062
1063 outbyte += 4;
1064
1065 COPYSAMPLE(sf, sg);
1066 }
1067 }
1068 }
1069
1070
1071 static void ExpandHalfTextureToGrid( byte *data, int width, int height)
1072 {
1073 int x, y;
1074
1075 for (y = height / 2; y > 0; y--)
1076 {
1077 byte *outbyte = data + ((y * 2 - 1) * (width) - 2) * 4;
1078 byte *inbyte = data + (y * (width / 2) - 1) * 4;
1079
1080 for (x = width / 2; x > 0; x--)
1081 {
1082 COPYSAMPLE(outbyte, inbyte);
1083
1084 outbyte -= 8;
1085 inbyte -= 4;
1086 }
1087 }
1088 }
1089
1090 static void FillInNormalizedZ(const byte *in, byte *out, int width, int height)
1091 {
1092 int x, y;
1093
1094 for (y = 0; y < height; y++)
1095 {
1096 const byte *inbyte = in + y * width * 4;
1097 byte *outbyte = out + y * width * 4;
1098
1099 for (x = 0; x < width; x++)
1100 {
1101 byte nx, ny, nz, h;
1102 float fnx, fny, fll, fnz;
1103
1104 nx = *inbyte++;
1105 ny = *inbyte++;
1106 inbyte++;
1107 h = *inbyte++;
1108
1109 fnx = OffsetByteToFloat(nx);
1110 fny = OffsetByteToFloat(ny);
1111 fll = 1.0f - fnx * fnx - fny * fny;
1112 if (fll >= 0.0f)
1113 fnz = (float)sqrt(fll);
1114 else
1115 fnz = 0.0f;
1116
1117 nz = FloatToOffsetByte(fnz);
1118
1119 *outbyte++ = nx;
1120 *outbyte++ = ny;
1121 *outbyte++ = nz;
1122 *outbyte++ = h;
1123 }
1124 }
1125 }
1126
1127
1128 // size must be even
1129 #define WORKBLOCK_SIZE 128
1130 #define WORKBLOCK_BORDER 4
1131 #define WORKBLOCK_REALSIZE (WORKBLOCK_SIZE + WORKBLOCK_BORDER * 2)
1132
1133 // assumes that data has already been expanded into a 2x2 grid
1134 static void FCBIByBlock(byte *data, int width, int height, qboolean clampToEdge, qboolean normalized)
1135 {
1136 byte workdata[WORKBLOCK_REALSIZE * WORKBLOCK_REALSIZE * 4];
1137 byte outdata[WORKBLOCK_REALSIZE * WORKBLOCK_REALSIZE * 4];
1138 byte *inbyte, *outbyte;
1139 int x, y;
1140 int srcx, srcy;
1141
1142 ExpandHalfTextureToGrid(data, width, height);
1143
1144 for (y = 0; y < height; y += WORKBLOCK_SIZE)
1145 {
1146 for (x = 0; x < width; x += WORKBLOCK_SIZE)
1147 {
1148 int x2, y2;
1149 int workwidth, workheight, fullworkwidth, fullworkheight;
1150
1151 workwidth = MIN(WORKBLOCK_SIZE, width - x);
1152 workheight = MIN(WORKBLOCK_SIZE, height - y);
1153
1154 fullworkwidth = workwidth + WORKBLOCK_BORDER * 2;
1155 fullworkheight = workheight + WORKBLOCK_BORDER * 2;
1156
1157 //memset(workdata, 0, WORKBLOCK_REALSIZE * WORKBLOCK_REALSIZE * 4);
1158
1159 // fill in work block
1160 for (y2 = 0; y2 < fullworkheight; y2 += 2)
1161 {
1162 srcy = y + y2 - WORKBLOCK_BORDER;
1163
1164 if (clampToEdge)
1165 {
1166 srcy = CLAMP(srcy, 0, height - 2);
1167 }
1168 else
1169 {
1170 srcy = (srcy + height) % height;
1171 }
1172
1173 outbyte = workdata + y2 * fullworkwidth * 4;
1174 inbyte = data + srcy * width * 4;
1175
1176 for (x2 = 0; x2 < fullworkwidth; x2 += 2)
1177 {
1178 srcx = x + x2 - WORKBLOCK_BORDER;
1179
1180 if (clampToEdge)
1181 {
1182 srcx = CLAMP(srcx, 0, width - 2);
1183 }
1184 else
1185 {
1186 srcx = (srcx + width) % width;
1187 }
1188
1189 COPYSAMPLE(outbyte, inbyte + srcx * 4);
1190 outbyte += 8;
1191 }
1192 }
1193
1194 // submit work block
1195 DoLinear(workdata, outdata, fullworkwidth, fullworkheight);
1196
1197 if (!normalized)
1198 {
1199 switch (r_imageUpsampleType->integer)
1200 {
1201 case 0:
1202 break;
1203 case 1:
1204 DoFCBIQuick(workdata, outdata, fullworkwidth, fullworkheight, 0);
1205 break;
1206 case 2:
1207 default:
1208 DoFCBI(workdata, outdata, fullworkwidth, fullworkheight, 0);
1209 break;
1210 }
1211 }
1212 else
1213 {
1214 switch (r_imageUpsampleType->integer)
1215 {
1216 case 0:
1217 break;
1218 case 1:
1219 DoFCBIQuick(workdata, outdata, fullworkwidth, fullworkheight, 0);
1220 DoFCBIQuick(workdata, outdata, fullworkwidth, fullworkheight, 1);
1221 break;
1222 case 2:
1223 default:
1224 DoFCBI(workdata, outdata, fullworkwidth, fullworkheight, 0);
1225 DoFCBI(workdata, outdata, fullworkwidth, fullworkheight, 1);
1226 break;
1227 }
1228 }
1229
1230 // copy back work block
1231 for (y2 = 0; y2 < workheight; y2++)
1232 {
1233 inbyte = outdata + ((y2 + WORKBLOCK_BORDER) * fullworkwidth + WORKBLOCK_BORDER) * 4;
1234 outbyte = data + ((y + y2) * width + x) * 4;
1235 for (x2 = 0; x2 < workwidth; x2++)
1236 {
1237 COPYSAMPLE(outbyte, inbyte);
1238 outbyte += 4;
1239 inbyte += 4;
1240 }
1241 }
1242 }
1243 }
1244 }
1245 #undef COPYSAMPLE
1246
1247 /*
1248 ================
1249 R_LightScaleTexture
1250
1251 Scale up the pixel values in a texture to increase the
1252 lighting range
1253 ================
1254 */
1255 void R_LightScaleTexture (byte *in, int inwidth, int inheight, qboolean only_gamma )
1256 {
1257 if ( only_gamma )
1258 {
1259 if ( !glConfig.deviceSupportsGamma )
1260 {
1261 int i, c;
1262 byte *p;
1263
1264 p = in;
1265
1266 c = inwidth*inheight;
1267 for (i=0 ; i<c ; i++, p+=4)
1268 {
1269 p[0] = s_gammatable[p[0]];
1270 p[1] = s_gammatable[p[1]];
1271 p[2] = s_gammatable[p[2]];
1272 }
1273 }
1274 }
1275 else
1276 {
1277 int i, c;
1278 byte *p;
1279
1280 p = in;
1281
1282 c = inwidth*inheight;
1283
1284 if ( glConfig.deviceSupportsGamma )
1285 {
1286 for (i=0 ; i<c ; i++, p+=4)
1287 {
1288 p[0] = s_intensitytable[p[0]];
1289 p[1] = s_intensitytable[p[1]];
1290 p[2] = s_intensitytable[p[2]];
1291 }
1292 }
1293 else
1294 {
1295 for (i=0 ; i<c ; i++, p+=4)
1296 {
1297 p[0] = s_gammatable[s_intensitytable[p[0]]];
1298 p[1] = s_gammatable[s_intensitytable[p[1]]];
1299 p[2] = s_gammatable[s_intensitytable[p[2]]];
1300 }
1301 }
1302 }
1303 }
1304
1305
1306 /*
1307 ================
1308 R_MipMapsRGB
1309
1310 Operates in place, quartering the size of the texture
1311 Colors are gamma correct
1312 ================
1313 */
1314 static void R_MipMapsRGB( byte *in, int inWidth, int inHeight)
1315 {
1316 int x, y, c, stride;
1317 const byte *in2;
1318 float total;
1319 static float downmipSrgbLookup[256];
1320 static int downmipSrgbLookupSet = 0;
1321 byte *out = in;
1322
1323 if (!downmipSrgbLookupSet) {
1324 for (x = 0; x < 256; x++)
1325 downmipSrgbLookup[x] = powf(x / 255.0f, 2.2f) * 0.25f;
1326 downmipSrgbLookupSet = 1;
1327 }
1328
1329 if (inWidth == 1 && inHeight == 1)
1330 return;
1331
1332 if (inWidth == 1 || inHeight == 1) {
1333 for (x = (inWidth * inHeight) >> 1; x; x--) {
1334 for (c = 3; c; c--, in++) {
1335 total = (downmipSrgbLookup[*(in)] + downmipSrgbLookup[*(in + 4)]) * 2.0f;
1336
1337 *out++ = (byte)(powf(total, 1.0f / 2.2f) * 255.0f);
1338 }
1339 *out++ = (*(in) + *(in + 4)) >> 1; in += 5;
1340 }
1341
1342 return;
1343 }
1344
1345 stride = inWidth * 4;
1346 inWidth >>= 1; inHeight >>= 1;
1347
1348 in2 = in + stride;
1349 for (y = inHeight; y; y--, in += stride, in2 += stride) {
1350 for (x = inWidth; x; x--) {
1351 for (c = 3; c; c--, in++, in2++) {
1352 total = downmipSrgbLookup[*(in)] + downmipSrgbLookup[*(in + 4)]
1353 + downmipSrgbLookup[*(in2)] + downmipSrgbLookup[*(in2 + 4)];
1354
1355 *out++ = (byte)(powf(total, 1.0f / 2.2f) * 255.0f);
1356 }
1357
1358 *out++ = (*(in) + *(in + 4) + *(in2) + *(in2 + 4)) >> 2; in += 5, in2 += 5;
1359 }
1360 }
1361 }
1362
1363
1364 static void R_MipMapNormalHeight (const byte *in, byte *out, int width, int height, qboolean swizzle)
1365 {
1366 int i, j;
1367 int row;
1368 int sx = swizzle ? 3 : 0;
1369 int sa = swizzle ? 0 : 3;
1370
1371 if ( width == 1 && height == 1 ) {
1372 return;
1373 }
1374
1375 row = width * 4;
1376 width >>= 1;
1377 height >>= 1;
1378
1379 for (i=0 ; i<height ; i++, in+=row) {
1380 for (j=0 ; j<width ; j++, out+=4, in+=8) {
1381 vec3_t v;
1382
1383 v[0] = OffsetByteToFloat(in[sx ]);
1384 v[1] = OffsetByteToFloat(in[ 1]);
1385 v[2] = OffsetByteToFloat(in[ 2]);
1386
1387 v[0] += OffsetByteToFloat(in[sx +4]);
1388 v[1] += OffsetByteToFloat(in[ 5]);
1389 v[2] += OffsetByteToFloat(in[ 6]);
1390
1391 v[0] += OffsetByteToFloat(in[sx+row ]);
1392 v[1] += OffsetByteToFloat(in[ row+1]);
1393 v[2] += OffsetByteToFloat(in[ row+2]);
1394
1395 v[0] += OffsetByteToFloat(in[sx+row+4]);
1396 v[1] += OffsetByteToFloat(in[ row+5]);
1397 v[2] += OffsetByteToFloat(in[ row+6]);
1398
1399 VectorNormalizeFast(v);
1400
1401 //v[0] *= 0.25f;
1402 //v[1] *= 0.25f;
1403 //v[2] = 1.0f - v[0] * v[0] - v[1] * v[1];
1404 //v[2] = sqrt(MAX(v[2], 0.0f));
1405
1406 out[sx] = FloatToOffsetByte(v[0]);
1407 out[1 ] = FloatToOffsetByte(v[1]);
1408 out[2 ] = FloatToOffsetByte(v[2]);
1409 out[sa] = MAX(MAX(in[sa], in[sa+4]), MAX(in[sa+row], in[sa+row+4]));
1410 }
1411 }
1412 }
1413
1414
1415 /*
1416 ==================
1417 R_BlendOverTexture
1418
1419 Apply a color blend over a set of pixels
1420 ==================
1421 */
1422 static void R_BlendOverTexture( byte *data, int pixelCount, byte blend[4] ) {
1423 int i;
1424 int inverseAlpha;
1425 int premult[3];
1426
1427 inverseAlpha = 255 - blend[3];
1428 premult[0] = blend[0] * blend[3];
1429 premult[1] = blend[1] * blend[3];
1430 premult[2] = blend[2] * blend[3];
1431
1432 for ( i = 0 ; i < pixelCount ; i++, data+=4 ) {
1433 data[0] = ( data[0] * inverseAlpha + premult[0] ) >> 9;
1434 data[1] = ( data[1] * inverseAlpha + premult[1] ) >> 9;
1435 data[2] = ( data[2] * inverseAlpha + premult[2] ) >> 9;
1436 }
1437 }
1438
1439 byte mipBlendColors[16][4] = {
1440 {0,0,0,0},
1441 {255,0,0,128},
1442 {0,255,0,128},
1443 {0,0,255,128},
1444 {255,0,0,128},
1445 {0,255,0,128},
1446 {0,0,255,128},
1447 {255,0,0,128},
1448 {0,255,0,128},
1449 {0,0,255,128},
1450 {255,0,0,128},
1451 {0,255,0,128},
1452 {0,0,255,128},
1453 {255,0,0,128},
1454 {0,255,0,128},
1455 {0,0,255,128},
1456 };
1457
1458 /*
1459 ==================
1460 R_ConvertTextureFormat
1461
1462 Convert RGBA unsigned byte to specified format and type
1463 ==================
1464 */
1465 #define ROW_PADDING( width, bpp, alignment ) PAD( (width) * (bpp), (alignment) ) - (width) * (bpp)
1466 void R_ConvertTextureFormat( const byte *in, int width, int height, GLenum format, GLenum type, byte *out )
1467 {
1468 int x, y, rowPadding;
1469 int unpackAlign = 4; // matches GL_UNPACK_ALIGNMENT default
1470
1471 if ( format == GL_RGB && type == GL_UNSIGNED_BYTE )
1472 {
1473 rowPadding = ROW_PADDING( width, 3, unpackAlign );
1474
1475 for ( y = 0; y < height; y++ )
1476 {
1477 for ( x = 0; x < width; x++ )
1478 {
1479 *out++ = *in++;
1480 *out++ = *in++;
1481 *out++ = *in++;
1482 in++;
1483 }
1484
1485 out += rowPadding;
1486 }
1487 }
1488 else if ( format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE )
1489 {
1490 rowPadding = ROW_PADDING( width, 1, unpackAlign );
1491
1492 for ( y = 0; y < height; y++ )
1493 {
1494 for ( x = 0; x < width; x++ )
1495 {
1496 *out++ = *in++; // red
1497 in += 3;
1498 }
1499
1500 out += rowPadding;
1501 }
1502 }
1503 else if ( format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_BYTE )
1504 {
1505 rowPadding = ROW_PADDING( width, 2, unpackAlign );
1506
1507 for ( y = 0; y < height; y++ )
1508 {
1509 for ( x = 0; x < width; x++ )
1510 {
1511 *out++ = *in++; // red
1512 in += 2;
1513 *out++ = *in++; // alpha
1514 }
1515
1516 out += rowPadding;
1517 }
1518 }
1519 else if ( format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5 )
1520 {
1521 rowPadding = ROW_PADDING( width, 2, unpackAlign );
1522
1523 for ( y = 0; y < height; y++ )
1524 {
1525 for ( x = 0; x < width; x++, in += 4, out += 2 )
1526 {
1527 *((unsigned short*)out) = ( (unsigned short)( in[0] >> 3 ) << 11 )
1528 | ( (unsigned short)( in[1] >> 2 ) << 5 )
1529 | ( (unsigned short)( in[2] >> 3 ) << 0 );
1530 }
1531
1532 out += rowPadding;
1533 }
1534 }
1535 else if ( format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 )
1536 {
1537 rowPadding = ROW_PADDING( width, 2, unpackAlign );
1538
1539 for ( y = 0; y < height; y++ )
1540 {
1541 for ( x = 0; x < width; x++, in += 4, out += 2 )
1542 {
1543 *((unsigned short*)out) = ( (unsigned short)( in[0] >> 4 ) << 12 )
1544 | ( (unsigned short)( in[1] >> 4 ) << 8 )
1545 | ( (unsigned short)( in[2] >> 4 ) << 4 )
1546 | ( (unsigned short)( in[3] >> 4 ) << 0 );
1547 }
1548
1549 out += rowPadding;
1550 }
1551 }
1552 else
1553 {
1554 ri.Error( ERR_DROP, "Unable to convert RGBA image to OpenGL format 0x%X and type 0x%X", format, type );
1555 }
1556 }
1557
1558 static void RawImage_SwizzleRA( byte *data, int width, int height )
1559 {
1560 int i;
1561 byte *ptr = data, swap;
1562
1563 for (i=0; i<width*height; i++, ptr+=4)
1564 {
1565 // swap red and alpha
1566 swap = ptr[0];
1567 ptr[0] = ptr[3];
1568 ptr[3] = swap;
1569 }
1570 }
1571
1572
1573 /*
1574 ===============
1575 RawImage_ScaleToPower2
1576
1577 ===============
1578 */
1579 static qboolean RawImage_ScaleToPower2( byte **data, int *inout_width, int *inout_height, imgType_t type, imgFlags_t flags, byte **resampledBuffer)
1580 {
1581 int width = *inout_width;
1582 int height = *inout_height;
1583 int scaled_width;
1584 int scaled_height;
1585 qboolean picmip = !!(flags & IMGFLAG_PICMIP);
1586 qboolean mipmap = !!(flags & IMGFLAG_MIPMAP);
1587 qboolean clampToEdge = !!(flags & IMGFLAG_CLAMPTOEDGE);
1588 qboolean scaled;
1589
1590 //
1591 // convert to exact power of 2 sizes
1592 //
1593 if (!mipmap)
1594 {
1595 scaled_width = width;
1596 scaled_height = height;
1597 }
1598 else
1599 {
1600 scaled_width = NextPowerOfTwo(width);
1601 scaled_height = NextPowerOfTwo(height);
1602 }
1603
1604 if ( r_roundImagesDown->integer && scaled_width > width )
1605 scaled_width >>= 1;
1606 if ( r_roundImagesDown->integer && scaled_height > height )
1607 scaled_height >>= 1;
1608
1609 if ( picmip && data && resampledBuffer && r_imageUpsample->integer &&
1610 scaled_width < r_imageUpsampleMaxSize->integer && scaled_height < r_imageUpsampleMaxSize->integer)
1611 {
1612 int finalwidth, finalheight;
1613 //int startTime, endTime;
1614
1615 //startTime = ri.Milliseconds();
1616
1617 finalwidth = scaled_width << r_imageUpsample->integer;
1618 finalheight = scaled_height << r_imageUpsample->integer;
1619
1620 while ( finalwidth > r_imageUpsampleMaxSize->integer
1621 || finalheight > r_imageUpsampleMaxSize->integer ) {
1622 finalwidth >>= 1;
1623 finalheight >>= 1;
1624 }
1625
1626 while ( finalwidth > glConfig.maxTextureSize
1627 || finalheight > glConfig.maxTextureSize ) {
1628 finalwidth >>= 1;
1629 finalheight >>= 1;
1630 }
1631
1632 *resampledBuffer = ri.Hunk_AllocateTempMemory( finalwidth * finalheight * 4 );
1633
1634 if (scaled_width != width || scaled_height != height)
1635 ResampleTexture (*data, width, height, *resampledBuffer, scaled_width, scaled_height);
1636 else
1637 Com_Memcpy(*resampledBuffer, *data, width * height * 4);
1638
1639 if (type == IMGTYPE_COLORALPHA)
1640 RGBAtoYCoCgA(*resampledBuffer, *resampledBuffer, scaled_width, scaled_height);
1641
1642 while (scaled_width < finalwidth || scaled_height < finalheight)
1643 {
1644 scaled_width <<= 1;
1645 scaled_height <<= 1;
1646
1647 FCBIByBlock(*resampledBuffer, scaled_width, scaled_height, clampToEdge, (type == IMGTYPE_NORMAL || type == IMGTYPE_NORMALHEIGHT));
1648 }
1649
1650 if (type == IMGTYPE_COLORALPHA)
1651 YCoCgAtoRGBA(*resampledBuffer, *resampledBuffer, scaled_width, scaled_height);
1652 else if (type == IMGTYPE_NORMAL || type == IMGTYPE_NORMALHEIGHT)
1653 FillInNormalizedZ(*resampledBuffer, *resampledBuffer, scaled_width, scaled_height);
1654
1655 //endTime = ri.Milliseconds();
1656
1657 //ri.Printf(PRINT_ALL, "upsampled %dx%d to %dx%d in %dms\n", width, height, scaled_width, scaled_height, endTime - startTime);
1658
1659 *data = *resampledBuffer;
1660 }
1661 else if ( scaled_width != width || scaled_height != height )
1662 {
1663 if (data && resampledBuffer)
1664 {
1665 *resampledBuffer = ri.Hunk_AllocateTempMemory( scaled_width * scaled_height * 4 );
1666 ResampleTexture (*data, width, height, *resampledBuffer, scaled_width, scaled_height);
1667 *data = *resampledBuffer;
1668 }
1669 }
1670
1671 width = scaled_width;
1672 height = scaled_height;
1673
1674 //
1675 // perform optional picmip operation
1676 //
1677 if ( picmip ) {
1678 scaled_width >>= r_picmip->integer;
1679 scaled_height >>= r_picmip->integer;
1680 }
1681
1682 //
1683 // clamp to the current upper OpenGL limit
1684 // scale both axis down equally so we don't have to
1685 // deal with a half mip resampling
1686 //
1687 while ( scaled_width > glConfig.maxTextureSize
1688 || scaled_height > glConfig.maxTextureSize ) {
1689 scaled_width >>= 1;
1690 scaled_height >>= 1;
1691 }
1692
1693 //
1694 // clamp to minimum size
1695 //
1696 scaled_width = MAX(1, scaled_width);
1697 scaled_height = MAX(1, scaled_height);
1698
1699 scaled = (width != scaled_width) || (height != scaled_height);
1700
1701 //
1702 // rescale texture to new size using existing mipmap functions
1703 //
1704 if (data)
1705 {
1706 while (width > scaled_width || height > scaled_height)
1707 {
1708 if (type == IMGTYPE_NORMAL || type == IMGTYPE_NORMALHEIGHT)
1709 R_MipMapNormalHeight(*data, *data, width, height, qfalse);
1710 else
1711 R_MipMapsRGB(*data, width, height);
1712
1713 width = MAX(1, width >> 1);
1714 height = MAX(1, height >> 1);
1715 }
1716 }
1717
1718 *inout_width = width;
1719 *inout_height = height;
1720
1721 return scaled;
1722 }
1723
1724
1725 static qboolean RawImage_HasAlpha(const byte *scan, int numPixels)
1726 {
1727 int i;
1728
1729 if (!scan)
1730 return qtrue;
1731
1732 for ( i = 0; i < numPixels; i++ )
1733 {
1734 if ( scan[i*4 + 3] != 255 )
1735 {
1736 return qtrue;
1737 }
1738 }
1739
1740 return qfalse;
1741 }
1742
1743 static GLenum RawImage_GetFormat(const byte *data, int numPixels, GLenum picFormat, qboolean lightMap, imgType_t type, imgFlags_t flags)
1744 {
1745 int samples = 3;
1746 GLenum internalFormat = GL_RGB;
1747 qboolean forceNoCompression = !!(flags & IMGFLAG_NO_COMPRESSION);
1748 qboolean normalmap = (type == IMGTYPE_NORMAL || type == IMGTYPE_NORMALHEIGHT);
1749
1750 if (picFormat != GL_RGBA8)
1751 return picFormat;
1752
1753 if(normalmap)
1754 {
1755 if ((type == IMGTYPE_NORMALHEIGHT) && RawImage_HasAlpha(data, numPixels) && r_parallaxMapping->integer)
1756 {
1757 if (!forceNoCompression && glRefConfig.textureCompression & TCR_BPTC)
1758 {
1759 internalFormat = GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
1760 }
1761 else if (!forceNoCompression && glConfig.textureCompression == TC_S3TC_ARB)
1762 {
1763 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
1764 }
1765 else if ( r_texturebits->integer == 16 )
1766 {
1767 internalFormat = GL_RGBA4;
1768 }
1769 else if ( r_texturebits->integer == 32 )
1770 {
1771 internalFormat = GL_RGBA8;
1772 }
1773 else
1774 {
1775 internalFormat = GL_RGBA;
1776 }
1777 }
1778 else
1779 {
1780 if (!forceNoCompression && glRefConfig.textureCompression & TCR_RGTC)
1781 {
1782 internalFormat = GL_COMPRESSED_RG_RGTC2;
1783 }
1784 else if (!forceNoCompression && glRefConfig.textureCompression & TCR_BPTC)
1785 {
1786 internalFormat = GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
1787 }
1788 else if (!forceNoCompression && glConfig.textureCompression == TC_S3TC_ARB)
1789 {
1790 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
1791 }
1792 else if (r_texturebits->integer == 16)
1793 {
1794 internalFormat = GL_RGB5;
1795 }
1796 else if (r_texturebits->integer == 32)
1797 {
1798 internalFormat = GL_RGB8;
1799 }
1800 else
1801 {
1802 internalFormat = GL_RGB;
1803 }
1804 }
1805 }
1806 else if(lightMap)
1807 {
1808
1809 internalFormat = GL_RGBA;
1810 }
1811 else
1812 {
1813 if (RawImage_HasAlpha(data, numPixels))
1814 {
1815 samples = 4;
1816 }
1817
1818 // select proper internal format
1819 if ( samples == 3 )
1820 {
1821
1822 if ( !forceNoCompression && (glRefConfig.textureCompression & TCR_BPTC) )
1823 {
1824 internalFormat = GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
1825 }
1826 else if ( !forceNoCompression && glConfig.textureCompression == TC_S3TC_ARB )
1827 {
1828 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1829 }
1830 else if ( !forceNoCompression && glConfig.textureCompression == TC_S3TC )
1831 {
1832 internalFormat = GL_RGB4_S3TC;
1833 }
1834 else if ( r_texturebits->integer == 16 )
1835 {
1836 internalFormat = GL_RGB5;
1837 }
1838 else if ( r_texturebits->integer == 32 )
1839 {
1840 internalFormat = GL_RGB8;
1841 }
1842 else
1843 {
1844 internalFormat = GL_RGB;
1845 }
1846 }
1847 else if ( samples == 4 )
1848 {
1849 if ( !forceNoCompression && (glRefConfig.textureCompression & TCR_BPTC) )
1850 {
1851 internalFormat = GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
1852 }
1853 else if ( !forceNoCompression && glConfig.textureCompression == TC_S3TC_ARB )
1854 {
1855 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
1856 }
1857 else if ( r_texturebits->integer == 16 )
1858 {
1859 internalFormat = GL_RGBA4;
1860 }
1861 else if ( r_texturebits->integer == 32 )
1862 {
1863 internalFormat = GL_RGBA8;
1864 }
1865 else
1866 {
1867 internalFormat = GL_RGBA;
1868 }
1869 }
1870 }
1871
1872 return internalFormat;
1873 }
1874
1875 static void CompressMonoBlock(byte outdata[8], const byte indata[16])
1876 {
1877 int hi, lo, diff, bias, outbyte, shift, i;
1878 byte *p = outdata;
1879
1880 hi = lo = indata[0];
1881 for (i = 1; i < 16; i++)
1882 {
1883 hi = MAX(indata[i], hi);
1884 lo = MIN(indata[i], lo);
1885 }
1886
1887 *p++ = hi;
1888 *p++ = lo;
1889
1890 diff = hi - lo;
1891
1892 if (diff == 0)
1893 {
1894 outbyte = (hi == 255) ? 255 : 0;
1895
1896 for (i = 0; i < 6; i++)
1897 *p++ = outbyte;
1898
1899 return;
1900 }
1901
1902 bias = diff / 2 - lo * 7;
1903 outbyte = shift = 0;
1904 for (i = 0; i < 16; i++)
1905 {
1906 const byte fixIndex[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
1907 byte index = fixIndex[(indata[i] * 7 + bias) / diff];
1908
1909 outbyte |= index << shift;
1910 shift += 3;
1911 if (shift >= 8)
1912 {
1913 *p++ = outbyte & 0xff;
1914 shift -= 8;
1915 outbyte >>= 8;
1916 }
1917 }
1918 }
1919
1920 static void RawImage_UploadToRgtc2Texture(GLuint texture, int miplevel, int x, int y, int width, int height, byte *data)
1921 {
1922 int wBlocks, hBlocks, iy, ix, size;
1923 byte *compressedData, *p;
1924
1925 wBlocks = (width + 3) / 4;
1926 hBlocks = (height + 3) / 4;
1927 size = wBlocks * hBlocks * 16;
1928
1929 p = compressedData = ri.Hunk_AllocateTempMemory(size);
1930 for (iy = 0; iy < height; iy += 4)
1931 {
1932 int oh = MIN(4, height - iy);
1933
1934 for (ix = 0; ix < width; ix += 4)
1935 {
1936 byte workingData[16];
1937 int component;
1938
1939 int ow = MIN(4, width - ix);
1940
1941 for (component = 0; component < 2; component++)
1942 {
1943 int ox, oy;
1944
1945 for (oy = 0; oy < oh; oy++)
1946 for (ox = 0; ox < ow; ox++)
1947 workingData[oy * 4 + ox] = data[((iy + oy) * width + ix + ox) * 4 + component];
1948
1949 // dupe data to fill
1950 for (oy = 0; oy < 4; oy++)
1951 for (ox = (oy < oh) ? ow : 0; ox < 4; ox++)
1952 workingData[oy * 4 + ox] = workingData[(oy % oh) * 4 + ox % ow];
1953
1954 CompressMonoBlock(p, workingData);
1955 p += 8;
1956 }
1957 }
1958 }
1959
1960 // FIXME: Won't work for x/y that aren't multiples of 4.
1961 qglCompressedTextureSubImage2DEXT(texture, GL_TEXTURE_2D, miplevel, x, y, width, height, GL_COMPRESSED_RG_RGTC2, size, compressedData);
1962
1963 ri.Hunk_FreeTempMemory(compressedData);
1964 }
1965
1966 static int CalculateMipSize(int width, int height, GLenum picFormat)
1967 {
1968 int numBlocks = ((width + 3) / 4) * ((height + 3) / 4);
1969 int numPixels = width * height;
1970
1971 switch (picFormat)
1972 {
1973 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1974 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
1975 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
1976 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
1977 case GL_COMPRESSED_RED_RGTC1:
1978 case GL_COMPRESSED_SIGNED_RED_RGTC1:
1979 return numBlocks * 8;
1980
1981 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
1982 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
1983 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
1984 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
1985 case GL_COMPRESSED_RG_RGTC2:
1986 case GL_COMPRESSED_SIGNED_RG_RGTC2:
1987 case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB:
1988 case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB:
1989 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
1990 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
1991 return numBlocks * 16;
1992
1993 case GL_RGBA8:
1994 case GL_SRGB8_ALPHA8_EXT:
1995 return numPixels * 4;
1996
1997 case GL_RGBA16:
1998 return numPixels * 8;
1999
2000 default:
2001 ri.Printf(PRINT_ALL, "Unsupported texture format %08x\n", picFormat);
2002 return 0;
2003 }
2004
2005 return 0;
2006 }
2007
2008
2009 static GLenum PixelDataFormatFromInternalFormat(GLenum internalFormat)
2010 {
2011 switch (internalFormat)
2012 {
2013 case GL_DEPTH_COMPONENT:
2014 case GL_DEPTH_COMPONENT16_ARB:
2015 case GL_DEPTH_COMPONENT24_ARB:
2016 case GL_DEPTH_COMPONENT32_ARB:
2017 return GL_DEPTH_COMPONENT;
2018 default:
2019 return GL_RGBA;
2020 break;
2021 }
2022 }
2023
2024 static void RawImage_UploadTexture(GLuint texture, byte *data, int x, int y, int width, int height, GLenum target, GLenum picFormat, GLenum dataFormat, GLenum dataType, int numMips, GLenum internalFormat, imgType_t type, imgFlags_t flags, qboolean subtexture )
2025 {
2026 qboolean rgtc = internalFormat == GL_COMPRESSED_RG_RGTC2;
2027 qboolean rgba8 = picFormat == GL_RGBA8 || picFormat == GL_SRGB8_ALPHA8_EXT;
2028 qboolean rgba = rgba8 || picFormat == GL_RGBA16;
2029 qboolean mipmap = !!(flags & IMGFLAG_MIPMAP);
2030 int size, miplevel;
2031 qboolean lastMip = qfalse;
2032 byte *formatBuffer = NULL;
2033
2034 if (qglesMajorVersion && rgba8 && (dataFormat != GL_RGBA || dataType != GL_UNSIGNED_BYTE))
2035 {
2036 formatBuffer = ri.Hunk_AllocateTempMemory(4 * width * height);
2037 }
2038
2039 miplevel = 0;
2040 do
2041 {
2042 lastMip = (width == 1 && height == 1) || !mipmap;
2043 size = CalculateMipSize(width, height, picFormat);
2044
2045 if (!rgba)
2046 {
2047 qglCompressedTextureSubImage2DEXT(texture, target, miplevel, x, y, width, height, picFormat, size, data);
2048 }
2049 else
2050 {
2051 if (rgba8 && miplevel != 0 && r_colorMipLevels->integer)
2052 R_BlendOverTexture((byte *)data, width * height, mipBlendColors[miplevel]);
2053
2054 if (rgba8 && rgtc)
2055 RawImage_UploadToRgtc2Texture(texture, miplevel, x, y, width, height, data);
2056 else if (formatBuffer)
2057 {
2058 R_ConvertTextureFormat(data, width, height, dataFormat, dataType, formatBuffer);
2059 qglTextureSubImage2DEXT(texture, target, miplevel, x, y, width, height, dataFormat, dataType, formatBuffer);
2060 }
2061 else
2062 qglTextureSubImage2DEXT(texture, target, miplevel, x, y, width, height, dataFormat, dataType, data);
2063 }
2064
2065 if (!lastMip && numMips < 2)
2066 {
2067 if (glRefConfig.framebufferObject)
2068 {
2069 qglGenerateTextureMipmapEXT(texture, target);
2070 break;
2071 }
2072 else if (rgba8)
2073 {
2074 if (type == IMGTYPE_NORMAL || type == IMGTYPE_NORMALHEIGHT)
2075 R_MipMapNormalHeight(data, data, width, height, glRefConfig.swizzleNormalmap);
2076 else
2077 R_MipMapsRGB(data, width, height);
2078 }
2079 }
2080
2081 x >>= 1;
2082 y >>= 1;
2083 width = MAX(1, width >> 1);
2084 height = MAX(1, height >> 1);
2085 miplevel++;
2086
2087 if (numMips > 1)
2088 {
2089 data += size;
2090 numMips--;
2091 }
2092 }
2093 while (!lastMip);
2094
2095 if (formatBuffer != NULL)
2096 ri.Hunk_FreeTempMemory(formatBuffer);
2097 }
2098
2099
2100 /*
2101 ===============
2102 Upload32
2103
2104 ===============
2105 */
2106 static void Upload32(byte *data, int x, int y, int width, int height, GLenum picFormat, GLenum dataFormat, GLenum dataType, int numMips, image_t *image, qboolean scaled)
2107 {
2108 int i, c;
2109
2110 imgType_t type = image->type;
2111 imgFlags_t flags = image->flags;
2112 GLenum internalFormat = image->internalFormat;
2113 qboolean rgba8 = picFormat == GL_RGBA8 || picFormat == GL_SRGB8_ALPHA8_EXT;
2114 qboolean mipmap = !!(flags & IMGFLAG_MIPMAP) && (rgba8 || numMips > 1);
2115 qboolean cubemap = !!(flags & IMGFLAG_CUBEMAP);
2116
2117 // These operations cannot be performed on non-rgba8 images.
2118 if (rgba8 && !cubemap)
2119 {
2120 c = width*height;
2121
2122 if (type == IMGTYPE_COLORALPHA)
2123 {
2124
2125 // This corresponds to what the OpenGL1 renderer does.
2126 if (!(flags & IMGFLAG_NOLIGHTSCALE) && (scaled || mipmap))
2127 R_LightScaleTexture(data, width, height, !mipmap);
2128 }
2129
2130 if (glRefConfig.swizzleNormalmap && (type == IMGTYPE_NORMAL || type == IMGTYPE_NORMALHEIGHT))
2131 RawImage_SwizzleRA(data, width, height);
2132 }
2133
2134 if (cubemap)
2135 {
2136 for (i = 0; i < 6; i++)
2137 {
2138 int w2 = width, h2 = height;
2139 RawImage_UploadTexture(image->texnum, data, x, y, width, height, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, picFormat, dataFormat, dataType, numMips, internalFormat, type, flags, qfalse);
2140 for (c = numMips; c; c--)
2141 {
2142 data += CalculateMipSize(w2, h2, picFormat);
2143 w2 = MAX(1, w2 >> 1);
2144 h2 = MAX(1, h2 >> 1);
2145 }
2146 }
2147 }
2148 else
2149 {
2150 RawImage_UploadTexture(image->texnum, data, x, y, width, height, GL_TEXTURE_2D, picFormat, dataFormat, dataType, numMips, internalFormat, type, flags, qfalse);
2151 }
2152
2153 GL_CheckErrors();
2154 }
2155
2156
2157 /*
2158 ================
2159 R_CreateImage2
2160
2161 This is the only way any image_t are created
2162 ================
2163 */
2164 image_t *R_CreateImage2( const char *name, byte *pic, int width, int height, GLenum picFormat, int numMips, imgType_t type, imgFlags_t flags, int internalFormat ) {
2165 byte *resampledBuffer = NULL;
2166 image_t *image;
2167 qboolean isLightmap = qfalse, scaled = qfalse;
2168 long hash;
2169 int glWrapClampMode, mipWidth, mipHeight, miplevel;
2170 qboolean rgba8 = picFormat == GL_RGBA8 || picFormat == GL_SRGB8_ALPHA8_EXT;
2171 qboolean mipmap = !!(flags & IMGFLAG_MIPMAP);
2172 qboolean cubemap = !!(flags & IMGFLAG_CUBEMAP);
2173 qboolean picmip = !!(flags & IMGFLAG_PICMIP);
2174 qboolean lastMip;
2175 GLenum textureTarget = cubemap ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
2176 GLenum dataFormat, dataType;
2177
2178 if (strlen(name) >= MAX_QPATH ) {
2179 ri.Error (ERR_DROP, "R_CreateImage: \"%s\" is too long", name);
2180 }
2181 if ( !strncmp( name, "*lightmap", 9 ) ) {
2182 isLightmap = qtrue;
2183 }
2184
2185 if ( tr.numImages == MAX_DRAWIMAGES ) {
2186 ri.Error( ERR_DROP, "R_CreateImage: MAX_DRAWIMAGES hit");
2187 }
2188
2189 image = tr.images[tr.numImages] = ri.Hunk_Alloc( sizeof( image_t ), h_low );
2190 qglGenTextures(1, &image->texnum);
2191 tr.numImages++;
2192
2193 image->type = type;
2194 image->flags = flags;
2195
2196 strcpy (image->imgName, name);
2197
2198 image->width = width;
2199 image->height = height;
2200 if (flags & IMGFLAG_CLAMPTOEDGE)
2201 glWrapClampMode = GL_CLAMP_TO_EDGE;
2202 else
2203 glWrapClampMode = GL_REPEAT;
2204
2205 if (!internalFormat)
2206 internalFormat = RawImage_GetFormat(pic, width * height, picFormat, isLightmap, image->type, image->flags);
2207
2208 dataFormat = PixelDataFormatFromInternalFormat(internalFormat);
2209 dataType = picFormat == GL_RGBA16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
2210
2211 // Convert image data format for OpenGL ES, data is converted for each mip level
2212 if (qglesMajorVersion)
2213 {
2214 switch (internalFormat)
2215 {
2216 case GL_LUMINANCE:
2217 case GL_LUMINANCE8:
2218 internalFormat = GL_LUMINANCE;
2219 dataFormat = GL_LUMINANCE;
2220 dataType = GL_UNSIGNED_BYTE;
2221 break;
2222 case GL_LUMINANCE_ALPHA:
2223 case GL_LUMINANCE8_ALPHA8:
2224 internalFormat = GL_LUMINANCE_ALPHA;
2225 dataFormat = GL_LUMINANCE_ALPHA;
2226 dataType = GL_UNSIGNED_BYTE;
2227 break;
2228 case GL_RGB:
2229 case GL_RGB8:
2230 internalFormat = GL_RGB;
2231 dataFormat = GL_RGB;
2232 dataType = GL_UNSIGNED_BYTE;
2233 break;
2234 case GL_RGB5:
2235 internalFormat = GL_RGB;
2236 dataFormat = GL_RGB;
2237 dataType = GL_UNSIGNED_SHORT_5_6_5;
2238 break;
2239 case GL_RGBA:
2240 case GL_RGBA8:
2241 internalFormat = GL_RGBA;
2242 dataFormat = GL_RGBA;
2243 dataType = GL_UNSIGNED_BYTE;
2244 break;
2245 case GL_RGBA4:
2246 internalFormat = GL_RGBA;
2247 dataFormat = GL_RGBA;
2248 dataType = GL_UNSIGNED_SHORT_4_4_4_4;
2249 break;
2250 default:
2251 ri.Error( ERR_DROP, "Missing OpenGL ES support for image '%s' with internal format 0x%X\n", name, internalFormat );
2252 }
2253 }
2254
2255 image->internalFormat = internalFormat;
2256
2257 // Possibly scale image before uploading.
2258 // if not rgba8 and uploading an image, skip picmips.
2259 if (!cubemap)
2260 {
2261 if (rgba8)
2262 scaled = RawImage_ScaleToPower2(&pic, &width, &height, type, flags, &resampledBuffer);
2263 else if (pic && picmip)
2264 {
2265 for (miplevel = r_picmip->integer; miplevel > 0 && numMips > 1; miplevel--, numMips--)
2266 {
2267 int size = CalculateMipSize(width, height, picFormat);
2268 width = MAX(1, width >> 1);
2269 height = MAX(1, height >> 1);
2270 pic += size;
2271 }
2272 }
2273 }
2274
2275 image->uploadWidth = width;
2276 image->uploadHeight = height;
2277
2278 // Allocate texture storage so we don't have to worry about it later.
2279 mipWidth = width;
2280 mipHeight = height;
2281 miplevel = 0;
2282 do
2283 {
2284 lastMip = !mipmap || (mipWidth == 1 && mipHeight == 1);
2285 if (cubemap)
2286 {
2287 int i;
2288
2289 for (i = 0; i < 6; i++)
2290 qglTextureImage2DEXT(image->texnum, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, miplevel, internalFormat, mipWidth, mipHeight, 0, dataFormat, dataType, NULL);
2291 }
2292 else
2293 {
2294 qglTextureImage2DEXT(image->texnum, GL_TEXTURE_2D, miplevel, internalFormat, mipWidth, mipHeight, 0, dataFormat, dataType, NULL);
2295 }
2296
2297 mipWidth = MAX(1, mipWidth >> 1);
2298 mipHeight = MAX(1, mipHeight >> 1);
2299 miplevel++;
2300 }
2301 while (!lastMip);
2302
2303 // Upload data.
2304 if (pic)
2305 Upload32(pic, 0, 0, width, height, picFormat, dataFormat, dataType, numMips, image, scaled);
2306
2307 if (resampledBuffer != NULL)
2308 ri.Hunk_FreeTempMemory(resampledBuffer);
2309
2310 // Set all necessary texture parameters.
2311 qglTextureParameterfEXT(image->texnum, textureTarget, GL_TEXTURE_WRAP_S, glWrapClampMode);
2312 qglTextureParameterfEXT(image->texnum, textureTarget, GL_TEXTURE_WRAP_T, glWrapClampMode);
2313
2314 if (cubemap)
2315 qglTextureParameteriEXT(image->texnum, textureTarget, GL_TEXTURE_WRAP_R, glWrapClampMode);
2316
2317 if (textureFilterAnisotropic && !cubemap)
2318 qglTextureParameteriEXT(image->texnum, textureTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT,
2319 mipmap ? (GLint)Com_Clamp(1, maxAnisotropy, r_ext_max_anisotropy->integer) : 1);
2320
2321 switch(internalFormat)
2322 {
2323 case GL_DEPTH_COMPONENT:
2324 case GL_DEPTH_COMPONENT16_ARB:
2325 case GL_DEPTH_COMPONENT24_ARB:
2326 case GL_DEPTH_COMPONENT32_ARB:
2327 // Fix for sampling depth buffer on old nVidia cards.
2328 // from http://www.idevgames.com/forums/thread-4141-post-34844.html#pid34844
2329 if ( !QGL_VERSION_ATLEAST( 3, 0 ) ) {
2330 qglTextureParameterfEXT(image->texnum, textureTarget, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
2331 }
2332 qglTextureParameterfEXT(image->texnum, textureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2333 qglTextureParameterfEXT(image->texnum, textureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2334 break;
2335 default:
2336 qglTextureParameterfEXT(image->texnum, textureTarget, GL_TEXTURE_MIN_FILTER, mipmap ? gl_filter_min : GL_LINEAR);
2337 qglTextureParameterfEXT(image->texnum, textureTarget, GL_TEXTURE_MAG_FILTER, mipmap ? gl_filter_max : GL_LINEAR);
2338 break;
2339 }
2340
2341 GL_CheckErrors();
2342
2343 hash = generateHashValue(name);
2344 image->next = hashTable[hash];
2345 hashTable[hash] = image;
2346
2347 return image;
2348 }
2349
2350
2351 /*
2352 ================
2353 R_CreateImage
2354
2355 Wrapper for R_CreateImage2(), for the old parameters.
2356 ================
2357 */
2358 image_t *R_CreateImage(const char *name, byte *pic, int width, int height, imgType_t type, imgFlags_t flags, int internalFormat)
2359 {
2360 return R_CreateImage2(name, pic, width, height, GL_RGBA8, 0, type, flags, internalFormat);
2361 }
2362
2363
2364 void R_UpdateSubImage( image_t *image, byte *pic, int x, int y, int width, int height, GLenum picFormat )
2365 {
2366 GLenum dataFormat, dataType;
2367
2368 // TODO: This is fine for lightmaps but (unused) general RGBA images need to store dataFormat / dataType in image_t for OpenGL ES?
2369 dataFormat = PixelDataFormatFromInternalFormat(image->internalFormat);
2370 dataType = picFormat == GL_RGBA16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
2371
2372 Upload32(pic, x, y, width, height, picFormat, dataFormat, dataType, 0, image, qfalse);
2373 }
2374
2375 //===================================================================
2376
2377 // Prototype for dds loader function which isn't common to both renderers
2378 void R_LoadDDS(const char *filename, byte **pic, int *width, int *height, GLenum *picFormat, int *numMips);
2379
2380 typedef struct
2381 {
2382 char *ext;
2383 void (*ImageLoader)( const char *, unsigned char **, int *, int * );
2384 } imageExtToLoaderMap_t;
2385
2386 // Note that the ordering indicates the order of preference used
2387 // when there are multiple images of different formats available
2388 static imageExtToLoaderMap_t imageLoaders[ ] =
2389 {
2390 { "png", R_LoadPNG },
2391 { "tga", R_LoadTGA },
2392 { "jpg", R_LoadJPG },
2393 { "jpeg", R_LoadJPG },
2394 { "pcx", R_LoadPCX },
2395 { "bmp", R_LoadBMP },
2396 { "pvr", R_LoadPVR }
2397 };
2398
2399 static int numImageLoaders = ARRAY_LEN( imageLoaders );
2400
2401 /*
2402 =================
2403 R_LoadImage
2404
2405 Loads any of the supported image types into a canonical
2406 32 bit format.
2407 =================
2408 */
2409 void R_LoadImage( const char *name, byte **pic, int *width, int *height, GLenum *picFormat, int *numMips )
2410 {
2411 qboolean orgNameFailed = qfalse;
2412 int orgLoader = -1;
2413 int i;
2414 char base[ MAX_QPATH ];
2415 char localName[ MAX_QPATH ];
2416 char ddsName[ MAX_QPATH ];
2417 const char *ext;
2418 char *altName;
2419
2420 *pic = NULL;
2421 *width = 0;
2422 *height = 0;
2423 *picFormat = GL_RGBA8;
2424 *numMips = 0;
2425
2426 Q_strncpyz(localName, name, sizeof(localName));
2427 ext = COM_GetExtension(localName);
2428 COM_StripExtension(name, base, sizeof(base));
2429
2430 // Build DDS name
2431 Q_strncpyz(ddsName, base, sizeof(ddsName));
2432 Q_strcat(ddsName, sizeof(ddsName), ".dds");
2433
2434 if (r_ext_compressed_textures->integer) {
2435 // Try DDS first
2436 R_LoadDDS(ddsName, pic, width, height, picFormat, numMips);
2437 if (*pic) return;
2438
2439 // Then try explicitly requested extension (if not DDS)
2440 if (ext && *ext && Q_stricmp(ext, "dds")) {
2441 for (i = 0; i < numImageLoaders; i++) {
2442 if (!Q_stricmp(ext, imageLoaders[i].ext)) {
2443 imageLoaders[i].ImageLoader(localName, pic, width, height);
2444 if (*pic) return;
2445 orgNameFailed = qtrue;
2446 orgLoader = i;
2447 break;
2448 }
2449 }
2450 COM_StripExtension(name, localName, sizeof(localName));
2451 }
2452
2453 // Then probe all supported formats except the failed one
2454 for (i = 0; i < numImageLoaders; i++) {
2455 if (i == orgLoader)
2456 continue;
2457 altName = va("%s.%s", localName, imageLoaders[i].ext);
2458 imageLoaders[i].ImageLoader(altName, pic, width, height);
2459 if (*pic) {
2460 if (orgNameFailed)
2461 ri.Printf(PRINT_DEVELOPER, "WARNING: %s not present, using %s instead\n", name, altName);
2462 return;
2463 }
2464 }
2465
2466 return;
2467 }
2468
2469 // Explicit non-dds extension requested, so try its image loader
2470 if (ext && *ext && Q_stricmp(ext, "dds")) {
2471 for (i = 0; i < numImageLoaders; i++) {
2472 if (!Q_stricmp(ext, imageLoaders[i].ext)) {
2473 imageLoaders[i].ImageLoader(localName, pic, width, height);
2474 if (*pic) return;
2475 orgNameFailed = qtrue;
2476 orgLoader = i;
2477 break;
2478 }
2479 }
2480 COM_StripExtension(name, localName, sizeof(localName));
2481 }
2482
2483 // Try all other uncompressed formats
2484 for (i = 0; i < numImageLoaders; i++) {
2485 if (i == orgLoader)
2486 continue;
2487 if (!Q_stricmp(imageLoaders[i].ext, "dds"))
2488 continue;
2489 altName = va("%s.%s", localName, imageLoaders[i].ext);
2490 imageLoaders[i].ImageLoader(altName, pic, width, height);
2491 if (*pic) {
2492 if (orgNameFailed)
2493 ri.Printf(PRINT_DEVELOPER, "WARNING: %s not present, using %s instead\n", name, altName);
2494 return;
2495 }
2496 }
2497
2498 // Finally, allow DDS fallback even when compressed textures are off
2499 if (!*pic) {
2500 R_LoadDDS(ddsName, pic, width, height, picFormat, numMips);
2501 if (*pic) ri.Printf(PRINT_DEVELOPER, "WARNING: falling back to compressed texture %s when r_ext_compressed_textures=0\n", ddsName);
2502 }
2503 }
2504
2505
2506 /*
2507 ===============
2508 R_FindImageFile
2509
2510 Finds or loads the given image.
2511 Returns NULL if it fails, not a default image.
2512 ==============
2513 */
2514 image_t *R_FindImageFile( const char *name, imgType_t type, imgFlags_t flags )
2515 {
2516 image_t *image;
2517 int width, height;
2518 byte *pic;
2519 GLenum picFormat;
2520 int picNumMips;
2521 long hash;
2522 imgFlags_t checkFlagsTrue, checkFlagsFalse;
2523
2524 if (!name) {
2525 return NULL;
2526 }
2527
2528 hash = generateHashValue(name);
2529
2530 //
2531 // see if the image is already loaded
2532 //
2533 for (image=hashTable[hash]; image; image=image->next) {
2534 if ( !strcmp( name, image->imgName ) ) {
2535 // the white image can be used with any set of parms, but other mismatches are errors
2536 if ( strcmp( name, "*white" ) ) {
2537 if ( image->flags != flags ) {
2538 ri.Printf( PRINT_DEVELOPER, "WARNING: reused image %s with mixed flags (%i vs %i)\n", name, image->flags, flags );
2539 }
2540 }
2541 return image;
2542 }
2543 }
2544
2545 //
2546 // load the pic from disk
2547 //
2548 R_LoadImage( name, &pic, &width, &height, &picFormat, &picNumMips );
2549 if ( pic == NULL ) {
2550 return NULL;
2551 }
2552
2553 checkFlagsTrue = IMGFLAG_PICMIP | IMGFLAG_MIPMAP | IMGFLAG_GENNORMALMAP;
2554 checkFlagsFalse = IMGFLAG_CUBEMAP;
2555 if (r_normalMapping->integer && (picFormat == GL_RGBA8) && (type == IMGTYPE_COLORALPHA) &&
2556 ((flags & checkFlagsTrue) == checkFlagsTrue) && !(flags & checkFlagsFalse))
2557 {
2558 char normalName[MAX_QPATH];
2559 image_t *normalImage;
2560 int normalWidth, normalHeight;
2561 imgFlags_t normalFlags;
2562
2563 normalFlags = (flags & ~IMGFLAG_GENNORMALMAP) | IMGFLAG_NOLIGHTSCALE;
2564
2565 COM_StripExtension(name, normalName, MAX_QPATH);
2566 Q_strcat(normalName, MAX_QPATH, "_n");
2567
2568 // find normalmap in case it's there
2569 normalImage = R_FindImageFile(normalName, IMGTYPE_NORMAL, normalFlags);
2570
2571 // if not, generate it
2572 if (normalImage == NULL)
2573 {
2574 byte *normalPic;
2575 int x, y;
2576
2577 normalWidth = width;
2578 normalHeight = height;
2579 normalPic = ri.Malloc(width * height * 4);
2580 RGBAtoNormal(pic, normalPic, width, height, flags & IMGFLAG_CLAMPTOEDGE);
2581
2582 #if 1
2583 // Brighten up the original image to work with the normal map
2584 RGBAtoYCoCgA(pic, pic, width, height);
2585 for (y = 0; y < height; y++)
2586 {
2587 byte *picbyte = pic + y * width * 4;
2588 byte *normbyte = normalPic + y * width * 4;
2589 for (x = 0; x < width; x++)
2590 {
2591 int div = MAX(normbyte[2] - 127, 16);
2592 picbyte[0] = CLAMP(picbyte[0] * 128 / div, 0, 255);
2593 picbyte += 4;
2594 normbyte += 4;
2595 }
2596 }
2597 YCoCgAtoRGBA(pic, pic, width, height);
2598 #else
2599 // Blur original image's luma to work with the normal map
2600 {
2601 byte *blurPic;
2602
2603 RGBAtoYCoCgA(pic, pic, width, height);
2604 blurPic = ri.Malloc(width * height);
2605
2606 for (y = 1; y < height - 1; y++)
2607 {
2608 byte *picbyte = pic + y * width * 4;
2609 byte *blurbyte = blurPic + y * width;
2610
2611 picbyte += 4;
2612 blurbyte += 1;
2613
2614 for (x = 1; x < width - 1; x++)
2615 {
2616 int result;
2617
2618 result = *(picbyte - (width + 1) * 4) + *(picbyte - width * 4) + *(picbyte - (width - 1) * 4) +
2619 *(picbyte - 1 * 4) + *(picbyte ) + *(picbyte + 1 * 4) +
2620 *(picbyte + (width - 1) * 4) + *(picbyte + width * 4) + *(picbyte + (width + 1) * 4);
2621
2622 result /= 9;
2623
2624 *blurbyte = result;
2625 picbyte += 4;
2626 blurbyte += 1;
2627 }
2628 }
2629
2630 // FIXME: do borders
2631
2632 for (y = 1; y < height - 1; y++)
2633 {
2634 byte *picbyte = pic + y * width * 4;
2635 byte *blurbyte = blurPic + y * width;
2636
2637 picbyte += 4;
2638 blurbyte += 1;
2639
2640 for (x = 1; x < width - 1; x++)
2641 {
2642 picbyte[0] = *blurbyte;
2643 picbyte += 4;
2644 blurbyte += 1;
2645 }
2646 }
2647
2648 ri.Free(blurPic);
2649
2650 YCoCgAtoRGBA(pic, pic, width, height);
2651 }
2652 #endif
2653
2654 R_CreateImage( normalName, normalPic, normalWidth, normalHeight, IMGTYPE_NORMAL, normalFlags, 0 );
2655 ri.Free( normalPic );
2656 }
2657 }
2658
2659 // force mipmaps off if image is compressed but doesn't have enough mips
2660 if ((flags & IMGFLAG_MIPMAP) && picFormat != GL_RGBA8 && picFormat != GL_SRGB8_ALPHA8_EXT)
2661 {
2662 int wh = MAX(width, height);
2663 int neededMips = 0;
2664 while (wh)
2665 {
2666 neededMips++;
2667 wh >>= 1;
2668 }
2669 if (neededMips > picNumMips)
2670 flags &= ~IMGFLAG_MIPMAP;
2671 }
2672
2673 image = R_CreateImage2( ( char * ) name, pic, width, height, picFormat, picNumMips, type, flags, 0 );
2674 ri.Free( pic );
2675 return image;
2676 }
2677
2678
2679 /*
2680 ================
2681 R_CreateDlightImage
2682 ================
2683 */
2684 #define DLIGHT_SIZE 16
2685 static void R_CreateDlightImage( void ) {
2686 int x,y;
2687 byte data[DLIGHT_SIZE][DLIGHT_SIZE][4];
2688 int b;
2689
2690 // make a centered inverse-square falloff blob for dynamic lighting
2691 for (x=0 ; x<DLIGHT_SIZE ; x++) {
2692 for (y=0 ; y<DLIGHT_SIZE ; y++) {
2693 float d;
2694
2695 d = ( DLIGHT_SIZE/2 - 0.5f - x ) * ( DLIGHT_SIZE/2 - 0.5f - x ) +
2696 ( DLIGHT_SIZE/2 - 0.5f - y ) * ( DLIGHT_SIZE/2 - 0.5f - y );
2697 b = 4000 / d;
2698 if (b > 255) {
2699 b = 255;
2700 } else if ( b < 75 ) {
2701 b = 0;
2702 }
2703 data[y][x][0] =
2704 data[y][x][1] =
2705 data[y][x][2] = b;
2706 data[y][x][3] = 255;
2707 }
2708 }
2709 tr.dlightImage = R_CreateImage("*dlight", (byte *)data, DLIGHT_SIZE, DLIGHT_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE, 0 );
2710 }
2711
2712
2713 /*
2714 =================
2715 R_InitFogTable
2716 =================
2717 */
2718 void R_InitFogTable( void ) {
2719 int i;
2720 float d;
2721 float exp;
2722
2723 exp = 0.5;
2724
2725 for ( i = 0 ; i < FOG_TABLE_SIZE ; i++ ) {
2726 d = pow ( (float)i/(FOG_TABLE_SIZE-1), exp );
2727
2728 tr.fogTable[i] = d;
2729 }
2730 }
2731
2732 /*
2733 ================
2734 R_FogFactor
2735
2736 Returns a 0.0 to 1.0 fog density value
2737 This is called for each texel of the fog texture on startup
2738 and for each vertex of transparent shaders in fog dynamically
2739 ================
2740 */
2741 float R_FogFactor( float s, float t ) {
2742 float d;
2743
2744 s -= 1.0/512;
2745 if ( s < 0 ) {
2746 return 0;
2747 }
2748 if ( t < 1.0/32 ) {
2749 return 0;
2750 }
2751 if ( t < 31.0/32 ) {
2752 s *= (t - 1.0f/32.0f) / (30.0f/32.0f);
2753 }
2754
2755 // we need to leave a lot of clamp range
2756 s *= 8;
2757
2758 if ( s > 1.0 ) {
2759 s = 1.0;
2760 }
2761
2762 d = tr.fogTable[ (int)(s * (FOG_TABLE_SIZE-1)) ];
2763
2764 return d;
2765 }
2766
2767 /*
2768 ================
2769 R_CreateFogImage
2770 ================
2771 */
2772 #define FOG_S 256
2773 #define FOG_T 32
2774 static void R_CreateFogImage( void ) {
2775 int x,y;
2776 byte *data;
2777 float d;
2778
2779 data = ri.Hunk_AllocateTempMemory( FOG_S * FOG_T * 4 );
2780
2781 // S is distance, T is depth
2782 for (x=0 ; x<FOG_S ; x++) {
2783 for (y=0 ; y<FOG_T ; y++) {
2784 d = R_FogFactor( ( x + 0.5f ) / FOG_S, ( y + 0.5f ) / FOG_T );
2785
2786 data[(y*FOG_S+x)*4+0] =
2787 data[(y*FOG_S+x)*4+1] =
2788 data[(y*FOG_S+x)*4+2] = 255;
2789 data[(y*FOG_S+x)*4+3] = 255*d;
2790 }
2791 }
2792 tr.fogImage = R_CreateImage("*fog", (byte *)data, FOG_S, FOG_T, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE, 0 );
2793 ri.Hunk_FreeTempMemory( data );
2794 }
2795
2796 /*
2797 ==================
2798 R_CreateDefaultImage
2799 ==================
2800 */
2801 #define DEFAULT_SIZE 16
2802 static void R_CreateDefaultImage( void ) {
2803 int x;
2804 byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
2805
2806 // the default image will be a box, to allow you to see the mapping coordinates
2807 Com_Memset( data, 32, sizeof( data ) );
2808 for ( x = 0 ; x < DEFAULT_SIZE ; x++ ) {
2809 data[0][x][0] =
2810 data[0][x][1] =
2811 data[0][x][2] =
2812 data[0][x][3] = 255;
2813
2814 data[x][0][0] =
2815 data[x][0][1] =
2816 data[x][0][2] =
2817 data[x][0][3] = 255;
2818
2819 data[DEFAULT_SIZE-1][x][0] =
2820 data[DEFAULT_SIZE-1][x][1] =
2821 data[DEFAULT_SIZE-1][x][2] =
2822 data[DEFAULT_SIZE-1][x][3] = 255;
2823
2824 data[x][DEFAULT_SIZE-1][0] =
2825 data[x][DEFAULT_SIZE-1][1] =
2826 data[x][DEFAULT_SIZE-1][2] =
2827 data[x][DEFAULT_SIZE-1][3] = 255;
2828 }
2829 tr.defaultImage = R_CreateImage("*default", (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_MIPMAP, 0);
2830 }
2831
2832 /*
2833 ==================
2834 R_CreateBuiltinImages
2835 ==================
2836 */
2837 void R_CreateBuiltinImages( void ) {
2838 int x,y;
2839 byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
2840
2841 R_CreateDefaultImage();
2842
2843 // we use a solid white image instead of disabling texturing
2844 Com_Memset( data, 255, sizeof( data ) );
2845 tr.whiteImage = R_CreateImage("*white", (byte *)data, 8, 8, IMGTYPE_COLORALPHA, IMGFLAG_NONE, 0);
2846
2847 if (r_dlightMode->integer >= 2)
2848 {
2849 for( x = 0; x < MAX_DLIGHTS; x++)
2850 {
2851 tr.shadowCubemaps[x] = R_CreateImage(va("*shadowcubemap%i", x), NULL, PSHADOW_MAP_SIZE, PSHADOW_MAP_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE | IMGFLAG_CUBEMAP, 0);
2852 }
2853 }
2854
2855 // with overbright bits active, we need an image which is some fraction of full color,
2856 // for default lightmaps, etc
2857 for (x=0 ; x<DEFAULT_SIZE ; x++) {
2858 for (y=0 ; y<DEFAULT_SIZE ; y++) {
2859 data[y][x][0] =
2860 data[y][x][1] =
2861 data[y][x][2] = tr.identityLightByte;
2862 data[y][x][3] = 255;
2863 }
2864 }
2865
2866 tr.identityLightImage = R_CreateImage("*identityLight", (byte *)data, 8, 8, IMGTYPE_COLORALPHA, IMGFLAG_NONE, 0);
2867
2868
2869 for(x=0;x<32;x++) {
2870 // scratchimage is usually used for cinematic drawing
2871 tr.scratchImage[x] = R_CreateImage("*scratch", (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_PICMIP | IMGFLAG_CLAMPTOEDGE, 0);
2872 }
2873
2874 R_CreateDlightImage();
2875 R_CreateFogImage();
2876
2877 if (glRefConfig.framebufferObject)
2878 {
2879 int width, height, hdrFormat, rgbFormat;
2880
2881 width = glConfig.vidWidth;
2882 height = glConfig.vidHeight;
2883
2884 hdrFormat = GL_RGBA8;
2885 if (r_hdr->integer && glRefConfig.textureFloat)
2886 hdrFormat = GL_RGBA16F_ARB;
2887
2888 rgbFormat = GL_RGBA8;
2889
2890 tr.renderImage = R_CreateImage("_render", NULL, width, height, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, hdrFormat);
2891
2892 if (r_shadowBlur->integer || r_hdr->integer)
2893 tr.screenScratchImage = R_CreateImage("screenScratch", NULL, width, height, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, rgbFormat);
2894
2895 if (r_shadowBlur->integer || r_ssao->integer)
2896 tr.hdrDepthImage = R_CreateImage("*hdrDepth", NULL, width, height, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_R32F);
2897
2898 if (r_drawSunRays->integer)
2899 tr.sunRaysImage = R_CreateImage("*sunRays", NULL, width, height, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, rgbFormat);
2900
2901 tr.renderDepthImage = R_CreateImage("*renderdepth", NULL, width, height, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_DEPTH_COMPONENT24);
2902 tr.textureDepthImage = R_CreateImage("*texturedepth", NULL, PSHADOW_MAP_SIZE, PSHADOW_MAP_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_DEPTH_COMPONENT24);
2903
2904 {
2905 void *p;
2906
2907 data[0][0][0] = 0;
2908 data[0][0][1] = 0.45f * 255;
2909 data[0][0][2] = 255;
2910 data[0][0][3] = 255;
2911 p = data;
2912
2913 tr.calcLevelsImage = R_CreateImage("*calcLevels", p, 1, 1, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, hdrFormat);
2914 tr.targetLevelsImage = R_CreateImage("*targetLevels", p, 1, 1, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, hdrFormat);
2915 tr.fixedLevelsImage = R_CreateImage("*fixedLevels", p, 1, 1, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, hdrFormat);
2916 }
2917
2918 for (x = 0; x < 2; x++)
2919 {
2920 tr.textureScratchImage[x] = R_CreateImage(va("*textureScratch%d", x), NULL, 256, 256, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8);
2921 }
2922 for (x = 0; x < 2; x++)
2923 {
2924 tr.quarterImage[x] = R_CreateImage(va("*quarter%d", x), NULL, width / 2, height / 2, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8);
2925 }
2926
2927 if (r_ssao->integer)
2928 {
2929 tr.screenSsaoImage = R_CreateImage("*screenSsao", NULL, width / 2, height / 2, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8);
2930 }
2931
2932 for( x = 0; x < MAX_DRAWN_PSHADOWS; x++)
2933 {
2934 tr.pshadowMaps[x] = R_CreateImage(va("*shadowmap%i", x), NULL, PSHADOW_MAP_SIZE, PSHADOW_MAP_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_DEPTH_COMPONENT24);
2935 //qglTextureParameterfEXT(tr.pshadowMaps[x]->texnum, GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
2936 //qglTextureParameterfEXT(tr.pshadowMaps[x]->texnum, GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
2937 }
2938
2939 if (r_sunlightMode->integer)
2940 {
2941 for ( x = 0; x < 4; x++)
2942 {
2943 tr.sunShadowDepthImage[x] = R_CreateImage(va("*sunshadowdepth%i", x), NULL, r_shadowMapSize->integer, r_shadowMapSize->integer, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_DEPTH_COMPONENT24);
2944 qglTextureParameterfEXT(tr.sunShadowDepthImage[x]->texnum, GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
2945 qglTextureParameterfEXT(tr.sunShadowDepthImage[x]->texnum, GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
2946 }
2947
2948 tr.screenShadowImage = R_CreateImage("*screenShadow", NULL, width, height, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8);
2949 }
2950
2951 if (r_cubeMapping->integer)
2952 {
2953 tr.renderCubeImage = R_CreateImage("*renderCube", NULL, r_cubemapSize->integer, r_cubemapSize->integer, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE | IMGFLAG_MIPMAP | IMGFLAG_CUBEMAP, rgbFormat);
2954 }
2955 }
2956 }
2957
2958
2959 /*
2960 ===============
2961 R_SetColorMappings
2962 ===============
2963 */
2964 void R_SetColorMappings( void ) {
2965 int i, j;
2966 float g;
2967 int inf;
2968
2969 // setup the overbright lighting
2970 tr.overbrightBits = r_overBrightBits->integer;
2971
2972 // allow 2 overbright bits
2973 if ( tr.overbrightBits > 2 ) {
2974 tr.overbrightBits = 2;
2975 } else if ( tr.overbrightBits < 0 ) {
2976 tr.overbrightBits = 0;
2977 }
2978
2979 // don't allow more overbright bits than map overbright bits
2980 if ( tr.overbrightBits > r_mapOverBrightBits->integer ) {
2981 tr.overbrightBits = r_mapOverBrightBits->integer;
2982 }
2983
2984 tr.identityLight = 1.0f / ( 1 << tr.overbrightBits );
2985 tr.identityLightByte = 255 * tr.identityLight;
2986
2987
2988 if ( r_intensity->value <= 1 ) {
2989 ri.Cvar_Set( "r_intensity", "1" );
2990 }
2991
2992 if ( r_gamma->value < 0.5f ) {
2993 ri.Cvar_Set( "r_gamma", "0.5" );
2994 } else if ( r_gamma->value > 3.0f ) {
2995 ri.Cvar_Set( "r_gamma", "3.0" );
2996 }
2997
2998 g = r_gamma->value;
2999
3000 for ( i = 0; i < 256; i++ ) {
3001 if ( g == 1 ) {
3002 inf = i;
3003 } else {
3004 inf = 255 * pow ( i/255.0f, 1.0f / g ) + 0.5f;
3005 }
3006
3007 if (inf < 0) {
3008 inf = 0;
3009 }
3010 if (inf > 255) {
3011 inf = 255;
3012 }
3013 s_gammatable[i] = inf;
3014 }
3015
3016 for (i=0 ; i<256 ; i++) {
3017 j = i * r_intensity->value;
3018 if (j > 255) {
3019 j = 255;
3020 }
3021 s_intensitytable[i] = j;
3022 }
3023
3024 if ( glConfig.deviceSupportsGamma )
3025 {
3026 GLimp_SetGamma( s_gammatable, s_gammatable, s_gammatable );
3027 }
3028 }
3029
3030 /*
3031 ===============
3032 R_InitImages
3033 ===============
3034 */
3035 void R_InitImages( void ) {
3036 Com_Memset(hashTable, 0, sizeof(hashTable));
3037 // build brightness translation tables
3038 R_SetColorMappings();
3039
3040 // create default texture and white texture
3041 R_CreateBuiltinImages();
3042 }
3043
3044 /*
3045 ===============
3046 R_DeleteTextures
3047 ===============
3048 */
3049 void R_DeleteTextures( void ) {
3050 int i;
3051
3052 for ( i=0; i<tr.numImages ; i++ ) {
3053 qglDeleteTextures( 1, &tr.images[i]->texnum );
3054 }
3055 Com_Memset( tr.images, 0, sizeof( tr.images ) );
3056
3057 tr.numImages = 0;
3058
3059 GL_BindNullTextures();
3060 }
3061
3062 /*
3063 ============================================================================
3064
3065 SKINS
3066
3067 ============================================================================
3068 */
3069
3070 /*
3071 ==================
3072 CommaParse
3073
3074 This is unfortunate, but the skin files aren't
3075 compatible with our normal parsing rules.
3076 ==================
3077 */
3078 static char *CommaParse( char **data_p ) {
3079 int c = 0, len;
3080 char *data;
3081 static char com_token[MAX_TOKEN_CHARS];
3082
3083 data = *data_p;
3084 len = 0;
3085 com_token[0] = 0;
3086
3087 // make sure incoming data is valid
3088 if ( !data ) {
3089 *data_p = NULL;
3090 return com_token;
3091 }
3092
3093 while ( 1 ) {
3094 // skip whitespace
3095 while( (c = *data) <= ' ') {
3096 if( !c ) {
3097 break;
3098 }
3099 data++;
3100 }
3101
3102
3103 c = *data;
3104
3105 // skip double slash comments
3106 if ( c == '/' && data[1] == '/' )
3107 {
3108 data += 2;
3109 while (*data && *data != '\n') {
3110 data++;
3111 }
3112 }
3113 // skip /* */ comments
3114 else if ( c=='/' && data[1] == '*' )
3115 {
3116 data += 2;
3117 while ( *data && ( *data != '*' || data[1] != '/' ) )
3118 {
3119 data++;
3120 }
3121 if ( *data )
3122 {
3123 data += 2;
3124 }
3125 }
3126 else
3127 {
3128 break;
3129 }
3130 }
3131
3132 if ( c == 0 ) {
3133 return "";
3134 }
3135
3136 // handle quoted strings
3137 if (c == '\"')
3138 {
3139 data++;
3140 while (1)
3141 {
3142 c = *data++;
3143 if (c=='\"' || !c)
3144 {
3145 com_token[len] = 0;
3146 *data_p = ( char * ) data;
3147 return com_token;
3148 }
3149 if (len < MAX_TOKEN_CHARS - 1)
3150 {
3151 com_token[len] = c;
3152 len++;
3153 }
3154 }
3155 }
3156
3157 // parse a regular word
3158 do
3159 {
3160 if (len < MAX_TOKEN_CHARS - 1)
3161 {
3162 com_token[len] = c;
3163 len++;
3164 }
3165 data++;
3166 c = *data;
3167 } while (c>32 && c != ',' );
3168
3169 com_token[len] = 0;
3170
3171 *data_p = ( char * ) data;
3172 return com_token;
3173 }
3174
3175
3176 /*
3177 ===============
3178 RE_RegisterSkin
3179
3180 ===============
3181 */
3182 qhandle_t RE_RegisterSkin( const char *name ) {
3183 skinSurface_t parseSurfaces[MAX_SKIN_SURFACES];
3184 qhandle_t hSkin;
3185 skin_t *skin;
3186 skinSurface_t *surf;
3187 union {
3188 char *c;
3189 void *v;
3190 } text;
3191 char *text_p;
3192 char *token;
3193 char surfName[MAX_QPATH];
3194 int totalSurfaces;
3195
3196 if ( !name || !name[0] ) {
3197 ri.Printf( PRINT_DEVELOPER, "Empty name passed to RE_RegisterSkin\n" );
3198 return 0;
3199 }
3200
3201 if ( strlen( name ) >= MAX_QPATH ) {
3202 ri.Printf( PRINT_DEVELOPER, "Skin name exceeds MAX_QPATH\n" );
3203 return 0;
3204 }
3205
3206
3207 // see if the skin is already loaded
3208 for ( hSkin = 1; hSkin < tr.numSkins ; hSkin++ ) {
3209 skin = tr.skins[hSkin];
3210 if ( !Q_stricmp( skin->name, name ) ) {
3211 if( skin->numSurfaces == 0 ) {
3212 return 0; // default skin
3213 }
3214 return hSkin;
3215 }
3216 }
3217
3218 // allocate a new skin
3219 if ( tr.numSkins == MAX_SKINS ) {
3220 ri.Printf( PRINT_WARNING, "WARNING: RE_RegisterSkin( '%s' ) MAX_SKINS hit\n", name );
3221 return 0;
3222 }
3223 tr.numSkins++;
3224 skin = ri.Hunk_Alloc( sizeof( skin_t ), h_low );
3225 tr.skins[hSkin] = skin;
3226 Q_strncpyz( skin->name, name, sizeof( skin->name ) );
3227 skin->numSurfaces = 0;
3228
3229 R_IssuePendingRenderCommands();
3230
3231 // If not a .skin file, load as a single shader
3232 if ( strcmp( name + strlen( name ) - 5, ".skin" ) ) {
3233 skin->numSurfaces = 1;
3234 skin->surfaces = ri.Hunk_Alloc( sizeof( skinSurface_t ), h_low );
3235 skin->surfaces[0].shader = R_FindShader( name, LIGHTMAP_NONE, qtrue );
3236 return hSkin;
3237 }
3238
3239 // load and parse the skin file
3240 ri.FS_ReadFile( name, &text.v );
3241 if ( !text.c ) {
3242 return 0;
3243 }
3244
3245 totalSurfaces = 0;
3246 text_p = text.c;
3247 while ( text_p && *text_p ) {
3248 // get surface name
3249 token = CommaParse( &text_p );
3250 Q_strncpyz( surfName, token, sizeof( surfName ) );
3251
3252 if ( !token[0] ) {
3253 break;
3254 }
3255 // lowercase the surface name so skin compares are faster
3256 Q_strlwr( surfName );
3257
3258 if ( *text_p == ',' ) {
3259 text_p++;
3260 }
3261
3262 if ( strstr( token, "tag_" ) ) {
3263 continue;
3264 }
3265
3266 // parse the shader name
3267 token = CommaParse( &text_p );
3268
3269 if ( skin->numSurfaces < MAX_SKIN_SURFACES ) {
3270 surf = &parseSurfaces[skin->numSurfaces];
3271 Q_strncpyz( surf->name, surfName, sizeof( surf->name ) );
3272 surf->shader = R_FindShader( token, LIGHTMAP_NONE, qtrue );
3273 skin->numSurfaces++;
3274 }
3275
3276 totalSurfaces++;
3277 }
3278
3279 ri.FS_FreeFile( text.v );
3280
3281 if ( totalSurfaces > MAX_SKIN_SURFACES ) {
3282 ri.Printf( PRINT_WARNING, "WARNING: Ignoring excess surfaces (found %d, max is %d) in skin '%s'!\n",
3283 totalSurfaces, MAX_SKIN_SURFACES, name );
3284 }
3285
3286 // never let a skin have 0 shaders
3287 if ( skin->numSurfaces == 0 ) {
3288 return 0; // use default skin
3289 }
3290
3291 // copy surfaces to skin
3292 skin->surfaces = ri.Hunk_Alloc( skin->numSurfaces * sizeof( skinSurface_t ), h_low );
3293 memcpy( skin->surfaces, parseSurfaces, skin->numSurfaces * sizeof( skinSurface_t ) );
3294
3295 return hSkin;
3296 }
3297
3298
3299 /*
3300 ===============
3301 R_InitSkins
3302 ===============
3303 */
3304 void R_InitSkins( void ) {
3305 skin_t *skin;
3306
3307 tr.numSkins = 1;
3308
3309 // make the default skin have all default shaders
3310 skin = tr.skins[0] = ri.Hunk_Alloc( sizeof( skin_t ), h_low );
3311 Q_strncpyz( skin->name, "<default skin>", sizeof( skin->name ) );
3312 skin->numSurfaces = 1;
3313 skin->surfaces = ri.Hunk_Alloc( sizeof( skinSurface_t ), h_low );
3314 skin->surfaces[0].shader = tr.defaultShader;
3315 }
3316
3317 /*
3318 ===============
3319 R_GetSkinByHandle
3320 ===============
3321 */
3322 skin_t *R_GetSkinByHandle( qhandle_t hSkin ) {
3323 if ( hSkin < 1 || hSkin >= tr.numSkins ) {
3324 return tr.skins[0];
3325 }
3326 return tr.skins[ hSkin ];
3327 }
3328
3329 /*
3330 ===============
3331 R_SkinList_f
3332 ===============
3333 */
3334 void R_SkinList_f( void ) {
3335 int i, j;
3336 skin_t *skin;
3337
3338 ri.Printf (PRINT_ALL, "------------------\n");
3339
3340 for ( i = 0 ; i < tr.numSkins ; i++ ) {
3341 skin = tr.skins[i];
3342
3343 ri.Printf( PRINT_ALL, "%3i:%s (%d surfaces)\n", i, skin->name, skin->numSurfaces );
3344 for ( j = 0 ; j < skin->numSurfaces ; j++ ) {
3345 ri.Printf( PRINT_ALL, " %s = %s\n",
3346 skin->surfaces[j].name, skin->surfaces[j].shader->name );
3347 }
3348 }
3349 ri.Printf (PRINT_ALL, "------------------\n");
3350 }
3351
3352