aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_flt/imp_flt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_flt/imp_flt.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_flt/imp_flt.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_flt/imp_flt.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_flt/imp_flt.c
new file mode 100644
index 0000000..6a92b5a
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_flt/imp_flt.c
@@ -0,0 +1,175 @@
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 <g3d/types.h>
24#include <g3d/context.h>
25#include <g3d/stream.h>
26#include <g3d/material.h>
27#include <g3d/model.h>
28#include <g3d/matrix.h>
29
30#include "imp_flt_opcodes.h"
31
32FltOpcode *flt_opcode_info(guint32 opcode);
33
34EAPI
35gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
36 G3DModel *model, gpointer user_data)
37{
38 guint16 opcode, rlen;
39 FltOpcode *oi;
40 FltGlobalData *gd;
41 FltLocalData *ld;
42 G3DObject *g3dobj = NULL;
43 gpointer level_object = NULL;
44 gchar *pad;
45 G3DFloat prev_pcnt = 0.0, pcnt;
46 G3DMatrix rmatrix[16];
47
48 gd = g_new0(FltGlobalData, 1);
49 gd->context = context;
50 gd->model = model;
51 gd->stream = stream;
52 gd->oqueue = g_queue_new();
53
54 while(!g3d_stream_eof(stream)) {
55 /* get record information */
56 opcode = g3d_stream_read_int16_be(stream);
57 rlen = g3d_stream_read_int16_be(stream);
58
59 /* create local data */
60 ld = g_new0(FltLocalData, 1);
61 ld->opcode = opcode;
62 ld->nb = rlen - 4;
63 ld->g3dobj = g3dobj;
64 ld->level_object = level_object;
65
66 if(opcode == 0) {
67 /* end of file or error */
68 break;
69 }
70
71 oi = flt_opcode_info(opcode);
72
73 pad = g_strnfill(gd->level, ' ');
74 g_debug("\\%s[%04d][%c] %8d: %s", pad, opcode,
75 oi ? (oi->callback ? 'f' : ' ') : ' ',
76 rlen,
77 oi ? oi->description : "unknown");
78 g_free(pad);
79
80 if(oi && oi->callback)
81 oi->callback(gd, ld);
82
83 /* skip remaining bytes */
84 if(ld->nb > 0)
85 g3d_stream_skip(stream, ld->nb);
86
87 /* free local data */
88 g3dobj = ld->g3dobj;
89 level_object = ld->level_object;
90 g_free(ld);
91
92 /* update caller */
93 pcnt = (G3DFloat)g3d_stream_tell(stream) /
94 (G3DFloat)g3d_stream_size(stream);
95 if((pcnt - prev_pcnt) > 0.002) {
96 prev_pcnt = pcnt;
97 g3d_context_update_progress_bar(context, pcnt, TRUE);
98 }
99 g3d_context_update_interface(context);
100 }
101
102 g_queue_free(gd->oqueue);
103 if(gd->vertex_palette) {
104 g_free(gd->vertex_palette->offsets);
105 g_free(gd->vertex_palette->flags);
106 g_free(gd->vertex_palette->vertex_data);
107 g_free(gd->vertex_palette->normal_data);
108 g_free(gd->vertex_palette->tex_vertex_data);
109 g_free(gd->vertex_palette->vertex_materials);
110 g_free(gd->vertex_palette);
111 }
112 if(gd->texture_palette) {
113 g_free(gd->texture_palette->offsets);
114 g_free(gd->texture_palette->textures);
115 g_free(gd->texture_palette);
116 }
117 g_free(gd);
118
119 g3d_matrix_identity(rmatrix);
120 g3d_matrix_rotate_xyz(G_PI * -90.0 / 180, 0.0, 0.0, rmatrix);
121 g3d_model_transform(model, rmatrix);
122
123 return TRUE;
124}
125
126EAPI
127gchar *plugin_description(G3DContext *context)
128{
129 return g_strdup("OpenFlight models.");
130}
131
132EAPI
133gchar **plugin_extensions(G3DContext *context)
134{
135 return g_strsplit("flt", ":", 0);
136}
137
138/*
139 * FLT specific
140 */
141
142FltOpcode *flt_opcode_info(guint32 opcode)
143{
144 guint32 i;
145
146 for(i = 0; flt_opcodes[i].opcode != 0; i ++)
147 if(flt_opcodes[i].opcode == opcode)
148 return &(flt_opcodes[i]);
149 return NULL;
150}
151
152guint32 flt_read_typed_index(G3DStream *stream, guint32 type, gint32 *len)
153{
154 guint32 val = 0;
155
156 switch(type) {
157 case 1:
158 val = g3d_stream_read_int8(stream);
159 *len -= 1;
160 break;
161 case 2:
162 val = g3d_stream_read_int16_be(stream);
163 *len -= 2;
164 break;
165 case 4:
166 val = g3d_stream_read_int32_be(stream);
167 *len -= 4;
168 break;
169 default:
170 g_warning("FLT: unknown index type %d\n", type);
171 break;
172 }
173 return val;
174}
175