aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ase/imp_ase.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ase/imp_ase.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ase/imp_ase.c257
1 files changed, 257 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ase/imp_ase.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ase/imp_ase.c
new file mode 100644
index 0000000..ed2904a
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ase/imp_ase.c
@@ -0,0 +1,257 @@
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#include <stdlib.h>
25#include <string.h>
26#include <locale.h>
27
28#include <g3d/types.h>
29#include <g3d/context.h>
30#include <g3d/stream.h>
31#include <g3d/material.h>
32#include <g3d/texture.h>
33#include <g3d/model.h>
34#include <g3d/matrix.h>
35
36EAPI
37gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
38 G3DModel *model)
39{
40 gchar line[2048], tmp[128], *s;
41 guint32 i, j, a, b, c, ab, bc, ca, mtlid, glid = 0, tvertcnt = 0, lnum = 0;
42 G3DVector x, y, z, *tverts = NULL;
43 G3DObject *object = NULL;
44 G3DMaterial *material;
45 G3DFace *face = NULL;
46 G3DMatrix rmatrix[16];
47
48 setlocale(LC_NUMERIC, "C");
49
50 while(g3d_stream_read_line(stream, line, 2048)) {
51 lnum ++;
52 g_strstrip(line);
53
54#if DEBUG > 4
55 g_debug("ASE: %s", line);
56#endif
57
58 if(strncmp(line, "*GEOMOBJECT ", 12) == 0)
59 {
60 object = g_new0(G3DObject, 1);
61 object->name = g_strdup("(unnamed object)");
62
63 model->objects = g_slist_append(model->objects, object);
64 }
65 else if(strncmp(line, "*MATERIAL ", 10) == 0)
66 {
67 material = g3d_material_new();
68 material->name = g_strdup_printf("material %d",
69 g_slist_length(model->materials));
70 model->materials = g_slist_append(model->materials, material);
71
72 if(sscanf(line, "*MATERIAL %u \"%s", &i, tmp) == 2)
73 {
74 tmp[strlen(tmp) - 1] = '\0';
75#if DEBUG > 2
76 g_debug("ASE: material file: %s", tmp);
77#endif
78 /* TODO: parse .fx file */
79 s = g_strdup_printf("%.*s.jpg", ((int) strlen(tmp)) - 3, tmp);
80 material->tex_image =
81 g3d_texture_load_cached(context, model, s);
82 if(material->tex_image)
83 material->tex_image->tex_id = ++ glid;
84 g_free(s);
85 }
86 }
87 else if(strncmp(line, "*NODE_NAME ", 11) == 0)
88 {
89 if(object)
90 {
91 if(object->name)
92 g_free(object->name);
93
94 object->name = g_strdup(line + 11);
95 }
96 }
97 else if(strncmp(line, "*MESH_NUMVERTEX ", 16) == 0)
98 {
99 if(sscanf(line, "*MESH_NUMVERTEX %u", &i) == 1)
100 {
101 if(object)
102 {
103 object->vertex_count = i;
104 object->vertex_data = g_new0(G3DFloat, i * 3);
105 }
106 }
107 }
108 else if(strncmp(line, "*MESH_VERTEX ", 13) == 0)
109 {
110 if(sscanf(line, "*MESH_VERTEX %u " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &i, &x, &y, &z) == 4)
111 {
112 if(object && (i < object->vertex_count))
113 {
114 object->vertex_data[i * 3 + 0] = x;
115 object->vertex_data[i * 3 + 1] = y;
116 object->vertex_data[i * 3 + 2] = z;
117 }
118 }
119 }
120 else if(strncmp(line, "*MESH_FACE ", 11) == 0)
121 {
122 if(object && (sscanf(line, "*MESH_FACE %u: A: %u B: %u C: %u "
123 "AB: %u BC: %u CA: %u *MESH_SMOOTHING %s *MESH_MTLID %u",
124 &i, &a, &b, &c, &ab, &bc, &ca, tmp, &mtlid) == 9))
125 {
126 face = g_new0(G3DFace, 1);
127 face->vertex_count = 3;
128 face->vertex_indices = g_new0(guint32, 3);
129 face->vertex_indices[0] = a;
130 face->vertex_indices[1] = b;
131 face->vertex_indices[2] = c;
132 face->material = g_slist_nth_data(model->materials, mtlid);
133 if(face->material == NULL)
134 face->material = g_slist_nth_data(model->materials, 0);
135 if(face->material == NULL) {
136 face->material = g3d_material_new();
137 face->material->name = g_strdup("(fallback material)");
138 model->materials = g_slist_append(model->materials,
139 face->material);
140 }
141
142 object->faces = g_slist_append(object->faces, face);
143 face = NULL;
144 }
145 }
146 else if(strncmp(line, "*MESH_FACENORMAL ", 17) == 0)
147 {
148 if(object && (sscanf(line, "*MESH_FACENORMAL %u " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT,
149 &i, &x, &y, &z) == 4))
150 {
151 face = g_slist_nth_data(object->faces, i);
152 if(face)
153 {
154 face->flags |= G3D_FLAG_FAC_NORMALS;
155 face->normals = g_new0(G3DFloat, 3 * 3);
156 for(j = 0; j < 3; j ++)
157 {
158 face->normals[j * 3 + 0] = x;
159 face->normals[j * 3 + 1] = y;
160 face->normals[j * 3 + 2] = z;
161 }
162 }
163 }
164 }
165 else if(strncmp(line, "*MESH_VERTEXNORMAL ", 19) == 0)
166 {
167 if(face && face->normals && (sscanf(line,
168 "*MESH_VERTEXNORMAL %u " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &i, &x, &y, &z) == 4))
169 {
170 for(j = 0; j < 3; j ++)
171 {
172 if(face->vertex_indices[j] == i)
173 {
174 face->normals[j * 3 + 0] = x;
175 face->normals[j * 3 + 1] = y;
176 face->normals[j * 3 + 2] = z;
177 }
178 }
179 }
180 }
181 else if(strncmp(line, "*MESH_NUMTVERTEX ", 17) == 0)
182 {
183 /* clear old tvertex data */
184 if(tverts)
185 {
186 g_free(tverts);
187 tvertcnt = 0;
188 }
189
190 if(sscanf(line, "*MESH_NUMTVERTEX %u", &tvertcnt) == 1)
191 tverts = g_new0(G3DFloat, tvertcnt * 2);
192 }
193 else if(strncmp(line, "*MESH_TVERT ", 12) == 0)
194 {
195 if(sscanf(line, "*MESH_TVERT %u " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &i, &x, &y, &z) == 4)
196 {
197 if(i < tvertcnt)
198 {
199 tverts[i * 2 + 0] = x;
200 tverts[i * 2 + 1] = y;
201 }
202 }
203 }
204 else if(strncmp(line, "*MESH_TFACE ", 12) == 0)
205 {
206 if(object && (sscanf(line, "*MESH_TFACE %u %u %u %u",
207 &i, &a, &b, &c) == 4))
208 {
209 face = g_slist_nth_data(object->faces, i);
210 if(face && face->material->tex_image &&
211 (a < tvertcnt) && (b < tvertcnt) && (c < tvertcnt))
212 {
213 face->flags |= G3D_FLAG_FAC_TEXMAP;
214 face->tex_image = face->material->tex_image;
215 face->tex_vertex_count = 3;
216 face->tex_vertex_data = g_new0(G3DFloat, 2 * 3);
217 face->tex_vertex_data[0 * 2 + 0] = tverts[a * 2 + 0];
218 face->tex_vertex_data[0 * 2 + 1] = 1.0 - tverts[a * 2 + 1];
219 face->tex_vertex_data[1 * 2 + 0] = tverts[b * 2 + 0];
220 face->tex_vertex_data[1 * 2 + 1] = 1.0 - tverts[b * 2 + 1];
221 face->tex_vertex_data[2 * 2 + 0] = tverts[c * 2 + 0];
222 face->tex_vertex_data[2 * 2 + 1] = 1.0 - tverts[c * 2 + 1];
223 }
224 }
225 }
226 if((lnum % 10) == 0)
227 g3d_context_update_progress_bar(context,
228 (G3DFloat)g3d_stream_tell(stream) /
229 (G3DFloat)g3d_stream_size(stream), TRUE);
230 } /* read line */
231
232 /* clean up */
233 if(tverts) {
234 g_free(tverts);
235 tvertcnt = 0;
236 }
237 g3d_context_update_progress_bar(context, 0.0, FALSE);
238
239 g3d_matrix_identity(rmatrix);
240 g3d_matrix_rotate_xyz(G_PI * -90.0 / 180, 0.0, 0.0, rmatrix);
241 g3d_model_transform(model, rmatrix);
242
243 return TRUE;
244}
245
246EAPI
247gchar *plugin_description(void)
248{
249 return g_strdup("ASCII Scene Exporter models.");
250}
251
252EAPI
253gchar **plugin_extensions(void)
254{
255 return g_strsplit("ase", ":", 0);
256}
257