Mostly formatting commit, small elaboration in extra handling API (take pointer to importer).

This commit is contained in:
Nathan Letwory 2011-03-25 09:52:36 +00:00
parent 5f5d091554
commit c5c4b31d6b
4 changed files with 867 additions and 889 deletions

@ -92,60 +92,23 @@
// creates empties for each imported bone on layer 2, for debugging
// #define ARMATURE_TEST
/** Class that needs to be implemented by a writer.
IMPORTANT: The write functions are called in arbitrary order.*/
/*
private:
std::string mFilename;
bContext *mContext;
UnitConverter unit_converter;
ArmatureImporter armature_importer;
MeshImporter mesh_importer;
AnimationImporter anim_importer;
std::map<COLLADAFW::UniqueId, Image*> uid_image_map;
std::map<COLLADAFW::UniqueId, Material*> uid_material_map;
std::map<COLLADAFW::UniqueId, Material*> uid_effect_map;
std::map<COLLADAFW::UniqueId, Camera*> uid_camera_map;
std::map<COLLADAFW::UniqueId, Lamp*> uid_lamp_map;
std::map<Material*, TexIndexTextureArrayMap> material_texture_mapping_map;
std::map<COLLADAFW::UniqueId, Object*> object_map;
std::map<COLLADAFW::UniqueId, COLLADAFW::Node*> node_map;
std::vector<const COLLADAFW::VisualScene*> vscenes;
std::vector<Object*> libnode_ob;
std::map<COLLADAFW::UniqueId, COLLADAFW::Node*> root_map;
*/
// find root joint by child joint uid, for bone tree evaluation during resampling
// animation
// std::map<COLLADAFW::UniqueId, std::vector<FCurve*> > uid_fcurve_map;
// Nodes don't share AnimationLists (Arystan)
// std::map<COLLADAFW::UniqueId, Animation> uid_animated_map; // AnimationList->uniqueId to AnimatedObject map
//public:
/** Constructor. */
DocumentImporter::DocumentImporter(bContext *C, const char *filename) :
DocumentImporter::DocumentImporter(bContext *C, const char *filename) :
mImportStage(General),
mFilename(filename),
mContext(C),
armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(C)),
mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)),
anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C))
{}
{}
/** Destructor. */
DocumentImporter::~DocumentImporter() {}
DocumentImporter::~DocumentImporter() {}
bool DocumentImporter::import()
{
bool DocumentImporter::import()
{
/** TODO Add error handler (implement COLLADASaxFWL::IErrorHandler */
COLLADASaxFWL::Loader loader;
COLLADAFW::Root root(&loader, this);
ExtraHandler *ehandler = new ExtraHandler();
ExtraHandler *ehandler = new ExtraHandler(this);
loader.registerExtraDataCallbackHandler(ehandler);
@ -167,23 +130,22 @@ private:
delete ehandler;
return true;
}
}
void DocumentImporter::cancel(const COLLADAFW::String& errorMessage)
{
void DocumentImporter::cancel(const COLLADAFW::String& errorMessage)
{
// TODO: if possible show error info
//
// Should we get rid of invisible Meshes that were created so far
// or maybe create objects at coordinate space origin?
//
// The latter sounds better.
}
}
void DocumentImporter::start(){}
void DocumentImporter::start(){}
/** This method is called after the last write* method. No other methods will be called after this.*/
void DocumentImporter::finish()
{
void DocumentImporter::finish()
{
if(mImportStage!=General)
return;
@ -257,11 +219,11 @@ private:
DAG_scene_sort(CTX_data_main(mContext), sce);
DAG_ids_flush_update(CTX_data_main(mContext), 0);
}
}
}
void DocumentImporter::translate_anim_recursive(COLLADAFW::Node *node, COLLADAFW::Node *par = NULL, Object *parob = NULL)
{
void DocumentImporter::translate_anim_recursive(COLLADAFW::Node *node, COLLADAFW::Node *par = NULL, Object *parob = NULL)
{
if (par && par->getType() == COLLADAFW::Node::JOINT) {
// par is root if there's no corresp. key in root_map
if (root_map.find(par->getUniqueId()) == root_map.end())
@ -287,26 +249,26 @@ private:
for (i = 0; i < children.getCount(); i++) {
translate_anim_recursive(children[i], node, ob);
}
}
}
/** When this method is called, the writer must write the global document asset.
/** When this method is called, the writer must write the global document asset.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeGlobalAsset ( const COLLADAFW::FileInfo* asset )
{
bool DocumentImporter::writeGlobalAsset ( const COLLADAFW::FileInfo* asset )
{
unit_converter.read_asset(asset);
return true;
}
}
/** When this method is called, the writer must write the scene.
/** When this method is called, the writer must write the scene.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeScene ( const COLLADAFW::Scene* scene )
{
bool DocumentImporter::writeScene ( const COLLADAFW::Scene* scene )
{
// XXX could store the scene id, but do nothing for now
return true;
}
Object* DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce)
{
}
Object* DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce)
{
const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId();
if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) {
fprintf(stderr, "Couldn't find camera by UID.\n");
@ -320,10 +282,10 @@ private:
if (old_cam->id.us == 0)
free_libblock(&G.main->camera, old_cam);
return ob;
}
}
Object* DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce)
{
Object* DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce)
{
const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId();
if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) {
fprintf(stderr, "Couldn't find lamp by UID. \n");
@ -337,10 +299,10 @@ private:
if (old_lamp->id.us == 0)
free_libblock(&G.main->lamp, old_lamp);
return ob;
}
}
Object* DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node)
{
Object* DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node)
{
Object *obn = copy_object(source_ob);
obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
scene_add_base(sce, obn);
@ -399,10 +361,10 @@ private:
// its correct location gets overwritten in write_node(). Fixes bug #26012.
if(instance_node) return NULL;
else return obn;
}
}
void DocumentImporter::write_node (COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node)
{
void DocumentImporter::write_node (COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node)
{
Object *ob = NULL;
bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
@ -488,12 +450,12 @@ private:
for (unsigned int i = 0; i < child_nodes.getCount(); i++) {
write_node(child_nodes[i], node, sce, ob, is_library_node);
}
}
}
/** When this method is called, the writer must write the entire visual scene.
/** When this method is called, the writer must write the entire visual scene.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeVisualScene ( const COLLADAFW::VisualScene* visualScene )
{
bool DocumentImporter::writeVisualScene ( const COLLADAFW::VisualScene* visualScene )
{
if(mImportStage!=General)
return true;
@ -510,13 +472,13 @@ private:
vscenes.push_back(visualScene);
return true;
}
}
/** When this method is called, the writer must handle all nodes contained in the
/** When this method is called, the writer must handle all nodes contained in the
library nodes.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeLibraryNodes ( const COLLADAFW::LibraryNodes* libraryNodes )
{
bool DocumentImporter::writeLibraryNodes ( const COLLADAFW::LibraryNodes* libraryNodes )
{
if(mImportStage!=General)
return true;
@ -529,22 +491,22 @@ private:
}
return true;
}
}
/** When this method is called, the writer must write the geometry.
/** When this method is called, the writer must write the geometry.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeGeometry ( const COLLADAFW::Geometry* geom )
{
bool DocumentImporter::writeGeometry ( const COLLADAFW::Geometry* geom )
{
if(mImportStage!=General)
return true;
return mesh_importer.write_geometry(geom);
}
}
/** When this method is called, the writer must write the material.
/** When this method is called, the writer must write the material.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeMaterial( const COLLADAFW::Material* cmat )
{
bool DocumentImporter::writeMaterial( const COLLADAFW::Material* cmat )
{
if(mImportStage!=General)
return true;
@ -555,12 +517,12 @@ private:
this->uid_material_map[cmat->getUniqueId()] = ma;
return true;
}
}
// create mtex, create texture, set texture image
MTex* DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
// create mtex, create texture, set texture image
MTex* DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
int i, TexIndexTextureArrayMap &texindex_texarray_map)
{
{
COLLADAFW::SamplerPointerArray& samp_array = ef->getSamplerPointerArray();
COLLADAFW::Sampler *sampler = samp_array[ctex.getSamplerId()];
@ -581,10 +543,10 @@ private:
texindex_texarray_map[ctex.getTextureMapId()].push_back(ma->mtex[i]);
return ma->mtex[i];
}
}
void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
{
void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
{
COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
// blinn
@ -702,7 +664,7 @@ private:
}
// TRANSPARENT
// color
// if (ef->getOpacity().isColor()) {
// if (ef->getOpacity().isColor()) {
// // XXX don't know what to do here
// }
// // texture
@ -715,13 +677,13 @@ private:
// }
// }
material_texture_mapping_map[ma] = texindex_texarray_map;
}
}
/** When this method is called, the writer must write the effect.
/** When this method is called, the writer must write the effect.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeEffect( const COLLADAFW::Effect* effect )
{
bool DocumentImporter::writeEffect( const COLLADAFW::Effect* effect )
{
if(mImportStage!=General)
return true;
@ -744,13 +706,13 @@ private:
write_profile_COMMON(ef, ma);
return true;
}
}
/** When this method is called, the writer must write the camera.
/** When this method is called, the writer must write the camera.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeCamera( const COLLADAFW::Camera* camera )
{
bool DocumentImporter::writeCamera( const COLLADAFW::Camera* camera )
{
if(mImportStage!=General)
return true;
@ -860,12 +822,12 @@ private:
this->uid_camera_map[camera->getUniqueId()] = cam;
// XXX import camera options
return true;
}
}
/** When this method is called, the writer must write the image.
/** When this method is called, the writer must write the image.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeImage( const COLLADAFW::Image* image )
{
bool DocumentImporter::writeImage( const COLLADAFW::Image* image )
{
if(mImportStage!=General)
return true;
@ -885,12 +847,12 @@ private:
this->uid_image_map[image->getUniqueId()] = ima;
return true;
}
}
/** When this method is called, the writer must write the light.
/** When this method is called, the writer must write the light.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeLight( const COLLADAFW::Light* light )
{
bool DocumentImporter::writeLight( const COLLADAFW::Light* light )
{
if(mImportStage!=General)
return true;
@ -987,53 +949,56 @@ private:
this->uid_lamp_map[light->getUniqueId()] = lamp;
return true;
}
}
// this function is called only for animations that pass COLLADAFW::validate
bool DocumentImporter::writeAnimation( const COLLADAFW::Animation* anim )
{
// this function is called only for animations that pass COLLADAFW::validate
bool DocumentImporter::writeAnimation( const COLLADAFW::Animation* anim )
{
if(mImportStage!=General)
return true;
// return true;
return anim_importer.write_animation(anim);
}
}
// called on post-process stage after writeVisualScenes
bool DocumentImporter::writeAnimationList( const COLLADAFW::AnimationList* animationList )
{
// called on post-process stage after writeVisualScenes
bool DocumentImporter::writeAnimationList( const COLLADAFW::AnimationList* animationList )
{
if(mImportStage!=General)
return true;
// return true;
return anim_importer.write_animation_list(animationList);
}
}
/** When this method is called, the writer must write the skin controller data.
/** When this method is called, the writer must write the skin controller data.
@return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeSkinControllerData( const COLLADAFW::SkinControllerData* skin )
{
bool DocumentImporter::writeSkinControllerData( const COLLADAFW::SkinControllerData* skin )
{
return armature_importer.write_skin_controller_data(skin);
}
}
// this is called on postprocess, before writeVisualScenes
bool DocumentImporter::writeController( const COLLADAFW::Controller* controller )
{
// this is called on postprocess, before writeVisualScenes
bool DocumentImporter::writeController( const COLLADAFW::Controller* controller )
{
if(mImportStage!=General)
return true;
return armature_importer.write_controller(controller);
}
}
bool DocumentImporter::writeFormulas( const COLLADAFW::Formulas* formulas )
{
bool DocumentImporter::writeFormulas( const COLLADAFW::Formulas* formulas )
{
return true;
}
}
bool DocumentImporter::writeKinematicsScene( const COLLADAFW::KinematicsScene* kinematicsScene )
{
bool DocumentImporter::writeKinematicsScene( const COLLADAFW::KinematicsScene* kinematicsScene )
{
return true;
}
}
bool DocumentImporter::addElementData( const COLLADAFW::UniqueId &uid)
{
return true;
}

@ -53,7 +53,7 @@ struct bContext;
/** Importer class. */
class DocumentImporter : COLLADAFW::IWriter
{
public:
public:
//! Enumeration to denote the stage of import
enum ImportStage {
General, //!< First pass to collect all data except controller
@ -121,7 +121,10 @@ class DocumentImporter : COLLADAFW::IWriter
bool writeKinematicsScene(const COLLADAFW::KinematicsScene*);
private:
/** Add element and data for UniqueId */
bool addElementData(const COLLADAFW::UniqueId &uid);
private:
/** Current import stage we're in. */
ImportStage mImportStage;

@ -27,14 +27,16 @@
*/
#include <stddef.h>
#include "BLI_string.h"
#include "ExtraHandler.h"
ExtraHandler::ExtraHandler(){}
ExtraHandler::ExtraHandler(DocumentImporter *dimp)
{
this->dimp = dimp;
}
ExtraHandler::~ExtraHandler(){}
ExtraHandler::~ExtraHandler() {}
bool ExtraHandler::elementBegin( const char* elementName, const char** attributes)
{
@ -45,6 +47,7 @@ bool ExtraHandler::elementBegin( const char* elementName, const char** attribute
bool ExtraHandler::elementEnd(const char* elementName )
{
printf("end: %s\n", elementName);
currentUid = COLLADAFW::UniqueId();
return true;
}
@ -62,6 +65,7 @@ bool ExtraHandler::parseElement (
const COLLADAFW::UniqueId& uniqueId ) {
if(BLI_strcaseeq(profileName, "blender")) {
printf("In parseElement for supported profile %s for id %s\n", profileName, uniqueId.toAscii().c_str());
currentUid = uniqueId;
return true;
}
printf("In parseElement for unsupported profile %s for id %s\n", profileName, uniqueId.toAscii().c_str());

@ -33,6 +33,7 @@
#include "COLLADASaxFWLIExtraDataCallbackHandler.h"
#include "DocumentImporter.h"
/** \brief Handler class for <extra> data, through which different
* profiles can be handled
@ -41,7 +42,7 @@ class ExtraHandler : public COLLADASaxFWL::IExtraDataCallbackHandler
{
public:
/** Constructor. */
ExtraHandler();
ExtraHandler(DocumentImporter *dimp);
/** Destructor. */
virtual ~ExtraHandler();
@ -65,5 +66,10 @@ private:
ExtraHandler( const ExtraHandler& pre );
/** Disable default assignment operator. */
const ExtraHandler& operator= ( const ExtraHandler& pre );
/** Handle to DocumentImporter for interface to extra element data saving. */
DocumentImporter* dimp;
/** Holds Id of element for which <extra> XML elements are handled. */
COLLADAFW::UniqueId currentUid;
};