SourceForge: k3d/k3d: changeset 2648:295b0b18a2bd
ENH: Added the OgreXmlMeshWriter plugin.
authorMattia Barbiero <barraemme@gmail.com>
Sat Oct 24 16:14:29 2009 -0600 (4 weeks ago)
changeset 2648295b0b18a2bd
parent 264725ee5f27bfa7
child 26495406e5d48061
ENH: Added the OgreXmlMeshWriter plugin.
CMakeLists.txt
modules/CMakeLists.txt
modules/obj_io/mesh_writer.cpp
modules/ogre_io/CMakeLists.txt
modules/ogre_io/mesh_writer.cpp
modules/ogre_io/module.cpp
     1.1 --- a/CMakeLists.txt	Sat Oct 24 11:12:23 2009 -0600
     1.2 +++ b/CMakeLists.txt	Sat Oct 24 16:14:29 2009 -0600
     1.3 @@ -177,6 +177,7 @@
     1.4  OPTION(K3D_BUILD_NURBS_SOURCES_MODULE "Build the NURBS sources plugin module" ON)
     1.5  OPTION(K3D_BUILD_OBJ_IO_MODULE "Build the Wavefront .obj file format module" ON)
     1.6  OPTION(K3D_BUILD_ODE_MODULE "Build the Open Dynamics Engine (ODE) integration module" ${K3D_ODE_FOUND})
     1.7 +OPTION(K3D_BUILD_OGRE_IO_MODULE "Build the Object-oriented Graphics Engine (OGRE) file I/O module" ON)
     1.8  OPTION(K3D_BUILD_OPENCASCADE_MODULE "Build the OpenCascade module" ${K3D_OPENCASCADE_FOUND})
     1.9  OPTION(K3D_BUILD_OPENEXR_IO_MODULE "Build the OpenEXR file format module" ${K3D_OPENEXR_FOUND})
    1.10  OPTION(K3D_BUILD_OPENGL_MODULE "Build the OpenGL integration module" ON)
     2.1 --- a/modules/CMakeLists.txt	Sat Oct 24 11:12:23 2009 -0600
     2.2 +++ b/modules/CMakeLists.txt	Sat Oct 24 16:14:29 2009 -0600
     2.3 @@ -149,6 +149,7 @@
     2.4  K3D_CONDITIONAL_BUILD(K3D_BUILD_NURBS_SOURCES_MODULE nurbs_sources)
     2.5  K3D_CONDITIONAL_BUILD(K3D_BUILD_OBJ_IO_MODULE obj_io)
     2.6  K3D_CONDITIONAL_BUILD(K3D_BUILD_ODE_MODULE ode)
     2.7 +K3D_CONDITIONAL_BUILD(K3D_BUILD_OGRE_IO_MODULE ogre_io)
     2.8  K3D_CONDITIONAL_BUILD(K3D_BUILD_OPENCASCADE_MODULE opencascade)
     2.9  K3D_CONDITIONAL_BUILD(K3D_BUILD_OPENEXR_IO_MODULE openexr_io)
    2.10  K3D_CONDITIONAL_BUILD(K3D_BUILD_OPENGL_MODULE opengl)
     3.1 --- a/modules/obj_io/mesh_writer.cpp	Sat Oct 24 11:12:23 2009 -0600
     3.2 +++ b/modules/obj_io/mesh_writer.cpp	Sat Oct 24 16:14:29 2009 -0600
     3.3 @@ -10,12 +10,12 @@
     3.4  //
     3.5  // This program is distributed in the hope that it will be useful,
     3.6  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     3.7 -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     3.8 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
     3.9  // General Public License for more details.
    3.10  //
    3.11  // You should have received a copy of the GNU General Public
    3.12  // License along with this program; if not, write to the Free Software
    3.13 -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.14 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA	02111-1307	USA
    3.15  
    3.16  /** \file
    3.17  	\author Tim Shead (tshead@k-3d.com)
    3.18 @@ -43,7 +43,7 @@
    3.19  {
    3.20  
    3.21  /////////////////////////////////////////////////////////////////////////////
    3.22 -// mesh_writerio
    3.23 +// mesh_writer
    3.24  
    3.25  class mesh_writer :
    3.26  	public k3d::mesh_writer<k3d::node >
    3.27 @@ -87,42 +87,42 @@
    3.28  		// Store polyhedra ...
    3.29  		for(k3d::mesh::primitives_t::const_iterator primitive = Input.primitives.begin(); primitive != Input.primitives.end(); ++primitive)
    3.30  		{
    3.31 -      boost::scoped_ptr<k3d::polyhedron::const_primitive> polyhedron(k3d::polyhedron::validate(Input, **primitive));
    3.32 -      if(!polyhedron)
    3.33 -        continue;
    3.34 +			boost::scoped_ptr<k3d::polyhedron::const_primitive> polyhedron(k3d::polyhedron::validate(Input, **primitive));
    3.35 +			if(!polyhedron)
    3.36 +				continue;
    3.37  
    3.38 -      const k3d::uint_t shell_begin = 0;
    3.39 -      const k3d::uint_t shell_end = shell_begin + polyhedron->shell_first_faces.size();
    3.40 -      for(k3d::uint_t shell = shell_begin; shell != shell_end; ++shell)
    3.41 -      {
    3.42 -        const k3d::uint_t face_begin = polyhedron->shell_first_faces[shell];
    3.43 -        const k3d::uint_t face_end = face_begin + polyhedron->shell_face_counts[shell];
    3.44 -        for(k3d::uint_t face = face_begin; face != face_end; ++face)
    3.45 -        {
    3.46 -          Output << "f";
    3.47 +			const k3d::uint_t shell_begin = 0;
    3.48 +			const k3d::uint_t shell_end = shell_begin + polyhedron->shell_first_faces.size();
    3.49 +			for(k3d::uint_t shell = shell_begin; shell != shell_end; ++shell)
    3.50 +			{
    3.51 +				const k3d::uint_t face_begin = polyhedron->shell_first_faces[shell];
    3.52 +				const k3d::uint_t face_end = face_begin + polyhedron->shell_face_counts[shell];
    3.53 +				for(k3d::uint_t face = face_begin; face != face_end; ++face)
    3.54 +				{
    3.55 +					Output << "f";
    3.56  
    3.57 -          const k3d::uint_t loop_begin = polyhedron->face_first_loops[face];
    3.58 -          const k3d::uint_t loop_end = loop_begin + polyhedron->face_loop_counts[face];
    3.59 -          for(k3d::uint_t loop = loop_begin; loop != loop_end; ++loop)
    3.60 -          {
    3.61 -            const k3d::uint_t first_edge = polyhedron->loop_first_edges[loop];
    3.62 -            for(k3d::uint_t edge = first_edge; ; )
    3.63 -            {
    3.64 -              Output << " " << polyhedron->vertex_points[edge] + 1; //specs want indices starting with 1
    3.65 +					const k3d::uint_t loop_begin = polyhedron->face_first_loops[face];
    3.66 +					const k3d::uint_t loop_end = loop_begin + polyhedron->face_loop_counts[face];
    3.67 +					for(k3d::uint_t loop = loop_begin; loop != loop_end; ++loop)
    3.68 +					{
    3.69 +						const k3d::uint_t first_edge = polyhedron->loop_first_edges[loop];
    3.70 +						for(k3d::uint_t edge = first_edge; ; )
    3.71 +						{
    3.72 +							Output << " " << polyhedron->vertex_points[edge] + 1; //specs want indices starting with 1
    3.73  
    3.74 -              edge = polyhedron->clockwise_edges[edge];
    3.75 -              if(edge == first_edge)
    3.76 -                break;
    3.77 -            }
    3.78 +							edge = polyhedron->clockwise_edges[edge];
    3.79 +							if(edge == first_edge)
    3.80 +								break;
    3.81 +						}
    3.82  
    3.83 -            /** \todo Support faces with holes */
    3.84 -            break;
    3.85 -          }
    3.86 +						/** \todo Support faces with holes */
    3.87 +						break;
    3.88 +					}
    3.89  
    3.90 -          Output << "\n";
    3.91 -        }
    3.92 -      }
    3.93 -    }
    3.94 +					Output << "\n";
    3.95 +				}
    3.96 +			}
    3.97 +		}
    3.98  	}
    3.99  };
   3.100  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/modules/ogre_io/CMakeLists.txt	Sat Oct 24 16:14:29 2009 -0600
     4.3 @@ -0,0 +1,3 @@
     4.4 +K3D_BUILD_MODULE(k3d-ogre-io)
     4.5 +K3D_CREATE_MODULE_PROXY(k3d-ogre-io)
     4.6 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/modules/ogre_io/mesh_writer.cpp	Sat Oct 24 16:14:29 2009 -0600
     5.3 @@ -0,0 +1,154 @@
     5.4 +#include <k3dsdk/document_plugin_factory.h>
     5.5 +#include <k3dsdk/fstream.h>
     5.6 +#include <k3dsdk/mesh_writer.h>
     5.7 +#include <k3dsdk/node.h>
     5.8 +#include <k3dsdk/polyhedron.h>
     5.9 +
    5.10 +#include <boost/scoped_ptr.hpp>
    5.11 +
    5.12 +namespace module
    5.13 +{
    5.14 +
    5.15 +namespace ogre
    5.16 +{
    5.17 +
    5.18 +namespace io
    5.19 +{
    5.20 +	
    5.21 +class mesh_writer :
    5.22 +	public k3d::mesh_writer<k3d::node>
    5.23 +{
    5.24 +	typedef k3d::mesh_writer<k3d::node> base;
    5.25 +	
    5.26 +public:
    5.27 +	mesh_writer(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
    5.28 +		base(Factory, Document)
    5.29 +	{
    5.30 +	}
    5.31 +
    5.32 +	static k3d::iplugin_factory& get_factory()
    5.33 +	{
    5.34 +		static k3d::document_plugin_factory<mesh_writer, k3d::interface_list<k3d::imesh_sink > > factory(
    5.35 +			k3d::uuid(0x1f8118fa, 0x4341982b, 0x642d068f, 0x0d3c70e4),
    5.36 +			"OgreXmlMeshWriter",
    5.37 +			_("Mesh writer that saves external Ogre XML Mesh files"),
    5.38 +			"MeshWriter",
    5.39 +			k3d::iplugin_factory::EXPERIMENTAL);
    5.40 +
    5.41 +		return factory;
    5.42 +	}
    5.43 +
    5.44 +private:
    5.45 +	void on_write_mesh(const k3d::mesh& Input, const k3d::filesystem::path& OutputPath, std::ostream& Output)
    5.46 +	{
    5.47 +		std::stringstream sstream;
    5.48 +	
    5.49 +		k3d::xml::element top("mesh");
    5.50 +		
    5.51 +		// Store points ...
    5.52 +		if(Input.points)
    5.53 +		{
    5.54 +			const k3d::mesh::points_t& points = *Input.points;
    5.55 +			
    5.56 +			k3d::xml::element shared_geom("sharedgeometry");
    5.57 +			sstream << points.size();
    5.58 +			shared_geom.append(k3d::xml::attribute("vertexcount", sstream.str()));
    5.59 +			sstream.str("");
    5.60 +			
    5.61 +			k3d::xml::element vertexbuffer("vertexbuffer");
    5.62 +			vertexbuffer.append(k3d::xml::attribute("positions", "true"));
    5.63 +			
    5.64 +			const size_t point_begin = 0;
    5.65 +			const size_t point_end = point_begin + points.size();
    5.66 +			for(size_t point = point_begin; point != point_end; ++point) {
    5.67 +				k3d::xml::element vertex("vertex");
    5.68 +				k3d::xml::element position("position");
    5.69 +				
    5.70 +				sstream << points[point][0];
    5.71 +				position.append(k3d::xml::attribute("x", sstream.str()));
    5.72 +				sstream.str("");
    5.73 +				
    5.74 +				sstream << points[point][1];
    5.75 +				position.append(k3d::xml::attribute("y", sstream.str()));
    5.76 +				sstream.str("");
    5.77 +				
    5.78 +				sstream << points[point][2];
    5.79 +				position.append(k3d::xml::attribute("z", sstream.str()));
    5.80 +				sstream.str("");
    5.81 +				
    5.82 +				vertex.append(position);
    5.83 +				vertexbuffer.append(vertex);
    5.84 +			}
    5.85 +			
    5.86 +			
    5.87 +			shared_geom.append(vertexbuffer);
    5.88 +			top.append(shared_geom);
    5.89 +		}
    5.90 +		
    5.91 +		// Store polyhedra ...
    5.92 +		for(k3d::mesh::primitives_t::const_iterator primitive = Input.primitives.begin(); primitive != Input.primitives.end(); ++primitive)
    5.93 +		{
    5.94 +			boost::scoped_ptr<k3d::polyhedron::const_primitive> polyhedron(k3d::polyhedron::validate(Input, **primitive));
    5.95 +			if(!polyhedron)
    5.96 +				continue;
    5.97 +
    5.98 +			if(!k3d::polyhedron::is_triangles(*polyhedron))
    5.99 +			{
   5.100 +				k3d::log() << warning << factory().name() << " cannot export polyhedron containing non-triangles." << std::endl;
   5.101 +				continue;
   5.102 +			}
   5.103 +
   5.104 +			const k3d::mesh::indices_t& face_first_loops = polyhedron->face_first_loops;
   5.105 +			const k3d::mesh::indices_t& loop_first_edges = polyhedron->loop_first_edges;
   5.106 +			const k3d::mesh::indices_t& clockwise_edges = polyhedron->clockwise_edges;
   5.107 +			const k3d::mesh::indices_t& vertex_points = polyhedron->vertex_points;
   5.108 +
   5.109 +			k3d::xml::element submeshes("submeshes");
   5.110 +			k3d::xml::element submesh("submesh");
   5.111 +			k3d::xml::element faces("faces");
   5.112 +			
   5.113 +			sstream << face_first_loops.size();
   5.114 +			faces.append(k3d::xml::attribute("count", sstream.str()));
   5.115 +			sstream.str("");
   5.116 +			
   5.117 +			const k3d::uint_t face_begin = 0;
   5.118 +			const k3d::uint_t face_end = face_begin + face_first_loops.size();
   5.119 +			for(k3d::uint_t face = face_begin; face != face_end; ++face)
   5.120 +			{
   5.121 +				k3d::uint_t first_edge = loop_first_edges[face_first_loops[face]];
   5.122 +				k3d::uint_t edge = first_edge;
   5.123 +				k3d::xml::element face_xml("face");
   5.124 +				for(k3d::uint_t i = 1; i < 4; i++) {
   5.125 +					sstream << 'v' << i;
   5.126 +					std::string name(sstream.str());
   5.127 +					sstream.str("");
   5.128 +					sstream << vertex_points[edge];
   5.129 +					std::string value(sstream.str());
   5.130 +					sstream.str("");
   5.131 +					face_xml.append(k3d::xml::attribute(name, value));
   5.132 +					edge = clockwise_edges[edge];
   5.133 +				}
   5.134 +				faces.append(face_xml);
   5.135 +			}
   5.136 +			
   5.137 +			submesh.append(faces);
   5.138 +			submeshes.append(submesh);
   5.139 +			top.append(submeshes);
   5.140 +			
   5.141 +		}
   5.142 +		
   5.143 +		Output << k3d::xml::declaration() <<  top;
   5.144 +	}
   5.145 +};
   5.146 +
   5.147 +k3d::iplugin_factory& mesh_writer_factory()
   5.148 +{
   5.149 +	return mesh_writer::get_factory();
   5.150 +}
   5.151 +
   5.152 +} // namespace io
   5.153 +
   5.154 +} // namespace ogre
   5.155 +
   5.156 +} // namespace module
   5.157 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/modules/ogre_io/module.cpp	Sat Oct 24 16:14:29 2009 -0600
     6.3 @@ -0,0 +1,22 @@
     6.4 +#include <k3dsdk/module.h>
     6.5 +
     6.6 +namespace module
     6.7 +{
     6.8 +	
     6.9 +namespace ogre
    6.10 +{
    6.11 +
    6.12 +namespace io
    6.13 +{
    6.14 +	
    6.15 +extern k3d::iplugin_factory& mesh_writer_factory();
    6.16 +
    6.17 +} // namespace io
    6.18 +
    6.19 +} // namespace ogre
    6.20 +
    6.21 +} // namespace module
    6.22 +
    6.23 +K3D_MODULE_START(Registry)
    6.24 +	Registry.register_factory(module::ogre::io::mesh_writer_factory());
    6.25 +K3D_MODULE_END