Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/RayIntersector.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef RayIntersector_INCLUDE_ONCE
00033 #define RayIntersector_INCLUDE_ONCE
00034 
00035 #include <vlCore/Ray.hpp>
00036 #include <vlGraphics/Actor.hpp>
00037 #include <vlGraphics/Geometry.hpp>
00038 #include <vlCore/Vector4.hpp>
00039 #include <vlCore/Matrix4.hpp>
00040 #include <vlGraphics/Frustum.hpp>
00041 
00042 namespace vl
00043 {
00044   class SceneManager;
00045   //-----------------------------------------------------------------------------
00046   // RayIntersection
00047   //-----------------------------------------------------------------------------
00050   class RayIntersection: public Object
00051   {
00052     VL_INSTRUMENT_CLASS(vl::RayIntersection, Object)
00053 
00054   public:
00055     RayIntersection(): mActor(NULL), mDistance(0.0f) 
00056     {
00057       VL_DEBUG_SET_OBJECT_NAME()
00058     }
00059 
00061     void setActor(Actor* a) { mActor = a; }
00063     const Actor* actor() const { return mActor; }
00065     Actor* actor() { return mActor; }
00066 
00068     const vec3& intersectionPoint() const { return mIntersectionPoint; }
00070     void setIntersectionPoint(const vec3& v) { mIntersectionPoint = v; }
00071 
00073     real distance() const { return mDistance; }
00075     void setDistance(real dist) { mDistance = dist; }
00076 
00077   protected:
00078     vec3 mIntersectionPoint;
00079     Actor* mActor;
00080     real mDistance;
00081   };
00082   //-----------------------------------------------------------------------------
00083   // RayIntersectionGeometry
00084   //-----------------------------------------------------------------------------
00087   class RayIntersectionGeometry: public RayIntersection
00088   {
00089     VL_INSTRUMENT_CLASS(vl::RayIntersectionGeometry, RayIntersection)
00090 
00091   public:
00092     RayIntersectionGeometry(): mGeometry(NULL), mDrawCalls(NULL), mTriangleIndex(-1) 
00093     {
00094       VL_DEBUG_SET_OBJECT_NAME()
00095       memset(mTriangle, 0xFF, sizeof(mTriangle));
00096     }
00097 
00099     Geometry* geometry() { return mGeometry; }
00101     const Geometry* geometry() const { return mGeometry; }
00103     DrawCall* drawCalls() { return mDrawCalls; }
00105     const DrawCall* drawCalls() const { return mDrawCalls; }
00107     int triangleIndex() const { return mTriangleIndex; }
00109     int lineIndex() const { return mLineIndex; }
00111     const int* triangle() const { return mTriangle; }
00113     const int* line() { return mLine; }
00115     int nearestPoint() { return mNearestPoint; }
00116 
00118     void setGeometry(Geometry* g) { mGeometry = g; }
00120     void setPrimitives(DrawCall* p) { mDrawCalls = p; }
00122     void setTriangleIndex(int t_idx) { mTriangleIndex = t_idx; }
00124     void setTriangle(int a, int b, int c) { mTriangle[0] = a; mTriangle[1] = b; mTriangle[2] = c; }
00126     void setLineIndex(int l_idx) { mLineIndex = l_idx; }
00128     void setLine(int a, int b) { mLine[0] = a; mLine[1] = b; }
00130     void setNearestPoint(int a) { mNearestPoint = a; }
00131 
00132   protected:
00133     vec3 mIntersectionPoint;
00134     Geometry* mGeometry;
00135     DrawCall* mDrawCalls;
00136     int mTriangleIndex;
00137     int mTriangle[3];
00138     int mLine[2];
00139     int mNearestPoint;
00140     int mLineIndex;
00141     float mDistance;
00142   };
00143   //-----------------------------------------------------------------------------
00144   // RayIntersector
00145   //-----------------------------------------------------------------------------
00148   class VLGRAPHICS_EXPORT RayIntersector: public Object
00149   {
00150     VL_INSTRUMENT_CLASS(vl::RayIntersector, Object)
00151 
00152   public:
00153     RayIntersector()
00154     {
00155       VL_DEBUG_SET_OBJECT_NAME()
00156       mActors = new ActorCollection;
00157     }
00158 
00160     const ActorCollection* actors() const { return mActors.get(); }
00162     ActorCollection* actors() { return mActors.get(); }
00163 
00165     const Ray& ray() const { return mRay; }
00167     void setRay(const Ray& ray) { mRay = ray; }
00168 
00170     const Frustum& frustum() const { return mFrustum; }
00172     void setFrustum(const Frustum& frustum) { mFrustum = frustum; }
00173 
00175     const std::vector< ref<RayIntersection> >& intersections() const { return mIntersections; }
00176 
00181     void intersect();
00182 
00192     void intersect(const Ray& ray, SceneManager* scene_manager);
00193 
00194   protected:
00195     static bool sorter(const ref<RayIntersection>& a, const ref<RayIntersection>& b) { return a->distance() < b->distance(); }
00196 
00197     void intersect(Actor* act);
00198     void intersectGeometry(Actor* act, Geometry* geom);
00199 
00200     // T should be either fvec3-4 or dvec3-4
00201     template<class T>
00202     void intersectTriangle(const T& a, const T& b, const T& c, int ia, int ib, int ic, Actor*, Geometry* geom, DrawCall* prim, int prim_idx);
00203 
00204     template<class T>
00205     void intersectLine(const T& a, const T& b, int ia, int ib, Actor*, Geometry* geom, DrawCall* prim, int prim_idx);
00206 
00207     template<class T>
00208     void intersectPoint(const T& a, int ia, Actor*, Geometry* geom, DrawCall* prim, int prim_idx);
00209 
00210   protected:
00211     Frustum mFrustum;
00212     std::vector< ref<RayIntersection> > mIntersections;
00213     ref<ActorCollection> mActors;
00214     Ray mRay;
00215   };
00216 }
00217 
00218 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:39.
Permission is granted to use this page to write and publish articles regarding Visualization Library.