diff options
Diffstat (limited to '')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_mpd.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_mpd.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_mpd.c new file mode 100644 index 0000000..a699f2a --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_mpd.c | |||
@@ -0,0 +1,121 @@ | |||
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 <string.h> | ||
23 | #include <stdio.h> | ||
24 | |||
25 | #include <g3d/types.h> | ||
26 | #include <g3d/matrix.h> | ||
27 | #include <g3d/object.h> | ||
28 | |||
29 | #include "imp_ldraw_types.h" | ||
30 | #include "imp_ldraw_part.h" | ||
31 | #include "imp_ldraw_library.h" | ||
32 | |||
33 | static inline void ldraw_create_subpart(LDrawLibrary *lib, gchar *name, | ||
34 | gchar *buffer, GSList **partsp) | ||
35 | { | ||
36 | G3DStream *substream; | ||
37 | LDrawPart *part; | ||
38 | |||
39 | #if DEBUG > 2 | ||
40 | g_debug("LDraw: adding sub-file %s to library", name); | ||
41 | #endif | ||
42 | |||
43 | substream = g3d_stream_from_buffer((guint8 *)buffer, strlen(buffer), | ||
44 | name, TRUE); | ||
45 | if(substream == NULL) { | ||
46 | g_warning("LDraw: failed to create stream for %s", name); | ||
47 | return; | ||
48 | } | ||
49 | part = g_new0(LDrawPart, 1); | ||
50 | part->name = g_ascii_strup(name, -1); | ||
51 | part->stream = substream; | ||
52 | |||
53 | ldraw_library_insert(lib, part->name, part); | ||
54 | *partsp = g_slist_append(*partsp, part); | ||
55 | } | ||
56 | |||
57 | gboolean ldraw_mpd_load(G3DStream *stream, G3DModel *model, | ||
58 | LDrawLibrary *lib) | ||
59 | { | ||
60 | G3DObject *object = NULL; | ||
61 | GSList *parts = NULL, *item; | ||
62 | LDrawPart *part; | ||
63 | gchar buffer[1024], name[256], *streambuf = NULL; | ||
64 | gsize size; | ||
65 | goffset off; | ||
66 | G3DFloat m[16]; | ||
67 | |||
68 | while(!g3d_stream_eof(stream)) { | ||
69 | memset(buffer, 0, 1024); | ||
70 | g3d_stream_read_line(stream, buffer, 1023); | ||
71 | if(strncmp(buffer, "0 FILE ", 7) == 0) { | ||
72 | if(streambuf) { | ||
73 | ldraw_create_subpart(lib, name, streambuf, &parts); | ||
74 | streambuf = NULL; | ||
75 | } | ||
76 | memset(name, 0, 256); | ||
77 | if(sscanf(buffer + 7, "%255s", name) == 1) | ||
78 | streambuf = g_strdup(""); | ||
79 | } else if(strncmp(buffer, "0 NOFILE", 8) == 0) { | ||
80 | if(streambuf) { | ||
81 | ldraw_create_subpart(lib, name, streambuf, &parts); | ||
82 | streambuf = NULL; | ||
83 | } | ||
84 | break; | ||
85 | } else if(streambuf) { | ||
86 | /* append line to buffer */ | ||
87 | size = strlen(buffer) + strlen(streambuf) + 1; | ||
88 | off = strlen(streambuf); | ||
89 | streambuf = g_realloc(streambuf, size * sizeof(gchar)); | ||
90 | strcpy(streambuf + off, buffer); | ||
91 | } | ||
92 | } /* !eof */ | ||
93 | |||
94 | if(streambuf) | ||
95 | ldraw_create_subpart(lib, name, streambuf, &parts); | ||
96 | |||
97 | if(parts) { | ||
98 | part = parts->data; | ||
99 | part->master = TRUE; | ||
100 | object = ldraw_part_get_object(part, lib); | ||
101 | if(object != NULL) { | ||
102 | g3d_matrix_identity(m); | ||
103 | g3d_matrix_rotate_xyz(0.0, 0.0, G_PI, m); | ||
104 | g3d_object_transform(object, m); | ||
105 | model->objects = g_slist_append(model->objects, object); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | #if 1 | ||
110 | /* close open streams */ | ||
111 | for(item = parts; item != NULL; item = item->next) { | ||
112 | part = item->data; | ||
113 | if(part->stream) { | ||
114 | g3d_stream_close(part->stream); | ||
115 | part->stream = NULL; | ||
116 | } | ||
117 | } | ||
118 | #endif | ||
119 | return (object != NULL); | ||
120 | } | ||
121 | |||