aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_vrml/imp_vrml.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_vrml/imp_vrml.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_vrml/imp_vrml.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_vrml/imp_vrml.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_vrml/imp_vrml.c
new file mode 100644
index 0000000..15069d3
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_vrml/imp_vrml.c
@@ -0,0 +1,122 @@
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 <string.h>
25#include <locale.h>
26
27#include <glib.h>
28
29#include <g3d/types.h>
30#include <g3d/stream.h>
31#include <g3d/material.h>
32
33#include "imp_vrml_v1.h"
34
35#define VRML_FT_VRML 0x01
36#define VRML_FT_INVENTOR 0x02
37
38#define MAX_LINE_SIZE 2048
39
40EAPI
41gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
42 G3DModel *model, gpointer user_data)
43{
44 yyscan_t scanner;
45 YY_BUFFER_STATE bufstate;
46 G3DMaterial *material;
47 gchar line[MAX_LINE_SIZE + 1], *buffer, *bufp;
48 guint32 ver_maj, ver_min, filetype, buflen;
49
50 memset(line, 0, MAX_LINE_SIZE);
51 g3d_stream_read_line(stream, line, MAX_LINE_SIZE);
52 if(strncmp(line, "#VRML", 5) == 0)
53 filetype = VRML_FT_VRML;
54 else if(strncmp(line, "#Inventor", 9) == 0)
55 filetype = VRML_FT_INVENTOR;
56 else {
57 g_warning("file '%s' is not a VRML file", stream->uri);
58 return FALSE;
59 }
60
61 /* FIXME: more than one space between VRML and Vx.x? */
62 ver_maj = line[7] - '0';
63 ver_min = line[9] - '0';
64
65#if DEBUG > 0
66 g_debug("VRML: version %d.%d", ver_maj, ver_min);
67#endif
68
69 setlocale(LC_NUMERIC, "C");
70
71 if((filetype == VRML_FT_INVENTOR) || (ver_maj == 1)) {
72 /* Inventor / VRML 1.x */
73 /* read complete file to buffer */
74 buflen = g3d_stream_size(stream) + 1;
75 buffer = g_new0(gchar, buflen);
76 bufp = buffer;
77 memset(buffer, 0, buflen);
78 memset(line, 0, MAX_LINE_SIZE);
79 while(!g3d_stream_eof(stream) &&
80 g3d_stream_read_line(stream, line, MAX_LINE_SIZE)) {
81 memcpy(bufp, line, strlen(line));
82 bufp += strlen(line);
83 }
84 material = g3d_material_new();
85 material->name = g_strdup("fallback material");
86 model->materials = g_slist_append(model->materials, material);
87
88 vrml_v1_yylex_init(&scanner);
89 vrml_v1_yyset_extra(model, scanner);
90 bufstate = vrml_v1_yy_scan_string(buffer, scanner);
91 if(bufstate) {
92 vrml_v1_yylex(scanner);
93 vrml_v1_yy_delete_buffer(bufstate, scanner);
94 }
95 vrml_v1_yylex_destroy(scanner);
96 g_free(buffer);
97 } else if(ver_maj == 2) {
98 g_warning("VRML 2 is not yet supported");
99 return FALSE;
100 } else {
101 g_warning("unknown VRML version %d.%d", ver_maj, ver_min);
102 return FALSE;
103 }
104
105 return TRUE;
106}
107
108EAPI
109gchar *plugin_description(void)
110{
111 return g_strdup("VRML 1.x & SGI inventor models.");
112}
113
114EAPI
115gchar **plugin_extensions(void)
116{
117 return g_strsplit("vrml:iv", ":", 0);
118}
119
120/* FIXME */
121extern int yywrap(yyscan_t yyscanner);
122int vrml_v1_yywrap(yyscan_t yyscanner) { return yywrap(yyscanner); }