From f9e03850ddcc2be24390b889144d5b724a35562c Mon Sep 17 00:00:00 2001 From: David Rose Date: Mon, 24 Dec 2012 19:47:31 +0000 Subject: [PATCH] support drz's 'xvt' obj tag --- pandatool/src/objegg/objToEggConverter.cxx | 82 ++++++++++++++++++++++ pandatool/src/objegg/objToEggConverter.h | 6 +- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/pandatool/src/objegg/objToEggConverter.cxx b/pandatool/src/objegg/objToEggConverter.cxx index efffa5ca81..bb73ae594d 100755 --- a/pandatool/src/objegg/objToEggConverter.cxx +++ b/pandatool/src/objegg/objToEggConverter.cxx @@ -133,7 +133,9 @@ process(const Filename &filename) { _vi = 1; _vti = 1; + _xvti = 1; _vni = 1; + _ref_plane_res.set(1.0, 1.0); _vpool = new EggVertexPool("vpool"); _egg_data->add_child(_vpool); @@ -151,6 +153,12 @@ process(const Filename &filename) { continue; } + if (line.substr(0, 15) == "#_ref_plane_res") { + process_ref_plane_res(line); + line = sr.readline(); + continue; + } + if (line[0] == '#') { line = sr.readline(); continue; @@ -182,6 +190,8 @@ process_line(const string &line) { return process_v(words); } else if (tag == "vt") { return process_vt(words); + } else if (tag == "xvt") { + return process_xvt(words); } else if (tag == "vn") { return process_vn(words); } else if (tag == "f") { @@ -199,6 +209,41 @@ process_line(const string &line) { return true; } +//////////////////////////////////////////////////////////////////// +// Function: ObjToEggConverter::process_line +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +bool ObjToEggConverter:: +process_ref_plane_res(const string &line) { + // the #_ref_plane_res line is a DRZ extension that defines the + // pixel resolution of the projector device. It's needed to + // properly scale the xvt lines. + + vector_string words; + tokenize(line, words, " \t", true); + nassertr(!words.empty(), false); + + if (words.size() != 3) { + objegg_cat.error() + << "Wrong number of tokens at line " << _line_number << "\n"; + return false; + } + + bool okflag = true; + LPoint3d pos; + okflag &= string_to_double(words[1], _ref_plane_res[0]); + okflag &= string_to_double(words[2], _ref_plane_res[1]); + + if (!okflag) { + objegg_cat.error() + << "Invalid number at line " << _line_number << "\n"; + return false; + } + + return true; +} + //////////////////////////////////////////////////////////////////// // Function: ObjToEggConverter::process_v // Access: Protected @@ -286,6 +331,43 @@ process_vt(vector_string &words) { return true; } +//////////////////////////////////////////////////////////////////// +// Function: ObjToEggConverter::process_xvt +// Access: Protected +// Description: "xvt" is an extended column invented by DRZ. It +// includes texture coordinates in pixel space of the +// projector device, as well as for each camera. We map +// it to the nominal texture coordinates here. +//////////////////////////////////////////////////////////////////// +bool ObjToEggConverter:: +process_xvt(vector_string &words) { + if (words.size() < 3) { + objegg_cat.error() + << "Wrong number of tokens at line " << _line_number << "\n"; + return false; + } + + bool okflag = true; + LTexCoordd uv; + okflag &= string_to_double(words[1], uv[0]); + okflag &= string_to_double(words[2], uv[1]); + + if (!okflag) { + objegg_cat.error() + << "Invalid number at line " << _line_number << "\n"; + return false; + } + + uv[0] /= _ref_plane_res[0]; + uv[1] = 1.0 - uv[1] / _ref_plane_res[1]; + + EggVertex *vertex = get_vertex(_xvti); + vertex->set_uv("", uv); + ++_xvti; + + return true; +} + //////////////////////////////////////////////////////////////////// // Function: ObjToEggConverter::process_vn // Access: Protected diff --git a/pandatool/src/objegg/objToEggConverter.h b/pandatool/src/objegg/objToEggConverter.h index ebbbde25e9..f798b7a762 100755 --- a/pandatool/src/objegg/objToEggConverter.h +++ b/pandatool/src/objegg/objToEggConverter.h @@ -42,9 +42,11 @@ public: protected: bool process(const Filename &filename); bool process_line(const string &line); + bool process_ref_plane_res(const string &line); bool process_v(vector_string &words); bool process_vt(vector_string &words); + bool process_xvt(vector_string &words); bool process_vn(vector_string &words); bool process_f(vector_string &words); bool process_g(vector_string &words); @@ -53,11 +55,13 @@ protected: EggVertex *get_face_vertex(const string &face_reference); int _line_number; - int _vi, _vti, _vni; + int _vi, _vti, _xvti, _vni; PT(EggVertexPool) _vpool; PT(EggGroup) _root_group; EggGroup *_current_group; + LVecBase2d _ref_plane_res; + pset _ignored_tags; };