1 /*
2      PLIB - A Suite of Portable Game Libraries
3      Copyright (C) 1998,2002  Steve Baker
4 
5      This library is free software; you can redistribute it and/or
6      modify it under the terms of the GNU Library General Public
7      License as published by the Free Software Foundation; either
8      version 2 of the License, or (at your option) any later version.
9 
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Library General Public License for more details.
14 
15      You should have received a copy of the GNU Library General Public
16      License along with this library; if not, write to the Free
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 
19      For further information visit http://plib.sourceforge.net
20 
21      $Id: ssgLoadMDL_BGLTexture.cxx 2117 2007-09-13 23:21:09Z fayjf $
22 */
23 
24 //===========================================================================
25 //
26 // File: ssgLoadMDL_BGLTexture.cxx
27 //
28 // Created: Jun 1 22:20:31 2002
29 //
30 // Author: Thomas Engh Sevaldrud <tse@math.sintef.no>
31 //         Juergen Marquardt <juergen_marquardt@t-online.de>
32 //
33 // Revision: $Id: ssgLoadMDL_BGLTexture.cxx 2117 2007-09-13 23:21:09Z fayjf $
34 //
35 // Description:
36 //
37 // Changes:  by Juergen Marquardt
38 //           - separated from ssgLoadMDL
39 //             it's now used by ssgLoadMDL and ssgLoadBGL
40 //           - added support for translucatant textures
41 //           - rindex replaced by strrchr
42 //
43 //===========================================================================
44 
45 #include "ssgLocal.h"
46 #include "ssgMSFSPalette.h"
47 
48 //===========================================================================
49 // Texture palette values (from fs5.pal)
50 // Alpha channel added for more efficient
51 // rendering on 3dfx hardware.
52 
53 unsigned const char ssgFsTexPalette[1024] = {
54   0x00, 0x00, 0x00, 0xff,
55   0x08, 0x08, 0x08, 0xff,
56   0x10, 0x10, 0x10, 0xff,
57   0x18, 0x18, 0x18, 0xff,
58   0x20, 0x20, 0x20, 0xff,
59   0x28, 0x28, 0x28, 0xff,
60   0x30, 0x30, 0x30, 0xff,
61   0x38, 0x38, 0x38, 0xff,
62   0x41, 0x41, 0x41, 0xff,
63   0x49, 0x49, 0x49, 0xff,
64   0x51, 0x51, 0x51, 0xff,
65   0x59, 0x59, 0x59, 0xff,
66   0x61, 0x61, 0x61, 0xff,
67   0x69, 0x69, 0x69, 0xff,
68   0x71, 0x71, 0x71, 0xff,
69   0x79, 0x79, 0x79, 0xff,
70   0x82, 0x82, 0x82, 0xff,
71   0x8a, 0x8a, 0x8a, 0xff,
72   0x92, 0x92, 0x92, 0xff,
73   0x9a, 0x9a, 0x9a, 0xff,
74   0xa2, 0xa2, 0xa2, 0xff,
75   0xaa, 0xaa, 0xaa, 0xff,
76   0xb2, 0xb2, 0xb2, 0xff,
77   0xba, 0xba, 0xba, 0xff,
78   0xc3, 0xc3, 0xc3, 0xff,
79   0xcb, 0xcb, 0xcb, 0xff,
80   0xd3, 0xd3, 0xd3, 0xff,
81   0xdb, 0xdb, 0xdb, 0xff,
82   0xe3, 0xe3, 0xe3, 0xff,
83   0xeb, 0xeb, 0xeb, 0xff,
84   0xf7, 0xf7, 0xf7, 0xff,
85   0xff, 0xff, 0xff, 0xff,
86   0x15, 0x05, 0x05, 0xff,
87   0x2a, 0x0a, 0x0a, 0xff,
88   0x3f, 0x0f, 0x0f, 0xff,
89   0x54, 0x14, 0x14, 0xff,
90   0x69, 0x19, 0x19, 0xff,
91   0x7e, 0x1e, 0x1e, 0xff,
92   0x93, 0x23, 0x23, 0xff,
93   0xa8, 0x28, 0x28, 0xff,
94   0xbd, 0x2d, 0x2d, 0xff,
95   0xd2, 0x32, 0x32, 0xff,
96   0xe7, 0x37, 0x37, 0xff,
97   0xfc, 0x3c, 0x3c, 0xff,
98   0x05, 0x15, 0x05, 0xff,
99   0x0a, 0x2a, 0x0a, 0xff,
100   0x0f, 0x3f, 0x0f, 0xff,
101   0x14, 0x54, 0x14, 0xff,
102   0x19, 0x69, 0x19, 0xff,
103   0x1e, 0x7e, 0x1e, 0xff,
104   0x23, 0x93, 0x23, 0xff,
105   0x28, 0xa8, 0x28, 0xff,
106   0x2d, 0xbd, 0x2d, 0xff,
107   0x32, 0xd2, 0x32, 0xff,
108   0x37, 0xe7, 0x37, 0xff,
109   0x3c, 0xfc, 0x3c, 0xff,
110   0x00, 0x07, 0x17, 0xff,
111   0x00, 0x0f, 0x28, 0xff,
112   0x00, 0x17, 0x3a, 0xff,
113   0x00, 0x28, 0x54, 0xff,
114   0x00, 0x40, 0x68, 0xff,
115   0x00, 0x47, 0x7a, 0xff,
116   0x00, 0x57, 0x8f, 0xff,
117   0x00, 0x63, 0x9c, 0xff,
118   0x00, 0x70, 0xb3, 0xff,
119   0x00, 0x80, 0xc7, 0xff,
120   0x00, 0x8f, 0xd7, 0xff,
121   0x00, 0x99, 0xe6, 0xff,
122   0x1c, 0x0e, 0x00, 0xff,
123   0x38, 0x1c, 0x00, 0xff,
124   0x54, 0x2a, 0x00, 0xff,
125   0x70, 0x38, 0x00, 0xff,
126   0x8c, 0x46, 0x00, 0xff,
127   0xa8, 0x54, 0x00, 0xff,
128   0xc4, 0x62, 0x00, 0xff,
129   0xe0, 0x70, 0x00, 0xff,
130   0xfc, 0x7e, 0x00, 0xff,
131   0x1c, 0x1c, 0x00, 0xff,
132   0x38, 0x38, 0x00, 0xff,
133   0x54, 0x54, 0x00, 0xff,
134   0x70, 0x70, 0x00, 0xff,
135   0x8c, 0x8c, 0x00, 0xff,
136   0xa8, 0xa8, 0x00, 0xff,
137   0xc4, 0xc4, 0x00, 0xff,
138   0xe0, 0xe0, 0x00, 0xff,
139   0xfc, 0xfc, 0x00, 0xff,
140   0x19, 0x15, 0x10, 0xff,
141   0x32, 0x2a, 0x20, 0xff,
142   0x4b, 0x3f, 0x30, 0xff,
143   0x64, 0x54, 0x40, 0xff,
144   0x7d, 0x69, 0x50, 0xff,
145   0x96, 0x7e, 0x60, 0xff,
146   0xaf, 0x93, 0x70, 0xff,
147   0xc8, 0xa8, 0x80, 0xff,
148   0xe1, 0xbd, 0x90, 0xff,
149   0x1c, 0x0b, 0x07, 0xff,
150   0x38, 0x16, 0x0e, 0xff,
151   0x54, 0x21, 0x15, 0xff,
152   0x70, 0x2c, 0x1c, 0xff,
153   0x8c, 0x37, 0x23, 0xff,
154   0xa8, 0x42, 0x2a, 0xff,
155   0xc4, 0x4d, 0x31, 0xff,
156   0xe0, 0x58, 0x38, 0xff,
157   0xfc, 0x63, 0x3f, 0xff,
158   0x11, 0x16, 0x09, 0xff,
159   0x22, 0x2c, 0x12, 0xff,
160   0x33, 0x42, 0x1b, 0xff,
161   0x44, 0x58, 0x24, 0xff,
162   0x55, 0x6e, 0x2d, 0xff,
163   0x66, 0x84, 0x36, 0xff,
164   0x77, 0x9a, 0x3f, 0xff,
165   0x88, 0xb0, 0x48, 0xff,
166   0x99, 0xc6, 0x51, 0xff,
167   0x00, 0x3a, 0x68, 0xff,
168   0x00, 0x57, 0x70, 0xff,
169   0x2b, 0x70, 0x80, 0xff,
170   0xff, 0x00, 0x00, 0xff,
171   0x40, 0xff, 0x40, 0xff,
172   0x00, 0x00, 0xc0, 0xff,
173   0x00, 0x69, 0x69, 0xff,
174   0xff, 0x80, 0x00, 0xff,
175   0xff, 0xff, 0x00, 0xff,
176   0x51, 0x51, 0x51, 0xff,
177   0x79, 0x79, 0x79, 0xff,
178   0x92, 0x92, 0x92, 0xff,
179   0xaa, 0xaa, 0xaa, 0xff,
180   0xc3, 0xc3, 0xc3, 0xff,
181   0xe3, 0xe3, 0xe3, 0xff,
182   0x00, 0x00, 0x00, 0xff,
183   0x00, 0x00, 0x00, 0xff,
184   0x00, 0x00, 0x00, 0xff,
185   0x00, 0x00, 0x00, 0xff,
186   0x00, 0x00, 0x00, 0xff,
187   0x00, 0x00, 0x00, 0xff,
188   0x00, 0x00, 0x00, 0xff,
189   0x00, 0x00, 0x00, 0xff,
190   0x00, 0x00, 0x00, 0xff,
191   0x00, 0x00, 0x00, 0xff,
192   0x00, 0x00, 0x00, 0xff,
193   0x00, 0x00, 0x00, 0xff,
194   0x00, 0x00, 0x00, 0xff,
195   0x00, 0x00, 0x00, 0xff,
196   0x00, 0x00, 0x00, 0xff,
197   0x00, 0x00, 0x00, 0xff,
198   0x00, 0x00, 0x00, 0xff,
199   0x00, 0x00, 0x00, 0xff,
200   0x00, 0x00, 0x00, 0xff,
201   0x00, 0x00, 0x00, 0xff,
202   0x00, 0x00, 0x00, 0xff,
203   0x00, 0x00, 0x00, 0xff,
204   0x00, 0x00, 0x00, 0xff,
205   0x00, 0x00, 0x00, 0xff,
206   0x00, 0x00, 0x00, 0xff,
207   0x00, 0x00, 0x00, 0xff,
208   0x00, 0x00, 0x00, 0xff,
209   0x00, 0x00, 0x00, 0xff,
210   0x00, 0x00, 0x00, 0xff,
211   0x00, 0x00, 0x00, 0xff,
212   0x00, 0x00, 0x00, 0xff,
213   0x00, 0x00, 0x00, 0xff,
214   0xc0, 0xc0, 0xff, 0xff,
215   0xc8, 0xc8, 0xff, 0xff,
216   0xd0, 0xd0, 0xff, 0xff,
217   0xd8, 0xd8, 0xff, 0xff,
218   0xe0, 0xe0, 0xff, 0xff,
219   0xe8, 0xe8, 0xff, 0xff,
220   0xf0, 0xf0, 0xff, 0xff,
221   0xf8, 0xf8, 0xff, 0xff,
222   0xff, 0xff, 0xff, 0xff,
223   0xff, 0xff, 0xff, 0xff,
224   0xff, 0xff, 0xff, 0xff,
225   0xff, 0xff, 0xff, 0xff,
226   0xff, 0xff, 0xff, 0xff,
227   0xff, 0xff, 0xff, 0xff,
228   0xff, 0xff, 0xff, 0xff,
229   0xff, 0xff, 0xff, 0xff,
230   0x10, 0x48, 0x10, 0xff,
231   0x20, 0x50, 0x20, 0xff,
232   0x30, 0x58, 0x30, 0xff,
233   0x40, 0x60, 0x40, 0xff,
234   0x50, 0x68, 0x50, 0xff,
235   0x60, 0x70, 0x60, 0xff,
236   0x70, 0x78, 0x70, 0xff,
237   0x78, 0x7c, 0x78, 0xff,
238   0x80, 0x80, 0x80, 0xff,
239   0x80, 0x80, 0x80, 0xff,
240   0x80, 0x80, 0x80, 0xff,
241   0x80, 0x80, 0x80, 0xff,
242   0x80, 0x80, 0x80, 0xff,
243   0x80, 0x80, 0x80, 0xff,
244   0x80, 0x80, 0x80, 0xff,
245   0x80, 0x80, 0x80, 0xff,
246   0x21, 0x8c, 0xbd, 0xff,
247   0x39, 0x84, 0xa5, 0xff,
248   0xbd, 0x42, 0x42, 0xff,
249   0x9c, 0x42, 0x42, 0xff,
250   0x84, 0x4a, 0x4a, 0xff,
251   0x21, 0x52, 0x6b, 0xff,
252   0xd6, 0x5a, 0x52, 0xff,
253   0xbd, 0x5a, 0x52, 0xff,
254   0xa5, 0x5a, 0x52, 0xff,
255   0x7b, 0x39, 0x31, 0xff,
256   0x63, 0x39, 0x31, 0xff,
257   0x6b, 0x4a, 0x42, 0xff,
258   0x7b, 0x5a, 0x52, 0xff,
259   0xb5, 0x5a, 0x42, 0xff,
260   0x4a, 0x31, 0x29, 0xff,
261   0xbd, 0x73, 0x5a, 0xff,
262   0x8c, 0x5a, 0x31, 0xff,
263   0x21, 0x31, 0x4a, 0xff,
264   0xb5, 0x73, 0x31, 0xff,
265   0x63, 0x42, 0x21, 0xff,
266   0xa5, 0x73, 0x42, 0xff,
267   0x31, 0x29, 0x21, 0xff,
268   0xa5, 0x8c, 0x73, 0xff,
269   0xbd, 0xa5, 0x8c, 0xff,
270   0x39, 0x63, 0x7b, 0xff,
271   0xb5, 0x6b, 0x18, 0xff,
272   0xce, 0x7b, 0x21, 0xff,
273   0x9c, 0x63, 0x21, 0xff,
274   0x94, 0x6b, 0x31, 0xff,
275   0x6b, 0x52, 0x31, 0xff,
276   0x21, 0x21, 0x39, 0xff,
277   0x21, 0x73, 0xa5, 0xff,
278   0xd6, 0xd6, 0x21, 0xff,
279   0xad, 0xad, 0x21, 0xff,
280   0xc6, 0xc6, 0x29, 0xff,
281   0x8c, 0x8c, 0x21, 0xff,
282   0x73, 0x73, 0x21, 0xff,
283   0xbd, 0xbd, 0x39, 0xff,
284   0x9c, 0x9c, 0x31, 0xff,
285   0xad, 0xad, 0x39, 0xff,
286   0x7b, 0x7b, 0x31, 0xff,
287   0x7b, 0x7b, 0x42, 0xff,
288   0x4a, 0x4a, 0x31, 0xff,
289   0x7b, 0x7b, 0x5a, 0xff,
290   0x29, 0x29, 0x21, 0xff,
291   0x5a, 0x63, 0x39, 0xff,
292   0x6b, 0x73, 0x4a, 0xff,
293   0x7b, 0x94, 0x52, 0xff,
294   0x8c, 0xad, 0x63, 0xff,
295   0x84, 0x9c, 0x63, 0xff,
296   0x31, 0x42, 0x29, 0xff,
297   0x63, 0xa5, 0x5a, 0xff,
298   0x4a, 0xd6, 0x4a, 0xff,
299   0x39, 0x8c, 0x39, 0xff,
300   0x4a, 0xb5, 0x4a, 0xff,
301   0x5a, 0xc6, 0x5a, 0xff,
302   0x39, 0x7b, 0x39, 0xff,
303   0x31, 0x63, 0x31, 0xff,
304   0x5a, 0xa5, 0x5a, 0xff,
305   0x52, 0x94, 0x52, 0xff,
306   0x4a, 0x63, 0x4a, 0xff,
307   0x39, 0x73, 0x84, 0xff,
308   0x21, 0x63, 0x7b, 0xff,
309   0x4a, 0x73, 0x84, 0xff
310 };
311 
312 #ifdef SSG_LOAD_MDL_BGL_TEXTURE_SUPPORTED
313 // This really simple (raw paletted) format is used by older MSFS for textures
ssgLoadMDLTexture(const char * fname,ssgTextureInfo * info)314 bool ssgLoadMDLTexture ( const char *fname, ssgTextureInfo* info )
315 {
316   FILE *tfile;
317   int index = 0;
318   if ( (tfile = fopen(fname, "rb")) == NULL) {
319     char *dupfname = strdup( fname);
320     char *p = strrchr( dupfname,'_');
321     if (p != 0) {
322       *p = '\0';
323       p++;
324       index = atoi (p);
325       if ( (tfile = fopen(dupfname, "rb")) == NULL) {
326         ulSetError( UL_WARNING, "ssgLoadTexture: Failed to load '%s'.", dupfname );
327         free( dupfname);
328         return false ;
329       }
330       free( dupfname);
331     }
332     else {
333       ulSetError( UL_WARNING, "ssgLoadTexture: Failed to load '%s'.", fname );
334       free( dupfname);
335       return false ;
336     }
337   }
338 
339   fseek(tfile, 0, SEEK_END);
340   unsigned long file_length = ftell(tfile);
341 
342   if (file_length != 65536) {
343     // this is not a MSFS-formatted texture, so it's probably a BMP
344     fclose(tfile);
345     return ssgLoadBMP( fname, info );
346   } else {
347     fseek(tfile, 0, SEEK_SET);
348 
349     unsigned char *texels = new unsigned char[256 * 256 * 4];
350     int c = 0;
351     for (int y = 0; y < 256; y++) {
352       for (int x = 0; x < 256; x++) {
353         unsigned char b;
354         fread(&b, 1, 1, tfile);
355         texels[c++] = ssgFsTexPalette[b*4    ];
356         texels[c++] = ssgFsTexPalette[b*4 + 1];
357         texels[c++] = ssgFsTexPalette[b*4 + 2];
358         texels[c++] = (b<index)?0:255;
359       }
360     }
361 
362     fclose(tfile);
363 
364     if ( info != NULL )
365     {
366       info -> width = 256 ;
367       info -> height = 256 ;
368       info -> depth = 4 ;
369       info -> alpha = TRUE ;  //??
370     }
371 
372     return ssgMakeMipMaps ( texels, 256, 256, 4 ) ;
373   }
374 }
375 #else
376 
ssgLoadMDLTexture(const char * fname,ssgTextureInfo * info)377 bool ssgLoadMDLTexture ( const char *fname, ssgTextureInfo* info )
378 {
379   ulSetError ( UL_WARNING,
380     "ssgLoadTexture: '%s' - MDL/BGL Texture support not configured", fname ) ;
381   return false ;
382 }
383 
384 #endif
385