aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar.c183
1 files changed, 183 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar.c
new file mode 100644
index 0000000..58bd274
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar.c
@@ -0,0 +1,183 @@
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 <stdarg.h>
26#include <locale.h>
27
28#include <g3d/types.h>
29#include <g3d/read.h>
30#include <g3d/material.h>
31#include <g3d/matrix.h>
32
33#include "imp_ar.h"
34#include "imp_ar_decompress.h"
35#include "imp_ar_dof.h"
36#include "imp_ar_carini.h"
37
38static GSList *ar_read_directory(G3DStream *stream);
39static G3DObject *ar_load_subfile(G3DContext *context, G3DModel *model,
40 G3DStream *stream, const gchar *subfile);
41
42/*****************************************************************************/
43/* plugin interface */
44
45EAPI
46gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
47 G3DModel *model, gpointer plugin_data)
48{
49 GSList *dir, *item;
50 GHashTable *carini;
51 G3DMaterial *material;
52 G3DObject *object;
53 gchar *mname;
54 G3DFloat x, y, z;
55
56 setlocale(LC_NUMERIC, "C");
57
58 /* default material */
59 material = g3d_material_new();
60 material->name = g_strdup("default material");
61 model->materials = g_slist_append(model->materials, material);
62
63 if(g_strcasecmp(stream->uri + (strlen(stream->uri) - 4), ".dof") == 0) {
64 /* single DOF file */
65 ar_dof_load(context, model, stream);
66 } else {
67 /* compressed AR archive */
68 carini = ar_carini_load();
69
70 /* load directory */
71 dir = ar_read_directory(stream);
72
73 /* decompress files */
74 for(item = dir; item != NULL; item = item->next)
75 ar_decompress_to_file(stream, (ArDirEntry *)item->data);
76
77 /* load body */
78 mname = g_hash_table_lookup(carini, "body.model.file");
79 ar_load_subfile(context, model, stream, mname);
80 /* steering wheel */
81 mname = g_hash_table_lookup(carini, "steer.model.file");
82 if(mname != NULL)
83 {
84 printf("D: steering wheel (%s)\n", mname);
85 object = ar_load_subfile(context, model, stream, mname);
86 ar_carini_get_position(carini, "steer", &x, &y, &z);
87 object->transformation = g_new0(G3DTransformation, 1);
88 g3d_matrix_identity(object->transformation->matrix);
89 g3d_matrix_translate(x, y, z, object->transformation->matrix);
90 }
91
92 /* load wheel 0 */
93 mname = g_hash_table_lookup(carini, "wheel0~wheel_front.model.file");
94 if(mname != NULL)
95 {
96 printf("D: loading wheel 0 (%s)\n", mname);
97 object = ar_load_subfile(context, model, stream, mname);
98 x = ar_carini_get_float(carini, "susp0~susp_front.x");
99 y = ar_carini_get_float(carini, "susp_front.y") -
100 ar_carini_get_float(carini, "wheel_front.radius");
101 z = ar_carini_get_float(carini, "susp_front.z");
102 object->transformation = g_new0(G3DTransformation, 1);
103 g3d_matrix_identity(object->transformation->matrix);
104 g3d_matrix_translate(x, y, z, object->transformation->matrix);
105 }
106
107 ar_carini_free(carini);
108 }
109
110 return TRUE;
111}
112
113EAPI
114gchar *plugin_description(void)
115{
116 return g_strdup("Racer models.");
117}
118
119EAPI
120gchar **plugin_extensions(void)
121{
122 return g_strsplit("ar:dof", ":", 0);
123}
124
125/*****************************************************************************/
126
127static GSList *ar_read_directory(G3DStream *stream)
128{
129 ArDirEntry *dirent;
130 GSList *list = NULL;
131 guint32 fsize, dpos;
132 gint32 nbytes;
133 gchar buffer[128];
134
135 g3d_stream_seek(stream, -4, G_SEEK_END);
136 fsize = g3d_stream_tell(stream);
137 dpos = g3d_stream_read_int32_le(stream);
138
139 /* start of directory */
140 g3d_stream_seek(stream, dpos, G_SEEK_SET);
141 nbytes = fsize - dpos;
142#if DEBUG > 0
143 printf("D: AR: directory @ 0x%08x, %d bytes\n", dpos, nbytes);
144#endif
145
146 while(nbytes > 0) {
147 dirent = g_new0(ArDirEntry, 1);
148 list = g_slist_append(list, dirent);
149
150 nbytes -= g3d_stream_read_cstr(stream, buffer, 127);
151 dirent->name = g_strdup(buffer);
152 dirent->offset = g3d_stream_read_int32_le(stream);
153 dirent->size = g3d_stream_read_int32_le(stream);
154 nbytes -= 8;
155
156#if DEBUG > 0
157 printf("D: AR: * %s @ 0x%08x, %d bytes\n",
158 dirent->name, dirent->offset, dirent->size);
159#endif
160 }
161
162 return list;
163}
164
165static G3DObject *ar_load_subfile(G3DContext *context, G3DModel *model,
166 G3DStream *stream, const gchar *subfile)
167{
168 G3DStream *substream;
169 G3DObject *o;
170
171 if(subfile == NULL)
172 return NULL;
173
174 substream = g3d_stream_open_file(subfile, "rb");
175 if(substream == NULL)
176 return NULL;
177
178 o = ar_dof_load(context, model, substream);
179 g3d_stream_close(substream);
180
181 return o;
182}
183