-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMipMap.c
460 lines (402 loc) · 10.8 KB
/
MipMap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* The X Men, November 1997
* Copyright (c) 1996 Probe Entertainment Limited
* All Rights Reserved
*
* $Revision: 8 $
*
* $Header: /PcProjectX/MipMap.c 8 3/14/98 3:31p Phillipd $
*
* $Log: /PcProjectX/MipMap.c $
*
* 8 3/14/98 3:31p Phillipd
* bmp files for textures are now supported...
*
* 7 12/30/97 12:30p Phillipd
*
* 6 11/29/97 4:35p Phillipd
* Xmem is now in effect...use it allways....
*
* 5 11/19/97 6:25p Phillipd
*
* 4 11/19/97 6:22p Phillipd
*
* 3 11/13/97 6:51p Phillipd
*
* 2 11/10/97 4:13p Phillipd
*
* 1 11/10/97 4:08p Phillipd
*
*/
/*ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
Include File...
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ*/
#include "typedefs.h"
#include "tload.h"
#include "MipMap.h"
#include "XMem.h"
#ifdef OPT_ON
#pragma optimize( "gty", on )
#endif
//#define TESTMIPMAP
int Lod_Red[8] = { 255, 0, 0, 255, 0, 255, 255, 0 };
int Lod_Green[8]= { 0, 255, 0, 255, 255, 0, 255, 0 };
int Lod_Blue[8] = { 0, 0, 255, 0, 255, 255, 255, 0 };
extern BYTE GammaTab[256];
/***************************************************************************/
/* Loading a PPM file into a surface */
/***************************************************************************/
/*
* LoadSurface
* Loads a ppm file into a texture map DD surface of the given format. The
* memory flag specifies DDSCAPS_SYSTEMMEMORY or DDSCAPS_VIDEOMEMORY.
* Scale...divide X and Y by Scale..
*/
BOOL InitMipMap(LPCSTR lpName , int16 Scale , MIPMAPDESC * MipMapDesc )
{
DWORD dwWidth, dwHeight;
int i, j,e;
FILE *fp;
CHAR buf[100];
int XScale,YScale;
// char TempFilename[ 128 ];
uint8 * uint8pnt;
/*
* Find the image file and open it
*/
// Add_Path( "textures\\", (char *) lpName, &TempFilename[ 0 ] );
// fp = DataPath_fopen( &TempFilename[ 0 ], "rb" );
fp = DataPath_fopen( (char*) lpName, "rb" );
if (fp == NULL) {
Msg( "Cannot find %s.\n", lpName);
return FALSE;
}
/*
* Is it a PPM file?
*/
fgets(buf, sizeof buf, fp);
if (lstrcmp(buf, "P6\n")) {
fclose(fp);
Msg( "%s Is Not a PPM File\n", lpName);
return FALSE;
}
/*
* Skip any comments
*/
do {
fgets(buf, sizeof buf, fp);
} while (buf[0] == '#');
/*
* Read the width and height
*/
sscanf(buf, "%d %d\n", &dwWidth, &dwHeight);
fgets(buf, sizeof buf, fp); /* skip next line */
XScale = Scale;
YScale = Scale;
// Some cards Only Like Square Textures...
if( d3dappi.Driver[d3dappi.CurrDriver].bSquareOnly )
{
if( dwHeight != dwWidth )
{
if( dwHeight > dwWidth )
YScale++;
if( dwWidth > dwHeight )
XScale++;
}
}
for( i = 0 ; i < MAXMIPMAP ; i++ )
{
MipMapDesc->Data[i] = NULL;
}
MipMapDesc->Height = dwHeight / ( 1 << YScale );
MipMapDesc->Width = dwWidth / ( 1 << XScale );
MipMapDesc->Data[0] = (uint8*) malloc( dwWidth * dwHeight * 3 );
uint8pnt = MipMapDesc->Data[0];
if( !uint8pnt )
return FALSE;
for (j = 0; j < (int)dwHeight; j++)
{
for (i = 0; i < (int)dwWidth; i++)
{
#ifndef TESTMIPMAP
*uint8pnt++ = GammaTab[getc(fp)];
*uint8pnt++ = GammaTab[getc(fp)];
*uint8pnt++ = GammaTab[getc(fp)];
#else
*uint8pnt++ = Lod_Red[0];
*uint8pnt++ = Lod_Green[0];
*uint8pnt++ = Lod_Blue[0];
getc(fp);
getc(fp);
getc(fp);
#endif
if( XScale )
{
for( e = 0 ; e < ( ( 1 << XScale ) -1 ) ; e++ )
{
getc(fp);
getc(fp);
getc(fp);
}
}
}
if( YScale )
{
for( e = 0 ; e < ( ( 1 << YScale ) -1 ) * (int) dwWidth ; e++ )
{
getc(fp);
getc(fp);
getc(fp);
}
}
}
fclose(fp);
return TRUE;
}
/*Build the MipMaps for a MIPMAPDESC structure with the toplevel filled*/
int BuildMipMaps( int Width , int Height , MIPMAPDESC * MipMapDesc , int Count )
{
uint8* Src;
uint8* Dest;
int x,y,value,channel;
Src = MipMapDesc->Data[Count-1];
Width /= 2;
Height /= 2;
if( Width < MIPMAPMIN || Height < MIPMAPMIN || Count >= MAXMIPMAP )
return Count;
MipMapDesc->Data[Count] = (uint8*) malloc( Width * Height * 3 );
Dest = MipMapDesc->Data[Count];
if( !Dest )
return Count;
for(y=0;y<Height;y++)
{
for(x=0;x<Width;x++)
{
#ifndef TESTMIPMAP
for(channel=0;channel<3;channel++)
{
value = ((int)ColorOf(Src, 2*x, 2*y, channel , Width * 2 )+
(int)ColorOf(Src, 2*x+1, 2*y, channel, Width * 2)+
(int)ColorOf(Src, 2*x+1, 2*y+1, channel, Width * 2)+
(int)ColorOf(Src, 2*x, 2*y+1, channel, Width * 2))/4;
ColorOf(Dest, x, y, channel, Width )=(unsigned char)value;
}
#else
ColorOf(Dest, x, y, 0, Width )=(unsigned char)Lod_Red[Count];
ColorOf(Dest, x, y, 1, Width )=(unsigned char)Lod_Green[Count];
ColorOf(Dest, x, y, 2, Width )=(unsigned char)Lod_Blue[Count];
#endif
}
}
return BuildMipMaps( Width , Height , MipMapDesc , Count+1 );
}
/*Load and create a mipmapped texture surface*/
LPDIRECTDRAWSURFACE LoadMipMap(LPDIRECTDRAW lpDD, LPCSTR lpName,
LPDDSURFACEDESC lpFormat, DWORD memoryflag , int16 Scale )
{
LPDIRECTDRAWSURFACE lpDDS;
DDSURFACEDESC ddsd, format;
DWORD dwWidth, dwHeight;
HRESULT ddrval;
LPDIRECTDRAWSURFACE lpDDS_top;
DDSCAPS ddscaps;
int LOD;
MIPMAPDESC MipMapDesc;
uint8* uint8pnt;
//load the ppm file into a simple image structure defined in mipmap.h
if( !InitMipMap( lpName , Scale , &MipMapDesc ) )
return NULL;
MipMapDesc.LOD = BuildMipMaps( MipMapDesc.Width , MipMapDesc.Height , &MipMapDesc , 1 );
dwHeight = (DWORD) MipMapDesc.Height;
dwWidth = (DWORD) MipMapDesc.Width;
/*Setup the descriptor to create the surface*/
memcpy(&format, lpFormat, sizeof(DDSURFACEDESC));
memcpy(&ddsd, &format, sizeof(DDSURFACEDESC));
ddsd.dwSize = sizeof(DDSURFACEDESC);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT
| DDSD_MIPMAPCOUNT;
ddsd.dwMipMapCount=MipMapDesc.LOD;
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX |
DDSCAPS_MIPMAP | memoryflag;
ddsd.dwHeight = dwHeight;
ddsd.dwWidth = dwWidth;
ddrval = lpDD->lpVtbl->CreateSurface(lpDD, &ddsd, &lpDDS, NULL);
if (ddrval != DD_OK)
return (NULL);
lpDDS_top=lpDDS;
/*fill the MipMaps by looping through each Level of Detail*/
ddscaps.dwCaps=DDSCAPS_TEXTURE | DDSCAPS_MIPMAP;
for(LOD=0;LOD<MipMapDesc.LOD;LOD++)
{
/*Build a descriptor to lock the new mip map level*/
memset(&ddsd, 0, sizeof(DDSURFACEDESC));
ddsd.dwSize = sizeof(DDSURFACEDESC);
ddrval = lpDDS->lpVtbl->Lock(lpDDS, NULL, &ddsd, 0, NULL);
if (ddrval != DD_OK)
{
lpDDS->lpVtbl->Release(lpDDS);
return NULL;
}
/*Get the new mipmap image dependent on LOD*/
uint8pnt = MipMapDesc.Data[LOD];
/*Pack and copy the texture to its surface location*/
if( !PackAndCopy( ddsd.lpSurface, uint8pnt , lpFormat, ddsd.lPitch , dwWidth , dwHeight ) )
return NULL;
lpDDS->lpVtbl->Unlock(lpDDS, NULL);
/*Get the next mipmap level*/
ddrval=lpDDS->lpVtbl->GetAttachedSurface(lpDDS, &ddscaps, &lpDDS);
dwHeight /= 2;
dwWidth /= 2;
if(ddrval!=DD_OK && lpDDS!=NULL)
{
lpDDS->lpVtbl->Release(lpDDS);
return NULL;
}
}
/*free the loaded image data and return the surface*/
for(LOD=0;LOD<MipMapDesc.LOD;LOD++)
{
free( MipMapDesc.Data[LOD] );
}
return (lpDDS_top);
}
BOOL PackAndCopy(void *dest, uint8 * uint8pnt, LPDDSURFACEDESC lpFormat, int pitch , DWORD dwWidth , DWORD dwHeight )
{
DDSURFACEDESC format;
int i, j;
int r, g, b, a;
unsigned long* lpLP;
unsigned short* lpSP;
unsigned char* lpCP;
unsigned long m;
int s;
int red_shift, red_scale;
int green_shift, green_scale;
int blue_shift, blue_scale;
int alpha_shift, alpha_scale;
memcpy(&format, lpFormat, sizeof(DDSURFACEDESC));
/*
* Determine the red, green and blue masks' shift and scale.
*/
for (s = 0, m = format.ddpfPixelFormat.dwRBitMask; !(m & 1);
s++, m >>= 1);
red_shift = s;
red_scale = 255 / (format.ddpfPixelFormat.dwRBitMask >> s);
for (s = 0, m = format.ddpfPixelFormat.dwGBitMask; !(m & 1);
s++, m >>= 1);
green_shift = s;
green_scale = 255 / (format.ddpfPixelFormat.dwGBitMask >> s);
for (s = 0, m = format.ddpfPixelFormat.dwBBitMask; !(m & 1);
s++, m >>= 1);
blue_shift = s;
blue_scale = 255 / (format.ddpfPixelFormat.dwBBitMask >> s);
if( format.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS != 0 )
{
for (s = 0, m = format.ddpfPixelFormat.dwRGBAlphaBitMask ; !(m & 1);
s++, m >>= 1);
alpha_shift = s;
alpha_scale = 255 / (format.ddpfPixelFormat.dwRGBAlphaBitMask >> s);
}else{
alpha_shift = 0;
alpha_scale = 0;
}
/*
* Each RGB bit count requires different pointers
*/
switch (format.ddpfPixelFormat.dwRGBBitCount) {
case 32 :
for (j = 0; j < (int)dwHeight; j++) {
/*
* Point to next row in texture surface
*/
lpLP = (unsigned long*)(((char*)dest) +
pitch * j);
for (i = 0; i < (int)dwWidth; i++) {
/*
* Read each value, scale it and shift it into position
*/
r = *uint8pnt++ / red_scale;
g = *uint8pnt++ / green_scale;
b = *uint8pnt++ / blue_scale;
a = 0;
if( ( r + g + b ) != 0 )
{
if( ( r < red_scale ) && ( g < green_scale ) && ( b < blue_scale ) )
{
b = blue_scale;
}
}
if( alpha_scale != 0 )
{
if( r+g+b != 0 ) a = 255 / alpha_scale;
}
*lpLP = (r << red_shift) | (g << green_shift) |
(b << blue_shift) | ( a << alpha_shift );
lpLP++;
}
}
break;
case 16 :
for (j = 0; j < (int)dwHeight; j++) {
lpSP = (unsigned short*)(((char*)dest) +
pitch * j);
for (i = 0; i < (int)dwWidth; i++) {
r = *uint8pnt++;
g = *uint8pnt++;
b = *uint8pnt++;
if( ( r + g + b ) != 0 )
{
if( ( r < red_scale ) && ( g < green_scale ) && ( b < blue_scale ) )
{
b = blue_scale;
}
}
r /= red_scale;
g /= green_scale;
b /= blue_scale;
a = 0;
if( alpha_scale != 0 )
{
if( r+g+b != 0 ) a = 255 / alpha_scale;
}
*lpSP = (r << red_shift) | (g << green_shift) |
(b << blue_shift) | ( a << alpha_shift );
lpSP++;
}
}
break;
case 8:
for (j = 0; j < (int)dwHeight; j++) {
lpCP = (unsigned char*)(((char*)dest) +
pitch * j);
for (i = 0; i < (int)dwWidth; i++) {
r = *uint8pnt++ / red_scale;
g = *uint8pnt++ / green_scale;
b = *uint8pnt++ / blue_scale;
a = 0;
if( ( r + g + b ) != 0 )
{
if( ( r < red_scale ) && ( g < green_scale ) && ( b < blue_scale ) )
{
b = blue_scale;
}
}
if( alpha_scale != 0 )
{
if( r+g+b != 0 ) a = 255 / alpha_scale;
}
*lpCP = (r << red_shift) | (g << green_shift) |
(b << blue_shift) | ( a << alpha_shift );
lpCP++;
}
}
break;
default:
//This wasn't a format I recognize
return FALSE;
}
return TRUE;
}
#ifdef OPT_ON
#pragma optimize( "", off )
#endif