]> Dogcows Code - chaz/yoink/blobdiff - src/Scene.cc
ray-scene intersection
[chaz/yoink] / src / Scene.cc
index 452cc134cc85359894bac9877649806424f54938..fce451799db5e4d56a9649e79f7d9ce7fd38453a 100644 (file)
@@ -27,7 +27,6 @@
 *******************************************************************************/
 
 #include <map>
-#include <vector>
 
 #include <Moof/Aabb.hh>
 #include <Moof/Camera.hh>
@@ -50,7 +49,7 @@ struct Scene::Impl : public Mf::Library<Impl>
 {
        struct Quad : public Mf::Entity
        {
-               enum SURFACE
+               enum Surface
                {
                        NONE    = 0,
                        LEFT    = 1,
@@ -58,19 +57,20 @@ struct Scene::Impl : public Mf::Library<Impl>
                        TOP             = 3
                };
 
-               Quad(const Mf::Vector3 vertices[4], const std::string& texture,
+               Quad(const Mf::Vector3* vertices[4], const std::string& texture,
                                Tilemap::Index tileIndex) :
                        mTilemap(texture),
                        mBlending(false),
                        mFog(false),
                        mSurface(NONE)
                {
-                       for (int i = 0, num = 0; i < 4; ++i)
+                       for (int i = 0; i < 4; ++i)
                        {
-                               for (int j = 0; j < 3; ++j, ++num)
-                               {
-                                       mVertices[num] = vertices[i][j];
-                               }
+                               mVertices[i] = *vertices[i];
+                               //for (int j = 0; j < 3; ++j, ++num)
+                               //{
+                                       //mVertices[num] = (*vertices[i])[j];
+                               //}
                        }
 
                        if (!mTilemap.getTileCoords(tileIndex, mTexCoords))
@@ -84,7 +84,7 @@ struct Scene::Impl : public Mf::Library<Impl>
                                        mTexCoords[5] = mTexCoords[7] = 1.0;
                        }
 
-                       mAabb.encloseVertices(vertices, 4);
+                       mAabb.encloseVertices(mVertices, 4);
                        mSphere.point = mAabb.getCenter();
                        mSphere.radius = (mAabb.min - mSphere.point).length();
                }
@@ -99,12 +99,12 @@ struct Scene::Impl : public Mf::Library<Impl>
                        mFog = fog;
                }
 
-               void setSurface(SURFACE type)
+               void setSurface(Surface type)
                {
                        mSurface = type;
                }
 
-               SURFACE getSurface() const
+               Surface getSurface() const
                {
                        return mSurface;
                }
@@ -126,7 +126,7 @@ struct Scene::Impl : public Mf::Library<Impl>
                        //glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
                        mTilemap.bind();
 
-                       glVertexPointer(3, GL_SCALAR, 0, mVertices);
+                       glVertexPointer(3, GL_SCALAR, 0, mVertices[0].data());
                        glTexCoordPointer(2, GL_SCALAR, 0, mTexCoords);
 
                        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
@@ -141,14 +141,14 @@ struct Scene::Impl : public Mf::Library<Impl>
                }
 
 
-               Mf::Scalar              mVertices[12];
+               Mf::Vector3             mVertices[4];
                Mf::Scalar              mTexCoords[8];
                
                Tilemap                 mTilemap;
 
                bool                    mBlending;
                bool                    mFog;
-               SURFACE                 mSurface;
+               Surface                 mSurface;
        };
 
 
@@ -214,7 +214,11 @@ struct Scene::Impl : public Mf::Library<Impl>
        Mf::Script::Status load(Mf::Script& script)
        {
                std::string filePath = Scene::getPath(getName());
-               if (filePath == "") return Mf::Script::FILE_ERROR;
+               if (filePath == "")
+               {
+                       script.push("the scene file could not be found");
+                       return Mf::Script::FILE_ERROR;
+               }
 
                importSceneBindings(script);
                return script.doFile(filePath);
@@ -336,97 +340,105 @@ struct Scene::Impl : public Mf::Library<Impl>
                Mf::Script::Value table = script[1].requireTable();
                Mf::Script::Value top = script[-1];
 
-               Quad::SURFACE surface;
-               table.pushField("surface");
-               top.get(surface);
-               script.pop();
-
-               int width = 1;
-               int height = 1;
+               int                             width = 1;
+               int                             height = 1;
+               int                             nTiles = 0;
 
                table.pushField("width");
                top.get(width);
                script.pop();
 
-               int nTiles = 0;
-
-               //table.pushField("tiles");
-               Mf::Script::Value tiles = script.getTop();
-               nTiles = tiles.getLength();
-
+               nTiles = table.getLength();
                if (nTiles % width != 0) table.throwError("invalid number of tiles");
 
-               std::vector< std::vector<Tilemap::Index> > indices;
-
-               int i, w, h;
-
+               if (width == 0) table.throwError("width field must not be zero");
                height = nTiles / width;
-               indices.resize(height);
+
+               Mf::Vector3             vertices[height+1][width+1];
 
                // 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
 
-               i = 1;
-               for (h = height - 1; h >= 0; --h)
+               // do first row  and first column of vertices
+
+               for (int w = 0; w <= width; ++w)
+               {
+                       vertices[height][w] = Mf::demote(mTransform *
+                                       Mf::Vector4(w, height, 0.0, 1.0));
+               }
+               for (int h = 0; h < height; ++h)
                {
-                       std::vector<Tilemap::Index> row;
+                       vertices[h][0] = Mf::demote(mTransform *
+                                       Mf::Vector4(0.0, h, 0.0, 1.0));
+               }
 
-                       for (w = 0; w < width; ++w, ++i)
+               size_t i = 1;
+               for (int h = height - 1; h >= 0; --h)
+               {
+                       for (int w = 0; w < width; ++w, ++i)
                        {
-                               script.checkStack(2);
-                               script.push(i);
-                               tiles.pushField();
+                               int wPlus1 = w + 1;
+                               int hPlus1 = h + 1;
+
+                               table.pushField(i);
 
                                Tilemap::Index index;
                                top.get(index);
 
-                               row.push_back(index);
-                       }
+                               script.pop();
 
-                       indices[h] = row;
-               }
+                               vertices[h][wPlus1] = Mf::demote(mTransform *
+                                               Mf::Vector4(wPlus1, h, 0.0, 1.0));
 
-               Mf::Vector4 vertices[height+1][width+1];
+                               if (index == Tilemap::NO_TILE) continue;
 
-               Mf::Matrix4 transposedTransform = mTransform;
-               transposedTransform.transpose();
+                               const Mf::Vector3* corners[4] = {
+                                       &vertices[h][w],
+                                       &vertices[h][wPlus1],
+                                       &vertices[hPlus1][wPlus1],
+                                       &vertices[hPlus1][w]
+                               };
 
-               for (int h = 0; h <= height; ++h)
-               {
-                       for (int w = 0; w <= width; ++w)
-                       {
-                               vertices[h][w] = Mf::Vector4(w, h, 0.0, 1.0) * transposedTransform;
+                               Quad* quad = new Quad(corners, mTexture, index);
+                               //quad->setSurface(surface);
+
+                               boost::shared_ptr<Quad> quadPtr(quad);
+                               mObjects.push_back(quadPtr);
                        }
                }
 
-               for (int h = 0; h < height; ++h)
-               {
-                       for (int w = 0; w < width; ++w)
-                       {
-                               if (indices[h][w] == Tilemap::NO_TILE) continue;
+               Quad::Surface   surface = Quad::NONE;
 
-                               Mf::Vector3 demotedVertices[4];
+               table.pushField("surface");
+               top.get(surface);
+               script.pop();
 
-                               demotedVertices[0] = Mf::demote(vertices[h][w]);
-                               demotedVertices[1] = Mf::demote(vertices[h][w+1]);
-                               demotedVertices[2] = Mf::demote(vertices[h+1][w+1]);
-                               demotedVertices[3] = Mf::demote(vertices[h+1][w]);
+               if (surface != Quad::NONE)
+               {
+                       // need a 2d line for collisions
+                       // assuming the camera always looks directly to -z when the
+                       // scene is built, simply demoting the vector again should
+                       // project the points to the xy-plane
 
-                               Quad* quad = new Quad(demotedVertices, mTexture, indices[h][w]);
-                               quad->setSurface(surface);
+                       //Mf::Vector2 tr = Mf::demote(vertices[height+1][width+1]);
+                       //Mf::Vector2 bl = Mf::demote(vertices[0][0]);
 
-                               if (surface != Quad::NONE)
-                               {
-                                       // need a 2d line for collisions
-                                       // assuming the camera always looks directly to -z when the
-                                       // scene is built, simply demoting the vector again should
-                                       // project the points to the xy-plane
-                               }
+                       Mf::Vector2 bl = Mf::demote(vertices[0][0]);
+                       Mf::Vector2 tr = Mf::demote(vertices[height][width]);
+                       //Mf::logInfo("pt1: %f, %f", bl[0], bl[1]);
+                       //Mf::logInfo("pt2: %f, %f", tr[0], tr[1]);
 
-                               boost::shared_ptr<Quad> quadPtr(quad);
-                               //mOctree->insert(quadPtr);
-                               mObjects.push_back(quadPtr);
-                       }
+                       mLines.push_back(Mf::Line<2>(bl, tr));
+                       Mf::logInfo("new line");
+
+                       //if (tl == tr)
+                       //{
+                               //mLines.push_back(Mf::Line<2>(bl, tl));
+                       //}
+                       //else
+                       //{
+                               //mLines.push_back(Mf::Line<2>(bl, tl));
+                       //}
                }
 
                return 0;
@@ -462,10 +474,7 @@ struct Scene::Impl : public Mf::Library<Impl>
                        param.get(index);
                }
 
-               Mf::Vector4 vertices[2][width+1];
-
-               Mf::Matrix4 transposedTransform = mTransform;
-               transposedTransform.transpose();
+               Mf::Vector3 vertices[2][width+1];
 
                Mf::Scalar xf;
                Mf::Scalar increment = 1.0 / Mf::Scalar(width);
@@ -475,26 +484,27 @@ struct Scene::Impl : public Mf::Library<Impl>
                        xf = 0.0;
                        for (int w = 0; w <= width; ++w, xf += increment)
                        {
-                               vertices[h][w] = Mf::Vector4(xf, Mf::Scalar(h), 0.0, 1.0) *
-                                       transposedTransform;
+                               vertices[h][w] = Mf::demote(mTransform *
+                                               Mf::Vector4(xf, Mf::Scalar(h), 0.0, 1.0));
                        }
                }
 
                for (int w = 0; w < width; ++w)
                {
-                       Mf::Vector3 demotedVertices[4];
+                       int wPlus1 = w + 1;
 
-                       demotedVertices[0] = Mf::demote(vertices[0][w]);
-                       demotedVertices[1] = Mf::demote(vertices[0][w+1]);
-                       demotedVertices[2] = Mf::demote(vertices[1][w+1]);
-                       demotedVertices[3] = Mf::demote(vertices[1][w]);
+                       const Mf::Vector3* corners[4] = {
+                               &vertices[0][w],
+                               &vertices[0][wPlus1],
+                               &vertices[1][wPlus1],
+                               &vertices[1][w]
+                       };
 
-                       Quad* quad = new Quad(demotedVertices, mTexture, index);
+                       Quad* quad = new Quad(corners, mTexture, index);
                        quad->setBlending(blending);
                        quad->setFog(fog);
 
                        boost::shared_ptr<Quad> quadPtr(quad);
-                       //mOctree->insert(quadPtr);
                        mObjects.push_back(quadPtr);
                }
 
@@ -517,7 +527,6 @@ Mf::Script::Status Scene::load(Mf::Script& script)
 
 void Scene::draw(Mf::Scalar alpha) const
 {
-       //mImpl->mOctree->draw(alpha);
        std::list< boost::shared_ptr<Impl::Quad> >& objects = mImpl->mObjects;
        std::list< boost::shared_ptr<Impl::Quad> >::const_iterator it;
 
@@ -531,7 +540,6 @@ void Scene::draw(Mf::Scalar alpha) const
 
 void Scene::drawIfVisible(Mf::Scalar alpha, const Mf::Frustum& frustum) const
 {
-       //mImpl->mOctree->drawIfVisible(alpha, frustum);
        std::list< boost::shared_ptr<Impl::Quad> >& objects = mImpl->mObjects;
        std::list< boost::shared_ptr<Impl::Quad> >::const_iterator it;
 
@@ -540,10 +548,38 @@ void Scene::drawIfVisible(Mf::Scalar alpha, const Mf::Frustum& frustum) const
                (*it)->drawIfVisible(alpha, frustum);
        }
 
+       std::list< Mf::Line<2> >& lines = mImpl->mLines;
+       std::list< Mf::Line<2> >::const_iterator lit;
+
+       for (lit = lines.begin(); lit != lines.end(); ++lit)
+       {
+               (*lit).draw(alpha);
+       }
+
        mImpl->mBounds.draw();
 }
 
 
+bool Scene::castRay(const Mf::Ray<2>& ray,
+               std::list<Mf::Ray<2>::Intersection>& hits) const
+{
+       std::list< Mf::Line<2> >& lines = mImpl->mLines;
+       std::list< Mf::Line<2> >::const_iterator it;
+
+       for (it = lines.begin(); it != lines.end(); ++it)
+       {
+               Mf::Ray<2>::Intersection hit;
+               Mf::Scalar d = (*it).intersectRay(ray, hit);
+               if (d > 0.0)
+               {
+                       hits.push_back(hit);
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 bool Scene::checkForCollision(Character& character)
 {
        return false;
@@ -559,7 +595,7 @@ bool Scene::checkForCollision(Character& character)
 
        for (it = objects.begin(); it != objects.end(); ++it)
        {
-               Impl::Quad::SURFACE type = (*it)->getSurface();
+               Impl::Quad::Surface type = (*it)->getSurface();
                if (type == Impl::Quad::NONE) continue;
 
                if (Mf::checkCollision(sphere, (*it)->getSphere()))
This page took 0.029568 seconds and 4 git commands to generate.