aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_3dm/imp_3dm_callbacks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_3dm/imp_3dm_callbacks.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_3dm/imp_3dm_callbacks.c278
1 files changed, 278 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_3dm/imp_3dm_callbacks.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_3dm/imp_3dm_callbacks.c
new file mode 100644
index 0000000..8eba35a
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_3dm/imp_3dm_callbacks.c
@@ -0,0 +1,278 @@
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/config.h>
23
24#include <g3d/types.h>
25#include <g3d/debug.h>
26#include <g3d/stream.h>
27#include <g3d/vector.h>
28#include <g3d/material.h>
29
30#include "imp_3dm_types.h"
31#include "imp_3dm_callbacks.h"
32#include "imp_3dm_object.h"
33#include "imp_3dm_object_types.h"
34
35static TdmObjectTypeInfo *tdm_otype_get_info(guint32 code)
36{
37 gint32 i;
38
39 if(code == 0xFFFFFFFF)
40 return NULL;
41
42 for(i = 0; tdm_object_types[i].code <= code; i ++)
43 if(tdm_object_types[i].code == code)
44 return &(tdm_object_types[i]);
45 return NULL;
46}
47
48static gboolean tdm_read_chunk_version(TdmGlobal *global, TdmLocal *local)
49{
50 guint8 ver = g3d_stream_read_int8(global->stream);
51
52 if(!ver)
53 return FALSE;
54 local->len -= 1;
55 local->major_version = (ver >> 4) & 0x0F;
56 local->minor_version = ver & 0x0F;
57
58 return TRUE;
59}
60
61/* OpenNURBS class data */
62gboolean tdm_cb_0x00027ffc(TdmGlobal *global, TdmLocal *local)
63{
64 TdmObjectRecord *obj = local->object;
65 TdmObjectTypeInfo *tinfo;
66 G3DMaterial *material;
67
68 if(!obj) {
69 g_warning("tdm_cb_0x00027FFC: expecting object...");
70 return TRUE;
71 }
72
73 tinfo = tdm_otype_get_info(obj->otype);
74 if(!tinfo) {
75 g_warning("unknown object type 0x%08x", obj->otype);
76 return TRUE;
77 }
78
79 local->level ++;
80#if DEBUG > 0
81 g_debug("\\%so: [0x%08x][%c]: %s", debug_pad(local->level),
82 obj->otype,
83 tinfo->callback ? 'f' : ' ',
84 tinfo->description);
85#endif
86 obj->object = g_new0(G3DObject, 1);
87 obj->object->name = g_strdup_printf("%s object @ 0x%08x",
88 tinfo->description, (guint32)g3d_stream_tell(global->stream));
89 global->model->objects = g_slist_append(global->model->objects,
90 obj->object);
91
92 material = g3d_material_new();
93 material->name = g_strdup("(default material)");
94 obj->object->materials = g_slist_append(obj->object->materials, material);
95
96 if(tinfo->callback)
97 tinfo->callback(global, local);
98
99 return TRUE;
100}
101
102/* object record type */
103gboolean tdm_cb_0x02000071(TdmGlobal *global, TdmLocal *local)
104{
105 TdmObjectRecord *obj = local->object;
106
107 if(obj)
108 obj->otype = local->data;
109 return TRUE;
110}
111
112/* object record end */
113gboolean tdm_cb_0x0200007f(TdmGlobal *global, TdmLocal *local)
114{
115 TdmObjectRecord *obj = local->object;
116
117 if(obj) {
118 g_free(obj);
119 }
120 return TRUE;
121}
122
123/* object record */
124gboolean tdm_cb_0x20000070(TdmGlobal *global, TdmLocal *local)
125{
126 local->object = g_new0(TdmObjectRecord, 1);
127 return TRUE;
128}
129
130/* object: mesh */
131gboolean tdm_cb_o_0x00000020(TdmGlobal *global, TdmLocal *local)
132{
133 TdmObjectRecord *obj = local->object;
134 G3DFace *face;
135 gint32 i, j;
136 guint32 vcount, fcount, isize, csize, crc;
137 guint8 c;
138
139 if(!tdm_read_chunk_version(global, local))
140 return FALSE;
141 if((local->major_version != 3) && (local->major_version != 1))
142 return TRUE;
143
144 vcount = g3d_stream_read_int32_le(global->stream);
145 fcount = g3d_stream_read_int32_le(global->stream);
146 local->len -= 8;
147
148 /* packed tex domain */
149 g3d_stream_read_double_le(global->stream);
150 g3d_stream_read_double_le(global->stream);
151 g3d_stream_read_double_le(global->stream);
152 g3d_stream_read_double_le(global->stream);
153 local->len -= 32;
154 /* srf domain */
155 g3d_stream_read_double_le(global->stream);
156 g3d_stream_read_double_le(global->stream);
157 g3d_stream_read_double_le(global->stream);
158 g3d_stream_read_double_le(global->stream);
159 local->len -= 32;
160 /* srf scale */
161 g3d_stream_read_double_le(global->stream);
162 g3d_stream_read_double_le(global->stream);
163 local->len -= 16;
164 /* vbox */
165 g3d_stream_read_float_le(global->stream);
166 g3d_stream_read_float_le(global->stream);
167 g3d_stream_read_float_le(global->stream);
168 g3d_stream_read_float_le(global->stream);
169 g3d_stream_read_float_le(global->stream);
170 g3d_stream_read_float_le(global->stream);
171 local->len -= 24;
172 /* nbox */
173 g3d_stream_read_float_le(global->stream);
174 g3d_stream_read_float_le(global->stream);
175 g3d_stream_read_float_le(global->stream);
176 g3d_stream_read_float_le(global->stream);
177 g3d_stream_read_float_le(global->stream);
178 g3d_stream_read_float_le(global->stream);
179 local->len -= 24;
180 /* tbox */
181 g3d_stream_read_float_le(global->stream);
182 g3d_stream_read_float_le(global->stream);
183 g3d_stream_read_float_le(global->stream);
184 g3d_stream_read_float_le(global->stream);
185 local->len -= 16;
186 /* closed */
187 g3d_stream_read_int32_le(global->stream);
188 local->len -= 4;
189
190#if DEBUG > 0
191 g_debug("|%svcount = %u, fcount = %u", debug_pad(local->level),
192 vcount, fcount);
193#endif
194
195 /* mesh parameters */
196 c = g3d_stream_read_int8(global->stream);
197 local->len -= 1;
198 if(c) {
199 return TRUE;
200 /* TODO */
201 }
202
203 /* mesh curvature */
204 for(i = 0; i < 4; i ++) {
205 c = g3d_stream_read_int8(global->stream);
206 local->len -= 1;
207 if(c) {
208 return TRUE;
209 /* TODO */
210 }
211 }
212
213 /* face array */
214 isize = g3d_stream_read_int32_le(global->stream);
215 local->len -= 4;
216 for(i = 0; i < fcount; i ++) {
217 face = g_new0(G3DFace, 1);
218 face->material = g_slist_nth_data(obj->object->materials, 0);
219 face->vertex_count = 4;
220 face->vertex_indices = g_new0(guint32, 4);
221 for(j = 0; j < 4; j ++) {
222 switch(isize) {
223 case 1:
224 face->vertex_indices[j] =
225 g3d_stream_read_int8(global->stream);
226 local->len -= 1;
227 break;
228 case 2:
229 face->vertex_indices[j] =
230 g3d_stream_read_int16_le(global->stream);
231 local->len -= 2;
232 break;
233 case 4:
234 face->vertex_indices[j] =
235 g3d_stream_read_int32_le(global->stream);
236 local->len -= 4;
237 break;
238 default:
239 g_warning("unsupported isize: %d", isize);
240 return TRUE;
241 break;
242 } /* switch(isize) */
243 } /* j := 0..3 */
244
245 obj->object->faces = g_slist_prepend(obj->object->faces, face);
246#if DEBUG > 0
247 g_debug("|%s[face %04d] %u, %u, %u, %u", debug_pad(local->level), i,
248 face->vertex_indices[0], face->vertex_indices[1],
249 face->vertex_indices[2], face->vertex_indices[3]);
250#endif
251 } /* i := 0..fcount */
252
253 /* vertex stuff */
254 if(local->major_version == 1) {
255 /* TODO */
256 return TRUE;
257 }
258
259 if(vcount > 0) {
260 csize = g3d_stream_read_int32_le(global->stream);
261 local->len -= 4;
262
263 crc = g3d_stream_read_int32_le(global->stream);
264 local->len -= 4;
265
266 c = g3d_stream_read_int8(global->stream);
267 local->len -= 1;
268
269 obj->object->vertex_count = vcount;
270 obj->object->vertex_data = g3d_vector_new(3, vcount);
271 if(c)
272 tdm_object_read_vertex_data_compressed(global, local);
273 else
274 tdm_object_read_vertex_data_uncompressed(global, local);
275 } /* vcount > 0 */
276
277 return TRUE;
278}