]> git.tdb.fi Git - r2c2.git/commitdiff
Render tracks through GL::Objects
authorMikko Rasa <tdb@tdb.fi>
Sat, 8 Jan 2011 14:24:56 +0000 (14:24 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 8 Jan 2011 14:24:56 +0000 (14:24 +0000)
Change the main scene into an InstanceScene

12 files changed:
data/track.technique [new file with mode: 0644]
source/3d/catalogue.cpp
source/3d/catalogue.h
source/3d/layout.h
source/3d/path.cpp
source/3d/track.cpp
source/3d/track.h
source/3d/tracktype.cpp
source/3d/tracktype.h
source/libr2c2/catalogue.cpp
source/libr2c2/catalogue.h
tracks.dat

diff --git a/data/track.technique b/data/track.technique
new file mode 100644 (file)
index 0000000..1fd35dd
--- /dev/null
@@ -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";
+               };
+       };
+};
index 99148f8fb3c234bfd17a0b2a3452ba9594d6967e..c3b9e1f3693fdafa18ab4b326090a7f67586c6c5 100644 (file)
@@ -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();
 }
 
index caf104e5ef146bcb9ec844dd963037bf41294ab7..8572f6ce18e89545ca282ff21e5fecff26fffb45 100644 (file)
@@ -25,8 +25,6 @@ private:
        Catalogue &catalogue;
        std::map<const TrackType *, TrackType3D *> tracks;
        std::map<const VehicleType *, VehicleType3D *> 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 &);
index 5964a04ed4ce0b1d8a6b43dc47170215ca146f8f..c64386fcc1ca91e57f6a6f7d0478c3bdb37c70dc 100644 (file)
@@ -9,6 +9,7 @@ Distributed under the GPL
 #define R2C2_3D_LAYOUT_H_
 
 #include <sigc++/trackable.h>
+#include <msp/gl/instancescene.h>
 #include <msp/gl/simplescene.h>
 #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;
 
index e4e0b92989172a64780753af467a80e8f90d9cf3..bd6b5b18715eb68d596d084f1d1c3daa37323cd8 100644 (file)
@@ -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)
index 401dd2d9634956a15d645047881663e1d355c44c..c8bf0e6309211c1308bd6a56bbba803934e37ac5 100644 (file)
@@ -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<unsigned>(this));
+}
 
-       type.render(tag);
-
+void Track3D::finish_render(const GL::Tag &) const
+{
        glPopName();
+       GL::MatrixStack::modelview().pop();
 }
 
 } // namespace R2C2
index 7b8c23565f3024ecb1c954f6839a7f3c48fd8925..757955af21ff111f7fdfe3094761b3858f66dcaa 100644 (file)
@@ -9,9 +9,8 @@ Distributed under the GPL
 #define R2C2_3D_TRACK_H_
 
 #include <list>
-#include <msp/gl/renderable.h>
-#include <msp/gl/vertexarray.h>
-#include <msp/gl/vertexarraybuilder.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/objectinstance.h>
 #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
index 1e20c5d8aa82f966a904970f972d79e92b0feb5b..d76e8ebe6bfcb136b54c965ce593abf9bba01cfe 100644 (file)
@@ -7,7 +7,7 @@ Distributed under the GPL
 
 #include <algorithm>
 #include <cmath>
-#include <msp/gl/meshbuilder.h>
+#include <msp/gl/technique.h>
 #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<TrackPart> &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<TrackPart>::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<TrackPart>::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<TrackPart>::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<GL::Technique>(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<TrackPart>::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();
index 7bf1b2d5b9241ab931afefe140ec242366afa6e1..3c06c58d8234047d7b198f2d0f9a68cfb1744f4a 100644 (file)
@@ -10,7 +10,7 @@ Distributed under the GPL
 
 #include <msp/gl/mesh.h>
 #include <msp/gl/meshbuilder.h>
-#include <msp/gl/renderable.h>
+#include <msp/gl/object.h>
 #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<Msp::GL::Mesh *> path_meshes;
        std::vector<Point> 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 &);
 };
index 838b15bd254638fa22e8b367fdf843022e5276ab..d2ee1447639fd459296168fb8c48bfe649f19a9a 100644 (file)
@@ -82,6 +82,7 @@ Catalogue::Loader::Loader(Catalogue &c):
        add("scale", &Loader::scale);
        add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
        add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
+       add("track_technique", &Catalogue::track_technique);
        add("vehicle", static_cast<void (Loader::*)(unsigned)>(&Loader::vehicle));
        add("vehicle", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::vehicle));
 }
index c8b006ab42820b8b7b786e3ca494292fed7874a3..5ac3665f2eb2e7573715951560aafebfecd27658 100644 (file)
@@ -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;
index bd3187a9bf25fea01d4d77aa53691659002ea4db..33e4ca8dbf87546c8c3dffac7905cc2af3327de8 100644 (file)
@@ -17,6 +17,8 @@ ballast_profile
        point -20.0 0.0;
 };
 
+track_technique "track.technique";
+
 // Straight
 
 track 24064