X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FScene.cc;h=5e44221a93391a03afacb4b4059fe30ca47ffa37;hp=adec6ec42ccbfa1a38c6f63647c8e24001fceb9c;hb=bfa6212d09d8735d8fd5e2638188e4a99f21ada4;hpb=493ddb59a8620b49dfa0ff62ce93395ebfd02e86 diff --git a/src/Moof/Scene.cc b/src/Moof/Scene.cc index adec6ec..5e44221 100644 --- a/src/Moof/Scene.cc +++ b/src/Moof/Scene.cc @@ -36,7 +36,6 @@ #include "Entity.hh" #include "Math.hh" #include "Mippleton.hh" -#include "Octree.hh" #include "OpenGL.hh" #include "Scene.hh" #include "Serializable.hh" @@ -46,7 +45,7 @@ namespace Mf { -class Scene::SceneImpl : public Mippleton +class Scene::Impl : public Mippleton { class Quad : public Entity { @@ -70,6 +69,10 @@ class Scene::SceneImpl : public Mippleton { std::cerr << "no coords for tile's texture" << std::endl; } + + aabb_.encloseVertices(vertices, 4); + sphere_.point = aabb_.getCenter(); + sphere_.radius = (aabb_.min - sphere_.point).length(); } void setDetail(long detail) @@ -95,37 +98,27 @@ class Scene::SceneImpl : public Mippleton glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } + if (fog_) + { + glEnable(GL_FOG); + glFogi(GL_FOG_MODE, GL_LINEAR); + } + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); tilemap_.bind(); - //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_)); + glVertexPointer(3, GL_SCALAR, 0, vertices_); + glTexCoordPointer(2, GL_SCALAR, 0, texCoords_); - //glDisableClientState(GL_VERTEX_ARRAY); - //glDisableClientState(GL_TEXTURE_COORD_ARRAY); - - glBegin(GL_TRIANGLE_FAN); - 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(); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDisable(GL_BLEND); + glDisable(GL_FOG); } bool isVisible(const Camera& cam) const { - return aabb_.isVisible(cam); + return sphere_.isVisible(cam); } private: @@ -158,8 +151,8 @@ class Scene::SceneImpl : public Mippleton } public: - SceneImpl(const std::string& name) : - Mippleton(name) + Impl(const std::string& name) : + Mippleton(name) { loadFromFile(); } @@ -393,7 +386,7 @@ public: Quad* quad = new Quad(quadVertices, texture, indices[h][w]); boost::shared_ptr quadPtr(quad); - objects.push_back(quadPtr); + octree->insert(quadPtr); } } } @@ -404,39 +397,36 @@ public: std::map rootObj; std::map::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()) + if (root->get(rootObj)) { - long value; - if ((*it).second->get(value)) + if ((it = rootObj.find("tile")) != rootObj.end()) { - index = Tilemap::Index(value); + long 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("u_scale")) != rootObj.end()) + { + (*it).second->get(width); + } - if ((it = rootObj.find("blend")) != rootObj.end()) - { - (*it).second->get(blending); - } + if ((it = rootObj.find("blend")) != rootObj.end()) + { + (*it).second->get(blending); + } - if ((it = rootObj.find("fog")) != rootObj.end()) - { - (*it).second->get(fog); + if ((it = rootObj.find("fog")) != rootObj.end()) + { + (*it).second->get(fog); + } } @@ -473,14 +463,14 @@ public: boost::shared_ptr quadPtr(quad); - objects.push_back(quadPtr); + octree->insert(quadPtr); } } void loadFromFile() { - std::string filePath = Scene::getPathToResource(getName()); + std::string filePath = Scene::getPath(getName()); Deserializer deserializer(filePath, true); SerializablePtr root = deserializer.deserialize(); @@ -502,69 +492,85 @@ public: { loadBox(maximumBounds, (*it).second); } + else + { + std::cerr << "maximum bounds required in scene" << std::endl; + return; + } + + // create the tree to store the quads + octree = OctreePtr(new Octree(maximumBounds)); + if ((it = rootObj.find("instructions")) != rootObj.end()) { loadInstructions((*it).second); } + + octree->sort(); } - void draw(Scalar alpha) + void draw(Scalar alpha, const Camera& cam) const { - QuadVector::iterator it; + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); - for (it = objects.begin(); it != objects.end(); ++it) - { - //std::cout << "draw object"; - (*it)->draw(alpha); - } + octree->drawIfVisible(alpha, cam); - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + //glDisableClientState(GL_VERTEX_ARRAY); + //glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glBindTexture(GL_TEXTURE_2D, 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_LINE); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + //Texture::resetBind(); + //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); } Aabb playfieldBounds; Aabb maximumBounds; - typedef std::vector< boost::shared_ptr > QuadVector; - QuadVector objects; + OctreePtr octree; }; Scene::Scene(const std::string& name) : // pass through - impl_(Scene::SceneImpl::retain(name), &Scene::SceneImpl::release) {} + impl_(Scene::Impl::retain(name), &Scene::Impl::release) {} -void Scene::draw(Scalar alpha) const +void Scene::draw(Scalar alpha, const Camera& cam) const { // pass through - impl_->draw(alpha); + impl_->draw(alpha, cam); } void Scene::refresh() { - impl_->objects.clear(); + //impl_->objects.clear(); impl_->loadFromFile(); } +OctreePtr Scene::getOctree() const +{ + // pass through + return impl_->octree; +} + /** * Specialized search location for scene files. They can be found in the * "scenes" subdirectory of any of the searched directories. */ -std::string Scene::getPathToResource(const std::string& name) +std::string Scene::getPath(const std::string& name) { - return Resource::getPathToResource("scenes/" + name + ".json"); + return Resource::getPath("scenes/" + name + ".json"); }