From 9c3944c98ec4ca334458a719d2f6bcbfcf04f883 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 20 Feb 2008 02:46:32 +0000 Subject: [PATCH] added axis-aligned bounding box computation and retrieval function git-svn-id: svn://anubis/misc/wfobj@40 bd8a9e45-a331-0410-811e-c64571078777 --- WFObj.cc | 27 +++++++++++++++++++++++++++ WFObj.hh | 8 ++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/WFObj.cc b/WFObj.cc index c2a5ff5..9320fb9 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -72,6 +72,7 @@ static string basePath(const string & str) void WFObj::clear() { m_data = std::vector< std::vector >(); + m_loadedVertex = false; } int WFObj::filesize(const char * filename) @@ -304,6 +305,32 @@ GLuint WFObj::render() return list; } +void WFObj::updateAABB(const float * const vertex) +{ + if (m_loadedVertex) + { + if (vertex[0] < m_aabb[0]) + m_aabb[0] = vertex[0]; + else if (vertex[0] > m_aabb[3]) + m_aabb[3] = vertex[0]; + if (vertex[1] < m_aabb[1]) + m_aabb[1] = vertex[1]; + else if (vertex[1] > m_aabb[4]) + m_aabb[4] = vertex[1]; + if (vertex[2] < m_aabb[2]) + m_aabb[2] = vertex[2]; + else if (vertex[2] > m_aabb[5]) + m_aabb[5] = vertex[2]; + } + else + { + m_aabb[0] = m_aabb[3] = vertex[0]; + m_aabb[1] = m_aabb[4] = vertex[1]; + m_aabb[2] = m_aabb[5] = vertex[2]; + m_loadedVertex = true; + } +} + WFObj::Vertex WFObj::readVertex(const vector & parts) { int partslen = parts.size(); diff --git a/WFObj.hh b/WFObj.hh index f5dde51..4adaa21 100644 --- a/WFObj.hh +++ b/WFObj.hh @@ -10,8 +10,9 @@ public: typedef GLuint (*loadTextureFunc_t)(const char * texture); bool load(const std::string & filename, loadTextureFunc_t loadTexture); GLuint render(); + const float * const getAABB() { return m_aabb; } -private: +protected: /* types */ class Vertex { @@ -29,11 +30,14 @@ private: void processInputLine(const std::string & input); Vertex readVertex(const std::vector & parts); void parseVertexIndices(const std::string & vtxref, int * ret); + void updateAABB(const float * const vertex); /* variables */ std::vector< std::vector > m_data; loadTextureFunc_t m_loadTexture; std::string m_fileName; + float m_aabb[6]; + bool m_loadedVertex; }; @@ -44,7 +48,7 @@ public: void renderBegin(const std::string & mtlname); void renderEnd(const std::string & mtlname); -private: +protected: /* methods */ void clear(); int filesize(const char * filename);