diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ta/imp_ta_3do.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ta/imp_ta_3do.c | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ta/imp_ta_3do.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ta/imp_ta_3do.c new file mode 100644 index 0000000..5c0de7b --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ta/imp_ta_3do.c | |||
@@ -0,0 +1,183 @@ | |||
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 | |||
23 | #include <stdio.h> | ||
24 | |||
25 | #include <g3d/types.h> | ||
26 | #include <g3d/stream.h> | ||
27 | #include <g3d/matrix.h> | ||
28 | |||
29 | gboolean ta_3do_read_children(G3DContext *context, G3DStream *stream, | ||
30 | GSList **list, G3DMaterial *materials); | ||
31 | |||
32 | gboolean ta_3do_load_object(G3DContext *context, G3DStream *stream, | ||
33 | G3DModel *model, G3DMaterial *materials) | ||
34 | { | ||
35 | return ta_3do_read_children(context, stream, &(model->objects), materials); | ||
36 | } | ||
37 | |||
38 | gboolean ta_3do_read_children(G3DContext *context, G3DStream *stream, | ||
39 | GSList **list, G3DMaterial *materials) | ||
40 | { | ||
41 | G3DObject *object; | ||
42 | G3DFace *face; | ||
43 | goffset off_save, off_sav2, off_sibl, off_chld, off_vert, off_prim, off_i; | ||
44 | guint32 num_prims, colidx; | ||
45 | gchar buffer[1025]; | ||
46 | gint32 i, j, x, y, z; | ||
47 | |||
48 | while(!g3d_stream_eof(stream)) { | ||
49 | /* signature */ | ||
50 | if(g3d_stream_read_int32_le(stream) != 1) | ||
51 | return FALSE; | ||
52 | |||
53 | object = g_new0(G3DObject, 1); | ||
54 | *list = g_slist_append(*list, object); | ||
55 | |||
56 | /* number of vertices */ | ||
57 | object->vertex_count = g3d_stream_read_int32_le(stream); | ||
58 | object->vertex_data = g_new0(G3DFloat, 3 * object->vertex_count); | ||
59 | |||
60 | /* number of primitives */ | ||
61 | num_prims = g3d_stream_read_int32_le(stream); | ||
62 | |||
63 | /* offset of selection primitive */ | ||
64 | g3d_stream_read_int32_le(stream); | ||
65 | |||
66 | /* translation from parent */ | ||
67 | x = g3d_stream_read_int32_le(stream); | ||
68 | y = g3d_stream_read_int32_le(stream); | ||
69 | z = g3d_stream_read_int32_le(stream); | ||
70 | object->transformation = g_new0(G3DTransformation, 1); | ||
71 | g3d_matrix_identity(object->transformation->matrix); | ||
72 | g3d_matrix_translate(x, y, z, object->transformation->matrix); | ||
73 | |||
74 | /* offset of object name */ | ||
75 | off_save = g3d_stream_tell(stream) + 4; | ||
76 | g3d_stream_seek(stream, g3d_stream_read_int32_le(stream), G_SEEK_SET); | ||
77 | g3d_stream_read_cstr(stream, buffer, 1024); | ||
78 | buffer[1024] = '\0'; | ||
79 | object->name = g_strdup(buffer); | ||
80 | g3d_stream_seek(stream, off_save, G_SEEK_SET); | ||
81 | #if DEBUG > 1 | ||
82 | g_debug("TA: object '%s'", object->name); | ||
83 | #endif | ||
84 | |||
85 | /* always 0 */ | ||
86 | g3d_stream_read_int32_le(stream); | ||
87 | |||
88 | /* offset of vertex array */ | ||
89 | off_vert = g3d_stream_read_int32_le(stream); | ||
90 | off_save = g3d_stream_tell(stream); | ||
91 | g3d_stream_seek(stream, off_vert, G_SEEK_SET); | ||
92 | for(i = 0; i < object->vertex_count; i ++) | ||
93 | { | ||
94 | object->vertex_data[i * 3 + 0] = g3d_stream_read_int32_le(stream); | ||
95 | object->vertex_data[i * 3 + 1] = g3d_stream_read_int32_le(stream); | ||
96 | object->vertex_data[i * 3 + 2] = g3d_stream_read_int32_le(stream); | ||
97 | } | ||
98 | g3d_stream_seek(stream, off_save, G_SEEK_SET); | ||
99 | |||
100 | /* offset of primitive array */ | ||
101 | off_prim = g3d_stream_read_int32_le(stream); | ||
102 | off_save = g3d_stream_tell(stream); | ||
103 | g3d_stream_seek(stream, off_prim, G_SEEK_SET); | ||
104 | for(i = 0; i < num_prims; i ++) | ||
105 | { | ||
106 | face = g_new0(G3DFace, 1); | ||
107 | |||
108 | /* color index */ | ||
109 | colidx = g3d_stream_read_int32_le(stream); | ||
110 | #if DEBUG > 2 | ||
111 | g_debug("TA: color index: %d", colidx); | ||
112 | #endif | ||
113 | if(colidx > 255) | ||
114 | { | ||
115 | g_warning("TA: color index > 255 (%d)\n", colidx); | ||
116 | g_free(face); | ||
117 | g3d_stream_skip(stream, 28); | ||
118 | continue; | ||
119 | } | ||
120 | face->material = &(materials[colidx]); | ||
121 | |||
122 | /* number of vertices */ | ||
123 | face->vertex_count = g3d_stream_read_int32_le(stream); | ||
124 | if(face->vertex_count < 3) | ||
125 | { | ||
126 | /* skip this primitive */ | ||
127 | g_free(face); | ||
128 | g3d_stream_skip(stream, 24); | ||
129 | continue; | ||
130 | } | ||
131 | face->vertex_indices = g_new0(guint32, face->vertex_count); | ||
132 | |||
133 | /* always 0 */ | ||
134 | g3d_stream_read_int32_le(stream); | ||
135 | |||
136 | /* offset of vertex index array */ | ||
137 | off_i = g3d_stream_read_int32_le(stream); | ||
138 | off_sav2 = g3d_stream_tell(stream); | ||
139 | #if DEBUG > 2 | ||
140 | g_debug("TA: vertex index offset: 0x%08x", off_i); | ||
141 | #endif | ||
142 | g3d_stream_seek(stream, off_i, G_SEEK_SET); | ||
143 | for(j = 0; j < face->vertex_count; j ++) | ||
144 | face->vertex_indices[j] = g3d_stream_read_int16_le(stream); | ||
145 | g3d_stream_seek(stream, off_sav2, G_SEEK_SET); | ||
146 | |||
147 | /* offset of texture name */ | ||
148 | g3d_stream_read_int32_le(stream); | ||
149 | |||
150 | /* unknown */ | ||
151 | g3d_stream_skip(stream, 12); | ||
152 | |||
153 | object->faces = g_slist_prepend(object->faces, face); | ||
154 | } | ||
155 | g3d_stream_seek(stream, off_save, G_SEEK_SET); | ||
156 | |||
157 | /* offset of sibling object */ | ||
158 | off_sibl = g3d_stream_read_int32_le(stream); | ||
159 | |||
160 | /* offset of child object */ | ||
161 | off_chld = g3d_stream_read_int32_le(stream); | ||
162 | |||
163 | #if DEBUG > 3 | ||
164 | g_debug("TA: child @ 0x%08x, sibling @ 0x%08x", off_chld, off_sibl); | ||
165 | #endif | ||
166 | |||
167 | if(off_chld != 0) | ||
168 | { | ||
169 | off_save = g3d_stream_tell(stream); | ||
170 | g3d_stream_seek(stream, off_chld, G_SEEK_SET); | ||
171 | ta_3do_read_children(context, stream, &(object->objects), | ||
172 | materials); | ||
173 | g3d_stream_seek(stream, off_save, G_SEEK_SET); | ||
174 | } | ||
175 | |||
176 | if(off_sibl == 0) | ||
177 | return TRUE; | ||
178 | |||
179 | g3d_stream_seek(stream, off_sibl, G_SEEK_SET); | ||
180 | } | ||
181 | |||
182 | return FALSE; | ||
183 | } | ||