Split off scene export code.

This commit is contained in:
Nathan Letwory 2011-09-06 22:18:12 +00:00
parent 813d09cb59
commit 9baff83d72
4 changed files with 267 additions and 159 deletions

@ -61,6 +61,7 @@ set(SRC
MaterialExporter.cpp
MeshImporter.cpp
SkinInfo.cpp
SceneExporter.cpp
TransformReader.cpp
TransformWriter.cpp
collada.cpp
@ -85,6 +86,7 @@ set(SRC
MaterialExporter.h
MeshImporter.h
SkinInfo.h
SceneExporter.h
TransformReader.h
TransformWriter.h
collada.h

@ -34,6 +34,7 @@ extern "C"
{
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
#include "DNA_group_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_mesh_types.h"
#include "DNA_image_types.h"
@ -104,6 +105,7 @@ extern char build_rev[];
#include "COLLADASWConstants.h"
#include "COLLADASWLibraryControllers.h"
#include "COLLADASWInstanceController.h"
#include "COLLADASWInstanceNode.h"
#include "COLLADASWBaseInputElement.h"
#include "collada_internal.h"
@ -113,6 +115,7 @@ extern char build_rev[];
#include "InstanceWriter.h"
#include "TransformWriter.h"
#include "SceneExporter.h"
#include "ArmatureExporter.h"
#include "AnimationExporter.h"
#include "CameraExporter.h"
@ -142,165 +145,6 @@ char *bc_CustomData_get_active_layer_name(const CustomData *data, int type)
return data->layers[layer_index].name;
}
/*
Utilities to avoid code duplication.
Definition can take some time to understand, but they should be useful.
*/
template<class Functor>
void forEachObjectInScene(Scene *sce, Functor &f)
{
Base *base= (Base*) sce->base.first;
while(base) {
Object *ob = base->object;
f(ob);
base= base->next;
}
}
class SceneExporter: COLLADASW::LibraryVisualScenes, protected TransformWriter, protected InstanceWriter
{
ArmatureExporter *arm_exporter;
public:
SceneExporter(COLLADASW::StreamWriter *sw, ArmatureExporter *arm) : COLLADASW::LibraryVisualScenes(sw),
arm_exporter(arm) {}
void exportScene(Scene *sce, bool export_selected) {
// <library_visual_scenes> <visual_scene>
std::string id_naming = id_name(sce);
openVisualScene(translate_id(id_naming), id_naming);
// write <node>s
//forEachMeshObjectInScene(sce, *this);
//forEachCameraObjectInScene(sce, *this);
//forEachLampObjectInScene(sce, *this);
exportHierarchy(sce, export_selected);
// </visual_scene> </library_visual_scenes>
closeVisualScene();
closeLibrary();
}
void exportHierarchy(Scene *sce, bool export_selected)
{
Base *base= (Base*) sce->base.first;
while(base) {
Object *ob = base->object;
if (!ob->parent) {
if(sce->lay & ob->lay) {
switch(ob->type) {
case OB_MESH:
case OB_CAMERA:
case OB_LAMP:
case OB_ARMATURE:
case OB_EMPTY:
if (export_selected && !(ob->flag & SELECT)) {
break;
}
// write nodes....
writeNodes(ob, sce);
break;
}
}
}
base= base->next;
}
}
// called for each object
//void operator()(Object *ob) {
void writeNodes(Object *ob, Scene *sce)
{
COLLADASW::Node node(mSW);
node.setNodeId(translate_id(id_name(ob)));
node.setType(COLLADASW::Node::NODE);
node.start();
bool is_skinned_mesh = arm_exporter->is_skinned_mesh(ob);
if (ob->type == OB_MESH && is_skinned_mesh)
// for skinned mesh we write obmat in <bind_shape_matrix>
TransformWriter::add_node_transform_identity(node);
else
TransformWriter::add_node_transform_ob(node, ob);
// <instance_geometry>
if (ob->type == OB_MESH) {
if (is_skinned_mesh) {
arm_exporter->add_instance_controller(ob);
}
else {
COLLADASW::InstanceGeometry instGeom(mSW);
instGeom.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob)));
InstanceWriter::add_material_bindings(instGeom.getBindMaterial(), ob);
instGeom.add();
}
}
// <instance_controller>
else if (ob->type == OB_ARMATURE) {
arm_exporter->add_armature_bones(ob, sce);
// XXX this looks unstable...
node.end();
}
// <instance_camera>
else if (ob->type == OB_CAMERA) {
COLLADASW::InstanceCamera instCam(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_camera_id(ob)));
instCam.add();
}
// <instance_light>
else if (ob->type == OB_LAMP) {
COLLADASW::InstanceLight instLa(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_light_id(ob)));
instLa.add();
}
// empty object
else if (ob->type == OB_EMPTY) {
}
// write nodes for child objects
Base *b = (Base*) sce->base.first;
while(b) {
// cob - child object
Object *cob = b->object;
if (cob->parent == ob) {
switch(cob->type) {
case OB_MESH:
case OB_CAMERA:
case OB_LAMP:
case OB_EMPTY:
case OB_ARMATURE:
// write node...
writeNodes(cob, sce);
break;
}
}
b = b->next;
}
if (ob->type != OB_ARMATURE)
node.end();
}
};
// TODO: it would be better to instantiate animations rather than create a new one per object
// COLLADA allows this through multiple <channel>s in <animation>.
// For this to work, we need to know objects that use a certain action.

@ -0,0 +1,161 @@
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/collada/SceneExporter.cpp
* \ingroup collada
*/
#include "SceneExporter.h"
SceneExporter::SceneExporter(COLLADASW::StreamWriter *sw, ArmatureExporter *arm)
: COLLADASW::LibraryVisualScenes(sw), arm_exporter(arm)
{}
void SceneExporter::exportScene(Scene *sce, bool export_selected)
{
// <library_visual_scenes> <visual_scene>
std::string id_naming = id_name(sce);
openVisualScene(translate_id(id_naming), id_naming);
exportHierarchy(sce, export_selected);
closeVisualScene();
closeLibrary();
}
void SceneExporter::exportHierarchy(Scene *sce, bool export_selected)
{
Base *base= (Base*) sce->base.first;
while(base) {
Object *ob = base->object;
if (!ob->parent) {
if(sce->lay & ob->lay) {
switch(ob->type) {
case OB_MESH:
case OB_CAMERA:
case OB_LAMP:
case OB_ARMATURE:
case OB_EMPTY:
if (export_selected && !(ob->flag & SELECT)) {
break;
}
// write nodes....
writeNodes(ob, sce);
break;
}
}
}
base= base->next;
}
}
void SceneExporter::writeNodes(Object *ob, Scene *sce)
{
COLLADASW::Node node(mSW);
node.setNodeId(translate_id(id_name(ob)));
node.setType(COLLADASW::Node::NODE);
node.start();
bool is_skinned_mesh = arm_exporter->is_skinned_mesh(ob);
if (ob->type == OB_MESH && is_skinned_mesh)
// for skinned mesh we write obmat in <bind_shape_matrix>
TransformWriter::add_node_transform_identity(node);
else
TransformWriter::add_node_transform_ob(node, ob);
// <instance_geometry>
if (ob->type == OB_MESH) {
if (is_skinned_mesh) {
arm_exporter->add_instance_controller(ob);
}
else {
COLLADASW::InstanceGeometry instGeom(mSW);
instGeom.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob)));
InstanceWriter::add_material_bindings(instGeom.getBindMaterial(), ob);
instGeom.add();
}
}
// <instance_controller>
else if (ob->type == OB_ARMATURE) {
arm_exporter->add_armature_bones(ob, sce);
// XXX this looks unstable...
node.end();
}
// <instance_camera>
else if (ob->type == OB_CAMERA) {
COLLADASW::InstanceCamera instCam(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_camera_id(ob)));
instCam.add();
}
// <instance_light>
else if (ob->type == OB_LAMP) {
COLLADASW::InstanceLight instLa(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_light_id(ob)));
instLa.add();
}
// empty object
else if (ob->type == OB_EMPTY) { // TODO: handle groups (OB_DUPLIGROUP
if((ob->transflag & OB_DUPLIGROUP) == OB_DUPLIGROUP && ob->dup_group) {
GroupObject *go = NULL;
Group *gr = ob->dup_group;
printf("group detected %u\n", gr);
for(go = (GroupObject*)(gr->gobject.first); go; go=go->next) {
printf("\t%s\n", go->ob->id.name);
}
}
}
// write nodes for child objects
Base *b = (Base*) sce->base.first;
while(b) {
// cob - child object
Object *cob = b->object;
if (cob->parent == ob) {
switch(cob->type) {
case OB_MESH:
case OB_CAMERA:
case OB_LAMP:
case OB_EMPTY:
case OB_ARMATURE:
// write node...
writeNodes(cob, sce);
break;
}
}
b = b->next;
}
if (ob->type != OB_ARMATURE)
node.end();
}

@ -0,0 +1,101 @@
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file SceneExporter.h
* \ingroup collada
*/
#ifndef __SCENEEXPORTER_H__
#define __SCENEEXPORTER_H__
extern "C" {
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
#include "DNA_group_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_mesh_types.h"
#include "DNA_image_types.h"
#include "DNA_material_types.h"
#include "DNA_texture_types.h"
#include "DNA_anim_types.h"
#include "DNA_action_types.h"
#include "DNA_curve_types.h"
#include "DNA_armature_types.h"
#include "DNA_modifier_types.h"
#include "DNA_userdef_types.h"
#include "BKE_DerivedMesh.h"
#include "BKE_fcurve.h"
#include "BKE_animsys.h"
#include "BLI_path_util.h"
#include "BLI_fileops.h"
#include "ED_keyframing.h"
}
#include "COLLADASWAsset.h"
#include "COLLADASWLibraryVisualScenes.h"
#include "COLLADASWNode.h"
#include "COLLADASWSource.h"
#include "COLLADASWInstanceGeometry.h"
#include "COLLADASWInputList.h"
#include "COLLADASWPrimitves.h"
#include "COLLADASWVertices.h"
#include "COLLADASWLibraryAnimations.h"
#include "COLLADASWLibraryImages.h"
#include "COLLADASWLibraryEffects.h"
#include "COLLADASWImage.h"
#include "COLLADASWEffectProfile.h"
#include "COLLADASWColorOrTexture.h"
#include "COLLADASWParamTemplate.h"
#include "COLLADASWParamBase.h"
#include "COLLADASWSurfaceInitOption.h"
#include "COLLADASWSampler.h"
#include "COLLADASWScene.h"
#include "COLLADASWTechnique.h"
#include "COLLADASWTexture.h"
#include "COLLADASWLibraryMaterials.h"
#include "COLLADASWBindMaterial.h"
#include "COLLADASWInstanceCamera.h"
#include "COLLADASWInstanceLight.h"
#include "COLLADASWConstants.h"
#include "COLLADASWLibraryControllers.h"
#include "COLLADASWInstanceController.h"
#include "COLLADASWInstanceNode.h"
#include "COLLADASWBaseInputElement.h"
#include "ArmatureExporter.h"
class SceneExporter: COLLADASW::LibraryVisualScenes, protected TransformWriter, protected InstanceWriter
{
ArmatureExporter *arm_exporter;
public:
SceneExporter(COLLADASW::StreamWriter *sw, ArmatureExporter *arm);
void exportScene(Scene *sce, bool export_selected);
private:
void exportHierarchy(Scene *sce, bool export_selected);
void writeNodes(Object *ob, Scene *sce);
};
#endif