Blender  V2.59
GeometryExporter.h
Go to the documentation of this file.
00001 /*
00002  * $Id: GeometryExporter.h 38079 2011-07-04 08:59:28Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
00021  *                 Nathan Letwory
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00030 #ifndef __GEOMETRYEXPORTER_H__
00031 #define __GEOMETRYEXPORTER_H__
00032 
00033 #include <string>
00034 #include <vector>
00035 #include <set>
00036 
00037 #include "COLLADASWStreamWriter.h"
00038 #include "COLLADASWLibraryGeometries.h"
00039 #include "COLLADASWInputList.h"
00040 
00041 #include "DNA_mesh_types.h"
00042 #include "DNA_object_types.h"
00043 #include "DNA_scene_types.h"
00044 
00045 // TODO: optimize UV sets by making indexed list with duplicates removed
00046 class GeometryExporter : COLLADASW::LibraryGeometries
00047 {
00048         struct Face
00049         {
00050                 unsigned int v1, v2, v3, v4;
00051         };
00052 
00053         struct Normal
00054         {
00055                 float x, y, z;
00056         };
00057 
00058         Scene *mScene;
00059 
00060 public:
00061         GeometryExporter(COLLADASW::StreamWriter *sw);
00062 
00063         void exportGeom(Scene *sce, bool export_selected);
00064 
00065         void operator()(Object *ob);
00066 
00067         // powerful because it handles both cases when there is material and when there's not
00068         void createPolylist(int material_index,
00069                                                 bool has_uvs,
00070                                                 bool has_color,
00071                                                 Object *ob,
00072                                                 std::string& geom_id,
00073                                                 std::vector<Face>& norind);
00074         
00075         // creates <source> for positions
00076         void createVertsSource(std::string geom_id, Mesh *me);
00077 
00078         void createVertexColorSource(std::string geom_id, Mesh *me);
00079 
00080         std::string makeTexcoordSourceId(std::string& geom_id, int layer_index);
00081 
00082         //creates <source> for texcoords
00083         void createTexcoordsSource(std::string geom_id, Mesh *me);
00084 
00085         //creates <source> for normals
00086         void createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal>& nor);
00087 
00088         void create_normals(std::vector<Normal> &nor, std::vector<Face> &ind, Mesh *me);
00089         
00090         std::string getIdBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix = "");
00091         
00092         COLLADASW::URI getUrlBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix = "");
00093 
00094         COLLADASW::URI makeUrl(std::string id);
00095         
00096         /* int getTriCount(MFace *faces, int totface);*/
00097 private:
00098         std::set<std::string> exportedGeometry;
00099 };
00100 
00101 struct GeometryFunctor {
00102         // f should have
00103         // void operator()(Object* ob)
00104         template<class Functor>
00105         void forEachMeshObjectInScene(Scene *sce, Functor &f, bool export_selected)
00106         {
00107                 
00108                 Base *base= (Base*) sce->base.first;
00109                 while(base) {
00110                         Object *ob = base->object;
00111                         
00112                         if (ob->type == OB_MESH && ob->data
00113                                 && !(export_selected && !(ob->flag && SELECT))) {
00114                                 f(ob);
00115                         }
00116                         base= base->next;
00117                         
00118                 }
00119         }
00120 };
00121 
00122 #endif