Sirikata
liboh/plugins/ogre/OgreMeshRaytrace.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  OgreMeshRaytrace.hpp
00003  *
00004  *  Copyright (c) 2009, Patrick Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _OGRE_MESH_HPP_
00034 #define _OGRE_MESH_HPP_
00035 
00036 #include <vector>
00037 #include "OgreVector3.h"
00038 namespace Ogre {
00039   class Entity;
00040   class SubMesh;
00041   class Ray;
00042 }
00043 
00044 namespace Sirikata { namespace Graphics {
00045 
00046 struct TriVertex {
00047   Ogre::Vector3 coord;
00048   float u;
00049   float v;
00050   TriVertex() {}
00051   TriVertex(const Ogre::Vector3& coord, float u, float v)
00052     : coord(coord), u(u), v(v) {
00053   }
00054 };
00055 
00056 struct Triangle {
00057   Triangle() {}
00058   Triangle(const TriVertex &v1, const TriVertex &v2, const TriVertex &v3) : v1(v1), v2(v2), v3(v3)
00059   {
00060   }
00061 
00062   TriVertex v1, v2, v3;
00063 };
00064 
00065 struct IntersectResult {
00066     bool intersected;
00067     double distance;
00068     Vector3f normal;
00069     Triangle tri;
00070     float u;
00071     float v;
00072     IntersectResult()
00073     : intersected(false),
00074       distance(std::numeric_limits<Ogre::Real>::max()),
00075       normal(Vector3f(0,0,0)),
00076       u(0),
00077       v(0) {}
00078 };
00079 
00083 class OgreMesh {
00084 public:
00085   static Ogre::Ray transformRay(Ogre::Node *entity, const Ogre::Ray &original);
00086   static bool intersectTri(const Ogre::Ray &ray, IntersectResult &rtn, Triangle*itr, bool plane);
00087   void  intersect(Ogre::Node *node, const Ogre::Ray &ray, IntersectResult &res);
00088   OgreMesh(Ogre::SubMesh *submesh, bool texcoord, std::vector<TriVertex>&sharedVertices);
00089 protected:
00090   void syncFromOgreMesh(Ogre::SubMesh *mSubMesh, bool texcoord, std::vector<TriVertex>&sharedVertices);
00091   std::vector<Triangle> mTriangles;
00092 public:
00093   int64 size()const;
00094 };
00095 
00096 } }
00097 
00098 #endif