Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
RayIntersector.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
38 void RayIntersector::intersect(const Ray& ray, SceneManager* scene_manager)
39 {
40  actors()->clear();
41  scene_manager->extractVisibleActors( *actors(), NULL );
42  setRay(ray);
43  intersect();
44 }
45 //-----------------------------------------------------------------------------
47 {
48  mIntersections.clear();
49  for(size_t i=0; i<actors()->size(); ++i)
50  {
51  if (!frustum().cull(actors()->at(i)->boundingBox()))
52  {
53  intersect(actors()->at(i));
54  }
55  }
56 
57  std::sort( mIntersections.begin(), mIntersections.end(), sorter );
58 }
59 //-----------------------------------------------------------------------------
61 {
62  Geometry* geom = cast<Geometry>(act->lod(0));
63  if (geom)
64  intersectGeometry(act, geom);
65 }
66 //-----------------------------------------------------------------------------
68 {
69  ArrayAbstract* posarr = geom->vertexArray();
70  if (posarr)
71  {
72  mat4 matrix = act->transform() ? act->transform()->worldMatrix() : mat4();
73  for(size_t i=0; i<geom->drawCalls().size(); ++i)
74  {
75  DrawCall* prim = geom->drawCalls().at(i);
76  int itri = 0;
77  for(TriangleIterator trit = prim->triangleIterator(); trit.hasNext(); trit.next(), ++itri)
78  {
79  int ia = trit.a();
80  int ib = trit.b();
81  int ic = trit.c();
82  vec3 a = posarr->getAsVec3(ia);
83  vec3 b = posarr->getAsVec3(ib);
84  vec3 c = posarr->getAsVec3(ic);
85  if (act->transform())
86  {
87  a = matrix * a;
88  b = matrix * b;
89  c = matrix * c;
90  }
91  intersectTriangle(a, b, c, ia, ib, ic, act, geom, prim, itri);
92  }
93  }
94  }
95 }
96 //-----------------------------------------------------------------------------
97 template<class T>
98 void RayIntersector::intersectTriangle(const T& a, const T& b, const T& c, int ia, int ib, int ic, Actor* act, Geometry* geom, DrawCall* prim, int tri_idx)
99 {
100  T v1 = b-a;
101  T v2 = c-a;
102  T n = cross(v1,v2).normalize();
103  real det = (real)dot(n,(T)ray().direction());
104  if(det == 0)
105  return;
106  real t = (real)dot(n, a-(T)ray().origin()) / det;
107  if (t<0)
108  return;
109  vec3 rp = ray().origin() + ray().direction() * t;
110  T fp = (T)rp;
111  T pts[] = { a, b, c, a };
112  for(int i=0; i<3; ++i)
113  {
114  T bi_norm = -cross(pts[i+1]-pts[i],n).normalize();
115  if (dot(fp-pts[i],bi_norm) < 0)
116  return;
117  }
119  record->setIntersectionPoint( rp );
120  record->setTriangleIndex(tri_idx);
121  record->setTriangle(ia, ib, ic);
122  record->setActor(act);
123  record->setGeometry(geom);
124  record->setPrimitives(prim);
125  record->setDistance( t );
126  mIntersections.push_back(record);
127 }
128 //-----------------------------------------------------------------------------
The ArrayAbstract class defines an abstract interface to conveniently manipulate data stored in a Buf...
Definition: Array.hpp:58
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
bool hasNext()
Returns false if the iterator has reached the end of the triangle list.
const Renderable * lod(int lod_index) const
Returns the Renderable object representing the LOD level specifed by lod_index.
Definition: Actor.hpp:173
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:228
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
const vec3 & origin() const
Definition: Ray.hpp:45
void intersectGeometry(Actor *act, Geometry *geom)
The Ray class defines a ray as an origin and direction using real precision.
Definition: Ray.hpp:41
const ArrayAbstract * vertexArray() const
Conventional vertex array.
Definition: Geometry.hpp:248
void setDistance(real dist)
The intersection distance.
bool cull(const Sphere &sphere) const
Definition: Frustum.hpp:60
void intersect()
Executes the intersection test.
static bool sorter(const ref< RayIntersection > &a, const ref< RayIntersection > &b)
virtual void extractVisibleActors(ActorCollection &list, const Camera *camera)=0
Extracts all the enabled and visible Actors contained in the ActorTree hierarchy and appends them to ...
const vec3 & direction() const
Definition: Ray.hpp:44
fvec3 cross(const fvec3 &v1, const fvec3 &v2)
Definition: Vector3.hpp:278
virtual vec3 getAsVec3(size_t vector_index) const =0
Returns a vector from the buffer as a vec3 value.
void setTriangleIndex(int t_idx)
The starting index of the intersected primitive inside drawCalls()
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
Visualization Library main namespace.
float dot(float a, float b)
Definition: glsl_math.hpp:1111
void setActor(Actor *a)
The intersected Actor.
void setPrimitives(DrawCall *p)
The intersected DrawCall.
void setRay(const Ray &ray)
The ray in world coordinates to be intersected with the actors()
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)
virtual TriangleIterator triangleIterator() const =0
Returns a TriangleIterator used to iterate through the triangles of a DrawCall.
The RayIntersectionGeometry encapsulates all the information relative to a Ray/Actor intersection...
The SceneManager class is the base class for all the scene managers.
Iterator used to extract the indices of every single triangle of a DrawCall regardless of the primiti...
const Frustum & frustum() const
The frustum in world coordinates used to cull the objects.
const Ray & ray() const
The ray in world coordinates to be intersected with the actors()
#define NULL
Definition: OpenGLDefs.hpp:81
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
void setIntersectionPoint(const vec3 &v)
The intersection point, in world coordinates.
std::vector< ref< RayIntersection > > mIntersections
The base class of DrawArrays, DrawElements, MultiDrawElements and DrawRangeElements.
Definition: DrawCall.hpp:90
fmat4 mat4
Defined as: &#39;typedef fmat4 mat4&#39;. See also VL_PIPELINE_PRECISION.
Definition: Matrix4.hpp:1245
void setTriangle(int a, int b, int c)
An int[3] representing the indices of the intersected triangle.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
const ActorCollection * actors() const
The Actors against which the intersection test is performed.
size_t size() const
Definition: Collection.hpp:85
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.
Definition: Geometry.hpp:102
void setGeometry(Geometry *g)
The intersected Geometry.