Skip to content
Snippets Groups Projects
Commit aa1b5d11 authored by Ansgar Philippsen's avatar Ansgar Philippsen
Browse files

fixed scene exporter to allow reorientation to origin

parent ff322c50
Branches
Tags
No related merge requests found
...@@ -394,3 +394,16 @@ main_mod.scene.Stereo=Stereo ...@@ -394,3 +394,16 @@ main_mod.scene.Stereo=Stereo
import ost as ost_mod import ost as ost_mod
ost_mod.scene=Scene() ost_mod.scene=Scene()
ost_mod.scene.Stereo=Stereo ost_mod.scene.Stereo=Stereo
def GostExporter(file,scale=1.0,to_origin=True):
e=GostExporter_(file)
e.scale=scale
e.to_origin=to_origin
return e
def ColladaExporter(file,scale=1.0,to_origin=True):
e=ColladaExporter_(file)
e.scale=scale
e.to_origin=to_origin
return e
...@@ -28,11 +28,16 @@ using namespace ost::gfx; ...@@ -28,11 +28,16 @@ using namespace ost::gfx;
void export_Exporter() void export_Exporter()
{ {
class_<Exporter, boost::noncopyable>("Exporter", no_init); class_<Exporter, boost::noncopyable>("Exporter", no_init)
.add_property("scale",&Exporter::GetScale,&Exporter::SetScale)
.add_property("to_origin",&Exporter::GetToOrigin,&Exporter::SetToOrigin)
;
class_<GostExporter, bases<Exporter>, boost::noncopyable>("GostExporter", init<const std::string&>()) // internal class, factory function in __init__.py
class_<GostExporter, bases<Exporter>, boost::noncopyable>("GostExporter_", init<const std::string&>())
; ;
class_<ColladaExporter, bases<Exporter>, boost::noncopyable>("ColladaExporter", init<const std::string&, optional<float> >()) // internal class, factory function in __init__.py
class_<ColladaExporter, bases<Exporter>, boost::noncopyable>("ColladaExporter_", init<const std::string&>())
; ;
} }
...@@ -94,6 +94,7 @@ endif() ...@@ -94,6 +94,7 @@ endif()
set(OST_GFX_SOURCES set(OST_GFX_SOURCES
bitmap_io.cc bitmap_io.cc
exporter.cc
collada_exporter.cc collada_exporter.cc
color.cc color.cc
primitives.cc primitives.cc
......
...@@ -29,10 +29,10 @@ ...@@ -29,10 +29,10 @@
namespace ost { namespace gfx { namespace ost { namespace gfx {
ColladaExporter::ColladaExporter(const std::string& file, float scale): ColladaExporter::ColladaExporter(const std::string& file):
Exporter(),
file_(file), file_(file),
out_(file_.c_str()), out_(file_.c_str()),
scale_(scale),
obj_() obj_()
{ {
out_ << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; out_ << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
...@@ -75,13 +75,23 @@ void ColladaExporter::SceneEnd(const Scene* scene) ...@@ -75,13 +75,23 @@ void ColladaExporter::SceneEnd(const Scene* scene)
out_ << " </node>\n"; out_ << " </node>\n";
out_ << " <node id=\"Root\" name=\"Root\">\n"; out_ << " <node id=\"Root\" name=\"Root\">\n";
out_ << " <matrix>\n"; if(GetToOrigin()) {
geom::Mat4 tm=scene->GetTransform().GetMatrix(); out_ << " <matrix>\n";
out_ << " " << tm(0,0) << " " << tm(0,1) << " " << tm(0,2) << " " << tm(0,3) << "\n"; geom::Vec3 cen=scene->GetTransform().GetCenter();
out_ << " " << tm(1,0) << " " << tm(1,1) << " " << tm(1,2) << " " << tm(1,3) << "\n"; out_ << " 1 0 0 " << -cen[0] << "\n";
out_ << " " << tm(2,0) << " " << tm(2,1) << " " << tm(2,2) << " " << tm(2,3) << "\n"; out_ << " 0 1 0 " << -cen[1] << "\n";
out_ << " " << tm(3,0) << " " << tm(3,1) << " " << tm(3,2) << " " << tm(3,3) << "\n"; out_ << " 0 0 1 " << -cen[2] << "\n";
out_ << " </matrix>\n"; out_ << " 0 0 0 1\n";
out_ << " </matrix>\n";
} else {
out_ << " <matrix>\n";
geom::Mat4 tm=scene->GetTransform().GetMatrix();
out_ << " " << tm(0,0) << " " << tm(0,1) << " " << tm(0,2) << " " << tm(0,3) << "\n";
out_ << " " << tm(1,0) << " " << tm(1,1) << " " << tm(1,2) << " " << tm(1,3) << "\n";
out_ << " " << tm(2,0) << " " << tm(2,1) << " " << tm(2,2) << " " << tm(2,3) << "\n";
out_ << " " << tm(3,0) << " " << tm(3,1) << " " << tm(3,2) << " " << tm(3,3) << "\n";
out_ << " </matrix>\n";
}
for(std::vector<std::string>::const_iterator oit=obj_.begin();oit!=obj_.end();++oit) { for(std::vector<std::string>::const_iterator oit=obj_.begin();oit!=obj_.end();++oit) {
out_ << " <node id=\"" << *oit << "\" name=\"" << *oit <<"\">\n"; out_ << " <node id=\"" << *oit << "\" name=\"" << *oit <<"\">\n";
out_ << " <instance_geometry url=\"#" << *oit << "\"/>\n"; out_ << " <instance_geometry url=\"#" << *oit << "\"/>\n";
...@@ -126,10 +136,13 @@ void ColladaExporter::WriteVertexData(const float* vdata, ...@@ -126,10 +136,13 @@ void ColladaExporter::WriteVertexData(const float* vdata,
out_ << " <float_array count=\"" << count*3 << "\" id=\"" << name+"-Positions-array" << "\">\n"; out_ << " <float_array count=\"" << count*3 << "\" id=\"" << name+"-Positions-array" << "\">\n";
if(vdata) { if(vdata) {
const float* src=vdata; const float* src=vdata;
float tmpv[3];
for(unsigned int i=0;i<count;++i) { for(unsigned int i=0;i<count;++i) {
out_ << scale_*src[0] << " "; tmpv[0]=src[0]; tmpv[1]=src[1]; tmpv[2]=src[2];
out_ << scale_*src[1] << " "; TransformPosition(tmpv);
out_ << scale_*src[2] << " "; out_ << tmpv[0] << " ";
out_ << tmpv[1] << " ";
out_ << tmpv[2] << " ";
src+=stride; src+=stride;
} }
} else { } else {
...@@ -152,10 +165,13 @@ void ColladaExporter::WriteVertexData(const float* vdata, ...@@ -152,10 +165,13 @@ void ColladaExporter::WriteVertexData(const float* vdata,
out_ << " <float_array count=\"" << count*3 << "\" id=\"" << name+"-Normals-array" << "\">\n"; out_ << " <float_array count=\"" << count*3 << "\" id=\"" << name+"-Normals-array" << "\">\n";
if(ndata) { if(ndata) {
const float* src=ndata; const float* src=ndata;
float tmpn[3];
for(unsigned int i=0;i<count;++i) { for(unsigned int i=0;i<count;++i) {
out_ << src[0] << " "; tmpn[0]=src[0]; tmpn[1]=src[1]; tmpn[2]=src[2];
out_ << src[1] << " "; TransformNormal(tmpn);
out_ << src[2] << " "; out_ << tmpn[0] << " ";
out_ << tmpn[1] << " ";
out_ << tmpn[2] << " ";
src+=stride; src+=stride;
} }
} else { } else {
......
...@@ -35,7 +35,7 @@ namespace ost { namespace gfx { ...@@ -35,7 +35,7 @@ namespace ost { namespace gfx {
class DLLEXPORT_OST_GFX ColladaExporter: public Exporter class DLLEXPORT_OST_GFX ColladaExporter: public Exporter
{ {
public: public:
ColladaExporter(const std::string& collada_file, float scale=1.0); ColladaExporter(const std::string& collada_file);
virtual ~ColladaExporter(); virtual ~ColladaExporter();
// exporter interface // exporter interface
...@@ -53,7 +53,6 @@ public: ...@@ -53,7 +53,6 @@ public:
private: private:
std::string file_; std::string file_;
std::ofstream out_; std::ofstream out_;
float scale_;
std::vector<std::string> obj_; std::vector<std::string> obj_;
}; };
......
...@@ -19,6 +19,11 @@ ...@@ -19,6 +19,11 @@
#ifndef OST_GFX_EXPORTER_HH #ifndef OST_GFX_EXPORTER_HH
#define OST_GFX_EXPORTER_HH #define OST_GFX_EXPORTER_HH
#include <ost/geom/vec3.hh>
#include <ost/geom/vec4.hh>
#include <ost/geom/mat3.hh>
#include <ost/geom/mat4.hh>
#include <ost/gfx/module_config.hh> #include <ost/gfx/module_config.hh>
namespace ost { namespace gfx { namespace ost { namespace gfx {
...@@ -34,6 +39,10 @@ public: ...@@ -34,6 +39,10 @@ public:
OBJ=3 OBJ=3
}; };
Exporter() :
scale_(1.0),
to_origin_(true)
{}
virtual ~Exporter() {} virtual ~Exporter() {}
virtual void SceneStart(const Scene* scene) {} virtual void SceneStart(const Scene* scene) {}
virtual void SceneEnd(const Scene* scene) {} virtual void SceneEnd(const Scene* scene) {}
...@@ -49,8 +58,32 @@ public: ...@@ -49,8 +58,32 @@ public:
virtual void WriteLineData(const unsigned int* ij, size_t count) {} virtual void WriteLineData(const unsigned int* ij, size_t count) {}
virtual void WriteTriData(const unsigned int* ijk, size_t count) {} virtual void WriteTriData(const unsigned int* ijk, size_t count) {}
virtual void WriteQuadData(const unsigned int* ijkl, size_t count) {} virtual void WriteQuadData(const unsigned int* ijkl, size_t count) {}
// scale positions for absolute data formats (like dae)
void SetScale(float s) {scale_=s;}
float GetScale() const {return scale_;}
// if true (default), re-orient so that center is at (0,0,0)
// and viewing direction is along z
// (basically apply modelview matrix)
void SetToOrigin(bool b) {to_origin_=b;}
bool GetToOrigin() const {return to_origin_;}
// used by Scene::Export
void SetupTransform(const Scene* scene);
// used by WriteVertexData in derived classes
// modifies input arg!!
void TransformPosition(float* p) const;
void TransformNormal(float* n) const;
private:
float scale_;
bool to_origin_;
geom::Mat4 vertex_tf_;
geom::Mat3 normal_tf_;
}; };
}} // ns }} // ns
#endif #endif
...@@ -57,6 +57,7 @@ namespace ost { namespace gfx { ...@@ -57,6 +57,7 @@ namespace ost { namespace gfx {
} }
GostExporter::GostExporter(const std::string& fname): GostExporter::GostExporter(const std::string& fname):
Exporter(),
file_(0) file_(0)
{ {
file_=fopen(fname.c_str(),"w"); file_=fopen(fname.c_str(),"w");
......
...@@ -1784,6 +1784,7 @@ void Scene::ExportPov(const std::string& fname, const std::string& wdir) ...@@ -1784,6 +1784,7 @@ void Scene::ExportPov(const std::string& fname, const std::string& wdir)
void Scene::Export(Exporter* ex) const void Scene::Export(Exporter* ex) const
{ {
ex->SetupTransform(this);
ex->SceneStart(this); ex->SceneStart(this);
root_node_->Export(ex); root_node_->Export(ex);
ex->SceneEnd(this); ex->SceneEnd(this);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment