Blender V2.61 - r43446

SceneExporter.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00027 #include "SceneExporter.h"
00028 
00029 SceneExporter::SceneExporter(COLLADASW::StreamWriter *sw, ArmatureExporter *arm, const ExportSettings *export_settings)
00030     : COLLADASW::LibraryVisualScenes(sw), arm_exporter(arm), export_settings(export_settings)
00031 {}
00032     
00033 void SceneExporter::exportScene(Scene *sce)
00034 {
00035     // <library_visual_scenes> <visual_scene>
00036     std::string id_naming = id_name(sce);
00037     openVisualScene(translate_id(id_naming), id_naming);
00038     exportHierarchy(sce);
00039     closeVisualScene();
00040     closeLibrary();
00041 }
00042 
00043 void SceneExporter::exportHierarchy(Scene *sce)
00044 {
00045     Base *base= (Base*) sce->base.first;
00046     while(base) {
00047         Object *ob = base->object;
00048 
00049         if (!ob->parent) {
00050             if(sce->lay & ob->lay) {
00051                 switch(ob->type) {
00052                     case OB_MESH:
00053                     case OB_CAMERA:
00054                     case OB_LAMP:
00055                     case OB_ARMATURE:
00056                     case OB_EMPTY:
00057                         if (this->export_settings->selected && !(ob->flag & SELECT)) {
00058                             break;
00059                         }
00060                         // write nodes....
00061                         writeNodes(ob, sce);
00062                         break;
00063                 }
00064             }
00065         }
00066 
00067         base= base->next;
00068     }
00069 }
00070 
00071 void SceneExporter::writeNodes(Object *ob, Scene *sce)
00072 {
00073     COLLADASW::Node node(mSW);
00074     node.setNodeId(translate_id(id_name(ob)));
00075     node.setType(COLLADASW::Node::NODE);
00076 
00077     node.start();
00078 
00079     bool is_skinned_mesh = arm_exporter->is_skinned_mesh(ob);
00080 
00081     if (ob->type == OB_MESH && is_skinned_mesh)
00082         // for skinned mesh we write obmat in <bind_shape_matrix>
00083         TransformWriter::add_node_transform_identity(node);
00084     else
00085         TransformWriter::add_node_transform_ob(node, ob);
00086 
00087     // <instance_geometry>
00088     if (ob->type == OB_MESH) {
00089         if (is_skinned_mesh) {
00090             arm_exporter->add_instance_controller(ob);
00091         }
00092         else {
00093             COLLADASW::InstanceGeometry instGeom(mSW);
00094             instGeom.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob)));
00095 
00096             InstanceWriter::add_material_bindings(instGeom.getBindMaterial(), ob);
00097 
00098             instGeom.add();
00099         }
00100     }
00101 
00102     // <instance_controller>
00103     else if (ob->type == OB_ARMATURE) {
00104         arm_exporter->add_armature_bones(ob, sce);
00105 
00106         // XXX this looks unstable...
00107         node.end();
00108     }
00109 
00110     // <instance_camera>
00111     else if (ob->type == OB_CAMERA) {
00112         COLLADASW::InstanceCamera instCam(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_camera_id(ob)));
00113         instCam.add();
00114     }
00115 
00116     // <instance_light>
00117     else if (ob->type == OB_LAMP) {
00118         COLLADASW::InstanceLight instLa(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_light_id(ob)));
00119         instLa.add();
00120     }
00121 
00122     // empty object
00123     else if (ob->type == OB_EMPTY) { // TODO: handle groups (OB_DUPLIGROUP
00124         if((ob->transflag & OB_DUPLIGROUP) == OB_DUPLIGROUP && ob->dup_group) {
00125             GroupObject *go = NULL;
00126             Group *gr = ob->dup_group;
00127             /* printf("group detected '%s'\n", gr->id.name+2); */
00128             for(go = (GroupObject*)(gr->gobject.first); go; go=go->next) {
00129                 printf("\t%s\n", go->ob->id.name);
00130             }
00131         }
00132     }
00133 
00134     // write nodes for child objects
00135     Base *b = (Base*) sce->base.first;
00136     while(b) {
00137         // cob - child object
00138         Object *cob = b->object;
00139 
00140         if (cob->parent == ob) {
00141             switch(cob->type) {
00142                 case OB_MESH:
00143                 case OB_CAMERA:
00144                 case OB_LAMP:
00145                 case OB_EMPTY:
00146                 case OB_ARMATURE:
00147                     // write node...
00148                     writeNodes(cob, sce);
00149                     break;
00150             }
00151         }
00152 
00153         b = b->next;
00154     }
00155 
00156     if (ob->type != OB_ARMATURE)
00157         node.end();
00158 }
00159