]> Dogcows Code - chaz/yoink/commitdiff
revamped scene drawing in preparation for octree
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 16 Aug 2009 07:59:18 +0000 (01:59 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 16 Aug 2009 07:59:18 +0000 (01:59 -0600)
18 files changed:
data/scenes/Test.json
src/Makefile.am
src/Moof/Aabb.cc
src/Moof/Aabb.hh
src/Moof/Animation.cc
src/Moof/Camera.hh
src/Moof/Cullable.hh
src/Moof/Dispatcher.cc
src/Moof/Drawable.hh
src/Moof/Entity.hh
src/Moof/Frustum.hh
src/Moof/Math.hh
src/Moof/Resource.cc
src/Moof/Scene.cc
src/Moof/Serializable.hh
src/Moof/Settings.cc
src/Moof/Tilemap.hh
src/YoinkApp.cc

index 7b0fd6314073ada5325eb1b8feea63b9a0a577e3..562a3befec0cb3224423f30392716e4c6b2c8c54 100644 (file)
@@ -1,6 +1,6 @@
 {
        "playfield_bounds": [0, 0, -100, 1280, 500, 100],
-       "maximum_bounds": [-800, 0, -300, 2400, 1000, 600],
+       "maximum_bounds": [-160, 0, -192, 1440, 512, 224],
        "instructions":
        [
 
index ea68cde7fef42b8edc6039787f56204e067e5c16..62c10cafa3d2fe5ca81a7730c7dd49235efd6e1a 100644 (file)
@@ -85,7 +85,7 @@ EXTRA_DIST = Moof/cml
 YOINK_ENVIRONMENT = YOINK_DATADIR="$(top_srcdir)/data"
 
 run: all
-       $(YOINK_ENVIRONMENT) ./yoink
+       $(YOINK_ENVIRONMENT) ./yoink $(YOINK_OPTS)
 
 debug: all
        $(YOINK_ENVIRONMENT) gdb ./yoink
index 7b2ada7ba7f7f5b6704adf59af8e9962bd64d9aa..d81c971f78947d61a0df5d5b0de4344c46f93b16 100644 (file)
@@ -27,6 +27,7 @@
 *******************************************************************************/
 
 #include <Moof/Aabb.hh>
+#include <Moof/Camera.hh>
 
 
 namespace Mf {
@@ -43,7 +44,7 @@ void Aabb::draw(Scalar alpha) const
                                                 max[0], min[1], max[2],
                                                 max[0], max[1], max[2]};
 
-       GLubyte indicis[] = {0, 1, 2, 3,
+       GLubyte indices[] = {0, 1, 2, 3,
                                                 1, 2, 7, 4,
                                                 3, 0, 5, 6,
                                                 2, 3, 6, 7,
@@ -53,7 +54,12 @@ void Aabb::draw(Scalar alpha) const
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_SCALAR, 0, vertices);
 
-       glDrawElements(GL_QUADS, sizeof(indicis), GL_UNSIGNED_BYTE, indicis);
+       glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE, indices);
+}
+
+bool Aabb::isVisible(const Camera& cam) const
+{
+       return cam.getFrustum().checkAabb(*this);
 }
 
 
index b43e194784df06c96892f11d8b7129fe8fef4587..f51531a49d49db13e1fd3f87b77510c59ed747c4 100644 (file)
@@ -29,6 +29,7 @@
 #ifndef _MOOF_AABB_HH_
 #define _MOOF_AABB_HH_
 
+#include <Moof/Cullable.hh>
 #include <Moof/Drawable.hh>
 #include <Moof/Math.hh>
 
@@ -40,7 +41,7 @@ namespace Mf {
  * Axis-aligned Bounding Box
  */
 
-struct Aabb : public Drawable
+struct Aabb : public Cullable, public Drawable
 {
        Vector3 min;
        Vector3 max;
@@ -103,7 +104,8 @@ struct Aabb : public Drawable
                                           (min[2] + max[2]) / 2.0);
        }
 
-       void draw(Scalar alpha) const;
+       void draw(Scalar alpha = 0.0) const;
+       bool isVisible(const Camera& cam) const;
 };
 
 
index 3c418f7052027b17f32bb3305e821563be151667..38ffa6c9c5e5c8ca9fdb83806e46f731d9bd274b 100644 (file)
@@ -83,7 +83,7 @@ struct Animation::AnimationImpl
                                {
                                        std::map<std::string,SerializablePtr>::iterator it;
 
-                                       for (it = rootObj.begin(); it != rootObj.end(); it++)
+                                       for (it = rootObj.begin(); it != rootObj.end(); ++it)
                                        {
                                                std::string key = (*it).first;
                                                if (key == "index")
@@ -132,7 +132,7 @@ struct Animation::AnimationImpl
                                if (root->get(rootObj))
                                {
                                        std::map<std::string,SerializablePtr>::iterator it;
-                                       for (it = rootObj.begin(); it != rootObj.end(); it++)
+                                       for (it = rootObj.begin(); it != rootObj.end(); ++it)
                                        {
                                                std::string key = (*it).first;
 
@@ -145,7 +145,7 @@ struct Animation::AnimationImpl
                                                                std::vector<SerializablePtr>::iterator jt;
 
                                                                for (jt = framesObj.begin();
-                                                                               jt != framesObj.end(); jt++)
+                                                                               jt != framesObj.end(); ++jt)
                                                                {
                                                                        if (*jt)
                                                                        {
@@ -196,7 +196,7 @@ struct Animation::AnimationImpl
                                {
                                        std::map<std::string,SerializablePtr>::iterator it;
 
-                                       for (it = rootObj.begin(); it != rootObj.end(); it++)
+                                       for (it = rootObj.begin(); it != rootObj.end(); ++it)
                                        {
                                                sequences.insert(std::pair<std::string,Sequence>((*it).first,
                                                                        Sequence((*it).second)));
index 8b3b1dabe12e2760612b25554cf3c5007cb22dcf..0aa5dc9990cf5c3f97f23b0db1b76467e91fc143 100644 (file)
@@ -30,6 +30,7 @@
 #define _MOOF_CAMERA_HH_
 
 #include <Moof/Event.hh>
+#include <Moof/Frustum.hh>
 #include <Moof/Interpolator.hh>
 #include <Moof/Math.hh>
 
@@ -61,8 +62,12 @@ public:
                return transformation_;
        }
 
-       void adjustFromInput(const Event& event);
+       const Frustum& getFrustum() const
+       {
+               return frustum_;
+       }
 
+       void adjustFromInput(const Event& event);
        void update(Scalar t, Scalar dt);
 
 private:
@@ -70,6 +75,7 @@ private:
        Quaternion      rotation_;
 
        Matrix4         transformation_;
+       Frustum         frustum_;
 
        Lerpv3          pInterp_;
 
index 34a09b6a7ec3889d329a8053e5cec12868146525..b25840e880688a834d0d9328223f04451802fdaf 100644 (file)
 #ifndef _MOOF_CULLABLE_HH_
 #define _MOOF_CULLABLE_HH_
 
-#include <Moof/Camera.hh>
-
 
 namespace Mf {
 
 
+class Camera;
+
 /**
  * Interface for anything that can be culled.
  */
@@ -42,7 +42,9 @@ namespace Mf {
 class Cullable
 {
 public:
-       virtual bool isVisible(const Camera& cam) const
+       inline virtual ~Cullable() {}
+
+       inline virtual bool isVisible(const Camera& cam) const
        {
                return true;
        }
index fa7b3281818ca6a06a4f07753638c4509209d04a..e0d3d21bb839668593629818e281c7ff1f5ed9b3 100644 (file)
@@ -94,18 +94,18 @@ void Dispatcher::removeHandler(Handler id)
        std::pair<Dispatcher::DispatcherImpl::HandlerIter,Dispatcher::DispatcherImpl::HandlerIter>
                handlers(impl_->handlers.equal_range(id));
 
-       Dispatcher::DispatcherImpl::HandlerIter i;
-       for (i = handlers.first; i != handlers.second; i++)
+       Dispatcher::DispatcherImpl::HandlerIter it;
+       for (it = handlers.first; it != handlers.second; ++it)
        {
-               Dispatcher::DispatcherImpl::CallbackIter it = impl_->callbacks.find((*i).second);
+               Dispatcher::DispatcherImpl::CallbackIter first = impl_->callbacks.find((*it).second);
                Dispatcher::DispatcherImpl::CallbackIter last = impl_->callbacks.end();
 
-               Dispatcher::DispatcherImpl::CallbackIter j;
-               for (j = it; j != last; j++)
+               Dispatcher::DispatcherImpl::CallbackIter jt;
+               for (jt = first; jt != last; ++jt)
                {
-                       if (((*j).second).first == id)
+                       if (((*jt).second).first == id)
                        {
-                               impl_->callbacks.erase(j);
+                               impl_->callbacks.erase(jt);
                                break;
                        }
                }
@@ -125,10 +125,10 @@ void Dispatcher::dispatch(const std::string& message, const Notification& param)
        std::pair<Dispatcher::DispatcherImpl::CallbackIter,Dispatcher::DispatcherImpl::CallbackIter>
                callbacks(impl_->callbacks.equal_range(message));
 
-       Dispatcher::DispatcherImpl::CallbackIter i;
-       for (i = callbacks.first; i != callbacks.second; i++)
+       Dispatcher::DispatcherImpl::CallbackIter it;
+       for (it = callbacks.first; it != callbacks.second; ++it)
        {
-               Function callback = ((*i).second).second;
+               Function callback = ((*it).second).second;
                callback(param);
        }
 }
index 1554b11ffca84a52610136b21b7fefc993d2b313..5cfd628d23137815b2a1e43222f12d13e1ada4ec 100644 (file)
@@ -29,7 +29,6 @@
 #ifndef _MOOF_DRAWABLE_HH_
 #define _MOOF_DRAWABLE_HH_
 
-#include <Moof/Camera.hh>
 #include <Moof/Math.hh>
 
 
@@ -43,6 +42,8 @@ namespace Mf {
 class Drawable
 {
 public:
+       inline virtual ~Drawable() {}
+
        virtual void draw(Scalar alpha) const = 0;
 };
 
index 738e13785b0f3d77bcb09580ce83288dc1d02ffe..7595054e68e8aa534b7c58966c6687cb22f5f487 100644 (file)
@@ -32,8 +32,8 @@
 #include <boost/shared_ptr.hpp>
 
 #include <Moof/Aabb.hh>
-#include <Moof/Drawable.hh>
 #include <Moof/Cullable.hh>
+#include <Moof/Drawable.hh>
 
 
 namespace Mf {
index 82eba17140a0bcf6ecdb1e644d4969df0eb7ec82..ac2b353e263409f1be648a60854f249fc263ff9b 100644 (file)
 namespace Mf {
 
 
+class Aabb;
+
 class Frustum
 {
-       Matrix4 projection;
-       //Plane left, right, bottom, top, near, far;
+       //Matrix4 projection;
+       Plane left, right, bottom, top, near, far;
 
 public:
-       Frustum(Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f);
-       Frustum(Scalar fovy, Scalar aspect, Scalar near, Scalar far);
+       typedef enum
+       {
+               OUTSIDE         = 0,
+               INSIDE          = 1,
+               INTERSECT       = 2
+       } Collision;
+
+       //Frustum() {}
+       //Frustum(Scalar l, Scalar r, Scalar b, Scalar t, Scalar n, Scalar f);
+       //Frustum(Scalar fovy, Scalar aspect, Scalar near, Scalar far);
+
+       inline Collision checkAabb(const Aabb& aabb) const
+       {
+               return INSIDE;
+       }
 
-       const Matrix4& getMatrix() const;
+       //const Matrix4& getMatrix() const;
 };
 
 
index c4e3484ebebc56b5bad7e05cee6c13f9ef657557..4e1cda8c1a25f3a4f57cffe616a56564040f45f6 100644 (file)
@@ -57,6 +57,27 @@ typedef cml::matrix< Scalar, cml::fixed<4,4>,
 typedef cml::quaternion< Scalar, cml::fixed<>, cml::vector_first,
                cml::positive_cross >                                   Quaternion;
 
+typedef cml::constants<Scalar>                                 Constants;
+
+
+inline Vector3& demoteVector(Vector3& left, const Vector4& right)
+{
+       left[0] = right[0];
+       left[1] = right[1];
+       left[2] = right[2];
+       return left;
+}
+
+inline Vector4& promoteVector(Vector4& left, const Vector3& right)
+{
+       left[0] = right[0];
+       left[1] = right[1];
+       left[2] = right[2];
+       left[3] = 1.0;
+       return left;
+}
+
+
 
 const Scalar EPSILON = 0.000001;
 
index 5efe8f53f43c4ae0ebdb98102d5870df8deb27a7..e21c834e869ce4bc78debe11c0c57d618ab7313e 100644 (file)
@@ -58,7 +58,7 @@ std::string Resource::getPathToResource(const std::string& name)
 {
        std::vector<std::string>::iterator it;
 
-       for (it = searchPaths_.begin(); it != searchPaths_.end(); it++)
+       for (it = searchPaths_.begin(); it != searchPaths_.end(); ++it)
        {
                std::string fullPath(*it);
                fullPath += name;
index b8cb23388c24b3ea8c41665bda818566eb504ce6..adec6ec42ccbfa1a38c6f63647c8e24001fceb9c 100644 (file)
@@ -48,255 +48,113 @@ namespace Mf {
 
 class Scene::SceneImpl : public Mippleton<SceneImpl>
 {
-       class Scenery : public Entity
+       class Quad : public Entity
        {
        public:
-               Scenery(const Matrix4& transform, const std::string& textureName) :
-                       transformation(transform),
-                       image(textureName) {}
-
-       protected:
-               Matrix4         transformation;
-               Tilemap         image;
-               bool            blending;
-               long            detail;
-               bool            fog;
-       };
-
-       class TilePanel : public Scenery
-       {
-       public:
-               TilePanel(const Matrix4& transform, const std::string& textureName,
-                               SerializablePtr root) :
-                       Scenery(transform, textureName),
-                       width(1),
-                       height(1)
+               Quad(const Vector3 vertices[4], const std::string& texture,
+                               Tilemap::Index tileIndex) :
+                       tilemap_(texture),
+                       detail_(0),
+                       blending_(false),
+                       fog_(false)
                {
-                       std::map<std::string,SerializablePtr> rootObj;
-
-                       if (root->get(rootObj))
+                       for (int i = 0, num = 0; i < 4; ++i)
                        {
-                               std::map<std::string,SerializablePtr>::iterator it;
-
-                               if ((it = rootObj.find("width")) != rootObj.end())
+                               for (int j = 0; j < 3; ++j, ++num)
                                {
-                                       (*it).second->get(width);
-                               }
-                               if ((it = rootObj.find("tiles")) != rootObj.end())
-                               {
-                                       std::vector<SerializablePtr> theTiles;
-
-                                       if ((*it).second->get(theTiles))
-                                       {
-                                               std::vector<SerializablePtr>::iterator jt;
-
-                                               height = theTiles.size() / width;
-                                               int w, h;
-
-                                               indices.resize(height);
-
-                                               for (h = height - 1, jt = theTiles.begin();
-                                                               jt != theTiles.end(); h--)
-                                               {
-                                                       std::vector<Tilemap::Index> row;
-
-                                                       for (w = 0; w < width && jt != theTiles.end();
-                                                                       w++, jt++)
-                                                       {
-                                                               long index;
-
-                                                               if ((*jt)->get(index))
-                                                               {
-                                                                       row.push_back(Tilemap::Index(index));
-                                                               }
-                                                       }
-
-                                                       indices[h] = row;
-                                               }
-                                       }
+                                       vertices_[num] = vertices[i][j];
                                }
                        }
-               }
-
-               void draw(Scalar alpha) const
-               {
-                       glPushMatrix();
-                       //std::cout << "transforming..." << std::endl;
-                       //std::cout << transformation << std::endl;
-                       glMultMatrix(transformation.data());
-
-                       glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-                       image.bind();
-
-                       long x, y;
-                       Scalar xf, yf;
 
-                       for (y = 0, yf = 0.0; y < height; y++, yf += 1.0)
+                       if (!tilemap_.getTileCoords(tileIndex, texCoords_))
                        {
-                               for (x = 0, xf = 0.0; x < width; x++, xf += 1.0)
-                               {
-                                       Scalar texCoords[8];
-
-                                       Tilemap::Index index = indices[y][x];
-
-                                       if (image.getTileCoords(index, texCoords))
-                                       {
-                                               glBegin(GL_TRIANGLE_FAN);
-                                                       glTexCoord2f(texCoords[0], texCoords[1]);
-                                                       glVertex2f(xf, yf);
-                                                       glTexCoord2f(texCoords[2], texCoords[3]);
-                                                       glVertex2f(xf+1.0, yf);
-                                                       glTexCoord2f(texCoords[4], texCoords[5]);
-                                                       glVertex2f(xf+1.0, yf+1.0);
-                                                       glTexCoord2f(texCoords[6], texCoords[7]);
-                                                       glVertex2f(xf, yf+1.0);
-                                               glEnd();
-                                       }
-                               }
+                               std::cerr << "no coords for tile's texture" << std::endl;
                        }
-
-                       glPopMatrix();
                }
 
-               bool isVisible(const Camera& cam)
+               void setDetail(long detail)
                {
-                       return true;
+                       detail_ = detail;
                }
 
-       private:
-               long width, height;
-               std::vector<std::vector<Tilemap::Index> > indices;
-       };
-
-       class Billboard : public Scenery
-       {
-       public:
-               Billboard(const Matrix4& transform, const std::string& textureName,
-                               SerializablePtr root) :
-                       Scenery(transform, textureName),
-                       index(0),
-                       uScale(1)
+               void setBlending(bool blending)
                {
-                       std::map<std::string,SerializablePtr> rootObj;
-
-                       if (root->get(rootObj))
-                       {
-                               std::map<std::string,SerializablePtr>::iterator it;
-
-                               if ((it = rootObj.find("tile")) != rootObj.end())
-                               {
-                                       long value;
-                                       if ((*it).second->get(value))
-                                       {
-                                               index = Tilemap::Index(value);
-                                       }
-                               }
-                               if ((it = rootObj.find("u_scale")) != rootObj.end())
-                               {
-                                       (*it).second->get(uScale);
-                               }
-                               if ((it = rootObj.find("fog")) != rootObj.end())
-                               {
-                                       (*it).second->get(fog);
-                               }
-                               if ((it = rootObj.find("blend")) != rootObj.end())
-                               {
-                                       (*it).second->get(blending);
-                               }
-                       }
-
-                       image.getTileCoords(index, texCoords);
+                       blending_ = blending;
                }
 
-               void draw(Scalar alpha) const
+               void setFog(bool fog)
                {
-                       glPushMatrix();
-                       glMultMatrix(transformation.data());
+                       fog_ = fog;
+               }
 
-                       if (blending)
+               void draw(Scalar alpha = 0.0) const
+               {
+                       if (blending_)
                        {
                                glEnable(GL_BLEND);
                                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                        }
-                       /*if (fog) glEnable(GL_FOG);*/
 
                        glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-                       image.bind();
+                       tilemap_.bind();
 
-                       float increment = 1.0f / float(uScale);
-                       int x;
-                       float xf;
+                       //glEnableClientState(GL_VERTEX_ARRAY);
+                       //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+
+                       //glVertexPointer(3, GL_SCALAR, 0, vertices_);
+                       //glTexCoordPointer(2, GL_SCALAR, 0, texCoords_);
+
+                       //glDrawArrays(GL_TRIANGLE_FAN, 0, sizeof(vertices_));
+
+                       //glDisableClientState(GL_VERTEX_ARRAY);
+                       //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
-                       for (x = 0, xf = 0.0f; x < uScale; x++, xf += increment)
-                       {
                        glBegin(GL_TRIANGLE_FAN);
-                               glTexCoord2f(texCoords[0], texCoords[1]);
-                               glVertex2f(xf, 0.0f);
-                               glTexCoord2f(texCoords[2], texCoords[3]);
-                               glVertex2f(xf+increment, 0.0f);
-                               glTexCoord2f(texCoords[4], texCoords[5]);
-                               glVertex2f(xf+increment, 1.0f);
-                               glTexCoord2f(texCoords[6], texCoords[7]);
-                               glVertex2f(xf, 1.0f);
+                               glTexCoord2f(texCoords_[0], texCoords_[1]);
+                               glVertex3v(vertices_);
+                               glTexCoord2f(texCoords_[2], texCoords_[3]);
+                               glVertex3v(vertices_+3);
+                               glTexCoord2f(texCoords_[4], texCoords_[5]);
+                               glVertex3v(vertices_+6);
+                               glTexCoord2f(texCoords_[6], texCoords_[7]);
+                               glVertex3v(vertices_+9);
                        glEnd();
-                       }
 
                        glDisable(GL_BLEND);
-                       glDisable(GL_FOG);
-
-                       glPopMatrix();
                }
 
-               bool isVisible(const Camera& cam)
+               bool isVisible(const Camera& cam) const
                {
-                       return false;
+                       return aabb_.isVisible(cam);
                }
 
        private:
-               Tilemap::Index  index;
-               Scalar                  texCoords[8];
-               long                    uScale;
+               Scalar          vertices_[12];
+               Scalar          texCoords_[8];
+               
+               Tilemap         tilemap_;
+
+               long            detail_;
+               bool            blending_;
+               bool            fog_;
        };
 
 
-       static bool loadBox(Aabb& theBox, SerializablePtr obj)
+       static void loadBox(Aabb& theBox, SerializablePtr obj)
        {
                std::vector<SerializablePtr> numbers;
 
-               if (obj->get(numbers))
+               if (obj->get(numbers) && numbers.size() == 6)
                {
-                       if (numbers.size() == 6)
-                       {
-                               double num;
-
-                               if (numbers[0]->getNumber(num))
-                               {
-                                       theBox.min[0] = Scalar(num);
-                               }
-                               if (numbers[1]->getNumber(num))
-                               {
-                                       theBox.min[1] = Scalar(num);
-                               }
-                               if (numbers[2]->getNumber(num))
-                               {
-                                       theBox.min[2] = Scalar(num);
-                               }
-                               if (numbers[3]->getNumber(num))
-                               {
-                                       theBox.max[0] = Scalar(num);
-                               }
-                               if (numbers[4]->getNumber(num))
-                               {
-                                       theBox.max[1] = Scalar(num);
-                               }
-                               if (numbers[5]->getNumber(num))
-                               {
-                                       theBox.max[2] = Scalar(num);
-                               }
-                       }
+                       double num;
+
+                       if (numbers[0]->getNumber(num)) theBox.min[0] = Scalar(num);
+                       if (numbers[1]->getNumber(num)) theBox.min[1] = Scalar(num);
+                       if (numbers[2]->getNumber(num)) theBox.min[2] = Scalar(num);
+                       if (numbers[3]->getNumber(num)) theBox.max[0] = Scalar(num);
+                       if (numbers[4]->getNumber(num)) theBox.max[1] = Scalar(num);
+                       if (numbers[5]->getNumber(num)) theBox.max[2] = Scalar(num);
                }
-
-               return false;
        }
 
 public:
@@ -306,42 +164,78 @@ public:
                loadFromFile();
        }
 
-       ~SceneImpl()
-       {
-       }
-
 
        void loadInstructions(SerializablePtr root)
        {
                std::vector<SerializablePtr> rootObj;
+               std::vector<SerializablePtr>::iterator it;
 
-               if (root->get(rootObj))
+               if (!root->get(rootObj))
                {
-                       std::vector<SerializablePtr>::iterator it;
+                       std::cerr << "error loading scene instructions" << std::endl;
+                       return;
+               }
 
-                       Matrix4         transform;
-                       std::string     texture;
+               Matrix4         transform;
+               std::string     texture;
 
-                       for (it = rootObj.begin(); it != rootObj.end(); it++)
-                       {
-                               std::string instruction;
+               for (it = rootObj.begin(); it != rootObj.end(); ++it)
+               {
+                       std::string instruction;
 
-                               if ((*it)->get(instruction))
+                       if ((*it)->get(instruction))
+                       {
+                               if (instruction == "reset_transform")
+                               {
+                                       transform.identity();
+                               }
+                               else if (instruction == "translate")
                                {
-                                       if (instruction == "reset_transform")
+                                       std::vector<SerializablePtr> values;
+
+                                       ++it;
+                                       if ((*it)->get(values))
                                        {
-                                               transform.identity();
+                                               Vector3 vec;
+
+                                               for (size_t i = 0; i < values.size(); ++i)
+                                               {
+                                                       double value;
+
+                                                       if (values[i]->getNumber(value))
+                                                       {
+                                                               vec[i] = value;
+                                                       }
+                                               }
+
+                                               Matrix4 translation;
+                                               cml::matrix_translation(translation, vec);
+                                               transform = translation * transform;
                                        }
-                                       else if (instruction == "translate")
+                               }
+                               else if (instruction == "scale")
+                               {
+                                       std::vector<SerializablePtr> values;
+
+                                       ++it;
+                                       if ((*it)->get(values))
                                        {
-                                               std::vector<SerializablePtr> values;
+                                               if (values.size() == 1)
+                                               {
+                                                       double value = 1.0;
+
+                                                       values[0]->getNumber(value);
 
-                                               it++;
-                                               if ((*it)->get(values))
+                                                       Matrix4 scaling;
+                                                       cml::matrix_uniform_scale(scaling,
+                                                                       Scalar(value));
+                                                       transform = scaling * transform;
+                                               }
+                                               else if (values.size() == 3)
                                                {
                                                        Vector3 vec;
 
-                                                       for (size_t i = 0; i < values.size(); i++)
+                                                       for (size_t i = 0; i < values.size(); ++i)
                                                        {
                                                                double value;
 
@@ -351,155 +245,275 @@ public:
                                                                }
                                                        }
 
-                                                       Matrix4 translation;
-                                                       cml::matrix_translation(translation, vec);
-                                                       transform = translation * transform;
+                                                       Matrix4 scaling;
+                                                       cml::matrix_scale(scaling, vec);
+                                                       transform = scaling * transform;
                                                }
                                        }
-                                       else if (instruction == "scale")
-                                       {
-                                               std::vector<SerializablePtr> values;
+                               }
+                               else if (instruction == "rotate")
+                               {
+                                       std::vector<SerializablePtr> values;
 
-                                               it++;
-                                               if ((*it)->get(values))
+                                       ++it;
+                                       if ((*it)->get(values))
+                                       {
+                                               if (values.size() == 2)
                                                {
-                                                       if (values.size() == 1)
-                                                       {
-                                                               double value = 1.0;
-
-                                                               values[0]->getNumber(value);
+                                                       std::string axis;
+                                                       size_t index = 0;
+                                                       double value = 0.0;
 
-                                                               Matrix4 scaling;
-                                                               cml::matrix_uniform_scale(scaling, Scalar(value));
-                                                               transform = scaling * transform;
-                                                       }
-                                                       else if (values.size() == 3)
+                                                       if (values[0]->get(axis))
                                                        {
-                                                               Vector3 vec;
-
-                                                               for (size_t i = 0; i < values.size(); i++)
-                                                               {
-                                                                       double value;
-
-                                                                       if (values[i]->getNumber(value))
-                                                                       {
-                                                                               vec[i] = value;
-                                                                       }
-                                                               }
+                                                               if (axis == "x")      index = 0;
+                                                               else if (axis == "y") index = 1;
+                                                               else if (axis == "z") index = 2;
 
-                                                               Matrix4 scaling;
-                                                               cml::matrix_scale(scaling, vec);
-                                                               transform = scaling * transform;
+                                                               values[1]->getNumber(value);
                                                        }
-                                               }
-                                       }
-                                       else if (instruction == "rotate")
-                                       {
-                                               std::vector<SerializablePtr> values;
-
-                                               it++;
-                                               if ((*it)->get(values))
-                                               {
-                                                       if (values.size() == 2)
-                                                       {
-                                                               std::string axis;
-                                                               size_t axisIndex = 0;
-                                                               double value = 0.0;
-                                                               Vector3 vec(0.0, 0.0, 0.0);
 
-                                                               if (values[0]->get(axis))
-                                                               {
-                                                                       if (axis == "x")
-                                                                       {
-                                                                               axisIndex = 0;
-                                                                               vec[0] = 1.0;
-                                                                       }
-                                                                       else if (axis == "y")
-                                                                       {
-                                                                               axisIndex = 1;
-                                                                               vec[1] = 1.0;
-                                                                       }
-                                                                       else if (axis == "z")
-                                                                       {
-                                                                               axisIndex = 2;
-                                                                               vec[2] = 1.0;
-                                                                       }
-                                                                       values[1]->getNumber(value);
-                                                               }
-
-                                                               cml::matrix_rotate_about_world_axis(transform,
-                                                                               axisIndex, Scalar(value * cml::constantsd::rad_per_deg()));
-                                                       }
+                                                       cml::matrix_rotate_about_world_axis(transform,
+                                                                       index, cml::rad(Scalar(value)));
                                                }
                                        }
-                                       else if (instruction == "texture")
-                                       {
-                                               it++;
-                                               (*it)->get(texture);
-                                       }
-                                       else if (instruction == "tilemap")
-                                       {
-                                               it++;
-                                               TilePanel* tilePanel = new TilePanel(transform, texture,
-                                                               *it);
-                                               boost::shared_ptr<Scenery> sceneItem(tilePanel);
-                                               objects.push_back(sceneItem);
-                                       }
-                                       else if (instruction == "billboard")
-                                       {
-                                               it++;
-                                               Billboard* billboard = new Billboard(transform, texture,
-                                                               *it);
-                                               boost::shared_ptr<Scenery> sceneItem(billboard);
-                                               objects.push_back(sceneItem);
-                                       }
+                               }
+                               else if (instruction == "texture")
+                               {
+                                       ++it;
+                                       (*it)->get(texture);
+                               }
+                               else if (instruction == "tilemap")
+                               {
+                                       ++it;
+                                       loadTilemap(*it, transform, texture);
+                               }
+                               else if (instruction == "billboard")
+                               {
+                                       ++it;
+                                       loadBillboard(*it, transform, texture);
                                }
                        }
                }
        }
 
 
-       void loadFromFile()
+       void loadTilemap(SerializablePtr root, const Matrix4& transform,
+                       const std::string& texture)
        {
-               std::string filePath = Scene::getPathToResource(getName());
+               std::map<std::string,SerializablePtr> rootObj;
+               std::map<std::string,SerializablePtr>::iterator it;
 
-               Deserializer deserializer(filePath, true);
+               if (!root->get(rootObj))
+               {
+                       std::cerr << "error loading scene tilemap object" << std::endl;
+                       return;
+               }
 
-               SerializablePtr root = deserializer.deserialize();
+               long width = 1;
+               long height = 1;
+               std::vector< std::vector<Tilemap::Index> > indices;
 
-               if (root)
+               if ((it = rootObj.find("width")) != rootObj.end())
                {
-                       std::map<std::string,SerializablePtr> rootObj;
+                       (*it).second->get(width);
+               }
+               else
+               {
+                       std::cerr << "width is a required field of a tilemap" << std::endl;
+                       return;
+               }
 
-                       if (root->get(rootObj))
+               std::vector<SerializablePtr> tiles;
+
+               if ((it = rootObj.find("tiles")) != rootObj.end() &&
+                               (*it).second->get(tiles) &&
+                               tiles.size() % width == 0)
+               {
+                       std::vector<SerializablePtr>::iterator jt;
+                       int w, h;
+
+                       height = tiles.size() / width;
+                       indices.resize(height);
+
+                       // the indices are stored upside-down in the scene file so that they
+                       // are easier to edit as text, so we'll need to load them last row
+                       // first
+
+                       for (h = height - 1, jt = tiles.begin(); jt != tiles.end(); --h)
                        {
-                               std::map<std::string,SerializablePtr>::iterator it;
+                               std::vector<Tilemap::Index> row;
 
-                               if ((it = rootObj.find("playfield_bounds")) != rootObj.end())
-                               {
-                                       loadBox(playfieldBounds, (*it).second);
-                               }
-                               if ((it = rootObj.find("maximum_bounds")) != rootObj.end())
+                               for (w = 0; w < width && jt != tiles.end(); ++w, ++jt)
                                {
-                                       loadBox(maximumBounds, (*it).second);
-                               }
-                               if ((it = rootObj.find("instructions")) != rootObj.end())
-                               {
-                                       loadInstructions((*it).second);
+                                       long index;
+
+                                       if ((*jt)->get(index))
+                                       {
+                                               row.push_back(Tilemap::Index(index));
+                                       }
                                }
+
+                               indices[h] = row;
+                       }
+               }
+               else
+               {
+                       std::cerr << "error loading tiles from tilemap object" << std::endl;
+                       return;
+               }
+
+               Vector4 vertices[height+1][width+1];
+
+               Matrix4 transposedTransform = transform;
+               transposedTransform.transpose();
+
+               for (int h = 0; h <= height; ++h)
+               {
+                       for (int w = 0; w <= width; ++w)
+                       {
+                               vertices[h][w] = Vector4(Scalar(w), Scalar(h), 0.0, 1.0) *
+                                       transposedTransform;
+                       }
+               }
+
+               for (int h = 0; h < height; ++h)
+               {
+                       for (int w = 0; w < width; ++w)
+                       {
+                               if (indices[h][w] == Tilemap::NO_TILE) continue;
+
+                               Vector3 quadVertices[4];
+
+                               demoteVector(quadVertices[0], vertices[h][w]);
+                               demoteVector(quadVertices[1], vertices[h][w+1]);
+                               demoteVector(quadVertices[2], vertices[h+1][w+1]);
+                               demoteVector(quadVertices[3], vertices[h+1][w]);
+
+                               Quad* quad = new Quad(quadVertices, texture, indices[h][w]);
+                               boost::shared_ptr<Quad> quadPtr(quad);
+
+                               objects.push_back(quadPtr);
+                       }
+               }
+       }
+
+       void loadBillboard(SerializablePtr root, const Matrix4& transform,
+                       const std::string& texture)
+       {
+               std::map<std::string,SerializablePtr> rootObj;
+               std::map<std::string,SerializablePtr>::iterator it;
+
+               if (!root->get(rootObj))
+               {
+                       std::cerr << "error loading scene billboard object" << std::endl;
+                       return;
+               }
+
+               Tilemap::Index  index = 0;
+               long                    width = 1;
+               bool                    blending = false;
+               bool                    fog = false;
+
+               if ((it = rootObj.find("tile")) != rootObj.end())
+               {
+                       long value;
+                       if ((*it).second->get(value))
+                       {
+                               index = Tilemap::Index(value);
                        }
                }
 
-               std::cout << "playfield: " << playfieldBounds.min << " ... " <<
-                       playfieldBounds.max << std::endl;
+               if ((it = rootObj.find("u_scale")) != rootObj.end())
+               {
+                       (*it).second->get(width);
+               }
+
+               if ((it = rootObj.find("blend")) != rootObj.end())
+               {
+                       (*it).second->get(blending);
+               }
+
+               if ((it = rootObj.find("fog")) != rootObj.end())
+               {
+                       (*it).second->get(fog);
+               }
+
+
+               Vector4 vertices[2][width+1];
+
+               Matrix4 transposedTransform = transform;
+               transposedTransform.transpose();
+
+               Scalar xf;
+               Scalar increment = 1.0 / Scalar(width);
+
+               for (int h = 0; h <= 1; ++h)
+               {
+                       xf = 0.0;
+                       for (int w = 0; w <= width; ++w, xf += increment)
+                       {
+                               vertices[h][w] = Vector4(xf, Scalar(h), 0.0, 1.0) *
+                                       transposedTransform;
+                       }
+               }
+
+               for (int w = 0; w < width; ++w)
+               {
+                       Vector3 quadVertices[4];
+
+                       demoteVector(quadVertices[0], vertices[0][w]);
+                       demoteVector(quadVertices[1], vertices[0][w+1]);
+                       demoteVector(quadVertices[2], vertices[1][w+1]);
+                       demoteVector(quadVertices[3], vertices[1][w]);
+
+                       Quad* quad = new Quad(quadVertices, texture, index);
+                       quad->setBlending(blending);
+                       quad->setFog(fog);
+
+                       boost::shared_ptr<Quad> quadPtr(quad);
+
+                       objects.push_back(quadPtr);
+               }
+       }
+
+
+       void loadFromFile()
+       {
+               std::string filePath = Scene::getPathToResource(getName());
+
+               Deserializer    deserializer(filePath, true);
+               SerializablePtr root = deserializer.deserialize();
+
+               std::map<std::string,SerializablePtr> rootObj;
+               std::map<std::string,SerializablePtr>::iterator it;
+
+               if (!root || !root->get(rootObj))
+               {
+                       std::cerr << "error loading scene file" << std::endl;
+                       return;
+               }
+
+               if ((it = rootObj.find("playfield_bounds")) != rootObj.end())
+               {
+                       loadBox(playfieldBounds, (*it).second);
+               }
+               if ((it = rootObj.find("maximum_bounds")) != rootObj.end())
+               {
+                       loadBox(maximumBounds, (*it).second);
+               }
+               if ((it = rootObj.find("instructions")) != rootObj.end())
+               {
+                       loadInstructions((*it).second);
+               }
        }
 
 
        void draw(Scalar alpha)
        {
-               SceneryVector::iterator it;
+               QuadVector::iterator it;
 
-               for (it = objects.begin(); it != objects.end(); it++)
+               for (it = objects.begin(); it != objects.end(); ++it)
                {
                        //std::cout << "draw object";
                        (*it)->draw(alpha);
@@ -508,8 +522,10 @@ public:
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
                glBindTexture(GL_TEXTURE_2D, 0);
-               glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
-               playfieldBounds.draw(0.0);
+               glColor3f(0.0f, 1.0f, 0.0f);
+               playfieldBounds.draw();
+               glColor3f(0.0f, 0.0f, 1.0f);
+               maximumBounds.draw();
 
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        }
@@ -518,8 +534,8 @@ public:
        Aabb playfieldBounds;
        Aabb maximumBounds;
 
-       typedef std::vector<boost::shared_ptr<Scenery> > SceneryVector;
-       SceneryVector objects;
+       typedef std::vector< boost::shared_ptr<Quad> > QuadVector;
+       QuadVector objects;
 };
 
 
index 458dbe8dcdf745f8d44ebc26ce498aaa179a137c..f00fb94b2c476fcfd38f21fa2a6c83eddc530353 100644 (file)
@@ -129,10 +129,10 @@ SerializableBase<std::vector<SerializablePtr> >::serialize(Serializer& out) cons
 {
        out.pushArrayHead();
 
-       std::vector<SerializablePtr>::const_iterator i;
-       for (i = value_.begin(); i < value_.end(); i++)
+       std::vector<SerializablePtr>::const_iterator it;
+       for (it = value_.begin(); it < value_.end(); ++it)
        {
-               (*i)->serialize(out);
+               (*it)->serialize(out);
        }
 
        out.pushArrayTail();
@@ -144,11 +144,11 @@ SerializableBase<std::map<std::string,SerializablePtr> >::serialize(Serializer&
 {
        out.pushMapHead();
 
-       std::map<std::string,SerializablePtr>::const_iterator i;
-       for (i = value_.begin(); i != value_.end(); i++)
+       std::map<std::string,SerializablePtr>::const_iterator it;
+       for (it = value_.begin(); it != value_.end(); ++it)
        {
-               out.push((*i).first);
-               (*i).second->serialize(out);
+               out.push((*it).first);
+               (*it).second->serialize(out);
        }
 
        out.pushMapTail();
index ccf690af65e395f93142a08eb891cde8e7a31c77..fe98f964d079b40e4c580141ef436fb784af0eff 100644 (file)
@@ -46,7 +46,7 @@ Settings::Settings(int argc, char* argv[])
 
 void Settings::parseArgs(int argc, char* argv[])
 {
-       for (int i = 1; i < argc; i++)
+       for (int i = 1; i < argc; ++i)
        {
                char* where = strchr(argv[i], (int)'=');
 
@@ -91,7 +91,7 @@ void Settings::loadFromFiles(const std::vector<std::string>& filePaths,
 
        char* home = getenv("HOME");
 
-       for (it = filePaths.begin(); it != filePaths.end(); it++)
+       for (it = filePaths.begin(); it != filePaths.end(); ++it)
        {
                std::string path = *it;
 
index f05688658cb659763d78d830f04bf9497b19f43a..80bede9a10967d0ca38e79cdfb2739e537f57a67 100644 (file)
@@ -58,6 +58,7 @@ public:
         */
 
        typedef unsigned Index;
+       static const Index NO_TILE = -1;
 
        typedef enum
        {
index 023b3a30ce51837365e60d22136cc8a2e04cdce3..1b9fa030f16d077ff8bca67d6b2fe9d79837fb81 100644 (file)
@@ -407,8 +407,6 @@ void YoinkApp::handleEvent(const Mf::Event& event)
 }
 
 
-#include <Moof/Tree.hh>
-
 int main(int argc, char* argv[])
 {
        std::cout << PACKAGE_STRING << std::endl
@@ -418,10 +416,6 @@ int main(int argc, char* argv[])
 
        int status = 0;
 
-       //Mf::Tree<Mf::Drawable> myTree;
-       //Mf::Tree<Mf::Drawable>::Ptr prev = myTree.previousSibling();
-       //myTree = *prev;
-
        try
        {
                YoinkApp app(argc, argv);
This page took 0.073253 seconds and 4 git commands to generate.