-
Notifications
You must be signed in to change notification settings - Fork 17
/
grgu3df.cpp
346 lines (288 loc) · 8.72 KB
/
grgu3df.cpp
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
//**************************************************************
//* OpenGLide - Glide to OpenGL Wrapper
//* http://openglide.sourceforge.net
//*
//* 3DF functions
//*
//* OpenGLide is OpenSource under LGPL license
//* Originally made by Fabio Barros
//* Modified by Paul for Glidos (http://www.glidos.net)
//* Linux version by Simon White
//**************************************************************
#include <string.h>
#include <stdio.h>
#include "GlOgl.h"
// extern variables and functions
FxU32 GetTexSize( const int Lod, const int aspectRatio, const int format );
// prototypes
int Read3dfHeader( const char *filename, Gu3dfInfo *data );
//*************************************************
FX_ENTRY FxBool FX_CALL
gu3dfGetInfo( const char *filename, Gu3dfInfo *info )
{
#ifdef OGL_PARTDONE
GlideMsg( "gu3dfGetInfo( %s, --- )\n", filename );
#endif
if ( Read3dfHeader( filename, info ) )
{
#ifdef OGL_DEBUG
GlideMsg( "==========================================\n" );
GlideMsg( "3df file Header: %s\n", filename );
GlideMsg( "Texture Format = %d\n", info->header.format );
GlideMsg( "Aspect = %d\n", info->header.aspect_ratio );
GlideMsg( "LargeLod = %d\nSmall Lod = %d\n", info->header.large_lod, info->header.small_lod );
GlideMsg( "Width = %d Height = %d\n", info->header.width, info->header.height );
GlideMsg( "MemRequired = %d\n", info->mem_required );
GlideMsg( "==========================================\n" );
#endif
return FXTRUE;
}
else
{
return FXFALSE;
}
}
static FxU32 ReadDataLong( FILE *fp )
{
FxU32 data;
FxU8 byte[4];
fread( byte, 4, 1, fp );
data = (((FxU32) byte[ 0 ]) << 24) |
(((FxU32) byte[ 1 ]) << 16) |
(((FxU32) byte[ 2 ]) << 8) |
((FxU32) byte[ 3 ]);
return data;
}
static FxU32 ReadDataShort( FILE *fp )
{
FxU32 data;
FxU8 byte[2];
fread( byte, 2, 1, fp );
data = (((FxU32) byte[ 0 ]) << 8) |
((FxU32) byte[ 1 ]);
return data;
}
//*************************************************
FX_ENTRY FxBool FX_CALL
gu3dfLoad( const char *filename, Gu3dfInfo *data )
{
#ifdef OGL_PARTDONE
GlideMsg( "gu3dfLoad( %s, --- )\n", filename );
#endif
FILE * file3df;
int jump = Read3dfHeader( filename, data );
#ifdef OGL_DEBUG
GlideMsg( "Start of Data (Offset) = %d\n", jump );
GlideMsg( "Total Bytes to be Read = %d\n", data->mem_required );
#endif
file3df = fopen( filename, "rb" );
fseek( file3df, jump, SEEK_SET );
if ( ( data->header.format == GR_TEXFMT_P_8 ) ||
( data->header.format == GR_TEXFMT_AP_88 ) )
{
for( int i = 0; i < 256; i++ )
{
data->table.palette.data[i] = ReadDataLong( file3df );
}
#ifdef OGL_DEBUG
GlideMsg( "Reading Palette\n" );
#endif
}
if ( ( data->header.format == GR_TEXFMT_YIQ_422 ) ||
( data->header.format == GR_TEXFMT_AYIQ_8422 ) )
{
int i;
int pi;
FxU32 pack;
GuNccTable *ncc = &(data->table.nccTable);
for ( i = 0; i < 16; i++ )
{
ncc->yRGB[i] = (FxU8) ReadDataShort( file3df );
}
for ( i = 0; i < 4; i++ )
{
/* Masking with 0x1ff is strange but correct apparently */
ncc->iRGB[i][0] = (FxI16) ( ReadDataShort( file3df ) & 0x1ff );
ncc->iRGB[i][1] = (FxI16) ( ReadDataShort( file3df ) & 0x1ff );
ncc->iRGB[i][2] = (FxI16) ( ReadDataShort( file3df ) & 0x1ff );
}
for ( i = 0; i < 4; i++ )
{
ncc->qRGB[i][0] = (FxI16) ( ReadDataShort( file3df ) & 0x1ff );
ncc->qRGB[i][1] = (FxI16) ( ReadDataShort( file3df ) & 0x1ff );
ncc->qRGB[i][2] = (FxI16) ( ReadDataShort( file3df ) & 0x1ff );
}
pi = 0;
for ( i = 0; i < 4; i++ )
{
pack = ( ncc->yRGB[i*4 + 0] );
pack |= ( ncc->yRGB[i*4 + 1] << 8 );
pack |= ( ncc->yRGB[i*4 + 2] << 16 );
pack |= ( ncc->yRGB[i*4 + 3] << 24 );
ncc->packed_data[pi++] = pack;
}
for ( i = 0; i < 4; i++ )
{
pack = ( ncc->iRGB[i][0] << 18 );
pack |= ( ncc->iRGB[i][1] << 9 );
pack |= ( ncc->iRGB[i][2] );
ncc->packed_data[pi++] = pack;
}
for ( i = 0; i < 4; i++ )
{
pack = ( ncc->qRGB[i][0] << 18 );
pack |= ( ncc->qRGB[i][1] << 9 );
pack |= ( ncc->qRGB[i][2] );
ncc->packed_data[pi++] = pack;
}
}
switch ( data->header.format )
{
case GR_TEXFMT_RGB_565:
case GR_TEXFMT_ARGB_8332:
case GR_TEXFMT_ARGB_1555:
case GR_TEXFMT_AYIQ_8422:
case GR_TEXFMT_ARGB_4444:
case GR_TEXFMT_ALPHA_INTENSITY_88:
case GR_TEXFMT_AP_88:
{
FxU16 *d = (FxU16 *) (data->data);
int i;
for ( i = data->mem_required; i > 0; i -= 2 )
{
*d++ = (FxU16) ReadDataShort( file3df );
}
}
break;
default:
fread( data->data, sizeof( FxU8 ), data->mem_required, file3df );
break;
}
fclose( file3df );
return FXTRUE;
}
GrTextureFormat_t ParseTextureFormat( const char * text )
{
if ( !strcmp( text, "argb1555\n" ) )
{
return GR_TEXFMT_ARGB_1555;
}
if ( !strcmp( text, "argb4444\n" ) )
{
return GR_TEXFMT_ARGB_4444;
}
if ( !strcmp( text, "rgb565\n" ) )
{
return GR_TEXFMT_RGB_565;
}
if ( !strcmp( text, "rgb332\n" ) )
{
return GR_TEXFMT_RGB_332;
}
if ( !strcmp( text, "argb8332\n" ) )
{
return GR_TEXFMT_ARGB_8332;
}
if ( !strcmp( text, "p8\n" ) )
{
return GR_TEXFMT_P_8;
}
if ( !strcmp( text, "ap88\n" ) )
{
return GR_TEXFMT_AP_88;
}
if ( !strcmp( text, "ai44\n" ) )
{
return GR_TEXFMT_ALPHA_INTENSITY_44;
}
if ( !strcmp( text, "yiq\n" ) )
{
return GR_TEXFMT_YIQ_422;
}
if ( !strcmp( text, "ayiq8422\n" ) )
{
return GR_TEXFMT_AYIQ_8422;
}
return 0;
}
int ParseLod( int Lod )
{
switch( Lod )
{
case 256: return GR_LOD_256;
case 128: return GR_LOD_128;
case 64: return GR_LOD_64;
case 32: return GR_LOD_32;
case 16: return GR_LOD_16;
case 8: return GR_LOD_8;
case 4: return GR_LOD_4;
case 2: return GR_LOD_2;
case 1: return GR_LOD_1;
}
return -1;
}
GrAspectRatio_t ParseAspect( int h, int v )
{
switch ( h )
{
case 8: return GR_ASPECT_8x1;
case 4: return GR_ASPECT_4x1;
case 2: return GR_ASPECT_2x1;
case 1:
switch ( v )
{
case 8: return GR_ASPECT_1x8;
case 4: return GR_ASPECT_1x4;
case 2: return GR_ASPECT_1x2;
case 1: return GR_ASPECT_1x1;
}
}
return 0;
}
int Read3dfHeader( const char *filename, Gu3dfInfo *data )
{
FILE * file3df;
char buffer[255];
int temp1,
temp2,
lod1,
lod2,
nWidth,
nHeight;
file3df = fopen( filename, "rb" );
if ( file3df == NULL )
{
return 0;
}
fgets( buffer, 255, file3df );
fgets( buffer, 255, file3df );
data->header.format = ParseTextureFormat( buffer );
fgets( buffer, 255, file3df );
sscanf( buffer, "lod range: %d %d\n", &lod1, &lod2 );
data->header.small_lod = ParseLod( lod1 );
data->header.large_lod = ParseLod( lod2 );
fgets( buffer, 255, file3df );
sscanf( buffer, "aspect ratio: %d %d\n", &temp1, &temp2 );
data->header.aspect_ratio = ParseAspect( temp1, temp2 );
switch ( data->header.aspect_ratio )
{
case GR_ASPECT_8x1: nWidth = lod2; nHeight = lod2 >> 3; break;
case GR_ASPECT_4x1: nWidth = lod2; nHeight = lod2 >> 2; break;
case GR_ASPECT_2x1: nWidth = lod2; nHeight = lod2 >> 1; break;
case GR_ASPECT_1x1: nWidth = lod2; nHeight = lod2; break;
case GR_ASPECT_1x2: nWidth = lod2 >> 1; nHeight = lod2; break;
case GR_ASPECT_1x4: nWidth = lod2 >> 2; nHeight = lod2; break;
case GR_ASPECT_1x8: nWidth = lod2 >> 3; nHeight = lod2; break;
}
data->header.width = nWidth;
data->header.height = nHeight;
{
GrLOD_t l;
data->mem_required = 0;
for ( l = data->header.large_lod; l <= data->header.small_lod; l++ )
data->mem_required += GetTexSize( l, data->header.aspect_ratio, data->header.format );
}
temp1 = ftell( file3df );
fclose( file3df );
return temp1;
}