Debian Codegraph @

code/renderergl1/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 static byte s_intensitytable[256];
26 static unsigned char s_gammatable[256];
27
28 int gl_filter_min = GL_LINEAR_MIPMAP_NEAREST;
29 int gl_filter_max = GL_LINEAR;
30
31 #define FILE_HASH_SIZE 1024
32 static image_t* hashTable[FILE_HASH_SIZE];
33
34 /*
35 ** R_GammaCorrect
36 */
37 void R_GammaCorrect( byte *buffer, int bufSize ) {
38 int i;
39
40 for ( i = 0; i < bufSize; i++ ) {
41 buffer[i] = s_gammatable[buffer[i]];
42 }
43 }
44
45 typedef struct {
46 char *name;
47 int minimize, maximize;
48 } textureMode_t;
49
50 textureMode_t modes[] = {
51 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
52 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
53 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
54 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
55 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
56 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
57 };
58
59 /*
60 ================
61 return a hash value for the filename
62 ================
63 */
64 static long generateHashValue( const char *fname ) {
65 int i;
66 long hash;
67 char letter;
68
69 hash = 0;
70 i = 0;
71 while (fname[i] != '\0') {
72 letter = tolower(fname[i]);
73 if (letter =='.') break; // don't include extension
74 if (letter =='\\') letter = '/'; // damn path names
75 hash+=(long)(letter)*(i+119);
76 i++;
77 }
78 hash &= (FILE_HASH_SIZE-1);
79 return hash;
80 }
81
82 /*
83 ===============
84 GL_TextureMode
85 ===============
86 */
87 void GL_TextureMode( const char *string ) {
88 int i;
89 image_t *glt;
90
91 for ( i=0 ; i< 6 ; i++ ) {
92 if ( !Q_stricmp( modes[i].name, string ) ) {
93 break;
94 }
95 }
96
97 // hack to prevent trilinear from being set on voodoo,
98 // because their driver freaks...
99 if ( i == 5 && glConfig.hardwareType == GLHW_3DFX_2D3D ) {
100 ri.Printf( PRINT_ALL, "Refusing to set trilinear on a voodoo.\n" );
101 i = 3;
102 }
103
104
105 if ( i == 6 ) {
106 ri.Printf (PRINT_ALL, "bad filter name\n");
107 return;
108 }
109
110 gl_filter_min = modes[i].minimize;
111 gl_filter_max = modes[i].maximize;
112
113 // change all the existing mipmap texture objects
114 for ( i = 0 ; i < tr.numImages ; i++ ) {
115 glt = tr.images[ i ];
116 if ( glt->flags & IMGFLAG_MIPMAP ) {
117 GL_Bind (glt);
118 qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
119 qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
120 }
121 }
122 }
123
124 /*
125 ===============
126 R_SumOfUsedImages
127 ===============
128 */
129 int R_SumOfUsedImages( void ) {
130 int total;
131 int i;
132
133 total = 0;
134 for ( i = 0; i < tr.numImages; i++ ) {
135 if ( tr.images[i]->frameUsed == tr.frameCount ) {
136 total += tr.images[i]->uploadWidth * tr.images[i]->uploadHeight;
137 }
138 }
139
140 return total;
141 }
142
143 /*
144 ===============
145 R_ImageList_f
146 ===============
147 */
148 void R_ImageList_f( void ) {
149 int i;
150 int estTotalSize = 0;
151
152 ri.Printf(PRINT_ALL, "\n -w-- -h-- type -size- --name-------\n");
153
154 for ( i = 0 ; i < tr.numImages ; i++ )
155 {
156 image_t *image = tr.images[i];
157 char *format = "???? ";
158 char *sizeSuffix;
159 int estSize;
160 int displaySize;
161
162 estSize = image->uploadHeight * image->uploadWidth;
163
164 switch(image->internalFormat)
165 {
166 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
167 format = "sDXT1";
168 // 64 bits per 16 pixels, so 4 bits per pixel
169 estSize /= 2;
170 break;
171 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
172 format = "sDXT5";
173 // 128 bits per 16 pixels, so 1 byte per pixel
174 break;
175 case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB:
176 format = "sBPTC";
177 // 128 bits per 16 pixels, so 1 byte per pixel
178 break;
179 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
180 format = "LATC ";
181 // 128 bits per 16 pixels, so 1 byte per pixel
182 break;
183 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
184 format = "DXT1 ";
185 // 64 bits per 16 pixels, so 4 bits per pixel
186 estSize /= 2;
187 break;
188 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
189 format = "DXT5 ";
190 // 128 bits per 16 pixels, so 1 byte per pixel
191 break;
192 case GL_COMPRESSED_RGBA_BPTC_UNORM_ARB:
193 format = "BPTC ";
194 // 128 bits per 16 pixels, so 1 byte per pixel
195 break;
196 case GL_RGB4_S3TC:
197 format = "S3TC ";
198 // same as DXT1?
199 estSize /= 2;
200 break;
201 case GL_RGBA4:
202 case GL_RGBA8:
203 case GL_RGBA:
204 format = "RGBA ";
205 // 4 bytes per pixel
206 estSize *= 4;
207 break;
208 case GL_LUMINANCE8:
209 case GL_LUMINANCE:
210 format = "L ";
211 // 1 byte per pixel?
212 break;
213 case GL_RGB5:
214 case GL_RGB8:
215 case GL_RGB:
216 format = "RGB ";
217 // 3 bytes per pixel?
218 estSize *= 3;
219 break;
220 case GL_LUMINANCE8_ALPHA8:
221 case GL_LUMINANCE_ALPHA:
222 format = "LA ";
223 // 2 bytes per pixel?
224 estSize *= 2;
225 break;
226 case GL_SRGB_EXT:
227 case GL_SRGB8_EXT:
228 format = "sRGB ";
229 // 3 bytes per pixel?
230 estSize *= 3;
231 break;
232 case GL_SRGB_ALPHA_EXT:
233 case GL_SRGB8_ALPHA8_EXT:
234 format = "sRGBA";
235 // 4 bytes per pixel?
236 estSize *= 4;
237 break;
238 case GL_SLUMINANCE_EXT:
239 case GL_SLUMINANCE8_EXT:
240 format = "sL ";
241 // 1 byte per pixel?
242 break;
243 case GL_SLUMINANCE_ALPHA_EXT:
244 case GL_SLUMINANCE8_ALPHA8_EXT:
245 format = "sLA ";
246 // 2 byte per pixel?
247 estSize *= 2;
248 break;
249 }
250
251 // mipmap adds about 50%
252 if (image->flags & IMGFLAG_MIPMAP)
253 estSize += estSize / 2;
254
255 sizeSuffix = "b ";
256 displaySize = estSize;
257
258 if (displaySize > 1024)
259 {
260 displaySize /= 1024;
261 sizeSuffix = "kb";
262 }
263
264 if (displaySize > 1024)
265 {
266 displaySize /= 1024;
267 sizeSuffix = "Mb";
268 }
269
270 if (displaySize > 1024)
271 {
272 displaySize /= 1024;
273 sizeSuffix = "Gb";
274 }
275
276 ri.Printf(PRINT_ALL, "%4i: %4ix%4i %s %4i%s %s\n", i, image->uploadWidth, image->uploadHeight, format, displaySize, sizeSuffix, image->imgName);
277 estTotalSize += estSize;
278 }
279
280 ri.Printf (PRINT_ALL, " ---------\n");
281 ri.Printf (PRINT_ALL, " approx %i bytes\n", estTotalSize);
282 ri.Printf (PRINT_ALL, " %i total images\n\n", tr.numImages );
283 }
284
285 //=======================================================================
286
287 /*
288 ================
289 ResampleTexture
290
291 Used to resample images in a more general than quartering fashion.
292
293 This will only be filtered properly if the resampled size
294 is greater than half the original size.
295
296 If a larger shrinking is needed, use the mipmap function
297 before or after.
298 ================
299 */
300 static void ResampleTexture( unsigned *in, int inwidth, int inheight, unsigned *out,
301 int outwidth, int outheight ) {
302 int i, j;
303 unsigned *inrow, *inrow2;
304 unsigned frac, fracstep;
305 unsigned p1[2048], p2[2048];
306 byte *pix1, *pix2, *pix3, *pix4;
307
308 if (outwidth>2048)
309 ri.Error(ERR_DROP, "ResampleTexture: max width");
310
311 fracstep = inwidth*0x10000/outwidth;
312
313 frac = fracstep>>2;
314 for ( i=0 ; i<outwidth ; i++ ) {
315 p1[i] = 4*(frac>>16);
316 frac += fracstep;
317 }
318 frac = 3*(fracstep>>2);
319 for ( i=0 ; i<outwidth ; i++ ) {
320 p2[i] = 4*(frac>>16);
321 frac += fracstep;
322 }
323
324 for (i=0 ; i<outheight ; i++, out += outwidth) {
325 inrow = in + inwidth*(int)((i+0.25)*inheight/outheight);
326 inrow2 = in + inwidth*(int)((i+0.75)*inheight/outheight);
327 for (j=0 ; j<outwidth ; j++) {
328 pix1 = (byte *)inrow + p1[j];
329 pix2 = (byte *)inrow + p2[j];
330 pix3 = (byte *)inrow2 + p1[j];
331 pix4 = (byte *)inrow2 + p2[j];
332 ((byte *)(out+j))[0] = (pix1[0] + pix2[0] + pix3[0] + pix4[0])>>2;
333 ((byte *)(out+j))[1] = (pix1[1] + pix2[1] + pix3[1] + pix4[1])>>2;
334 ((byte *)(out+j))[2] = (pix1[2] + pix2[2] + pix3[2] + pix4[2])>>2;
335 ((byte *)(out+j))[3] = (pix1[3] + pix2[3] + pix3[3] + pix4[3])>>2;
336 }
337 }
338 }
339
340 /*
341 ================
342 R_LightScaleTexture
343
344 Scale up the pixel values in a texture to increase the
345 lighting range
346 ================
347 */
348 void R_LightScaleTexture (unsigned *in, int inwidth, int inheight, qboolean only_gamma )
349 {
350 if ( only_gamma )
351 {
352 if ( !glConfig.deviceSupportsGamma )
353 {
354 int i, c;
355 byte *p;
356
357 p = (byte *)in;
358
359 c = inwidth*inheight;
360 for (i=0 ; i<c ; i++, p+=4)
361 {
362 p[0] = s_gammatable[p[0]];
363 p[1] = s_gammatable[p[1]];
364 p[2] = s_gammatable[p[2]];
365 }
366 }
367 }
368 else
369 {
370 int i, c;
371 byte *p;
372
373 p = (byte *)in;
374
375 c = inwidth*inheight;
376
377 if ( glConfig.deviceSupportsGamma )
378 {
379 for (i=0 ; i<c ; i++, p+=4)
380 {
381 p[0] = s_intensitytable[p[0]];
382 p[1] = s_intensitytable[p[1]];
383 p[2] = s_intensitytable[p[2]];
384 }
385 }
386 else
387 {
388 for (i=0 ; i<c ; i++, p+=4)
389 {
390 p[0] = s_gammatable[s_intensitytable[p[0]]];
391 p[1] = s_gammatable[s_intensitytable[p[1]]];
392 p[2] = s_gammatable[s_intensitytable[p[2]]];
393 }
394 }
395 }
396 }
397
398
399 /*
400 ================
401 R_MipMap2
402
403 Operates in place, quartering the size of the texture
404 Proper linear filter
405 ================
406 */
407 static void R_MipMap2( unsigned *in, int inWidth, int inHeight ) {
408 int i, j, k;
409 byte *outpix;
410 int inWidthMask, inHeightMask;
411 int total;
412 int outWidth, outHeight;
413 unsigned *temp;
414
415 outWidth = inWidth >> 1;
416 outHeight = inHeight >> 1;
417 temp = ri.Hunk_AllocateTempMemory( outWidth * outHeight * 4 );
418
419 inWidthMask = inWidth - 1;
420 inHeightMask = inHeight - 1;
421
422 for ( i = 0 ; i < outHeight ; i++ ) {
423 for ( j = 0 ; j < outWidth ; j++ ) {
424 outpix = (byte *) ( temp + i * outWidth + j );
425 for ( k = 0 ; k < 4 ; k++ ) {
426 total =
427 1 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
428 2 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
429 2 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
430 1 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] +
431
432 2 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
433 4 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
434 4 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
435 2 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] +
436
437 2 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
438 4 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
439 4 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
440 2 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] +
441
442 1 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] +
443 2 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] +
444 2 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] +
445 1 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k];
446 outpix[k] = total / 36;
447 }
448 }
449 }
450
451 Com_Memcpy( in, temp, outWidth * outHeight * 4 );
452 ri.Hunk_FreeTempMemory( temp );
453 }
454
455 /*
456 ================
457 R_MipMap
458
459 Operates in place, quartering the size of the texture
460 ================
461 */
462 static void R_MipMap (byte *in, int width, int height) {
463 int i, j;
464 byte *out;
465 int row;
466
467 if ( !r_simpleMipMaps->integer ) {
468 R_MipMap2( (unsigned *)in, width, height );
469 return;
470 }
471
472 if ( width == 1 && height == 1 ) {
473 return;
474 }
475
476 row = width * 4;
477 out = in;
478 width >>= 1;
479 height >>= 1;
480
481 if ( width == 0 || height == 0 ) {
482 width += height; // get largest
483 for (i=0 ; i<width ; i++, out+=4, in+=8 ) {
484 out[0] = ( in[0] + in[4] )>>1;
485 out[1] = ( in[1] + in[5] )>>1;
486 out[2] = ( in[2] + in[6] )>>1;
487 out[3] = ( in[3] + in[7] )>>1;
488 }
489 return;
490 }
491
492 for (i=0 ; i<height ; i++, in+=row) {
493 for (j=0 ; j<width ; j++, out+=4, in+=8) {
494 out[0] = (in[0] + in[4] + in[row+0] + in[row+4])>>2;
495 out[1] = (in[1] + in[5] + in[row+1] + in[row+5])>>2;
496 out[2] = (in[2] + in[6] + in[row+2] + in[row+6])>>2;
497 out[3] = (in[3] + in[7] + in[row+3] + in[row+7])>>2;
498 }
499 }
500 }
501
502
503 /*
504 ==================
505 R_BlendOverTexture
506
507 Apply a color blend over a set of pixels
508 ==================
509 */
510 static void R_BlendOverTexture( byte *data, int pixelCount, byte blend[4] ) {
511 int i;
512 int inverseAlpha;
513 int premult[3];
514
515 inverseAlpha = 255 - blend[3];
516 premult[0] = blend[0] * blend[3];
517 premult[1] = blend[1] * blend[3];
518 premult[2] = blend[2] * blend[3];
519
520 for ( i = 0 ; i < pixelCount ; i++, data+=4 ) {
521 data[0] = ( data[0] * inverseAlpha + premult[0] ) >> 9;
522 data[1] = ( data[1] * inverseAlpha + premult[1] ) >> 9;
523 data[2] = ( data[2] * inverseAlpha + premult[2] ) >> 9;
524 }
525 }
526
527 byte mipBlendColors[16][4] = {
528 {0,0,0,0},
529 {255,0,0,128},
530 {0,255,0,128},
531 {0,0,255,128},
532 {255,0,0,128},
533 {0,255,0,128},
534 {0,0,255,128},
535 {255,0,0,128},
536 {0,255,0,128},
537 {0,0,255,128},
538 {255,0,0,128},
539 {0,255,0,128},
540 {0,0,255,128},
541 {255,0,0,128},
542 {0,255,0,128},
543 {0,0,255,128},
544 };
545
546
547 /*
548 ===============
549 Upload32
550
551 ===============
552 */
553 static void Upload32( unsigned *data,
554 int width, int height,
555 qboolean mipmap,
556 qboolean picmip,
557 qboolean lightMap,
558 qboolean allowCompression,
559 int *format,
560 int *pUploadWidth, int *pUploadHeight )
561 {
562 int samples;
563 unsigned *scaledBuffer = NULL;
564 unsigned *resampledBuffer = NULL;
565 int scaled_width, scaled_height;
566 int i, c;
567 byte *scan;
568 GLenum internalFormat = GL_RGB;
569 float rMax = 0, gMax = 0, bMax = 0;
570
571 //
572 // convert to exact power of 2 sizes
573 //
574 for (scaled_width = 1 ; scaled_width < width ; scaled_width<<=1)
575 ;
576 for (scaled_height = 1 ; scaled_height < height ; scaled_height<<=1)
577 ;
578 if ( r_roundImagesDown->integer && scaled_width > width )
579 scaled_width >>= 1;
580 if ( r_roundImagesDown->integer && scaled_height > height )
581 scaled_height >>= 1;
582
583 if ( scaled_width != width || scaled_height != height ) {
584 resampledBuffer = ri.Hunk_AllocateTempMemory( scaled_width * scaled_height * 4 );
585 ResampleTexture (data, width, height, resampledBuffer, scaled_width, scaled_height);
586 data = resampledBuffer;
587 width = scaled_width;
588 height = scaled_height;
589 }
590
591 //
592 // perform optional picmip operation
593 //
594 if ( picmip ) {
595 scaled_width >>= r_picmip->integer;
596 scaled_height >>= r_picmip->integer;
597 }
598
599 //
600 // clamp to minimum size
601 //
602 if (scaled_width < 1) {
603 scaled_width = 1;
604 }
605 if (scaled_height < 1) {
606 scaled_height = 1;
607 }
608
609 //
610 // clamp to the current upper OpenGL limit
611 // scale both axis down equally so we don't have to
612 // deal with a half mip resampling
613 //
614 while ( scaled_width > glConfig.maxTextureSize
615 || scaled_height > glConfig.maxTextureSize ) {
616 scaled_width >>= 1;
617 scaled_height >>= 1;
618 }
619
620 scaledBuffer = ri.Hunk_AllocateTempMemory( sizeof( unsigned ) * scaled_width * scaled_height );
621
622 //
623 // scan the texture for each channel's max values
624 // and verify if the alpha channel is being used or not
625 //
626 c = width*height;
627 scan = ((byte *)data);
628 samples = 3;
629
630 if( r_greyscale->integer )
631 {
632 for ( i = 0; i < c; i++ )
633 {
634 byte luma = LUMA(scan[i*4], scan[i*4 + 1], scan[i*4 + 2]);
635 scan[i*4] = luma;
636 scan[i*4 + 1] = luma;
637 scan[i*4 + 2] = luma;
638 }
639 }
640 else if( r_greyscale->value )
641 {
642 for ( i = 0; i < c; i++ )
643 {
644 float luma = LUMA(scan[i*4], scan[i*4 + 1], scan[i*4 + 2]);
645 scan[i*4] = LERP(scan[i*4], luma, r_greyscale->value);
646 scan[i*4 + 1] = LERP(scan[i*4 + 1], luma, r_greyscale->value);
647 scan[i*4 + 2] = LERP(scan[i*4 + 2], luma, r_greyscale->value);
648 }
649 }
650
651 if(lightMap)
652 {
653 if(r_greyscale->integer)
654 internalFormat = GL_LUMINANCE;
655 else
656 internalFormat = GL_RGB;
657 }
658 else
659 {
660 for ( i = 0; i < c; i++ )
661 {
662 if ( scan[i*4+0] > rMax )
663 {
664 rMax = scan[i*4+0];
665 }
666 if ( scan[i*4+1] > gMax )
667 {
668 gMax = scan[i*4+1];
669 }
670 if ( scan[i*4+2] > bMax )
671 {
672 bMax = scan[i*4+2];
673 }
674 if ( scan[i*4 + 3] != 255 )
675 {
676 samples = 4;
677 break;
678 }
679 }
680 // select proper internal format
681 if ( samples == 3 )
682 {
683 if(r_greyscale->integer)
684 {
685 if(r_texturebits->integer == 16 || r_texturebits->integer == 32)
686 internalFormat = GL_LUMINANCE8;
687 else
688 internalFormat = GL_LUMINANCE;
689 }
690 else
691 {
692 if ( allowCompression && glConfig.textureCompression == TC_S3TC_ARB )
693 {
694 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
695 }
696 else if ( allowCompression && glConfig.textureCompression == TC_S3TC )
697 {
698 internalFormat = GL_RGB4_S3TC;
699 }
700 else if ( r_texturebits->integer == 16 )
701 {
702 internalFormat = GL_RGB5;
703 }
704 else if ( r_texturebits->integer == 32 )
705 {
706 internalFormat = GL_RGB8;
707 }
708 else
709 {
710 internalFormat = GL_RGB;
711 }
712 }
713 }
714 else if ( samples == 4 )
715 {
716 if(r_greyscale->integer)
717 {
718 if(r_texturebits->integer == 16 || r_texturebits->integer == 32)
719 internalFormat = GL_LUMINANCE8_ALPHA8;
720 else
721 internalFormat = GL_LUMINANCE_ALPHA;
722 }
723 else
724 {
725 if ( r_texturebits->integer == 16 )
726 {
727 internalFormat = GL_RGBA4;
728 }
729 else if ( r_texturebits->integer == 32 )
730 {
731 internalFormat = GL_RGBA8;
732 }
733 else
734 {
735 internalFormat = GL_RGBA;
736 }
737 }
738 }
739 }
740
741 // copy or resample data as appropriate for first MIP level
742 if ( ( scaled_width == width ) &&
743 ( scaled_height == height ) ) {
744 if (!mipmap)
745 {
746 qglTexImage2D (GL_TEXTURE_2D, 0, internalFormat, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
747 *pUploadWidth = scaled_width;
748 *pUploadHeight = scaled_height;
749 *format = internalFormat;
750
751 goto done;
752 }
753 Com_Memcpy (scaledBuffer, data, width*height*4);
754 }
755 else
756 {
757 // use the normal mip-mapping function to go down from here
758 while ( width > scaled_width || height > scaled_height ) {
759 R_MipMap( (byte *)data, width, height );
760 width >>= 1;
761 height >>= 1;
762 if ( width < 1 ) {
763 width = 1;
764 }
765 if ( height < 1 ) {
766 height = 1;
767 }
768 }
769 Com_Memcpy( scaledBuffer, data, width * height * 4 );
770 }
771
772 R_LightScaleTexture (scaledBuffer, scaled_width, scaled_height, !mipmap );
773
774 *pUploadWidth = scaled_width;
775 *pUploadHeight = scaled_height;
776 *format = internalFormat;
777
778 qglTexImage2D (GL_TEXTURE_2D, 0, internalFormat, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaledBuffer );
779
780 if (mipmap)
781 {
782 int miplevel;
783
784 miplevel = 0;
785 while (scaled_width > 1 || scaled_height > 1)
786 {
787 R_MipMap( (byte *)scaledBuffer, scaled_width, scaled_height );
788 scaled_width >>= 1;
789 scaled_height >>= 1;
790 if (scaled_width < 1)
791 scaled_width = 1;
792 if (scaled_height < 1)
793 scaled_height = 1;
794 miplevel++;
795
796 if ( r_colorMipLevels->integer ) {
797 R_BlendOverTexture( (byte *)scaledBuffer, scaled_width * scaled_height, mipBlendColors[miplevel] );
798 }
799
800 qglTexImage2D (GL_TEXTURE_2D, miplevel, internalFormat, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaledBuffer );
801 }
802 }
803 done:
804
805 if (mipmap)
806 {
807 if ( textureFilterAnisotropic )
808 qglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
809 (GLint)Com_Clamp( 1, maxAnisotropy, r_ext_max_anisotropy->integer ) );
810
811 qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
812 qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
813 }
814 else
815 {
816 if ( textureFilterAnisotropic )
817 qglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1 );
818
819 qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
820 qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
821 }
822
823 GL_CheckErrors();
824
825 if ( scaledBuffer != 0 )
826 ri.Hunk_FreeTempMemory( scaledBuffer );
827 if ( resampledBuffer != 0 )
828 ri.Hunk_FreeTempMemory( resampledBuffer );
829 }
830
831
832 /*
833 ================
834 R_CreateImage
835
836 This is the only way any image_t are created
837 ================
838 */
839 image_t *R_CreateImage( const char *name, byte *pic, int width, int height,
840 imgType_t type, imgFlags_t flags, int internalFormat ) {
841 image_t *image;
842 qboolean isLightmap = qfalse;
843 long hash;
844 int glWrapClampMode;
845
846 if (strlen(name) >= MAX_QPATH ) {
847 ri.Error (ERR_DROP, "R_CreateImage: \"%s\" is too long", name);
848 }
849 if ( !strncmp( name, "*lightmap", 9 ) ) {
850 isLightmap = qtrue;
851 }
852
853 if ( tr.numImages == MAX_DRAWIMAGES ) {
854 ri.Error( ERR_DROP, "R_CreateImage: MAX_DRAWIMAGES hit");
855 }
856
857 image = tr.images[tr.numImages] = ri.Hunk_Alloc( sizeof( image_t ), h_low );
858 qglGenTextures(1, &image->texnum);
859 tr.numImages++;
860
861 image->type = type;
862 image->flags = flags;
863
864 strcpy (image->imgName, name);
865
866 image->width = width;
867 image->height = height;
868 if (flags & IMGFLAG_CLAMPTOEDGE)
869 glWrapClampMode = haveClampToEdge ? GL_CLAMP_TO_EDGE : GL_CLAMP;
870 else
871 glWrapClampMode = GL_REPEAT;
872
873 // lightmaps are always allocated on TMU 1
874 if ( qglActiveTextureARB && isLightmap ) {
875 image->TMU = 1;
876 } else {
877 image->TMU = 0;
878 }
879
880 if ( qglActiveTextureARB ) {
881 GL_SelectTexture( image->TMU );
882 }
883
884 GL_Bind(image);
885
886 Upload32( (unsigned *)pic, image->width, image->height,
887 image->flags & IMGFLAG_MIPMAP,
888 image->flags & IMGFLAG_PICMIP,
889 isLightmap,
890 !(image->flags & IMGFLAG_NO_COMPRESSION),
891 &image->internalFormat,
892 &image->uploadWidth,
893 &image->uploadHeight );
894
895 qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glWrapClampMode );
896 qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glWrapClampMode );
897
898 glState.currenttextures[glState.currenttmu] = 0;
899 qglBindTexture( GL_TEXTURE_2D, 0 );
900
901 if ( image->TMU == 1 ) {
902 GL_SelectTexture( 0 );
903 }
904
905 hash = generateHashValue(name);
906 image->next = hashTable[hash];
907 hashTable[hash] = image;
908
909 return image;
910 }
911
912 //===================================================================
913
914 typedef struct
915 {
916 char *ext;
917 void (*ImageLoader)( const char *, unsigned char **, int *, int * );
918 } imageExtToLoaderMap_t;
919
920 // Note that the ordering indicates the order of preference used
921 // when there are multiple images of different formats available
922 static imageExtToLoaderMap_t imageLoaders[ ] =
923 {
924 { "tga", R_LoadTGA },
925 { "jpg", R_LoadJPG },
926 { "jpeg", R_LoadJPG },
927 { "png", R_LoadPNG },
928 { "pcx", R_LoadPCX },
929 { "bmp", R_LoadBMP },
930 { "pvr", R_LoadPVR }
931 };
932
933 static int numImageLoaders = ARRAY_LEN( imageLoaders );
934
935 /*
936 =================
937 R_LoadImage
938
939 Loads any of the supported image types into a canonical
940 32 bit format.
941 =================
942 */
943 void R_LoadImage( const char *name, byte **pic, int *width, int *height )
944 {
945 qboolean orgNameFailed = qfalse;
946 int orgLoader = -1;
947 int i;
948 char localName[ MAX_QPATH ];
949 const char *ext;
950 char *altName;
951
952 *pic = NULL;
953 *width = 0;
954 *height = 0;
955
956 Q_strncpyz( localName, name, MAX_QPATH );
957
958 ext = COM_GetExtension( localName );
959
960 if( *ext )
961 {
962 // Look for the correct loader and use it
963 for( i = 0; i < numImageLoaders; i++ )
964 {
965 if( !Q_stricmp( ext, imageLoaders[ i ].ext ) )
966 {
967 // Load
968 imageLoaders[ i ].ImageLoader( localName, pic, width, height );
969 break;
970 }
971 }
972
973 // A loader was found
974 if( i < numImageLoaders )
975 {
976 if( *pic == NULL )
977 {
978 // Loader failed, most likely because the file isn't there;
979 // try again without the extension
980 orgNameFailed = qtrue;
981 orgLoader = i;
982 COM_StripExtension( name, localName, MAX_QPATH );
983 }
984 else
985 {
986 // Something loaded
987 return;
988 }
989 }
990 }
991
992 // Try and find a suitable match using all
993 // the image formats supported
994 for( i = 0; i < numImageLoaders; i++ )
995 {
996 if (i == orgLoader)
997 continue;
998
999 altName = va( "%s.%s", localName, imageLoaders[ i ].ext );
1000
1001 // Load
1002 imageLoaders[ i ].ImageLoader( altName, pic, width, height );
1003
1004 if( *pic )
1005 {
1006 if( orgNameFailed )
1007 {
1008 ri.Printf( PRINT_DEVELOPER, "WARNING: %s not present, using %s instead\n",
1009 name, altName );
1010 }
1011
1012 break;
1013 }
1014 }
1015 }
1016
1017
1018 /*
1019 ===============
1020 R_FindImageFile
1021
1022 Finds or loads the given image.
1023 Returns NULL if it fails, not a default image.
1024 ==============
1025 */
1026 image_t *R_FindImageFile( const char *name, imgType_t type, imgFlags_t flags )
1027 {
1028 image_t *image;
1029 int width, height;
1030 byte *pic;
1031 long hash;
1032
1033 if (!name) {
1034 return NULL;
1035 }
1036
1037 hash = generateHashValue(name);
1038
1039 //
1040 // see if the image is already loaded
1041 //
1042 for (image=hashTable[hash]; image; image=image->next) {
1043 if ( !strcmp( name, image->imgName ) ) {
1044 // the white image can be used with any set of parms, but other mismatches are errors
1045 if ( strcmp( name, "*white" ) ) {
1046 if ( image->flags != flags ) {
1047 ri.Printf( PRINT_DEVELOPER, "WARNING: reused image %s with mixed flags (%i vs %i)\n", name, image->flags, flags );
1048 }
1049 }
1050 return image;
1051 }
1052 }
1053
1054 //
1055 // load the pic from disk
1056 //
1057 R_LoadImage( name, &pic, &width, &height );
1058 if ( pic == NULL ) {
1059 return NULL;
1060 }
1061
1062 image = R_CreateImage( ( char * ) name, pic, width, height, type, flags, 0 );
1063 ri.Free( pic );
1064 return image;
1065 }
1066
1067
1068 /*
1069 ================
1070 R_CreateDlightImage
1071 ================
1072 */
1073 #define DLIGHT_SIZE 16
1074 static void R_CreateDlightImage( void ) {
1075 int x,y;
1076 byte data[DLIGHT_SIZE][DLIGHT_SIZE][4];
1077 int b;
1078
1079 // make a centered inverse-square falloff blob for dynamic lighting
1080 for (x=0 ; x<DLIGHT_SIZE ; x++) {
1081 for (y=0 ; y<DLIGHT_SIZE ; y++) {
1082 float d;
1083
1084 d = ( DLIGHT_SIZE/2 - 0.5f - x ) * ( DLIGHT_SIZE/2 - 0.5f - x ) +
1085 ( DLIGHT_SIZE/2 - 0.5f - y ) * ( DLIGHT_SIZE/2 - 0.5f - y );
1086 b = 4000 / d;
1087 if (b > 255) {
1088 b = 255;
1089 } else if ( b < 75 ) {
1090 b = 0;
1091 }
1092 data[y][x][0] =
1093 data[y][x][1] =
1094 data[y][x][2] = b;
1095 data[y][x][3] = 255;
1096 }
1097 }
1098 tr.dlightImage = R_CreateImage("*dlight", (byte *)data, DLIGHT_SIZE, DLIGHT_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE, 0 );
1099 }
1100
1101
1102 /*
1103 =================
1104 R_InitFogTable
1105 =================
1106 */
1107 void R_InitFogTable( void ) {
1108 int i;
1109 float d;
1110 float exp;
1111
1112 exp = 0.5;
1113
1114 for ( i = 0 ; i < FOG_TABLE_SIZE ; i++ ) {
1115 d = pow ( (float)i/(FOG_TABLE_SIZE-1), exp );
1116
1117 tr.fogTable[i] = d;
1118 }
1119 }
1120
1121 /*
1122 ================
1123 R_FogFactor
1124
1125 Returns a 0.0 to 1.0 fog density value
1126 This is called for each texel of the fog texture on startup
1127 and for each vertex of transparent shaders in fog dynamically
1128 ================
1129 */
1130 float R_FogFactor( float s, float t ) {
1131 float d;
1132
1133 s -= 1.0/512;
1134 if ( s < 0 ) {
1135 return 0;
1136 }
1137 if ( t < 1.0/32 ) {
1138 return 0;
1139 }
1140 if ( t < 31.0/32 ) {
1141 s *= (t - 1.0f/32.0f) / (30.0f/32.0f);
1142 }
1143
1144 // we need to leave a lot of clamp range
1145 s *= 8;
1146
1147 if ( s > 1.0 ) {
1148 s = 1.0;
1149 }
1150
1151 d = tr.fogTable[ (int)(s * (FOG_TABLE_SIZE-1)) ];
1152
1153 return d;
1154 }
1155
1156 /*
1157 ================
1158 R_CreateFogImage
1159 ================
1160 */
1161 #define FOG_S 256
1162 #define FOG_T 32
1163 static void R_CreateFogImage( void ) {
1164 int x,y;
1165 byte *data;
1166 float d;
1167
1168 data = ri.Hunk_AllocateTempMemory( FOG_S * FOG_T * 4 );
1169
1170 // S is distance, T is depth
1171 for (x=0 ; x<FOG_S ; x++) {
1172 for (y=0 ; y<FOG_T ; y++) {
1173 d = R_FogFactor( ( x + 0.5f ) / FOG_S, ( y + 0.5f ) / FOG_T );
1174
1175 data[(y*FOG_S+x)*4+0] =
1176 data[(y*FOG_S+x)*4+1] =
1177 data[(y*FOG_S+x)*4+2] = 255;
1178 data[(y*FOG_S+x)*4+3] = 255*d;
1179 }
1180 }
1181 tr.fogImage = R_CreateImage("*fog", (byte *)data, FOG_S, FOG_T, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE, 0 );
1182 ri.Hunk_FreeTempMemory( data );
1183 }
1184
1185 /*
1186 ==================
1187 R_CreateDefaultImage
1188 ==================
1189 */
1190 #define DEFAULT_SIZE 16
1191 static void R_CreateDefaultImage( void ) {
1192 int x;
1193 byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
1194
1195 // the default image will be a box, to allow you to see the mapping coordinates
1196 Com_Memset( data, 32, sizeof( data ) );
1197 for ( x = 0 ; x < DEFAULT_SIZE ; x++ ) {
1198 data[0][x][0] =
1199 data[0][x][1] =
1200 data[0][x][2] =
1201 data[0][x][3] = 255;
1202
1203 data[x][0][0] =
1204 data[x][0][1] =
1205 data[x][0][2] =
1206 data[x][0][3] = 255;
1207
1208 data[DEFAULT_SIZE-1][x][0] =
1209 data[DEFAULT_SIZE-1][x][1] =
1210 data[DEFAULT_SIZE-1][x][2] =
1211 data[DEFAULT_SIZE-1][x][3] = 255;
1212
1213 data[x][DEFAULT_SIZE-1][0] =
1214 data[x][DEFAULT_SIZE-1][1] =
1215 data[x][DEFAULT_SIZE-1][2] =
1216 data[x][DEFAULT_SIZE-1][3] = 255;
1217 }
1218 tr.defaultImage = R_CreateImage("*default", (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_MIPMAP, 0);
1219 }
1220
1221 /*
1222 ==================
1223 R_CreateBuiltinImages
1224 ==================
1225 */
1226 void R_CreateBuiltinImages( void ) {
1227 int x,y;
1228 byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
1229
1230 R_CreateDefaultImage();
1231
1232 // we use a solid white image instead of disabling texturing
1233 Com_Memset( data, 255, sizeof( data ) );
1234 tr.whiteImage = R_CreateImage("*white", (byte *)data, 8, 8, IMGTYPE_COLORALPHA, IMGFLAG_NONE, 0);
1235
1236 // with overbright bits active, we need an image which is some fraction of full color,
1237 // for default lightmaps, etc
1238 for (x=0 ; x<DEFAULT_SIZE ; x++) {
1239 for (y=0 ; y<DEFAULT_SIZE ; y++) {
1240 data[y][x][0] =
1241 data[y][x][1] =
1242 data[y][x][2] = tr.identityLightByte;
1243 data[y][x][3] = 255;
1244 }
1245 }
1246
1247 tr.identityLightImage = R_CreateImage("*identityLight", (byte *)data, 8, 8, IMGTYPE_COLORALPHA, IMGFLAG_NONE, 0);
1248
1249
1250 for(x=0;x<32;x++) {
1251 // scratchimage is usually used for cinematic drawing
1252 tr.scratchImage[x] = R_CreateImage("*scratch", (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_PICMIP | IMGFLAG_CLAMPTOEDGE, 0);
1253 }
1254
1255 R_CreateDlightImage();
1256 R_CreateFogImage();
1257 }
1258
1259
1260 /*
1261 ===============
1262 R_SetColorMappings
1263 ===============
1264 */
1265 void R_SetColorMappings( void ) {
1266 int i, j;
1267 float g;
1268 int inf;
1269 int shift;
1270
1271 // setup the overbright lighting
1272 tr.overbrightBits = r_overBrightBits->integer;
1273 if ( !glConfig.deviceSupportsGamma ) {
1274 tr.overbrightBits = 0; // need hardware gamma for overbright
1275 }
1276
1277 // never overbright in windowed mode
1278 if ( !glConfig.isFullscreen )
1279 {
1280 tr.overbrightBits = 0;
1281 }
1282
1283 // allow 2 overbright bits in 24 bit, but only 1 in 16 bit
1284 if ( glConfig.colorBits > 16 ) {
1285 if ( tr.overbrightBits > 2 ) {
1286 tr.overbrightBits = 2;
1287 }
1288 } else {
1289 if ( tr.overbrightBits > 1 ) {
1290 tr.overbrightBits = 1;
1291 }
1292 }
1293 if ( tr.overbrightBits < 0 ) {
1294 tr.overbrightBits = 0;
1295 }
1296
1297 tr.identityLight = 1.0f / ( 1 << tr.overbrightBits );
1298 tr.identityLightByte = 255 * tr.identityLight;
1299
1300
1301 if ( r_intensity->value <= 1 ) {
1302 ri.Cvar_Set( "r_intensity", "1" );
1303 }
1304
1305 if ( r_gamma->value < 0.5f ) {
1306 ri.Cvar_Set( "r_gamma", "0.5" );
1307 } else if ( r_gamma->value > 3.0f ) {
1308 ri.Cvar_Set( "r_gamma", "3.0" );
1309 }
1310
1311 g = r_gamma->value;
1312
1313 shift = tr.overbrightBits;
1314
1315 for ( i = 0; i < 256; i++ ) {
1316 if ( g == 1 ) {
1317 inf = i;
1318 } else {
1319 inf = 255 * pow ( i/255.0f, 1.0f / g ) + 0.5f;
1320 }
1321 inf <<= shift;
1322 if (inf < 0) {
1323 inf = 0;
1324 }
1325 if (inf > 255) {
1326 inf = 255;
1327 }
1328 s_gammatable[i] = inf;
1329 }
1330
1331 for (i=0 ; i<256 ; i++) {
1332 j = i * r_intensity->value;
1333 if (j > 255) {
1334 j = 255;
1335 }
1336 s_intensitytable[i] = j;
1337 }
1338
1339 if ( glConfig.deviceSupportsGamma )
1340 {
1341 GLimp_SetGamma( s_gammatable, s_gammatable, s_gammatable );
1342 }
1343 }
1344
1345 /*
1346 ===============
1347 R_InitImages
1348 ===============
1349 */
1350 void R_InitImages( void ) {
1351 Com_Memset(hashTable, 0, sizeof(hashTable));
1352 // build brightness translation tables
1353 R_SetColorMappings();
1354
1355 // create default texture and white texture
1356 R_CreateBuiltinImages();
1357 }
1358
1359 /*
1360 ===============
1361 R_DeleteTextures
1362 ===============
1363 */
1364 void R_DeleteTextures( void ) {
1365 int i;
1366
1367 for ( i=0; i<tr.numImages ; i++ ) {
1368 qglDeleteTextures( 1, &tr.images[i]->texnum );
1369 }
1370 Com_Memset( tr.images, 0, sizeof( tr.images ) );
1371
1372 tr.numImages = 0;
1373
1374 Com_Memset( glState.currenttextures, 0, sizeof( glState.currenttextures ) );
1375 if ( qglActiveTextureARB ) {
1376 GL_SelectTexture( 1 );
1377 qglBindTexture( GL_TEXTURE_2D, 0 );
1378 GL_SelectTexture( 0 );
1379 qglBindTexture( GL_TEXTURE_2D, 0 );
1380 } else {
1381 qglBindTexture( GL_TEXTURE_2D, 0 );
1382 }
1383 }
1384
1385 /*
1386 ============================================================================
1387
1388 SKINS
1389
1390 ============================================================================
1391 */
1392
1393 /*
1394 ==================
1395 CommaParse
1396
1397 This is unfortunate, but the skin files aren't
1398 compatible with our normal parsing rules.
1399 ==================
1400 */
1401 static char *CommaParse( char **data_p ) {
1402 int c = 0, len;
1403 char *data;
1404 static char com_token[MAX_TOKEN_CHARS];
1405
1406 data = *data_p;
1407 len = 0;
1408 com_token[0] = 0;
1409
1410 // make sure incoming data is valid
1411 if ( !data ) {
1412 *data_p = NULL;
1413 return com_token;
1414 }
1415
1416 while ( 1 ) {
1417 // skip whitespace
1418 while( (c = *data) <= ' ') {
1419 if( !c ) {
1420 break;
1421 }
1422 data++;
1423 }
1424
1425
1426 c = *data;
1427
1428 // skip double slash comments
1429 if ( c == '/' && data[1] == '/' )
1430 {
1431 data += 2;
1432 while (*data && *data != '\n') {
1433 data++;
1434 }
1435 }
1436 // skip /* */ comments
1437 else if ( c=='/' && data[1] == '*' )
1438 {
1439 data += 2;
1440 while ( *data && ( *data != '*' || data[1] != '/' ) )
1441 {
1442 data++;
1443 }
1444 if ( *data )
1445 {
1446 data += 2;
1447 }
1448 }
1449 else
1450 {
1451 break;
1452 }
1453 }
1454
1455 if ( c == 0 ) {
1456 return "";
1457 }
1458
1459 // handle quoted strings
1460 if (c == '\"')
1461 {
1462 data++;
1463 while (1)
1464 {
1465 c = *data++;
1466 if (c=='\"' || !c)
1467 {
1468 com_token[len] = 0;
1469 *data_p = ( char * ) data;
1470 return com_token;
1471 }
1472 if (len < MAX_TOKEN_CHARS - 1)
1473 {
1474 com_token[len] = c;
1475 len++;
1476 }
1477 }
1478 }
1479
1480 // parse a regular word
1481 do
1482 {
1483 if (len < MAX_TOKEN_CHARS - 1)
1484 {
1485 com_token[len] = c;
1486 len++;
1487 }
1488 data++;
1489 c = *data;
1490 } while (c>32 && c != ',' );
1491
1492 com_token[len] = 0;
1493
1494 *data_p = ( char * ) data;
1495 return com_token;
1496 }
1497
1498
1499 /*
1500 ===============
1501 RE_RegisterSkin
1502
1503 ===============
1504 */
1505 qhandle_t RE_RegisterSkin( const char *name ) {
1506 skinSurface_t parseSurfaces[MAX_SKIN_SURFACES];
1507 qhandle_t hSkin;
1508 skin_t *skin;
1509 skinSurface_t *surf;
1510 union {
1511 char *c;
1512 void *v;
1513 } text;
1514 char *text_p;
1515 char *token;
1516 char surfName[MAX_QPATH];
1517 int totalSurfaces;
1518
1519 if ( !name || !name[0] ) {
1520 ri.Printf( PRINT_DEVELOPER, "Empty name passed to RE_RegisterSkin\n" );
1521 return 0;
1522 }
1523
1524 if ( strlen( name ) >= MAX_QPATH ) {
1525 ri.Printf( PRINT_DEVELOPER, "Skin name exceeds MAX_QPATH\n" );
1526 return 0;
1527 }
1528
1529
1530 // see if the skin is already loaded
1531 for ( hSkin = 1; hSkin < tr.numSkins ; hSkin++ ) {
1532 skin = tr.skins[hSkin];
1533 if ( !Q_stricmp( skin->name, name ) ) {
1534 if( skin->numSurfaces == 0 ) {
1535 return 0; // default skin
1536 }
1537 return hSkin;
1538 }
1539 }
1540
1541 // allocate a new skin
1542 if ( tr.numSkins == MAX_SKINS ) {
1543 ri.Printf( PRINT_WARNING, "WARNING: RE_RegisterSkin( '%s' ) MAX_SKINS hit\n", name );
1544 return 0;
1545 }
1546 tr.numSkins++;
1547 skin = ri.Hunk_Alloc( sizeof( skin_t ), h_low );
1548 tr.skins[hSkin] = skin;
1549 Q_strncpyz( skin->name, name, sizeof( skin->name ) );
1550 skin->numSurfaces = 0;
1551
1552 R_IssuePendingRenderCommands();
1553
1554 // If not a .skin file, load as a single shader
1555 if ( strcmp( name + strlen( name ) - 5, ".skin" ) ) {
1556 skin->numSurfaces = 1;
1557 skin->surfaces = ri.Hunk_Alloc( sizeof( skinSurface_t ), h_low );
1558 skin->surfaces[0].shader = R_FindShader( name, LIGHTMAP_NONE, qtrue );
1559 return hSkin;
1560 }
1561
1562 // load and parse the skin file
1563 ri.FS_ReadFile( name, &text.v );
1564 if ( !text.c ) {
1565 return 0;
1566 }
1567
1568 totalSurfaces = 0;
1569 text_p = text.c;
1570 while ( text_p && *text_p ) {
1571 // get surface name
1572 token = CommaParse( &text_p );
1573 Q_strncpyz( surfName, token, sizeof( surfName ) );
1574
1575 if ( !token[0] ) {
1576 break;
1577 }
1578 // lowercase the surface name so skin compares are faster
1579 Q_strlwr( surfName );
1580
1581 if ( *text_p == ',' ) {
1582 text_p++;
1583 }
1584
1585 if ( strstr( token, "tag_" ) ) {
1586 continue;
1587 }
1588
1589 // parse the shader name
1590 token = CommaParse( &text_p );
1591
1592 if ( skin->numSurfaces < MAX_SKIN_SURFACES ) {
1593 surf = &parseSurfaces[skin->numSurfaces];
1594 Q_strncpyz( surf->name, surfName, sizeof( surf->name ) );
1595 surf->shader = R_FindShader( token, LIGHTMAP_NONE, qtrue );
1596 skin->numSurfaces++;
1597 }
1598
1599 totalSurfaces++;
1600 }
1601
1602 ri.FS_FreeFile( text.v );
1603
1604 if ( totalSurfaces > MAX_SKIN_SURFACES ) {
1605 ri.Printf( PRINT_WARNING, "WARNING: Ignoring excess surfaces (found %d, max is %d) in skin '%s'!\n",
1606 totalSurfaces, MAX_SKIN_SURFACES, name );
1607 }
1608
1609 // never let a skin have 0 shaders
1610 if ( skin->numSurfaces == 0 ) {
1611 return 0; // use default skin
1612 }
1613
1614 // copy surfaces to skin
1615 skin->surfaces = ri.Hunk_Alloc( skin->numSurfaces * sizeof( skinSurface_t ), h_low );
1616 memcpy( skin->surfaces, parseSurfaces, skin->numSurfaces * sizeof( skinSurface_t ) );
1617
1618 return hSkin;
1619 }
1620
1621
1622 /*
1623 ===============
1624 R_InitSkins
1625 ===============
1626 */
1627 void R_InitSkins( void ) {
1628 skin_t *skin;
1629
1630 tr.numSkins = 1;
1631
1632 // make the default skin have all default shaders
1633 skin = tr.skins[0] = ri.Hunk_Alloc( sizeof( skin_t ), h_low );
1634 Q_strncpyz( skin->name, "<default skin>", sizeof( skin->name ) );
1635 skin->numSurfaces = 1;
1636 skin->surfaces = ri.Hunk_Alloc( sizeof( skinSurface_t ), h_low );
1637 skin->surfaces[0].shader = tr.defaultShader;
1638 }
1639
1640 /*
1641 ===============
1642 R_GetSkinByHandle
1643 ===============
1644 */
1645 skin_t *R_GetSkinByHandle( qhandle_t hSkin ) {
1646 if ( hSkin < 1 || hSkin >= tr.numSkins ) {
1647 return tr.skins[0];
1648 }
1649 return tr.skins[ hSkin ];
1650 }
1651
1652 /*
1653 ===============
1654 R_SkinList_f
1655 ===============
1656 */
1657 void R_SkinList_f( void ) {
1658 int i, j;
1659 skin_t *skin;
1660
1661 ri.Printf (PRINT_ALL, "------------------\n");
1662
1663 for ( i = 0 ; i < tr.numSkins ; i++ ) {
1664 skin = tr.skins[i];
1665
1666 ri.Printf( PRINT_ALL, "%3i:%s (%d surfaces)\n", i, skin->name, skin->numSurfaces );
1667 for ( j = 0 ; j < skin->numSurfaces ; j++ ) {
1668 ri.Printf( PRINT_ALL, " %s = %s\n",
1669 skin->surfaces[j].name, skin->surfaces[j].shader->name );
1670 }
1671 }
1672 ri.Printf (PRINT_ALL, "------------------\n");
1673 }
1674