From 096550f163648572b0b9f5c5bc13fc007d8d51ca Mon Sep 17 00:00:00 2001 From: curt Date: Thu, 3 May 2001 01:58:12 +0000 Subject: [PATCH] Initial revision. --- simgear/io/Makefile.am | 12 +++- simgear/io/decode_binobj.cxx | 107 +++++++++++++++++++++++++++++++++++ simgear/io/sg_binobj.cxx | 18 +++--- simgear/io/sg_binobj.hxx | 4 ++ 4 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 simgear/io/decode_binobj.cxx diff --git a/simgear/io/Makefile.am b/simgear/io/Makefile.am index d6d53c4c..d566f9fa 100644 --- a/simgear/io/Makefile.am +++ b/simgear/io/Makefile.am @@ -20,7 +20,7 @@ libsgio_a_SOURCES = \ INCLUDES += -I$(top_srcdir) -noinst_PROGRAMS = socktest lowtest +noinst_PROGRAMS = decode_binobj socktest lowtest socktest_SOURCES = socktest.cxx @@ -31,4 +31,12 @@ socktest_LDADD = \ lowtest_SOURCES = lowtest.cxx lowtest_LDADD = \ - $(top_builddir)/simgear/io/libsgio.a \ No newline at end of file + $(top_builddir)/simgear/io/libsgio.a + +decode_binobj_SOURCES = decode_binobj.cxx + +decode_binobj_LDADD = \ + $(top_builddir)/simgear/io/libsgio.a \ + $(top_builddir)/simgear/bucket/libsgbucket.a \ + $(top_builddir)/simgear/misc/libsgmisc.a \ + -lz \ No newline at end of file diff --git a/simgear/io/decode_binobj.cxx b/simgear/io/decode_binobj.cxx new file mode 100644 index 00000000..1af14819 --- /dev/null +++ b/simgear/io/decode_binobj.cxx @@ -0,0 +1,107 @@ +#include + +#include "sg_binobj.hxx" + + +int main( int argc, char **argv ) { + int i, j; + + // check usage + if ( argc != 2 ) { + cout << "Usage: " << argv[0] << " binary_obj_file" << endl; + } + + SGBinObject obj; + bool result = obj.read_bin( argv[1] ); + if ( !result ) { + cout << "error loading: " << argv[1] << endl; + exit(-1); + } + + cout << "# FGFS Scenery" << endl; + cout << "# Version " << obj.get_version() << endl; + cout << endl; + + printf("# gbs %.5f %.5f %.5f %.2f\n", obj.get_gbs_center().x(), + obj.get_gbs_center().y(), obj.get_gbs_center().z(), + obj.get_gbs_radius()); + cout << endl; + + point_list nodes = obj.get_wgs84_nodes(); + cout << "# vertex list" << endl; + for ( i = 0; i < (int)nodes.size(); ++i ) { + printf("v %.5f %.5f %.5f\n", nodes[i].x(), nodes[i].y(), nodes[i].z() ); + } + cout << endl; + + point_list normals = obj.get_normals(); + cout << "# vertex normal list" << endl; + for ( i = 0; i < (int)normals.size(); ++i ) { + printf("vn %.5f %.5f %.5f\n", + normals[i].x(), normals[i].y(), normals[i].z() ); + } + cout << endl; + + point_list texcoords = obj.get_texcoords(); + cout << "# texture coordinate list" << endl; + for ( i = 0; i < (int)texcoords.size(); ++i ) { + printf("vt %.5f %.5f\n", + texcoords[i].x(), texcoords[i].y() ); + } + cout << endl; + + cout << "# triangle groups" << endl; + cout << endl; + + string material; + int_list vertex_index; + int_list tex_index; + + // generate triangles + string_list tri_materials = obj.get_tri_materials(); + group_list tris_v = obj.get_tris_v(); + group_list tris_tc = obj.get_tris_tc(); + for ( i = 0; i < (int)tris_v.size(); ++i ) { + material = tri_materials[i]; + vertex_index = tris_v[i]; + tex_index = tris_tc[i]; + cout << "# usemtl " << material << endl; + cout << "f "; + for ( j = 0; j < (int)vertex_index.size(); ++j ) { + cout << vertex_index[j] << "/" << tex_index[j] << " "; + } + cout << endl; + } + + // generate strips + string_list strip_materials = obj.get_strip_materials(); + group_list strips_v = obj.get_strips_v(); + group_list strips_tc = obj.get_strips_tc(); + for ( i = 0; i < (int)strips_v.size(); ++i ) { + material = strip_materials[i]; + vertex_index = strips_v[i]; + tex_index = strips_tc[i]; + cout << "# usemtl " << material << endl; + cout << "ts "; + for ( j = 0; j < (int)vertex_index.size(); ++j ) { + cout << vertex_index[j] << "/" << tex_index[j] << " "; + } + cout << endl; + } + + // generate fans + string_list fan_materials = obj.get_fan_materials(); + group_list fans_v = obj.get_fans_v(); + group_list fans_tc = obj.get_fans_tc(); + for ( i = 0; i < (int)fans_v.size(); ++i ) { + material = fan_materials[i]; + vertex_index = fans_v[i]; + tex_index = fans_tc[i]; + cout << "# usemtl " << material << endl; + cout << "tf "; + for ( j = 0; j < (int)vertex_index.size(); ++j ) { + cout << vertex_index[j] << "/" << tex_index[j] << " "; + } + cout << endl; + } +} diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index df30fe4e..237f24d9 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -192,7 +192,6 @@ bool SGBinObject::read_bin( const string& file ) { // read headers unsigned int header; - unsigned short version; sgReadUInt( fp, &header ); if ( ((header & 0xFF000000) >> 24) == 'S' && ((header & 0x00FF0000) >> 16) == 'G' ) { @@ -327,9 +326,14 @@ bool SGBinObject::read_bin( const string& file ) { sgReadBytes( fp, nbytes, ptr ); int count = nbytes / 3; for ( k = 0; k < count; ++k ) { - p = Point3D( ptr[0] / 128.0 - 1.0, - ptr[1] / 128.0 - 1.0, - ptr[2] / 128.0 - 1.0 ); + sgdVec3 normal; + sgdSetVec3( normal, + (ptr[0]) / 127.5 - 1.0, + (ptr[1]) / 127.5 - 1.0, + (ptr[2]) / 127.5 - 1.0 ); + sgdNormalizeVec3( normal ); + + p = Point3D( normal[0], normal[1], normal[2] ); // cout << "normal = " << p << endl; normals.push_back( p ); ptr += 3; @@ -668,9 +672,9 @@ bool SGBinObject::write_bin( const string& base, const string& name, char normal[3]; for ( i = 0; i < (int)normals.size(); ++i ) { p = normals[i]; - normal[0] = (unsigned char)((p.x() + 1.0) * 128); - normal[1] = (unsigned char)((p.y() + 1.0) * 128); - normal[2] = (unsigned char)((p.z() + 1.0) * 128); + normal[0] = (unsigned char)((p.x() + 1.0) * 127.5); + normal[1] = (unsigned char)((p.y() + 1.0) * 127.5); + normal[2] = (unsigned char)((p.z() + 1.0) * 127.5); sgWriteBytes( fp, 3, normal ); } diff --git a/simgear/io/sg_binobj.hxx b/simgear/io/sg_binobj.hxx index dc0bdd14..5809cf08 100644 --- a/simgear/io/sg_binobj.hxx +++ b/simgear/io/sg_binobj.hxx @@ -91,6 +91,8 @@ typedef group_list::const_iterator const_group_list_iterator; * - vertex: FLOAT, FLOAT, FLOAT */ class SGBinObject { + unsigned short version; + Point3D gbs_center; float gbs_radius; point_list wgs84_nodes; @@ -108,6 +110,8 @@ class SGBinObject { public: + inline unsigned short get_version() const { return version; } + inline Point3D get_gbs_center() const { return gbs_center; } inline void set_gbs_center( Point3D p ) { gbs_center = p; } -- 2.39.5