1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/* $Id$ */
/*
libg3d - 3D object loading library
Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include <g3d/config.h>
#endif
#include <string.h>
#include <g3d/types.h>
#include <g3d/context.h>
#include <g3d/object.h>
#include <g3d/material.h>
#include <g3d/stream.h>
#include <g3d/debug.h>
#include "imp_max_chunks.h"
static gboolean max_read_subfile(G3DContext *context, G3DModel *model,
G3DStream *stream, const gchar *subfile);
static gboolean max_read_chunk(MaxGlobalData *global, gint32 *nb,
guint32 level, gint32 parentid, gpointer object, guint32 *l2cnt,
GNode *tree);
static MaxChunk *max_get_chunk_desc(guint16 id, gint32 parentid,
gboolean container);
static const gchar *max_subfiles[] = {
#if 0
"Config",
"VideoPostQueue",
"ScriptedCustAttribDefs",
"DllDirectory",
"ClassDirectory",
"ClassDirectory2",
"ClassDirectory3",
"ClassData",
#endif
"Scene",
NULL
};
typedef enum {
MAX_ROOT_NODE,
MAX_L2_NODE,
MAX_CNT_NODE,
MAX_DATA_NODE
} MaxNodeType;
typedef struct {
MaxNodeType type;
gchar *name;
guint32 cnt2034;
guint32 val2034;
gint32 data2034;
GSList *children;
} MaxNode;
typedef struct {
guint32 l2id;
guint32 id;
gchar *text;
G3DObject *object;
} MaxTreeItem;
gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
G3DModel *model)
{
gboolean retval = FALSE;
G3DMaterial *material;
const gchar **subfile = max_subfiles;
/* create default material */
material = g3d_material_new();
material->name = g_strdup("default material");
model->materials = g_slist_append(model->materials, material);
/* debugging material */
material = g3d_material_new();
material->r = 1.0;
material->g = 0.2;
material->b = 0.1;
material->name = g_strdup("debugging material");
model->materials = g_slist_append(model->materials, material);
while(*subfile) {
retval = max_read_subfile(context, model, stream, *subfile);
subfile ++;
}
g3d_context_update_progress_bar(context, 0.0, FALSE);
return retval;
}
gchar *plugin_description(void)
{
return g_strdup("import plugin for 3D Studio MAX files (EXPERIMENTAL)\n");
}
gchar **plugin_extensions(void)
{
return g_strsplit("max:gmax", ":", 0);
}
/*****************************************************************************
* max specific
*****************************************************************************/
static void max_walk_tree(GNode *tree, guint32 level)
{
GNode *node;
MaxTreeItem *mtitem;
mtitem = (MaxTreeItem *)tree->data;
#if DEBUG > 0
g_debug("\\%s(%u)[0x%04X][0x%04X] %s", debug_pad(level), level,
mtitem->l2id, mtitem->id, mtitem->text);
#endif
for(node = tree->children; node != NULL; node = node->next) {
max_walk_tree(node, level + 1);
}
}
static gboolean max_read_subfile(G3DContext *context, G3DModel *model,
G3DStream *stream, const gchar *subfile)
{
G3DStream *ssf;
MaxGlobalData *global;
gint32 fsize;
guint32 l2cnt = 0;
MaxTreeItem *rootitem;
GNode *tree;
rootitem = g_new0(MaxTreeItem, 1);
rootitem->text = g_strdup("ROOT");
tree = g_node_new(rootitem);
ssf = g3d_stream_open_structured_file_from_stream(stream, subfile);
if(ssf == NULL) {
g_warning("MAX: failed to open '%s' in structured file '%s'",
subfile, stream->uri);
return FALSE;
}
fsize = g3d_stream_size(ssf);
g_debug("\\%s (%d bytes)", subfile, fsize);
global = g_new0(MaxGlobalData, 1);
global->context = context;
global->model = model;
global->stream = ssf;
global->subfile = subfile;
while(max_read_chunk(global, &fsize, 1 /* level */, IDNONE, NULL, &l2cnt,
tree));
g_debug("MAX tree:");
max_walk_tree(tree, 0);
g_free(global);
g3d_stream_close(ssf);
return TRUE;
}
static GNode *max_find_node(GNode *tree, guint32 id)
{
GNode *node, *found;
MaxTreeItem *mtitem;
mtitem = (MaxTreeItem *)tree->data;
if(mtitem->l2id == id)
return tree;
for(node = tree->children; node != NULL; node = node->next) {
found = max_find_node(node, id);
if(found != NULL)
return found;
}
return NULL;
}
static gboolean max_create_l2_tree_object(MaxGlobalData *global,
MaxLocalData *local, G3DObject *parent)
{
G3DObject *object;
object = g_new0(G3DObject, 1);
object->name = g_strdup_printf("0x%04X object @ 0x%08x",
local->id, (guint32)g3d_stream_tell(global->stream));
local->object = object;
if(parent)
parent->objects = g_slist_append(parent->objects, object);
else
global->model->objects = g_slist_append(global->model->objects,
object);
global->object = object;
global->vertex_offset = 0;
return TRUE;
}
static gboolean max_read_chunk(MaxGlobalData *global, gint32 *nb,
guint32 level, gint32 parentid, gpointer object, guint32 *l2cnt,
GNode *tree)
{
guint16 id;
guint32 length;
gboolean container;
MaxChunk *chunk;
MaxLocalData *local;
MaxTreeItem *mtitem;
GNode *pnode = NULL, *node;
if(nb && (*nb < 6))
return FALSE;
id = g3d_stream_read_int16_le(global->stream);
length = g3d_stream_read_int32_le(global->stream);
container = (length & 0x80000000);
length &= 0x7FFFFFFF;
if(nb && (length > *nb))
return FALSE;
if(nb)
*nb -= length;
if((level == 2) && l2cnt)
(*l2cnt) ++;
chunk = max_get_chunk_desc(id, parentid, container);
#if DEBUG > 0
g_debug("\\%s(%d)[0x%04X][%c%c] %s -- %d (%d) bytes @ 0x%08x",
debug_pad(level), level,
id, (container ? 'c' : ' '),
(chunk && chunk->callback) ? 'f' : ' ',
chunk ? chunk->desc : (level == 2) ? "level 2 container" : "unknown",
length - 6, length,
(guint32)g3d_stream_tell(global->stream) - 6);
#endif
node = tree;
if(level == 2) {
pnode = max_find_node(tree, id);
if(pnode != NULL) {
mtitem = g_new0(MaxTreeItem, 1);
mtitem->l2id = *l2cnt;
mtitem->id = id;
mtitem->text = g_strdup("L2ITEM");
node = g_node_append_data(pnode, mtitem);
} else {
mtitem = g_new0(MaxTreeItem, 1);
mtitem->l2id = 0xFFFF;
mtitem->id = id;
mtitem->text = g_strdup_printf("OUTOFTREE: 0x%04X", id);
node = g_node_append_data(tree, mtitem);
}
} else if(level > 2) {
mtitem = g_new0(MaxTreeItem, 1);
mtitem->l2id = 0xFFFF;
mtitem->id = id;
mtitem->text = g_strdup_printf("REGITEM: 0x%04X: %s",
id, chunk ? chunk->desc : "unknown");
node = g_node_append_data(tree, mtitem);
}
local = g_new0(MaxLocalData, 1);
local->id = (level > 2) ? id : 0x0000;
local->parentid = parentid;
local->nb = length - 6;
local->level = level + 1;
local->object = object;
if((level > 2) && chunk && chunk->callback)
chunk->callback(global, local);
if(level == 2) {
mtitem = pnode ? pnode->data : NULL;
max_create_l2_tree_object(global, local,
mtitem ? mtitem->object : NULL);
mtitem = node->data;
mtitem->object = local->object;
}
if(container)
while(local->nb > 0)
if(!max_read_chunk(global, &(local->nb), level + 1, id,
local->object, l2cnt, node))
return FALSE;
if(local->nb > 0)
g3d_stream_skip(global->stream, local->nb);
g_free(local);
g3d_context_update_interface(global->context);
if(level < 3)
g3d_context_update_progress_bar(global->context,
(gfloat)g3d_stream_tell(global->stream) /
(gfloat)g3d_stream_size(global->stream),
TRUE);
return TRUE;
}
static MaxChunk *max_get_chunk_desc(guint16 id, gint32 parentid,
gboolean container)
{
MaxChunk *chunk, *chunks;
gint32 i;
if(container)
chunks = max_cnt_chunks;
else
chunks = max_chunks;
for(i = 0, chunk = &(chunks[i]); chunk->id != IDNONE;
i ++, chunk = &(chunks[i])) {
if((chunk->parentid == IDSOME) || (parentid == chunk->parentid) ||
(parentid == IDSOME) ||
((chunk->parentid == IDROOT) && ID_IS_ROOT(parentid)) ||
((chunk->parentid == IDGEOM) && ID_IS_GEOM(parentid)) ||
((chunk->parentid == IDMATG) && ID_IS_MATG(parentid)) ||
((chunk->parentid == IDFILE) && ID_IS_FILE(parentid))) {
if(chunk->id == id)
return chunk;
} /* parentid */
}
return NULL;
}
|