aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_skp/imp_skp_callbacks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_skp/imp_skp_callbacks.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_skp/imp_skp_callbacks.c614
1 files changed, 614 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_skp/imp_skp_callbacks.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_skp/imp_skp_callbacks.c
new file mode 100644
index 0000000..938a93c
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_skp/imp_skp_callbacks.c
@@ -0,0 +1,614 @@
1/* $Id$ */
2
3/*
4 libg3d - 3D object loading library
5
6 Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22#include <g3d/types.h>
23#include <g3d/material.h>
24
25#include "imp_skp.h"
26#include "imp_skp_callbacks.h"
27#include "imp_skp_read.h"
28#include "imp_skp_types.h"
29
30gboolean skp_cb_arc_curve(SkpGlobalData *global, SkpLocalData *local)
31{
32 guint16 w1;
33
34 w1 = g3d_stream_read_int16_le(global->stream);
35 g_return_val_if_fail(w1 == 0x0000, FALSE);
36
37 return TRUE;
38}
39
40/*****************************************************************************/
41
42gboolean skp_cb_attribute_container(SkpGlobalData *global, SkpLocalData *local)
43{
44 guint16 w1;
45
46 w1 = g3d_stream_read_int16_le(global->stream);
47 g_return_val_if_fail(w1 == 0x0000, FALSE);
48
49 return TRUE;
50}
51
52/*****************************************************************************/
53
54gboolean skp_cb_attribute_named(SkpGlobalData *global, SkpLocalData *local)
55{
56 guint32 x1;
57 guint16 w1, w2, w3;
58 guint8 u1, u2;
59 gdouble d1;
60 gchar *name, *val, *tmp;
61
62 w1 = g3d_stream_read_int16_le(global->stream);
63 w2 = g3d_stream_read_int16_le(global->stream);
64 w3 = g3d_stream_read_int16_le(global->stream);
65
66 name = skp_read_wchar(global->stream);
67 while(name != NULL) {
68 tmp = skp_read_wchar(global->stream);
69 if(tmp != NULL) {
70 /* got a section */
71#if DEBUG > 1
72 g_debug("\t%s", name);
73#endif
74 g_free(name);
75 name = tmp;
76 continue;
77 }
78 u1 = g3d_stream_read_int8(global->stream);
79 switch(u1) {
80 case 0x00: /* end of CAttributeNamed */
81 g_free(name);
82 return TRUE;
83 break;
84 case 0x04: /* 32-bit */
85 x1 = g3d_stream_read_int32_le(global->stream);
86#if DEBUG > 1
87 g_debug("\t\t%-20s: 0x%08x", name, x1);
88#endif
89 break;
90 case 0x06: /* double */
91 d1 = g3d_stream_read_double_le(global->stream);
92#if DEBUG > 1
93 g_debug("\t\t%-20s: %.4f", name, d1);
94#endif
95 break;
96 case 0x07: /* boolean */
97 u2 = g3d_stream_read_int8(global->stream);
98#if DEBUG > 1
99 g_debug("\t\t%-20s: %s", name, (u2 ? "true" : "false"));
100#endif
101 break;
102 case 0x09: /* end of section? */
103 g3d_stream_seek(global->stream, 7, G_SEEK_CUR);
104#if DEBUG > 1
105 g_debug("\t\t%-20s: EOS", name);
106#endif
107 break;
108 case 0x0A: /* string */
109 val = skp_read_wchar(global->stream);
110 g_return_val_if_fail(val != NULL, FALSE);
111#if DEBUG > 1
112 g_debug("\t\t%-20s: %s", name, val);
113#endif
114 g_free(val);
115 break;
116 default:
117 g_warning(
118 "SKP: CAttributeNamed: unknown attribute type: 0x%02X",
119 u1);
120 return FALSE;
121 break;
122 }
123
124 g_free(name);
125 name = skp_read_wchar(global->stream);
126 }
127 return TRUE;
128}
129
130/*****************************************************************************/
131
132gboolean skp_cb_component_definition(SkpGlobalData *global,
133 SkpLocalData *local)
134{
135 SkpComponent *comp;
136 guint32 x1, w1;
137 gchar *s;
138 gdouble d1, d2;
139
140 w1 = g3d_stream_read_int16_le(global->stream);
141 g_return_val_if_fail(w1 == 0x0000, FALSE);
142
143 skp_read_10b(global->stream);
144 skp_read_10b(global->stream);
145
146 x1 = g3d_stream_read_int32_le(global->stream);
147 g_debug("CComponentDefinition: %d", x1);
148
149 w1 = g3d_stream_read_int16_le(global->stream);
150 if(!(w1 & 0x8000)) {
151 g_warning("CComponentDefinition: layerid=0x%04x", w1);
152 return FALSE;
153 }
154 w1 &= 0x7FFF;
155 g3d_stream_read_int16_le(global->stream); /* 0000 */
156
157 s = skp_read_wchar(global->stream);
158 if(s == NULL) {
159 g_warning("CComponentDefinition: s[0] == NULL");
160 return FALSE;
161 }
162 g3d_stream_seek(global->stream, 3, G_SEEK_CUR);
163 s = skp_read_wchar(global->stream);
164 if(s == NULL) {
165 g_warning("CComponentDefinition: s[1] == NULL");
166 return FALSE;
167 }
168 g3d_stream_seek(global->stream, 6, G_SEEK_CUR);
169 s = skp_read_wchar(global->stream);
170 if(s == NULL) {
171 g_warning("CComponentDefinition: s[2] == NULL");
172 return FALSE;
173 }
174
175 d1 = g3d_stream_read_double_le(global->stream);
176 d2 = g3d_stream_read_double_le(global->stream);
177 g_debug("CComponentDefinition: d1=%.2f, d2=%.2f", d1, d2);
178
179 g3d_stream_seek(global->stream, 5, G_SEEK_CUR);
180
181 comp = g_new0(SkpComponent, 1);
182 comp->layerid = w1;
183
184 comp->id0 = g3d_stream_read_int16_le(global->stream);
185 g3d_stream_read_int32_le(global->stream);
186 g3d_stream_read_int8(global->stream);
187 comp->id1 = g3d_stream_read_int16_le(global->stream);
188
189 g_debug("Component: 0x%02x, 0x%02x (0x%02x)",
190 comp->id0, comp->id1, comp->layerid);
191
192 global->components = g_slist_append(global->components, comp);
193
194 return TRUE;
195}
196
197/*****************************************************************************/
198
199static gboolean skp_read_5b(G3DStream *stream)
200{
201 guint32 w1, w2, u1;
202
203 w1 = g3d_stream_read_int16_le(stream);
204 u1 = g3d_stream_read_int8(stream);
205 w2 = g3d_stream_read_int16_le(stream);
206 g_debug("\tread 5b: %04x %02x %04x", w1, u1, w2);
207 return TRUE;
208}
209
210gboolean skp_cb_edge_use(SkpGlobalData *global, SkpLocalData *local)
211{
212 guint16 w1, w2;
213 guint32 x1;
214 gdouble d1, d2, d3, d4;
215 gboolean handled;
216 GSList *item;
217 SkpComponent *comp;
218
219 w1 = g3d_stream_read_int16_le(global->stream);
220 g_return_val_if_fail(w1 == 0x0000, FALSE);
221
222 skp_read_5b(global->stream);
223
224 do {
225 w1 = g3d_stream_read_int16_le(global->stream);
226 handled = FALSE;
227
228 g_debug("opcode: 0x%04x", w1);
229 if(w1 == 0xFFFF) {
230 g3d_stream_seek(global->stream, -2, G_SEEK_CUR);
231 return TRUE;
232 }
233 if(w1 & 0x8000) {
234 g3d_stream_read_int16_le(global->stream);
235 w1 &= 0x7FFF;
236 }
237 if(w1 == 0x0000) {
238 handled = TRUE;
239 }
240
241 for(item = global->components;
242 (handled == FALSE) && (item != NULL);
243 item = item->next) {
244
245 comp = item->data;
246
247 switch(w1 - comp->id0) {
248 case 1:
249 skp_read_10b(global->stream);
250 w2 = g3d_stream_read_int16_le(global->stream);
251 handled = TRUE;
252 g_debug("\tw2=0x%04x", w2);
253 break;
254 case 3:
255 skp_read_dbl3(global->stream, &d1, &d2, &d3);
256 g_debug("\tvertex: %.4f, %.4f, %.4f", d1, d2, d3);
257 handled = TRUE;
258 break;
259 case 4:
260 case 5:
261 case 7:
262 g_debug("\tempty");
263 handled = TRUE;
264 break;
265 default:
266 break;
267 }
268 if(handled)
269 break;
270 switch(w1 - comp->id1) {
271 case 0:
272 skp_read_10b(global->stream);
273 d1 = g3d_stream_read_double_le(global->stream);
274 d2 = g3d_stream_read_double_le(global->stream);
275 d3 = g3d_stream_read_double_le(global->stream);
276 d4 = g3d_stream_read_double_le(global->stream);
277 x1 = g3d_stream_read_int32_le(global->stream);
278 g_debug("\tid1+4: dbl4: %.2f, %.2f, %.2f, %.2f (%i)",
279 d1, d2, d3, d4, x1);
280 handled = TRUE;
281 break;
282 case 2:
283 w2 = g3d_stream_read_int16_le(global->stream);
284 g_debug("\ti16: 0x%04x", w2);
285 handled = TRUE;
286 break;
287 case 4:
288 skp_read_5b(global->stream);
289 handled = TRUE;
290 break;
291 case 5:
292 case 9:
293 case 17:
294 case 19:
295 g_debug("\tempty");
296 handled = TRUE;
297 break;
298 default:
299 break;
300 }
301 } /* loop through known layers */
302
303 if(handled == FALSE) {
304 g_debug("vertex: unknown opcode %#04x (layer0: 0x%04x, 0x%04x)",
305 w1,
306 global->components ?
307 ((SkpComponent *)(global->components->data))->id0 : -1,
308 global->components ?
309 ((SkpComponent *)(global->components->data))->id1 : -1);
310 }
311 } while(TRUE);
312
313 return FALSE;
314
315}
316
317/*****************************************************************************/
318
319gboolean skp_cb_face_texture_coords(SkpGlobalData *global, SkpLocalData *local)
320{
321#if DEBUG > 1
322 gint32 i;
323 guint16 w1;
324 G3DFloat f1, f2, f3, f4;
325
326 w1 = g3d_stream_read_int16_le(global->stream);
327 g_return_val_if_fail(w1 == 0x0000, FALSE);
328
329 for(i = 0; i < 24; i ++) {
330 f1 = g3d_stream_read_float_le(global->stream);
331 f2 = g3d_stream_read_float_le(global->stream);
332 f3 = g3d_stream_read_float_le(global->stream);
333 f4 = g3d_stream_read_float_le(global->stream);
334 g_debug("\tf: %.4f, %.4f, %.4f, %.4f", f1, f2, f3, f4);
335 }
336#endif
337 return TRUE;
338}
339
340/*****************************************************************************/
341
342gboolean skp_cb_layer(SkpGlobalData *global, SkpLocalData *local)
343{
344 guint32 x1;
345 guint16 w1;
346 guint8 u1, u2, u3;
347 gchar *s1, *s2;
348
349 do {
350 w1 = g3d_stream_read_int16_le(global->stream);
351 g_return_val_if_fail(w1 == 0x0000, FALSE);
352
353 s1 = skp_read_wchar(global->stream);
354 u1 = g3d_stream_read_int8(global->stream);
355 u2 = g3d_stream_read_int8(global->stream);
356 u3 = g3d_stream_read_int8(global->stream);
357
358 g_debug("\\ %s (0x%02X, 0x%02X, 0x%02X)", s1, u1, u2, u3);
359
360 s2 = skp_read_wchar(global->stream);
361 u1 = g3d_stream_read_int8(global->stream);
362 u2 = g3d_stream_read_int8(global->stream);
363 g_return_val_if_fail(u2 == 0x01, FALSE);
364
365 /* layer color? */
366 x1 = g3d_stream_read_int32_le(global->stream);
367 g_debug("\\ %s (0x%02X, 0x%02X, 0x%08x)", s2, u1, u2, x1);
368
369 x1 = g3d_stream_read_int32_le(global->stream);
370 if(x1 == 0x00fffeff)
371 g3d_stream_seek(global->stream, 21, G_SEEK_CUR);
372 else if(x1 == 0xfffffeff)
373 g3d_stream_seek(global->stream, 20, G_SEEK_CUR);
374 else {
375 g_warning("CLayer: unexpected value 0x%08x @ 0x%08x", x1,
376 (guint32)g3d_stream_tell(global->stream) - 4);
377 return FALSE;
378 }
379 w1 = g3d_stream_read_int16_le(global->stream);
380 global->layers = g_slist_append(global->layers,
381 GINT_TO_POINTER(w1 & 0x7FFF));
382 g_debug("CLayer ID: 0x%2x", w1 & 0x7FFF);
383
384 if(s1)
385 g_free(s1);
386 if(s2)
387 g_free(s2);
388
389 } while(w1 & 0x8000);
390
391 x1 = g3d_stream_read_int32_le(global->stream);
392 g_debug("CLayer: last 0x%08x", x1);
393
394 return TRUE;
395}
396
397/*****************************************************************************/
398
399gboolean skp_cb_material(SkpGlobalData *global, SkpLocalData *local)
400{
401 gchar *name, *tmp;
402 guint8 u1, u2, u3;
403 guint32 x1, type, size;
404 G3DFloat r, g, b, a;
405 G3DMaterial *material;
406
407 g3d_stream_read_int16_le(global->stream);
408
409 name = skp_read_wchar(global->stream);
410 while(name) {
411 tmp = NULL;
412 size = 0;
413
414 material = g3d_material_new();
415 material->name = g_strdup(name);
416 global->model->materials = g_slist_append(global->model->materials,
417 material);
418
419 type = g3d_stream_read_int16_le(global->stream);
420 switch(type) {
421 case 0x0000: /* simple color */
422 r = g3d_stream_read_int8(global->stream);
423 g = g3d_stream_read_int8(global->stream);
424 b = g3d_stream_read_int8(global->stream);
425 a = g3d_stream_read_int8(global->stream);
426
427 material->r = r / 255.0;
428 material->g = g / 255.0;
429 material->b = b / 255.0;
430 material->a = a / 255.0;
431#if DEBUG > 1
432 g_debug(
433 "\tmaterial: %-30s 0x%04x, "
434 "color (%.1f, %.1f, %.1f)",
435 name, type, material->r, material->g, material->b);
436#endif
437 break;
438
439 case 0x0001: /* texture */
440 u1 = g3d_stream_read_int8(global->stream);
441 u2 = g3d_stream_read_int8(global->stream);
442 u3 = g3d_stream_read_int8(global->stream);
443 if(u3 == 0x80) {
444 /* number of textures? */
445 x1 = g3d_stream_read_int32_le(global->stream);
446 if(x1 > 0) {
447 size = g3d_stream_read_int32_le(global->stream);
448 g3d_stream_seek(global->stream, size, G_SEEK_CUR);
449 }
450 } else {
451 x1 = 0x0004;
452 }
453 switch(x1) {
454 case 0x0000:
455 g3d_stream_seek(global->stream, 12, G_SEEK_CUR);
456 break;
457 case 0x0001:
458 g3d_stream_seek(global->stream, 20, G_SEEK_CUR);
459 break;
460 case 0x0002:
461 g3d_stream_seek(global->stream, 16, G_SEEK_CUR);
462 break;
463 case 0x0004:
464 g3d_stream_seek(global->stream, 16, G_SEEK_CUR);
465 break;
466 default:
467 g3d_stream_seek(global->stream, 16, G_SEEK_CUR);
468 g_debug("SKP: mat0001: x1=%x: "
469 "%02X%02X %02X%02X %02X%02X %02X%02X", x1,
470 g3d_stream_read_int8(global->stream),
471 g3d_stream_read_int8(global->stream),
472 g3d_stream_read_int8(global->stream),
473 g3d_stream_read_int8(global->stream),
474 g3d_stream_read_int8(global->stream),
475 g3d_stream_read_int8(global->stream),
476 g3d_stream_read_int8(global->stream),
477 g3d_stream_read_int8(global->stream));
478 break;
479 }
480 tmp = skp_read_wchar(global->stream);
481 g3d_stream_seek(global->stream, 8, G_SEEK_CUR);
482#if DEBUG > 1
483 g_debug(
484 "\tmaterial: %-30s 0x%04x, %02X, %02X, %02X, 0x%08x\n"
485 "\t\ttexture (%d bytes, 0x%08x):\n\t\t%s",
486 name, type, u1, u2, u3, x1, size,
487 (guint32)g3d_stream_tell(global->stream), tmp);
488#endif
489
490 break;
491
492 case 0x0101: /* texture */
493 /* number of textures? */
494 x1 = g3d_stream_read_int32_le(global->stream);
495 size = g3d_stream_read_int32_le(global->stream);
496 g3d_stream_seek(global->stream, size, G_SEEK_CUR);
497 g3d_stream_seek(global->stream, 16, G_SEEK_CUR);
498 tmp = skp_read_wchar(global->stream);
499 g3d_stream_seek(global->stream, 8, G_SEEK_CUR);
500#if DEBUG > 1
501 g_debug(
502 "\tmaterial: %-30s 0x%04x, 0x%08x\n"
503 "\t\ttexture (%i bytes):\n\t\t%s",
504 name, type, x1, size, tmp);
505#endif
506 break;
507
508 default:
509 g_debug("SKP: material: unknown type 0x%04X @ 0x%08x",
510 type, (guint32)g3d_stream_tell(global->stream));
511 return FALSE;
512 }
513
514 x1 = g3d_stream_read_int32_be(global->stream);
515 switch(x1 & 0x00FFL) {
516 case 0x00:
517 g3d_stream_seek(global->stream, 21, G_SEEK_CUR);
518 break;
519 case 0xFF:
520 g3d_stream_seek(global->stream, 22, G_SEEK_CUR);
521 break;
522 default:
523 g_debug("x1: 0x%08x", x1);
524 break;
525 }
526
527 /* clean up */
528 if(tmp)
529 g_free(tmp);
530 g_free(name);
531
532 /* next one */
533 name = skp_read_wchar(global->stream);
534 }
535 return TRUE;
536}
537
538/*****************************************************************************/
539
540gboolean skp_cb_vertex(SkpGlobalData *global, SkpLocalData *local)
541{
542 guint16 w1;
543 gdouble d1, d2, d3;
544 gboolean handled;
545 GSList *item;
546 SkpComponent *comp;
547
548 return FALSE;
549
550 w1 = g3d_stream_read_int16_le(global->stream);
551 g_return_val_if_fail(w1 == 0x0000, FALSE);
552
553 skp_read_dbl3(global->stream, &d1, &d2, &d3);
554 g_debug("\tvertex: %.4f, %.4f, %.4f", d1, d2, d3);
555
556 do {
557 w1 = g3d_stream_read_int16_le(global->stream);
558 handled = FALSE;
559
560 g_debug("opcode: 0x%04x", w1);
561 if(w1 == 0xFFFF) {
562 g3d_stream_seek(global->stream, -2, G_SEEK_CUR);
563 return TRUE;
564 }
565 if(w1 & 0x8000) {
566 g3d_stream_read_int16_le(global->stream);
567 w1 &= 0x7FFF;
568 }
569 if(w1 == 0x0000) {
570 handled = TRUE;
571 }
572
573 for(item = global->components;
574 (handled == FALSE) && (item != NULL);
575 item = item->next) {
576
577 comp = item->data;
578
579 switch(w1 - comp->id0) {
580 case 1:
581 skp_read_10b(global->stream);
582 handled = TRUE;
583 break;
584 case 3:
585 skp_read_dbl3(global->stream, &d1, &d2, &d3);
586 g_debug("\tvertex: %.4f, %.4f, %.4f", d1, d2, d3);
587 handled = TRUE;
588 break;
589 case 4:
590 case 5:
591 case 7:
592 case 9:
593 case 11:
594 case 13:
595 g_debug("\tempty");
596 handled = TRUE;
597 break;
598 default:
599 break;
600 }
601 } /* loop through known layers */
602
603 if(handled == FALSE) {
604 g_debug("vertex: unknown opcode %#04x (layer0: 0x%04x, 0x%04x)",
605 w1,
606 global->components ?
607 ((SkpComponent *)(global->components->data))->id0 : -1,
608 global->components ?
609 ((SkpComponent *)(global->components->data))->id1 : -1);
610 }
611 } while(TRUE);
612
613 return FALSE;
614}