aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_nff/imp_nff.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_nff/imp_nff.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_nff/imp_nff.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_nff/imp_nff.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_nff/imp_nff.c
new file mode 100644
index 0000000..bb9009b
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_nff/imp_nff.c
@@ -0,0 +1,169 @@
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 <math.h>
26#include <locale.h>
27
28#include <g3d/g3d.h>
29#include <g3d/stream.h>
30
31/*****************************************************************************/
32/* plugin interface */
33/*****************************************************************************/
34
35#define NFF_SEC_NOSECTION 0
36#define NFF_SEC_VIEWPOINT 1
37#define NFF_SEC_BGCOLOR 2
38#define NFF_SEC_POSLIGHT 3
39#define NFF_SEC_MATERIAL 4
40#define NFF_SEC_CONECYL 5
41#define NFF_SEC_SPHERE 6
42#define NFF_SEC_POLYGON 7
43#define NFF_SEC_POLPATCH 8
44
45static gboolean nff_readline(G3DStream *stream, gchar *line, guint32 maxlen);
46
47EAPI
48gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
49 G3DModel *model, gpointer plugin_data)
50{
51 gchar line[1024];
52 G3DObject *object;
53 G3DMaterial *material = NULL;
54 G3DFace *face;
55 guint32 section = NFF_SEC_NOSECTION;
56 G3DFloat r,g,b, Kd, Ks, Sh, T, refr;
57 G3DFloat v1,v2,v3, n1,n2,n3;
58 gint32 i, num, index;
59 gchar name[128];
60
61 setlocale(LC_NUMERIC, "C");
62
63 g_return_val_if_fail(model != NULL, FALSE);
64
65 object = g_new0(G3DObject, 1);
66 object->name = g_strdup("NFF Object");
67 model->objects = g_slist_append(model->objects, object);
68
69 while(nff_readline(stream, line, 1024)) {
70 if(strcmp(line, "v") == 0) {
71 section = NFF_SEC_VIEWPOINT;
72 } else if(sscanf(line, "b " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &r, &g, &b) == 3) {
73 g3d_context_set_bgcolor(context, r, g, b, 1.0);
74 } else if(sscanf(line, "f " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT,
75 &r, &g, &b, &Kd, &Ks, &Sh, &T, &refr) == 8) {
76 material = g3d_material_new();
77 material->r = r;
78 material->g = g;
79 material->b = b;
80 material->a = 1.0 - T;
81 material->shininess = Sh;
82 material->specular[0] = r * Ks;
83 material->specular[1] = g * Ks;
84 material->specular[2] = b * Ks;
85 if(T > 0) material->flags |= G3D_FLAG_MAT_TWOSIDE;
86 object->materials = g_slist_append(object->materials, material);
87 g_snprintf(name, 128, "material #%d",
88 g_slist_length(object->materials));
89 material->name = g_strdup(name);
90 } else if((sscanf(line, "pp %d", &num) == 1) ||
91 (sscanf(line, "p %d", &num) == 1)) {
92 face = g_new0(G3DFace, 1);
93
94 if(strncmp(line, "pp", 2) == 0) section = NFF_SEC_POLPATCH;
95 else section = NFF_SEC_POLYGON;
96
97 object->vertex_count += num;
98 object->vertex_data = g_realloc(object->vertex_data,
99 object->vertex_count * 3 * sizeof(G3DFloat));
100
101 face->material = material;
102 face->vertex_count = num;
103 face->vertex_indices = g_malloc0(num * sizeof(guint32));
104 object->faces = g_slist_prepend(object->faces, face);
105 if(section == NFF_SEC_POLPATCH) {
106 face->flags |= G3D_FLAG_FAC_NORMALS;
107 face->normals = g_malloc0(num * 3 * sizeof(G3DFloat));
108 }
109 /* most faces are in this direction, but there are wrong models */
110 for(i = num - 1; i >= 0; i --) {
111 if(!nff_readline(stream, line, 1024)) {
112 g_warning("reading vertices failed");
113 return FALSE;
114 }
115 if((section == NFF_SEC_POLPATCH) &&
116 (sscanf(line, G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT,
117 &v1,&v2,&v3,
118 &n1,&n2,&n3) == 6)) {
119 index = object->vertex_count - num + i;
120 object->vertex_data[index*3+0] = v1;
121 object->vertex_data[index*3+1] = v2;
122 object->vertex_data[index*3+2] = v3;
123 face->vertex_indices[i] = index;
124 face->normals[i*3+0] = -n1;
125 face->normals[i*3+1] = -n2;
126 face->normals[i*3+2] = -n3;
127 } else if(sscanf(line, G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &v1,&v2,&v3) == 3) {
128 index = object->vertex_count - num + i;
129 object->vertex_data[index*3+0] = v1;
130 object->vertex_data[index*3+1] = v2;
131 object->vertex_data[index*3+2] = v3;
132 face->vertex_indices[i] = index;
133 } else
134 g_warning("error in line '%s'", line);
135 }
136 }
137 }
138 return TRUE;
139}
140
141EAPI
142gchar *plugin_description(G3DContext *context)
143{
144 return g_strdup("Neutral File Format models.");
145}
146
147EAPI
148gchar **plugin_extensions(G3DContext *context)
149{
150 return g_strsplit("nff", ":", 0);
151}
152
153/*****************************************************************************/
154/* private functions */
155/*****************************************************************************/
156
157static gboolean nff_readline(G3DStream *stream, gchar *line, guint32 maxlen)
158{
159 if(g3d_stream_read_line(stream, line, maxlen) == NULL)
160 return FALSE;
161 g_strstrip(line);
162
163 if((strlen(line) == 0) || (line[0] == '#')) {
164 /* get next line if empty or comment */
165 return nff_readline(stream, line, maxlen);
166 }
167 return TRUE;
168}
169