From d91ab10fd78ef29272282b020fa4e08063cb4808 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 8 Jan 2011 14:24:56 +0000 Subject: [PATCH] Render tracks through GL::Objects Change the main scene into an InstanceScene --- data/track.technique | 18 +++++++++++++++++ source/3d/catalogue.cpp | 3 --- source/3d/catalogue.h | 4 ---- source/3d/layout.h | 3 ++- source/3d/path.cpp | 7 ++++--- source/3d/track.cpp | 26 ++++++++++++++---------- source/3d/track.h | 12 +++++------ source/3d/tracktype.cpp | 39 ++++++++++++------------------------ source/3d/tracktype.h | 15 +++++++------- source/libr2c2/catalogue.cpp | 1 + source/libr2c2/catalogue.h | 2 ++ tracks.dat | 2 ++ 12 files changed, 70 insertions(+), 62 deletions(-) create mode 100644 data/track.technique diff --git a/data/track.technique b/data/track.technique new file mode 100644 index 0000000..1fd35dd --- /dev/null +++ b/data/track.technique @@ -0,0 +1,18 @@ +pass "" +{ + material + { + diffuse 1.0 1.0 1.0 1.0; + }; + + texunit 0 + { + texture2d + { + storage RGB 2 1; + min_filter NEAREST; + mag_filter NEAREST; + raw_data "\x40\x40\x40\xD0\xD0\xD0"; + }; + }; +}; diff --git a/source/3d/catalogue.cpp b/source/3d/catalogue.cpp index 99148f8..c3b9e1f 100644 --- a/source/3d/catalogue.cpp +++ b/source/3d/catalogue.cpp @@ -36,9 +36,6 @@ Catalogue3D::Catalogue3D(Catalogue &c): for(Catalogue::TrackMap::const_iterator i=trks.begin(); i!=trks.end(); ++i) track_added(*i->second); - ballast_material.set_diffuse(GL::Color(0.25, 0.25, 0.25)); - rail_material.set_diffuse(GL::Color(0.85, 0.85, 0.85)); - build_endpoint_mesh(); } diff --git a/source/3d/catalogue.h b/source/3d/catalogue.h index caf104e..8572f6c 100644 --- a/source/3d/catalogue.h +++ b/source/3d/catalogue.h @@ -25,8 +25,6 @@ private: Catalogue &catalogue; std::map tracks; std::map vehicles; - Msp::GL::Material ballast_material; - Msp::GL::Material rail_material; Msp::GL::Mesh endpoint_mesh; public: @@ -36,8 +34,6 @@ public: const Catalogue &get_catalogue() const { return catalogue; } const TrackType3D &get_track(const TrackType &) const; const VehicleType3D &get_vehicle(const VehicleType &) const; - const Msp::GL::Material &get_ballast_material() const { return ballast_material; } - const Msp::GL::Material &get_rail_material() const { return rail_material; } const Msp::GL::Mesh &get_endpoint_mesh() const { return endpoint_mesh; } private: void track_added(const TrackType &); diff --git a/source/3d/layout.h b/source/3d/layout.h index 5964a04..c64386f 100644 --- a/source/3d/layout.h +++ b/source/3d/layout.h @@ -9,6 +9,7 @@ Distributed under the GPL #define R2C2_3D_LAYOUT_H_ #include +#include #include #include "libr2c2/layout.h" #include "catalogue.h" @@ -29,7 +30,7 @@ private: Catalogue3D catalogue; TrackMap tracks; VehicleMap vehicles; - Msp::GL::SimpleScene scene; + Msp::GL::InstanceScene scene; Msp::GL::SimpleScene ep_scene; Msp::GL::SimpleScene path_scene; diff --git a/source/3d/path.cpp b/source/3d/path.cpp index e4e0b92..bd6b5b1 100644 --- a/source/3d/path.cpp +++ b/source/3d/path.cpp @@ -70,9 +70,10 @@ void Path3D::render(const GL::Tag &tag) const if(!mask) return; - GL::PushMatrix push_mat; - track.apply_matrix(); - GL::translate(0, 0, z_offs); + GL::MatrixStack::Push push_mtx(GL::MatrixStack::modelview()); + GL::Matrix matrix = track.get_matrix(); + matrix.translate(0, 0, z_offs); + GL::MatrixStack::modelview() *= matrix; glColor4f(color.r, color.g, color.b, color.a); for(unsigned i=0; mask; ++i, mask>>=1) diff --git a/source/3d/track.cpp b/source/3d/track.cpp index 401dd2d..c8bf0e6 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -21,6 +21,7 @@ using namespace Msp; namespace R2C2 { Track3D::Track3D(Layout3D &l, Track &t): + GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()), layout(l), track(t), type(layout.get_catalogue().get_track(track.get_type())), @@ -81,27 +82,30 @@ Point Track3D::get_node() const return Point(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02); } -void Track3D::apply_matrix() const +GL::Matrix Track3D::get_matrix() const { const Point &pos = track.get_position(); float rot = track.get_rotation(); - GL::translate(pos.x, pos.y, pos.z); - GL::rotate(rot*180/M_PI, 0, 0, 1); - GL::rotate(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0); + GL::Matrix matrix; + matrix.translate(pos.x, pos.y, pos.z); + matrix.rotate(rot, 0, 0, 1); + matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0); + + return matrix; } -void Track3D::render(const GL::Tag &tag) const +void Track3D::setup_render(const GL::Tag &) const { - GL::PushMatrix push_mat; - - apply_matrix(); - + GL::MatrixStack::modelview().push(); + GL::MatrixStack::modelview() *= get_matrix(); glPushName(reinterpret_cast(this)); +} - type.render(tag); - +void Track3D::finish_render(const GL::Tag &) const +{ glPopName(); + GL::MatrixStack::modelview().pop(); } } // namespace R2C2 diff --git a/source/3d/track.h b/source/3d/track.h index 7b8c235..757955a 100644 --- a/source/3d/track.h +++ b/source/3d/track.h @@ -9,9 +9,8 @@ Distributed under the GPL #define R2C2_3D_TRACK_H_ #include -#include -#include -#include +#include +#include #include "libr2c2/track.h" #include "libr2c2/trackpart.h" #include "object.h" @@ -23,7 +22,7 @@ class Layout3D; class Path3D; class TrackType3D; -class Track3D: public Object3D, public Msp::GL::Renderable +class Track3D: public Object3D, public Msp::GL::ObjectInstance { private: Layout3D &layout; @@ -45,8 +44,9 @@ public: virtual Point get_node() const; virtual bool is_visible() const { return true; } - void apply_matrix() const; - virtual void render(const Msp::GL::Tag &) const; + Msp::GL::Matrix get_matrix() const; + virtual void setup_render(const Msp::GL::Tag &) const; + virtual void finish_render(const Msp::GL::Tag &) const; }; } // namespace R2C2 diff --git a/source/3d/tracktype.cpp b/source/3d/tracktype.cpp index 1e20c5d..d76e8eb 100644 --- a/source/3d/tracktype.cpp +++ b/source/3d/tracktype.cpp @@ -7,7 +7,7 @@ Distributed under the GPL #include #include -#include +#include #include "catalogue.h" #include "tracktype.h" @@ -71,10 +71,9 @@ Iter graham_scan(Iter begin, Iter end) namespace R2C2 { -TrackType3D::TrackType3D(const Catalogue3D &cat3d, const TrackType &tt): +TrackType3D::TrackType3D(Catalogue3D &cat3d, const TrackType &tt): catalogue(cat3d), - ballast_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3)), - rail_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3)) + mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3)) { const Catalogue &cat = cat3d.get_catalogue(); const vector &parts = tt.get_parts(); @@ -93,37 +92,36 @@ TrackType3D::TrackType3D(const Catalogue3D &cat3d, const TrackType &tt): { unsigned index = 0; - GL::MeshBuilder bld(ballast_mesh); - bld.color(0.25f, 0.25f, 0.25f); + GL::MeshBuilder bld(mesh); + bld.texcoord(0.25, 0.5); for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) build_part(*i, ballast_profile, Point(0, -ballast_min.y), bld, index); - } - { - unsigned index = 0; - GL::MeshBuilder bld(rail_mesh); - bld.color(0.85f, 0.85f, 0.85f); + bld.texcoord(0.75, 0.5); float y = ballast_h-rail_min.y; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) build_part(*i, rail_profile, Point(-gauge/2-rail_max.x, y), bld, index); for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) build_part(*i, rail_profile, Point(gauge/2-rail_min.x, y), bld, index); } + + object.set_mesh(&mesh); + object.set_technique(catalogue.get(cat.get_track_technique())); unsigned paths = tt.get_paths(); for(unsigned i=0; paths; ++i, paths>>=1) { - GL::Mesh *mesh = 0; + GL::Mesh *m = 0; if(paths&1) { - mesh = new GL::Mesh(GL::VERTEX3); - GL::MeshBuilder bld(*mesh); + m = new GL::Mesh(GL::VERTEX3); + GL::MeshBuilder bld(*m); unsigned index = 0; for(vector::const_iterator j=parts.begin(); j!=parts.end(); ++j) if(j->get_path()==i) build_part(*j, cat.get_path_profile(), Point(0, ballast_h+1.5*rail_h), bld, index); } - path_meshes.push_back(mesh); + path_meshes.push_back(m); } min_z = max_z = border.front().z; @@ -169,17 +167,6 @@ const GL::Mesh &TrackType3D::get_path_mesh(unsigned p) const return *path_meshes[p]; } -void TrackType3D::render(const GL::Tag &tag) const -{ - if(tag==0) - { - catalogue.get_ballast_material().bind(); - ballast_mesh.draw(); - catalogue.get_rail_material().bind(); - rail_mesh.draw(); - } -} - void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Point &offset, GL::MeshBuilder &bld, unsigned &base_index) { float plen = part.get_length(); diff --git a/source/3d/tracktype.h b/source/3d/tracktype.h index 7bf1b2d..3c06c58 100644 --- a/source/3d/tracktype.h +++ b/source/3d/tracktype.h @@ -10,7 +10,7 @@ Distributed under the GPL #include #include -#include +#include #include "libr2c2/profile.h" #include "libr2c2/tracktype.h" @@ -18,26 +18,25 @@ namespace R2C2 { class Catalogue3D; -class TrackType3D: public Msp::GL::Renderable +class TrackType3D { private: - const Catalogue3D &catalogue; - Msp::GL::Mesh ballast_mesh; - Msp::GL::Mesh rail_mesh; + Catalogue3D &catalogue; + Msp::GL::Mesh mesh; + Msp::GL::Object object; std::vector path_meshes; std::vector border; float min_z; float max_z; public: - TrackType3D(const Catalogue3D &, const TrackType &); + TrackType3D(Catalogue3D &, const TrackType &); ~TrackType3D(); void get_bounds(float, Point &, Point &) const; + const Msp::GL::Object &get_object() const { return object; } const Msp::GL::Mesh &get_path_mesh(unsigned) const; - virtual void render(const Msp::GL::Tag &) const; - private: void build_part(const TrackPart &, const Profile &, const Point &, Msp::GL::MeshBuilder &, unsigned &); }; diff --git a/source/libr2c2/catalogue.cpp b/source/libr2c2/catalogue.cpp index 838b15b..d2ee144 100644 --- a/source/libr2c2/catalogue.cpp +++ b/source/libr2c2/catalogue.cpp @@ -82,6 +82,7 @@ Catalogue::Loader::Loader(Catalogue &c): add("scale", &Loader::scale); add("track", static_cast(&Loader::track)); add("track", static_cast(&Loader::track)); + add("track_technique", &Catalogue::track_technique); add("vehicle", static_cast(&Loader::vehicle)); add("vehicle", static_cast(&Loader::vehicle)); } diff --git a/source/libr2c2/catalogue.h b/source/libr2c2/catalogue.h index c8b006a..5ac3665 100644 --- a/source/libr2c2/catalogue.h +++ b/source/libr2c2/catalogue.h @@ -50,6 +50,7 @@ private: Profile rail_profile; Profile ballast_profile; Profile path_profile; + std::string track_technique; TrackMap tracks; VehicleMap vehicles; Layout layout; @@ -64,6 +65,7 @@ public: const Profile &get_rail_profile() const { return rail_profile; } const Profile &get_ballast_profile() const { return ballast_profile; } const Profile &get_path_profile() const { return path_profile; } + const std::string &get_track_technique() const { return track_technique; } void add_track(TrackType &); const TrackType &get_track(const ArticleNumber &) const; diff --git a/tracks.dat b/tracks.dat index bd3187a..33e4ca8 100644 --- a/tracks.dat +++ b/tracks.dat @@ -17,6 +17,8 @@ ballast_profile point -20.0 0.0; }; +track_technique "track.technique"; + // Straight track 24064 -- 2.43.0