aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_lwo/imp_lwo.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_lwo/imp_lwo.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_lwo/imp_lwo.c216
1 files changed, 216 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_lwo/imp_lwo.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_lwo/imp_lwo.c
new file mode 100644
index 0000000..d01b817
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_lwo/imp_lwo.c
@@ -0,0 +1,216 @@
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/*
24 * imp_lwo.c - LightWave import plugin
25 *
26 * (C) 2005,2006 Markus Dahms
27 *
28 * based on gtkglarea example viewlw:
29 * Copyright (C) 1998 Janne Löf <jlof@mail.student.oulu.fi>
30 */
31
32#include <string.h>
33
34#include <g3d/types.h>
35#include <g3d/context.h>
36#include <g3d/stream.h>
37#include <g3d/material.h>
38#include <g3d/iff.h>
39
40#define LW_MAX_POINTS 200
41#define LW_MAX_NAME_LEN 500
42#define LW_F_LWO2 1
43
44#include "imp_lwo.h"
45#include "imp_lwo_chunks.h"
46
47/*****************************************************************************/
48/* plugin interface */
49/*****************************************************************************/
50
51static void lwo_fix_texfaces(G3DModel *model);
52
53EAPI
54gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
55 G3DModel *model, gpointer user_data)
56{
57 LwoObject *obj;
58 G3DMaterial *material;
59 guint32 id;
60 gsize len;
61 G3DIffGlobal *global;
62 G3DIffLocal *local;
63
64 if(!g3d_iff_check(stream, &id, &len))
65 return FALSE;
66
67 if((id != G3D_IFF_MKID('L','W','O','B')) &&
68 (id != G3D_IFF_MKID('L','W','O','2'))) {
69 g_warning("'%s' is not a LightWave object", stream->uri);
70 return FALSE;
71 }
72
73 obj = g_new0(LwoObject, 1);
74
75 global = g_new0(G3DIffGlobal, 1);
76 global->stream = stream;
77 global->context = context;
78 global->model = model;
79 if(id == G3D_IFF_MKID('L','W','O','2'))
80 global->flags |= LWO_FLAG_LWO2;
81 global->user_data = obj;
82
83 local = g_new0(G3DIffLocal, 1);
84 local->id = id;
85 local->nb = len;
86
87 material = g3d_material_new();
88 material->name = g_strdup("fallback material");
89 model->materials = g_slist_append(model->materials, material);
90
91 g3d_iff_read_ctnr(global, local, lwo_chunks,
92 G3D_IFF_PAD2 | G3D_IFF_SUBCHUNK_LEN16);
93
94 lwo_fix_texfaces(model);
95
96 /* cleanup */
97 if(obj->ntags)
98 g_strfreev(obj->tags);
99
100 if(obj->nclips)
101 {
102 g_free(obj->clips);
103 g_strfreev(obj->clipfiles);
104 }
105
106 if(obj->tex_vertices)
107 g_free(obj->tex_vertices);
108
109 g_free(obj);
110
111 g_free(local);
112 g_free(global);
113
114 g3d_context_update_progress_bar(context, 0.0, FALSE);
115
116 return TRUE;
117}
118
119EAPI
120gchar *plugin_description(G3DContext *context)
121{
122 return g_strdup("LightWave models.\n"
123 "Author: Markus Dahms.");
124}
125
126EAPI
127gchar **plugin_extensions(G3DContext *context)
128{
129 return g_strsplit("lwo:lwb:lw", ":", 0);
130}
131
132/*****************************************************************************/
133/* private */
134/*****************************************************************************/
135
136G3DObject *lwo_create_object(G3DStream *stream, G3DModel *model, guint32 flags)
137{
138 G3DObject *object = g_new0(G3DObject, 1);
139 object->name = g_strdup_printf("LWO%c object @ 0x%08x",
140 (flags & LW_F_LWO2) ? '2' : 'B', (guint32)g3d_stream_tell(stream) - 8);
141 model->objects = g_slist_append(model->objects, object);
142
143#if 0
144 /* LWO files should have correct faces */
145 model->glflags &= ~G3D_FLAG_GL_ALLTWOSIDE;
146#endif
147
148 return object;
149}
150
151/*****************************************************************************/
152/* LWO specific */
153/*****************************************************************************/
154
155gint lwo_read_string(G3DStream *stream, char *s)
156{
157 gint c;
158 gint cnt = 0;
159 do {
160 c = g3d_stream_read_int8(stream);
161 if (cnt < LW_MAX_NAME_LEN)
162 s[cnt] = c;
163 else
164 s[LW_MAX_NAME_LEN-1] = 0;
165 cnt++;
166 } while (c != 0);
167 /* if length of string (including \0) is odd skip another byte */
168 if (cnt%2) {
169 g3d_stream_read_int8(stream);
170 cnt++;
171 }
172 return cnt;
173}
174
175guint32 lwo_read_vx(G3DStream *stream, guint *index)
176{
177 *index = g3d_stream_read_int16_be(stream);
178 if((*index & 0xFF00) == 0xFF00) {
179 *index <<= 16;
180 *index += g3d_stream_read_int16_be(stream);
181 *index &= 0x00FFFFFF;
182 return 4;
183 } else {
184 return 2;
185 }
186}
187
188static void lwo_fix_texfaces(G3DModel *model)
189{
190 GSList *olist, *flist;
191 G3DObject *object;
192 G3DFace *face;
193
194 olist = model->objects;
195 while(olist)
196 {
197 object = (G3DObject *)olist->data;
198 olist = olist->next;
199
200 flist = object->faces;
201 while(flist)
202 {
203 face = (G3DFace *)flist->data;
204 flist = flist->next;
205
206 if(face->flags & G3D_FLAG_FAC_TEXMAP)
207 {
208 face->tex_image = face->material->tex_image;
209 if(face->tex_image == NULL)
210 {
211 face->flags &= ~G3D_FLAG_FAC_TEXMAP;
212 }
213 }
214 }
215 }
216}