From ca514b559b22703b07efbb3ba988d8aa9d426c8d Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 14 Dec 2010 22:18:11 +0000 Subject: [PATCH] add -points, fix some obj2egg issues --- pandatool/src/eggbase/eggBase.cxx | 14 +++++ pandatool/src/eggbase/eggBase.h | 3 + pandatool/src/eggbase/eggMultiBase.cxx | 7 +++ pandatool/src/eggbase/eggWriter.cxx | 5 ++ pandatool/src/objegg/objToEggConverter.cxx | 59 ++++++++++++++++--- pandatool/src/objegg/objToEggConverter.h | 4 ++ pandatool/src/objprogs/objToEgg.cxx | 1 + .../src/ptloader/loaderFileTypePandatool.cxx | 4 +- 8 files changed, 87 insertions(+), 10 deletions(-) diff --git a/pandatool/src/eggbase/eggBase.cxx b/pandatool/src/eggbase/eggBase.cxx index 6837d3b16a..0c98141c27 100644 --- a/pandatool/src/eggbase/eggBase.cxx +++ b/pandatool/src/eggbase/eggBase.cxx @@ -40,6 +40,7 @@ EggBase() { _got_tbnall = false; _got_tbnauto = false; + _make_points = false; _got_transform = false; _transform = LMatrix4d::ident_mat(); @@ -109,6 +110,19 @@ add_normals_options() { &EggBase::dispatch_none, &_got_tbnauto); } +//////////////////////////////////////////////////////////////////// +// Function: EggBase::add_points_options +// Access: Public +// Description: Adds -points as a valid option for this program. +//////////////////////////////////////////////////////////////////// +void EggBase:: +add_points_options() { + add_option + ("points", "", 46, + "Construct entries for any unreferenced vertices, to make them visible.", + &EggBase::dispatch_none, &_make_points); +} + //////////////////////////////////////////////////////////////////// // Function: EggBase::add_transform_options // Access: Public diff --git a/pandatool/src/eggbase/eggBase.h b/pandatool/src/eggbase/eggBase.h index 038aa38569..ad28803ddf 100644 --- a/pandatool/src/eggbase/eggBase.h +++ b/pandatool/src/eggbase/eggBase.h @@ -34,6 +34,7 @@ public: EggBase(); void add_normals_options(); + void add_points_options(); void add_transform_options(); static void convert_paths(EggNode *node, PathReplace *path_replace, @@ -65,6 +66,8 @@ protected: vector_string _tbn_names; bool _got_tbnall; bool _got_tbnauto; + + bool _make_points; bool _got_transform; LMatrix4d _transform; diff --git a/pandatool/src/eggbase/eggMultiBase.cxx b/pandatool/src/eggbase/eggMultiBase.cxx index f47a5a0dd6..27513e6dfe 100644 --- a/pandatool/src/eggbase/eggMultiBase.cxx +++ b/pandatool/src/eggbase/eggMultiBase.cxx @@ -75,6 +75,13 @@ post_process_egg_files() { } } + if (_make_points) { + nout << "Making points\n"; + for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) { + (*ei)->make_point_primitives(); + } + } + switch (_normals_mode) { case NM_strip: nout << "Stripping normals.\n"; diff --git a/pandatool/src/eggbase/eggWriter.cxx b/pandatool/src/eggbase/eggWriter.cxx index ccd41097c2..1e95e94056 100644 --- a/pandatool/src/eggbase/eggWriter.cxx +++ b/pandatool/src/eggbase/eggWriter.cxx @@ -135,6 +135,11 @@ post_process_egg_file() { _data->transform(_transform); } + if (_make_points) { + nout << "Making points\n"; + _data->make_point_primitives(); + } + bool needs_remove = false; switch (_normals_mode) { diff --git a/pandatool/src/objegg/objToEggConverter.cxx b/pandatool/src/objegg/objToEggConverter.cxx index 6249b34eb1..358e222bbe 100755 --- a/pandatool/src/objegg/objToEggConverter.cxx +++ b/pandatool/src/objegg/objToEggConverter.cxx @@ -19,6 +19,7 @@ #include "streamReader.h" #include "virtualFileSystem.h" #include "eggPolygon.h" +#include "dcast.h" //////////////////////////////////////////////////////////////////// // Function: ObjToEggConverter::Constructor @@ -130,12 +131,15 @@ process(const Filename &filename) { return false; } - _vi = 0; - _vti = 0; - _vni = 0; + _vi = 1; + _vti = 1; + _vni = 1; _vpool = new EggVertexPool("vpool"); _egg_data->add_child(_vpool); + _root_group = new EggGroup("root"); + _egg_data->add_child(_root_group); + _current_group = _root_group; StreamReader sr(strm, true); string line = sr.readline(); @@ -143,10 +147,12 @@ process(const Filename &filename) { while (!line.empty()) { line = trim(line); if (line.empty()) { + line = sr.readline(); continue; } if (line[0] == '#') { + line = sr.readline(); continue; } @@ -180,6 +186,8 @@ process_line(const string &line) { return process_vn(words); } else if (tag == "f") { return process_f(words); + } else if (tag == "g") { + return process_g(words); } else { bool inserted = _ignored_tags.insert(tag).second; if (!inserted) { @@ -250,13 +258,13 @@ process_vt(vector_string &words) { return false; } - EggVertex *vertex = get_vertex(_vi); + EggVertex *vertex = get_vertex(_vti); if (words.size() == 4) { vertex->set_uvw("", uvw); } else { vertex->set_uv("", TexCoordd(uvw[0], uvw[1])); } - ++_vi; + ++_vti; return true; } @@ -287,9 +295,9 @@ process_vn(vector_string &words) { } normal.normalize(); - EggVertex *vertex = get_vertex(_vi); + EggVertex *vertex = get_vertex(_vni); vertex->set_normal(normal); - ++_vi; + ++_vni; return true; } @@ -297,7 +305,7 @@ process_vn(vector_string &words) { //////////////////////////////////////////////////////////////////// // Function: ObjToEggConverter::process_f // Access: Protected -// Description: +// Description: Defines a face in the obj file. //////////////////////////////////////////////////////////////////// bool ObjToEggConverter:: process_f(vector_string &words) { @@ -309,11 +317,40 @@ process_f(vector_string &words) { } poly->add_vertex(vertex); } - _egg_data->add_child(poly); + _current_group->add_child(poly); return true; } +//////////////////////////////////////////////////////////////////// +// Function: ObjToEggConverter::process_g +// Access: Protected +// Description: Defines a group in the obj file. +//////////////////////////////////////////////////////////////////// +bool ObjToEggConverter:: +process_g(vector_string &words) { + EggGroup *group = _root_group; + + // We assume the group names define a hierarchy of more-specific to + // less-specific group names, so that the first group name is the + // bottommost node, and the last group name is the topmost node. + + // Thus, iterate from the back to the front. + size_t i = words.size(); + while (i != 0) { + --i; + EggNode *child = group->find_child(words[i]); + if (child == NULL || !child->is_of_type(EggGroup::get_class_type())) { + child = new EggGroup(words[i]); + group->add_child(child); + } + group = DCAST(EggGroup, child); + } + + _current_group = group; + return true; +} + //////////////////////////////////////////////////////////////////// // Function: ObjToEggConverter::get_vertex // Access: Protected @@ -322,6 +359,10 @@ process_f(vector_string &words) { //////////////////////////////////////////////////////////////////// EggVertex *ObjToEggConverter:: get_vertex(int n) { + if (n < 0) { + // A negative index means to count backward from the end. + n = _vi + n; + } EggVertex *vertex = _vpool->get_vertex(n); if (vertex == NULL) { vertex = new EggVertex; diff --git a/pandatool/src/objegg/objToEggConverter.h b/pandatool/src/objegg/objToEggConverter.h index 15c9657e71..1d1f56ef17 100755 --- a/pandatool/src/objegg/objToEggConverter.h +++ b/pandatool/src/objegg/objToEggConverter.h @@ -19,6 +19,7 @@ #include "somethingToEggConverter.h" #include "eggVertexPool.h" +#include "eggGroup.h" //////////////////////////////////////////////////////////////////// // Class : ObjToEggConverter @@ -46,6 +47,7 @@ protected: bool process_vt(vector_string &words); bool process_vn(vector_string &words); bool process_f(vector_string &words); + bool process_g(vector_string &words); EggVertex *get_vertex(int n); EggVertex *get_face_vertex(const string &face_reference); @@ -53,6 +55,8 @@ protected: int _line_number; int _vi, _vti, _vni; PT(EggVertexPool) _vpool; + PT(EggGroup) _root_group; + EggGroup *_current_group; pset _ignored_tags; }; diff --git a/pandatool/src/objprogs/objToEgg.cxx b/pandatool/src/objprogs/objToEgg.cxx index 52f51f49af..1e175b7a82 100755 --- a/pandatool/src/objprogs/objToEgg.cxx +++ b/pandatool/src/objprogs/objToEgg.cxx @@ -27,6 +27,7 @@ ObjToEgg() : SomethingToEgg("obj", ".obj") { add_units_options(); + add_points_options(); add_normals_options(); add_transform_options(); diff --git a/pandatool/src/ptloader/loaderFileTypePandatool.cxx b/pandatool/src/ptloader/loaderFileTypePandatool.cxx index f44ce5f5d6..81af8232e7 100644 --- a/pandatool/src/ptloader/loaderFileTypePandatool.cxx +++ b/pandatool/src/ptloader/loaderFileTypePandatool.cxx @@ -147,7 +147,9 @@ load_file(const Filename &path, const LoaderOptions &options, egg_data->transform(LMatrix4d::scale_mat(scale)); } - if (!egg_data->has_normals()) { + if (!egg_data->has_primitives()) { + egg_data->make_point_primitives(); + } else if (!egg_data->has_normals()) { egg_data->recompute_polygon_normals(); }