/******************************************************************************* Copyright (c) 2009, Charles McGarvey All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include #include #include "Aabb.hh" #include "Camera.hh" #include "Deserializer.hh" #include "Entity.hh" #include "Log.hh" #include "Math.hh" #include "Scene.hh" #include "Serializable.hh" #include "Tilemap.hh" namespace Mf { static void loadBox(Aabb& theBox, SerializableP obj) { Serializable::Array numbers; if (obj->get(numbers) && numbers.size() == 6) { 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); } } static void loadTilemap(SerializableP root, const Matrix4& transform, const std::string& texture, OctreeP octree) { Serializable::Map rootObj; Serializable::Map::iterator it; if (!root->get(rootObj)) { logError("invalid tilemap instruction"); return; } long width = 1; long height = 1; std::vector< std::vector > indices; if ((it = rootObj.find("width")) != rootObj.end()) { (*it).second->get(width); } else { logError("missing required field width for tilemap instruction"); return; } Serializable::Array tiles; if ((it = rootObj.find("tiles")) != rootObj.end() && (*it).second->get(tiles) && tiles.size() % width == 0) { Serializable::Array::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::vector row; for (w = 0; w < width && jt != tiles.end(); ++w, ++jt) { Serializable::Integer index; if ((*jt)->get(index)) { row.push_back(Tilemap::Index(index)); } } indices[h] = row; } } else { logError("invalid tiles in tilemap instruction"); 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 quadPtr(quad); octree->insert(quadPtr); } } } static void loadBillboard(SerializableP root, const Matrix4& transform, const std::string& texture, OctreeP octree) { Serializable::Map rootObj; Serializable::Map::iterator it; Tilemap::Index index = 0; long width = 1; bool blending = false; bool fog = false; if (root->get(rootObj)) { if ((it = rootObj.find("tile")) != rootObj.end()) { Serializable::Integer value; if ((*it).second->get(value)) { index = Tilemap::Index(value); } } 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 quadPtr(quad); octree->insert(quadPtr); } } static void loadInstructions(SerializableP root, OctreeP octree) { Serializable::Array rootObj; Serializable::Array::iterator it; if (!root->get(rootObj)) { logError("scene instructions must be an array"); return; } Matrix4 transform; std::string texture; for (it = rootObj.begin(); it != rootObj.end(); ++it) { std::string instruction; if ((*it)->get(instruction)) { if (instruction == "reset_transform") { transform.identity(); } else if (instruction == "translate") { Serializable::Array values; ++it; if ((*it)->get(values)) { Vector3 vec; for (size_t i = 0; i < values.size(); ++i) { Serializable::Float value; if (values[i]->getNumber(value)) { vec[i] = value; } } Matrix4 translation; cml::matrix_translation(translation, vec); transform = translation * transform; } } else if (instruction == "scale") { Serializable::Array values; ++it; if ((*it)->get(values)) { if (values.size() == 1) { Serializable::Float value = 1.0; values[0]->getNumber(value); 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) { Serializable::Float value; if (values[i]->getNumber(value)) { vec[i] = value; } } Matrix4 scaling; cml::matrix_scale(scaling, vec); transform = scaling * transform; } } } else if (instruction == "rotate") { Serializable::Array values; ++it; if ((*it)->get(values)) { if (values.size() == 2) { std::string axis; size_t index = 0; Serializable::Float value = 0.0; if (values[0]->get(axis)) { if (axis == "x") index = 0; else if (axis == "y") index = 1; else if (axis == "z") index = 2; values[1]->getNumber(value); } 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); } } } } static std::string getPath(const std::string& name) { return Resource::getPath("scenes/" + name + ".json"); } OctreeP loadScene(const std::string& name) { std::string filePath = getPath(name); Deserializer deserializer(filePath, true); SerializableP root = deserializer.deserialize(); Serializable::Map rootObj; Serializable::Map::iterator it; if (!root || !root->get(rootObj)) { logError("no root map in scene file"); return OctreeP(); } Aabb playfieldBounds; Aabb maximumBounds; 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(); } // create the tree to store the quads OctreeP octree = Octree::alloc(maximumBounds); if ((it = rootObj.find("instructions")) != rootObj.end()) { loadInstructions((*it).second, octree); } octree->sort(); return octree; } } // namespace Mf /** vim: set ts=4 sw=4 tw=80: *************************************************/