aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CMY3DHelper.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CMY3DHelper.h')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CMY3DHelper.h447
1 files changed, 447 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CMY3DHelper.h b/src/others/irrlicht-1.8.1/source/Irrlicht/CMY3DHelper.h
new file mode 100644
index 0000000..e045444
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CMY3DHelper.h
@@ -0,0 +1,447 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4//
5// This file was originally written by ZDimitor.
6
7//----------------------------------------------------------------------
8// somefuncs.h - part of the My3D Tools
9//
10// This tool was created by Zhuck Dmitry (ZDimitor).
11// Everyone can use it as wants ( i'll be happy if it helps to someone :) ).
12//----------------------------------------------------------------------
13
14//**********************************************************************
15// some useful functions
16//**********************************************************************
17
18#ifndef __C_MY3D_HELPER_H_INCLUDED__
19#define __C_MY3D_HELPER_H_INCLUDED__
20
21#include <irrTypes.h>
22
23namespace irr
24{
25namespace scene
26{
27
28//**********************************************************************
29// MY3D stuff
30//**********************************************************************
31
32// byte-align structures
33#include "irrpack.h"
34
35struct SMyVector3
36{ SMyVector3 () {;}
37 SMyVector3 (f32 __X, f32 __Y, f32 __Z)
38 : X(__X), Y(__Y), Z(__Z) {}
39 f32 X, Y, Z;
40} PACK_STRUCT;
41
42struct SMyVector2
43{ SMyVector2 () {;}
44 SMyVector2(f32 __X, f32 __Y)
45 : X(__X), Y(__Y) {}
46 f32 X, Y;
47} PACK_STRUCT;
48
49struct SMyVertex
50{ SMyVertex () {;}
51 SMyVertex (SMyVector3 _Coord, SMyColor _Color, SMyVector3 _Normal)
52 :Coord(_Coord), Color(_Color), Normal(_Normal) {;}
53 SMyVector3 Coord;
54 SMyColor Color;
55 SMyVector3 Normal;
56} PACK_STRUCT;
57
58struct SMyTVertex
59{ SMyTVertex () {;}
60 SMyTVertex (SMyVector2 _TCoord)
61 : TCoord(_TCoord) {;}
62 SMyVector2 TCoord;
63} PACK_STRUCT;
64
65struct SMyFace
66{ SMyFace() {;}
67 SMyFace(u32 __A, u32 __B, u32 __C)
68 : A(__A), B(__B), C(__C) {}
69 u32 A, B, C;
70} PACK_STRUCT;
71
72// file header (6 bytes)
73struct SMyFileHeader
74{ u32 MyId; // MY3D
75 u16 Ver; // Version
76} PACK_STRUCT;
77
78// scene header
79struct SMySceneHeader
80{ SMyColor BackgrColor; // background color
81 SMyColor AmbientColor; // ambient color
82 s32 MaterialCount; // material count
83 s32 MeshCount; // mesh count
84} PACK_STRUCT;
85
86// mesh header
87struct SMyMeshHeader
88{ c8 Name[256]; // material name
89 u32 MatIndex; // index of the mesh material
90 u32 TChannelCnt; // mesh mapping channels count
91} PACK_STRUCT;
92
93// texture data header
94struct SMyTexDataHeader
95{ c8 Name[256]; // texture name
96 u32 ComprMode; //compression mode
97 u32 PixelFormat;
98 u32 Width; // image width
99 u32 Height; // image height
100} PACK_STRUCT;
101
102// pixel color 24bit (R8G8B8)
103struct SMyPixelColor24
104{ SMyPixelColor24() {;}
105 SMyPixelColor24(u8 __r, u8 __g, u8 __b)
106 : r(__r), g(__g), b(__b) {}
107 u8 r, g, b;
108} PACK_STRUCT;
109
110// pixel color 16bit (A1R5G5B5)
111struct SMyPixelColor16
112{ SMyPixelColor16() {;}
113 SMyPixelColor16(s16 _argb): argb(_argb) {;}
114 SMyPixelColor16(u8 r, u8 g, u8 b)
115 { argb = ((r&0x1F)<<10) | ((g&0x1F)<<5) | (b&0x1F);
116 }
117 s16 argb;
118} PACK_STRUCT;
119
120// RLE Header
121struct SMyRLEHeader
122{ SMyRLEHeader() {}
123 u32 nEncodedBytes;
124 u32 nDecodedBytes;
125} PACK_STRUCT;
126
127// Default alignment
128#include "irrunpack.h"
129
130} // end namespace
131} // end namespace
132
133//-----------------------------------------------------------------------------
134namespace irr
135{
136namespace core
137{
138
139//-----------------RLE stuff-----------------------------------------
140
141int rle_encode (
142 unsigned char *in_buf, int in_buf_size,
143 unsigned char *out_buf, int out_buf_size
144 );
145unsigned long process_comp(
146 unsigned char *buf, int buf_size,
147 unsigned char *out_buf, int out_buf_size
148 );
149void process_uncomp(
150 unsigned char, unsigned char *out_buf, int out_buf_size
151 );
152void flush_outbuf(
153 unsigned char *out_buf, int out_buf_size
154 );
155unsigned long get_byte (
156 unsigned char *ch,
157 unsigned char *in_buf, int in_buf_size,
158 unsigned char *out_buf, int out_buf_size
159 );
160void put_byte(
161 unsigned char ch, unsigned char *out_buf, int out_buf_size
162 );
163//-----------------------------------------------------------
164const unsigned long LIMIT = 1; // was #define LIMIT 1
165const unsigned long NON_MATCH = 2; // was: #define NON_MATCH 2
166const unsigned long EOD_FOUND = 3; // was: #define EOD_FOUND 3
167const unsigned long EOD = 0x00454f44; // was: #define EOD 'EOD'
168//-----------------------------------------------------------
169// number of decoded bytes
170static int nDecodedBytes=0;
171// number of coded bytes
172static int nCodedBytes=0;
173// number of read bytes
174static int nReadedBytes=0;
175// table used to look for sequences of repeating bytes
176static unsigned char tmpbuf[4]; // we use subscripts 1 - 3
177static int tmpbuf_cnt;
178// output buffer for non-compressed output data
179static unsigned char outbuf[128];
180static int outbuf_cnt;
181
182
183//-----------------------------------------------------------
184int rle_encode (
185 unsigned char *in_buf, int in_buf_size,
186 unsigned char *out_buf, int out_buf_size
187 )
188{
189 unsigned long ret_code;
190
191 unsigned char ch;
192
193 nCodedBytes=0;
194 nReadedBytes=0;
195
196 tmpbuf_cnt = 0; // no. of char's in tmpbuf
197 outbuf_cnt = 0; // no. of char's in outbuf
198 while (1)
199 {
200 if (get_byte(&ch, in_buf, in_buf_size,
201 out_buf, out_buf_size) == (int)EOD) // read next byte into ch
202 break;
203
204 tmpbuf[++tmpbuf_cnt] = (unsigned char) ch;
205 if (tmpbuf_cnt == 3)
206 {
207 // see if all 3 match each other
208 if ((tmpbuf[1] == tmpbuf[2]) && (tmpbuf[2] == tmpbuf[3]))
209 {
210 // they do - add compression
211 // this will process all bytes in input file until
212 // a non-match occurs, or 128 bytes are processed,
213 // or we find eod */
214 ret_code = process_comp(in_buf, in_buf_size, out_buf, out_buf_size);
215 if (ret_code == (int)EOD_FOUND)
216 break; // stop compressing
217 if (ret_code == (int)NON_MATCH)
218 tmpbuf_cnt=1; /* save the char that didn't match */
219 else
220 // we just compressed the max. of 128 bytes
221 tmpbuf_cnt=0; /* start over for next chunk */
222 }
223 else
224 {
225 // we know the first byte doesn't match 2 or more
226 // others, so just send it out as uncompressed. */
227 process_uncomp(tmpbuf[1], out_buf, out_buf_size);
228
229 // see if the last 2 bytes in the buffer match
230 if (tmpbuf[2] == tmpbuf[3])
231 {
232 // move byte 3 to position 1 and pretend we just
233 // have 2 bytes -- note that the first byte was
234 // already sent to output */
235 tmpbuf[1]=tmpbuf[3];
236 tmpbuf_cnt=2;
237 }
238 else
239 {
240 // send byte 2 and keep byte 3 - it may match the
241 // next byte. Move byte 3 to position 1 and set
242 // count to 1. Note that the first byte was
243 // already sent to output
244 process_uncomp(tmpbuf[2], out_buf, out_buf_size);
245 tmpbuf[1]=tmpbuf[3];
246 tmpbuf_cnt=1;
247 }
248 }
249 }
250 } // end while
251 flush_outbuf(out_buf, out_buf_size);
252
253 return nCodedBytes;
254}
255
256
257//------------------------------------------------------------------
258// This flushes any non-compressed data not yet sent, then it processes
259// repeating bytes until > 128, or EOD, or non-match.
260// return values: LIMIT, EOD_FOUND, NON_MATCH
261// Prior to ANY return, it writes out the 2 byte compressed code.
262// If a NON_MATCH was found, this returns with the non-matching char
263// residing in tmpbuf[0].
264// Inputs: tmpbuf[0], input file
265// Outputs: tmpbuf[0] (sometimes), output file, and return code
266//------------------------------------------------------------------
267unsigned long process_comp(
268 unsigned char *buf, int buf_size,
269 unsigned char *out_buf, int out_buf_size)
270{
271 // we start out with 3 repeating bytes
272 register int len = 3;
273
274 unsigned char ch;
275
276 // we're starting a repeating chunk - end the non-repeaters
277 flush_outbuf(out_buf, out_buf_size);
278
279 while (get_byte(&ch, buf, buf_size, out_buf, out_buf_size) != (int)EOD)
280 {
281 if (ch != tmpbuf[1])
282 {
283 // send no. of repeated bytes to be encoded
284 put_byte((unsigned char)((--len) | 0x80), out_buf, out_buf_size);
285 // send the byte's value being repeated
286 put_byte((unsigned char)tmpbuf[1], out_buf, out_buf_size);
287 /* save the non-matching character just read */
288 tmpbuf[1]=(unsigned char) ch;
289 return NON_MATCH;
290 }
291 /* we know the new byte is part of the repeating seq */
292 len++;
293 if (len == 128)
294 {
295 // send no. of repeated bytes to be encoded
296 put_byte((unsigned char)((--len) | 0x80), out_buf, out_buf_size);
297 // send the byte's value being repeated
298 put_byte((unsigned char)tmpbuf[1], out_buf, out_buf_size);
299 return LIMIT;
300 }
301 } // end while
302
303 // if flow comes here, we just read an EOD
304 // send no. of repeated bytes to be encoded
305 put_byte((unsigned char)((--len) | 0x80), out_buf, out_buf_size);
306 // send the byte's value being repeated
307 put_byte((unsigned char)tmpbuf[1], out_buf, out_buf_size);
308 return EOD_FOUND;
309}
310
311
312//----------------------------------------------------------------
313// This adds 1 non-repeating byte to outbuf. If outbuf becomes full
314// with 128 bytes, it flushes outbuf.
315// There are no return codes and no bytes are read from the input.
316//----------------------------------------------------------------
317void process_uncomp(
318 unsigned char char1, unsigned char *out_buf, int out_buf_size
319 )
320{
321 outbuf[outbuf_cnt++] = char1;
322 if (outbuf_cnt == 128)
323 flush_outbuf(out_buf, out_buf_size);
324}
325//-----------------------------------------------------------
326// This flushes any non-compressed data not yet sent.
327// On exit, outbuf_cnt will equal zero.
328//-----------------------------------------------------------
329void flush_outbuf(unsigned char *out_buf, int out_buf_size)
330{
331 register int pos=0;
332
333 if(!outbuf_cnt)
334 return; // nothing to do */
335
336 // send no. of unencoded bytes to be sent
337 put_byte((unsigned char)(outbuf_cnt - 1), out_buf, out_buf_size);
338
339 for ( ; outbuf_cnt; outbuf_cnt--)
340 put_byte((unsigned char)outbuf[pos++], out_buf, out_buf_size);
341}
342//---------------------------------------------------
343void put_byte(unsigned char ch, unsigned char *out_buf, int out_buf_size)
344{
345 if (nCodedBytes<=(out_buf_size-1))
346 { out_buf[nCodedBytes++]=ch;
347 out_buf[nCodedBytes]=0;
348 }
349}
350//---------------------------------------------------
351// This reads the next byte into ch. It returns EOD
352// at end-of-data
353//---------------------------------------------------
354unsigned long get_byte(
355 unsigned char *ch,
356 unsigned char *in_buf, int in_buf_size,
357 unsigned char *out_buf, int out_buf_size
358 )
359{
360 if (nReadedBytes>=in_buf_size)
361 {
362 // there are either 0, 1, or 2 char's to write before we quit
363 if (tmpbuf_cnt == 1)
364 process_uncomp(tmpbuf[1], out_buf, out_buf_size);
365 else
366 {
367 if (tmpbuf_cnt == 2)
368 {
369 process_uncomp(tmpbuf[1], out_buf, out_buf_size);
370 process_uncomp(tmpbuf[2], out_buf, out_buf_size);
371 }
372 }
373 nReadedBytes =0;
374
375 return EOD;
376 }
377
378 (*ch) = (unsigned char)in_buf[nReadedBytes++];
379
380 return 0;
381}
382//-----------------------------------------------------------
383int rle_decode (
384 unsigned char *in_buf, int in_buf_size,
385 unsigned char *out_buf, int out_buf_size
386 )
387{
388 nDecodedBytes=0;
389 nReadedBytes=0;
390
391 int ch, i;
392 while (1)
393 {
394
395 if (nReadedBytes>=in_buf_size)
396 break;
397 else
398 ch=in_buf[nReadedBytes];
399 nReadedBytes++;
400
401 if (ch > 127)
402 {
403 i = ch - 127; // i is the number of repetitions
404 // get the byte to be repeated
405 if (nReadedBytes>=in_buf_size)
406 break;
407 else
408 ch=in_buf[nReadedBytes];
409 nReadedBytes++;
410
411 // uncompress a chunk
412 for ( ; i ; i--)
413 {
414 if (nDecodedBytes<out_buf_size)
415 out_buf[nDecodedBytes] = ch;
416 nDecodedBytes++;
417 }
418 }
419 else
420 {
421 // copy out some uncompressed bytes
422 i = ch + 1; // i is the no. of bytes
423 // uncompress a chunk
424 for ( ; i ; i--)
425 {
426 if (nReadedBytes>=in_buf_size)
427 break;
428 else
429 ch=in_buf[nReadedBytes];
430 nReadedBytes++;
431
432 if (nDecodedBytes<out_buf_size)
433 out_buf[nDecodedBytes] = ch;
434 nDecodedBytes++;
435 }
436 }
437 } // end while
438
439 return nDecodedBytes;
440}
441
442} //end namespace core
443} //end namespace irr
444
445
446#endif // __C_MY3D_HELPER_H_INCLUDED__
447