X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FScene.cc;h=418c0512e3c690e4f29406181cac8346c67ada80;hp=5f4a440c4ce0d06655afe62ed06de1b9b1d456db;hb=25aefe01ef7dbdb603c51411e04b0d6a6107684f;hpb=16d1a05b0777e97a45c48e2874aa4e5cc791282e diff --git a/src/Moof/Scene.cc b/src/Moof/Scene.cc index 5f4a440..418c051 100644 --- a/src/Moof/Scene.cc +++ b/src/Moof/Scene.cc @@ -26,18 +26,15 @@ *******************************************************************************/ -#include #include #include #include "Aabb.hh" #include "Camera.hh" -#include "Cullable.hh" #include "Deserializer.hh" -#include "Drawable.hh" +#include "Entity.hh" +#include "Log.hh" #include "Math.hh" -#include "Mippleton.hh" -#include "OpenGL.hh" #include "Scene.hh" #include "Serializable.hh" #include "Tilemap.hh" @@ -46,461 +43,383 @@ namespace Mf { -class Scene::SceneImpl : public Mippleton +static void loadBox(Aabb& theBox, SerializableP obj) { - class Scenery : public Drawable, public Cullable - { - 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 + Serializable::Array numbers; + + if (obj->get(numbers) && numbers.size() == 6) { - public: - TilePanel(const Matrix4& transform, const std::string& textureName, - SerializablePtr root) : - Scenery(transform, textureName), - width(1), - height(1) - { - std::map rootObj; + Serializable::Float 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); + } +} - if (root->get(rootObj)) - { - std::map::iterator it; - if ((it = rootObj.find("width")) != rootObj.end()) - { - (*it).second->get(width); - } - if ((it = rootObj.find("tiles")) != rootObj.end()) - { - std::vector theTiles; +static void loadTilemap(SerializableP root, const Matrix4& transform, + const std::string& texture, OctreeP octree) +{ + Serializable::Map rootObj; + Serializable::Map::iterator it; - if ((*it).second->get(theTiles)) - { - std::vector::iterator jt; + if (!root->get(rootObj)) + { + logError("invalid tilemap instruction"); + return; + } - height = theTiles.size() / width; - int w, h; + long width = 1; + long height = 1; + std::vector< std::vector > indices; - indices.resize(height); + if ((it = rootObj.find("width")) != rootObj.end()) + { + (*it).second->get(width); + } + else + { + logError("missing required field width for tilemap instruction"); + return; + } - for (h = height - 1, jt = theTiles.begin(); - jt != theTiles.end(); h--) - { - std::vector row; + Serializable::Array tiles; - for (w = 0; w < width && jt != theTiles.end(); - w++, jt++) - { - long index; + if ((it = rootObj.find("tiles")) != rootObj.end() && + (*it).second->get(tiles) && + tiles.size() % width == 0) + { + Serializable::Array::iterator jt; + int w, h; - if ((*jt)->get(index)) - { - row.push_back(Tilemap::Index(index)); - } - } + height = tiles.size() / width; + indices.resize(height); - indices[h] = row; - } - } - } - } - } + // 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 - void draw(Scalar alpha) + for (h = height - 1, jt = tiles.begin(); jt != tiles.end(); --h) { - 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; + std::vector row; - for (y = 0, yf = 0.0; y < height; y++, yf += 1.0) + for (w = 0; w < width && jt != tiles.end(); ++w, ++jt) { - for (x = 0, xf = 0.0; x < width; x++, xf += 1.0) - { - Scalar texCoords[8]; - - Tilemap::Index index = indices[y][x]; + Serializable::Integer index; - 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(); - } + if ((*jt)->get(index)) + { + row.push_back(Tilemap::Index(index)); } } - glPopMatrix(); + indices[h] = row; } + } + else + { + logError("invalid tiles in tilemap instruction"); + return; + } + + Vector4 vertices[height+1][width+1]; + + Matrix4 transposedTransform = transform; + transposedTransform.transpose(); - bool isVisible(const Camera& cam) + for (int h = 0; h <= height; ++h) + { + for (int w = 0; w <= width; ++w) { - return true; + vertices[h][w] = Vector4(Scalar(w), Scalar(h), 0.0, 1.0) * + transposedTransform; } + } - private: - long width, height; - std::vector > indices; - }; - - class Billboard : public Scenery + for (int h = 0; h < height; ++h) { - public: - Billboard(const Matrix4& transform, const std::string& textureName, - SerializablePtr root) : - Scenery(transform, textureName), - index(0), - uScale(1) + for (int w = 0; w < width; ++w) { - std::map rootObj; + if (indices[h][w] == Tilemap::NO_TILE) continue; - if (root->get(rootObj)) - { - std::map::iterator it; + Vector3 quadVertices[4]; - 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); - } - } + 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]); - image.getTileCoords(index, texCoords); - } + Quad* quad = new Quad(quadVertices, texture, indices[h][w]); + boost::shared_ptr quadPtr(quad); - void draw(Scalar alpha) - { - glPushMatrix(); - glMultMatrix(transformation.data()); + octree->insert(quadPtr); + } + } +} - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - image.bind(); +static void loadBillboard(SerializableP root, const Matrix4& transform, + const std::string& texture, OctreeP octree) +{ + Serializable::Map rootObj; + Serializable::Map::iterator it; - float increment = 1.0f / float(uScale); - int x; - float xf; + Tilemap::Index index = 0; + long width = 1; + bool blending = false; + bool fog = false; - for (x = 0, xf = 0.0f; x < uScale; x++, xf += increment) + if (root->get(rootObj)) + { + if ((it = rootObj.find("tile")) != rootObj.end()) + { + Serializable::Integer value; + if ((*it).second->get(value)) { - 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); - glEnd(); + index = Tilemap::Index(value); } + } - glPopMatrix(); + if ((it = rootObj.find("u_scale")) != rootObj.end()) + { + (*it).second->get(width); } - bool isVisible(const Camera& cam) + if ((it = rootObj.find("blend")) != rootObj.end()) { - return false; + (*it).second->get(blending); } - private: - Tilemap::Index index; - Scalar texCoords[8]; - long uScale; - }; + if ((it = rootObj.find("fog")) != rootObj.end()) + { + (*it).second->get(fog); + } + } - static bool loadBox(Aabb& theBox, SerializablePtr obj) - { - std::vector numbers; + Vector4 vertices[2][width+1]; - if (obj->get(numbers)) - { - if (numbers.size() == 6) - { - double num; + Matrix4 transposedTransform = transform; + transposedTransform.transpose(); - if (numbers[0]->getNumber(num)) - { + 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; } - - return false; } -public: - SceneImpl(const std::string& name) : - Mippleton(name) + for (int w = 0; w < width; ++w) { - loadFromFile(); + 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 quadPtr(quad); + + octree->insert(quadPtr); } +} + +static void loadInstructions(SerializableP root, OctreeP octree) +{ + Serializable::Array rootObj; + Serializable::Array::iterator it; - void loadInstructions(SerializablePtr root) + if (!root->get(rootObj)) { - std::vector rootObj; + logError("scene instructions must be an array"); + return; + } - if (root->get(rootObj)) - { - std::vector::iterator it; + 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++) + if ((*it)->get(instruction)) + { + if (instruction == "reset_transform") { - std::string instruction; + transform.identity(); + } + else if (instruction == "translate") + { + Serializable::Array values; - if ((*it)->get(instruction)) + ++it; + if ((*it)->get(values)) { - if (instruction == "reset_transform") - { - transform.identity(); - //std::cout << "===================RESET=====================" << std::endl; - } - else if (instruction == "translate") + Vector3 vec; + + for (size_t i = 0; i < values.size(); ++i) { - std::vector values; + Serializable::Float value; - it++; - if ((*it)->get(values)) + if (values[i]->getNumber(value)) { - Vector3 vec; + vec[i] = value; + } + } - for (size_t i = 0; i < values.size(); i++) - { - double value; + Matrix4 translation; + cml::matrix_translation(translation, vec); + transform = translation * transform; + } + } + else if (instruction == "scale") + { + Serializable::Array values; - if (values[i]->getNumber(value)) - { - vec[i] = value; - } - } + ++it; + if ((*it)->get(values)) + { + if (values.size() == 1) + { + Serializable::Float value = 1.0; - Matrix4 translation; - cml::matrix_translation(translation, vec); - transform = translation * transform; - //std::cout << "TRANSLATE\t" << vec << std::endl - //<< transform << std::endl; - } + values[0]->getNumber(value); + + Matrix4 scaling; + cml::matrix_uniform_scale(scaling, + Scalar(value)); + transform = scaling * transform; } - else if (instruction == "scale") + else if (values.size() == 3) { - std::vector values; + Vector3 vec; - it++; - if ((*it)->get(values)) + for (size_t i = 0; i < values.size(); ++i) { - if (values.size() == 1) - { - double value = 1.0; - - values[0]->getNumber(value); + Serializable::Float value; - Matrix4 scaling; - cml::matrix_uniform_scale(scaling, Scalar(value)); - transform = scaling * transform; - //std::cout << "SCALE\t\t" << value << std::endl - //<< transform << std::endl; - } - else if (values.size() == 3) + if (values[i]->getNumber(value)) { - Vector3 vec; - - for (size_t i = 0; i < values.size(); i++) - { - double value; - - if (values[i]->getNumber(value)) - { - vec[i] = value; - } - } - - Matrix4 scaling; - cml::matrix_scale(scaling, vec); - transform = scaling * transform; - //std::cout << "SCALE\t\t" << vec << std::endl - //<< transform << std::endl; + vec[i] = value; } } + + Matrix4 scaling; + cml::matrix_scale(scaling, vec); + transform = scaling * transform; } - else if (instruction == "rotate") + } + } + else if (instruction == "rotate") + { + Serializable::Array values; + + ++it; + if ((*it)->get(values)) + { + if (values.size() == 2) { - std::vector values; + std::string axis; + size_t index = 0; + Serializable::Float value = 0.0; - it++; - if ((*it)->get(values)) + if (values[0]->get(axis)) { - if (values.size() == 2) - { - std::string axis; - size_t axisIndex = 0; - double value = 0.0; - - if (values[0]->get(axis)) - { - if (axis == "x") - { - axisIndex = 0; - } - else if (axis == "y") - { - axisIndex = 1; - } - else if (axis == "z") - { - axisIndex = 2; - } - values[1]->getNumber(value); - } - - cml::matrix_rotate_about_local_axis(transform, - axisIndex, Scalar(value * cml::constantsd::rad_per_deg())); - //std::cout << "ROTATE\t" << axis << " " << value << std::endl - //<< transform << std::endl; - } + if (axis == "x") index = 0; + else if (axis == "y") index = 1; + else if (axis == "z") index = 2; + + values[1]->getNumber(value); } - } - else if (instruction == "texture") - { - it++; - (*it)->get(texture); - } - else if (instruction == "tilemap") - { - //std::cout << "TILEMAP\t" << texture<< std::endl; - //std::cout << transform << std::endl; - - it++; - TilePanel* tilePanel = new TilePanel(transform, texture, - *it); - boost::shared_ptr sceneItem(tilePanel); - objects.push_back(sceneItem); - } - else if (instruction == "billboard") - { - //std::cout << "BILLBOARD\t" << texture << std::endl; - //std::cout << transform << std::endl; - - it++; - Billboard* billboard = new Billboard(transform, texture, - *it); - boost::shared_ptr sceneItem(billboard); - objects.push_back(sceneItem); + + 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; + loadTilemap(*it, transform, texture, octree); + } + else if (instruction == "billboard") + { + ++it; + loadBillboard(*it, transform, texture, octree); + } } } +} - void loadFromFile() - { - std::string filePath = Scene::getPathToResource(getName()); - - Deserializer deserializer(filePath, true); - - SerializablePtr root = deserializer.deserialize(); - - if (root) - { - std::map rootObj; +static std::string getPath(const std::string& name) +{ + return Resource::getPath("scenes/" + name + ".json"); +} - if (root->get(rootObj)) - { - std::map::iterator it; +OctreeP loadScene(const std::string& name) +{ + std::string filePath = getPath(name); - 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); - } - } - } - } + Deserializer deserializer(filePath, true); + SerializableP root = deserializer.deserialize(); + Serializable::Map rootObj; + Serializable::Map::iterator it; - void draw(Scalar alpha) + if (!root || !root->get(rootObj)) { - SceneryVector::iterator it; - - for (it = objects.begin(); it != objects.end(); it++) - { - //std::cout << "draw object"; - (*it)->draw(alpha); - } + logError("no root map in scene file"); + return OctreeP(); } - Aabb playfieldBounds; Aabb maximumBounds; - typedef std::vector > SceneryVector; - SceneryVector objects; -}; - - -Scene::Scene(const std::string& name) : - // pass through - impl_(Scene::SceneImpl::retain(name), &Scene::SceneImpl::release) {} - + if ((it = rootObj.find("playfield_bounds")) != rootObj.end()) + { + loadBox(playfieldBounds, (*it).second); + } + if ((it = rootObj.find("maximum_bounds")) != rootObj.end()) + { + loadBox(maximumBounds, (*it).second); + } + else + { + logError("missing required maximum bounds"); + return OctreeP(); + } -void Scene::draw(Scalar alpha) -{ - // pass through - impl_->draw(alpha); -} + // create the tree to store the quads + OctreeP octree = Octree::alloc(maximumBounds); + if ((it = rootObj.find("instructions")) != rootObj.end()) + { + loadInstructions((*it).second, octree); + } -/** - * Specialized search location for scene files. They can be found in the - * "scenes" subdirectory of any of the searched directories. - */ + octree->sort(); -std::string Scene::getPathToResource(const std::string& name) -{ - return Resource::getPathToResource("scenes/" + name + ".json"); + return octree; }