SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRay.h
Go to the documentation of this file.
1 /**
2  * \file SLRay.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLRAY_H
11 #define SLRAY_H
12 
13 #include <SLMaterial.h>
14 #include <SLMesh.h>
15 
16 class SLNode;
17 class SLSceneView;
18 
19 //-----------------------------------------------------------------------------
20 //! SLRayType enumeration for specifying ray type in ray tracing
22 {
23  PRIMARY = 0,
24  REFLECTED = 1,
25  REFRACTED = 2,
26  SHADOW = 3
27 };
28 
29 //! Ray tracing constant for max. allowed recursion depth
30 #define SL_MAXTRACE 15
31 //-----------------------------------------------------------------------------
32 //! Ray class with ray and intersection properties
33 /*!
34 Ray class for Ray Tracing. It not only holds informations about the ray itself
35 but also about the node hit by the ray. With that information the method
36 reflect calculates a reflected ray and the method transmit calculates a
37 REFRACTED ray.
38 */
39 class SLRay
40 {
41 public:
42  //! default ctor
43  explicit SLRay(SLSceneView* sv = nullptr);
44 
45  //! ctor for primary rays
46  SLRay(const SLVec3f& Origin,
47  const SLVec3f& Dir,
48  SLfloat X,
49  SLfloat Y,
50  const SLCol4f& backColor,
51  SLSceneView* sv);
52 
53  //! ctor for shadow rays
54  SLRay(SLfloat distToLight,
55  const SLVec3f& dirToLight,
56  SLRay* rayFromHitPoint);
57 
58  void reflect(SLRay* reflected) const;
59  void refract(SLRay* refracted);
60  bool reflectMC(SLRay* reflected, const SLMat3f& rotMat) const;
61  void refractMC(SLRay* refracted, const SLMat3f& rotMat) const;
62  void diffuseMC(SLRay* scattered) const;
63  void print() const;
64 
65  // Helper methods
66  inline void setDir(const SLVec3f& Dir);
67  inline void setDirOS(const SLVec3f& Dir);
68  inline void normalizeNormal();
69  inline SLbool isShaded() const;
70  inline SLbool hitMatIsReflective() const;
71  inline SLbool hitMatIsTransparent() const;
72  inline SLbool hitMatIsDiffuse() const;
73 
74  // Classic ray members
75  SLVec3f origin; //!< Vector to the origin of ray in WS
76  SLVec3f dir; //!< Direction vector of ray in WS
77  SLfloat length; //!< length from origin to an intersection
78  SLint depth; //!< Recursion depth for ray tracing
79  SLfloat contrib; //!< Current contribution of ray to color
80  SLVec3f originOS; //!< Vector to the origin of ray in OS
81  SLVec3f dirOS; //!< Direction vector of ray in OS
82 
83  //! Total NO. of rays shot during RT
90 
91  // Additional info for intersection
92  SLRayType type; //!< PRIMARY, REFLECTED, REFRACTED, SHADOW
93  SLfloat lightDist; //!< Distance to light for shadow rays
94  SLfloat x, y; //!< Pixel position for primary rays
95  SLbool isOutside; //!< Flag if ray is inside of a material
96  SLbool isInsideVolume; //!< Flag if ray is in Volume
97  SLNode* srcNode; //!< Points to the node at ray origin
98  SLMesh* srcMesh; //!< Points to the mesh at ray origin
99  SLint srcTriangle; //!< Points to the triangle at ray origin
100  SLCol4f backgroundColor; //!< Background color at pixel x,y
101  SLSceneView* sv; //!< Pointer to the sceneview
102 
103  // Members set after at intersection
104  SLfloat hitU, hitV; //!< barycentric coords in hit triangle
105  SLNode* hitNode; //!< Points to the intersected node
106  SLMesh* hitMesh; //!< Points to the intersected mesh
107  SLint hitTriangle; //!< Points to the intersected triangle
108 
109  // Members set before shading
110  SLVec3f hitPoint; //!< Point of intersection
111  SLVec3f hitNormal; //!< Surface normal at intersection point
112  SLCol4f hitTexColor; //!< Color at intersection for texture or color attributes
113  SLfloat hitAO; //!< Ambient occlusion factor at intersection point
114 
115  // Helpers for fast AABB intersection
116  SLVec3f invDir; //!< Inverse ray dir for fast AABB hit in WS
117  SLVec3f invDirOS; //!< Inverse ray dir for fast AABB hit in OS
118  SLint sign[3]; //!< Sign of invDir for fast AABB hit in WS
119  SLint signOS[3]; //!< Sign of invDir for fast AABB hit in OS
120  SLfloat tmin; //!< min. dist. of last AABB intersection
121  SLfloat tmax; //!< max. dist. of last AABB intersection
122 
123  // static variables for statistics
124  static SLint maxDepth; //!< Max. recursion depth
125  static SLfloat minContrib; //!< Min. contibution to color (1/256)
126  static SLuint primaryRays; //!< NO. of primary rays shot
127  static SLuint reflectedRays; //!< NO. of reflected rays
128  static SLuint refractedRays; //!< NO. of refracted rays
129  static SLuint ignoredRays; //!< NO. of ignore refraction rays
130  static SLuint shadowRays; //!< NO. of shadow rays
131  static SLuint tirRays; //!< NO. of TIR refraction rays
132  static SLuint tests; //!< NO. of intersection tests
133  static SLuint intersections; //!< NO. of intersection
134  static SLint depthReached; //!< depth reached for a primary ray
135  static SLint maxDepthReached; //!< max. depth reached for all rays
136  static SLfloat avgDepth; //!< average depth reached
137  static SLuint subsampledRays; //!< NO. of of subsampled rays
138  static SLuint subsampledPixels; //!< NO. of of subsampled pixels
139 };
140 
141 //-----------------------------------------------------------------------------
142 // inline functions
143 //-----------------------------------------------------------------------------
144 //! Setter for the rays direction in world space also setting the inverse direction
145 inline void
147 {
148  dir = Dir;
149  invDir.x = (SLfloat)(1 / dir.x);
150  invDir.y = (SLfloat)(1 / dir.y);
151  invDir.z = (SLfloat)(1 / dir.z);
152  sign[0] = (invDir.x < 0);
153  sign[1] = (invDir.y < 0);
154  sign[2] = (invDir.z < 0);
155 }
156 //-----------------------------------------------------------------------------
157 //! Setter for the rays direction in object space also setting the inverse direction
158 inline void
160 {
161  dirOS = Dir;
162  invDirOS.x = (SLfloat)(1 / dirOS.x);
163  invDirOS.y = (SLfloat)(1 / dirOS.y);
164  invDirOS.z = (SLfloat)(1 / dirOS.z);
165  signOS[0] = (invDirOS.x < 0);
166  signOS[1] = (invDirOS.y < 0);
167  signOS[2] = (invDirOS.z < 0);
168 }
169 //-----------------------------------------------------------------------------
170 /*!
171 SLRay::normalizeNormal does a careful normalization of the normal only when the
172 squared length is > 1.0+FLT_EPSILON or < 1.0-FLT_EPSILON.
173 */
174 inline void
176 {
177  SLfloat nLenSqr = hitNormal.lengthSqr();
178  if (nLenSqr > 1.0f + FLT_EPSILON || nLenSqr < 1.0f - FLT_EPSILON)
179  {
180  SLfloat len = sqrt(nLenSqr);
181  hitNormal /= len;
182  }
183 }
184 //-----------------------------------------------------------------------------
185 //! Returns true if a shadow ray hits an object on the ray to the light
186 inline SLbool
188 {
189  return type == SHADOW && length < lightDist;
190 }
191 //-----------------------------------------------------------------------------
192 //! Returns true if the hit material specular color is not black
193 inline SLbool
195 {
196  if (!hitMesh) return false;
197  SLMaterial* mat = hitMesh->mat();
198  return ((mat->specular().r > 0.0f) ||
199  (mat->specular().g > 0.0f) ||
200  (mat->specular().b > 0.0f));
201 }
202 //-----------------------------------------------------------------------------
203 //! Returns true if the hit material transmission color is not black
204 inline SLbool
206 {
207  if (!hitMesh) return false;
208  SLMaterial* mat = hitMesh->mat();
209  return ((mat->transmissive().r > 0.0f) ||
210  (mat->transmissive().g > 0.0f) ||
211  (mat->transmissive().b > 0.0f));
212 }
213 //-----------------------------------------------------------------------------
214 //! Returns true if the hit material diffuse color is not black
215 inline SLbool
217 {
218  if (!hitMesh) return false;
219  SLMaterial* mat = hitMesh->mat();
220  return ((mat->diffuse().r > 0.0f) ||
221  (mat->diffuse().g > 0.0f) ||
222  (mat->diffuse().b > 0.0f));
223 }
224 //-----------------------------------------------------------------------------
225 #endif
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
int SLint
Definition: SL.h:170
SLRayType
SLRayType enumeration for specifying ray type in ray tracing.
Definition: SLRay.h:22
@ SHADOW
Definition: SLRay.h:26
@ PRIMARY
Definition: SLRay.h:23
@ REFLECTED
Definition: SLRay.h:24
@ REFRACTED
Definition: SLRay.h:25
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
void specular(const SLCol4f &spec)
Definition: SLMaterial.h:173
void diffuse(const SLCol4f &diff)
Definition: SLMaterial.h:171
void transmissive(const SLCol4f &transm)
Definition: SLMaterial.h:175
An SLMesh object is a triangulated mesh, drawn with one draw call.
Definition: SLMesh.h:134
SLMaterial * mat() const
Definition: SLMesh.h:177
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
Ray class with ray and intersection properties.
Definition: SLRay.h:40
SLfloat tmax
max. dist. of last AABB intersection
Definition: SLRay.h:121
SLint sign[3]
Sign of invDir for fast AABB hit in WS.
Definition: SLRay.h:118
SLint hitTriangle
Points to the intersected triangle.
Definition: SLRay.h:107
SLbool hitMatIsTransparent() const
Returns true if the hit material transmission color is not black.
Definition: SLRay.h:205
SLVec3f origin
Vector to the origin of ray in WS.
Definition: SLRay.h:75
SLRay()
default ctor
Definition: SLRayMC.cpp:44
static SLint depthReached
depth reached for a primary ray
Definition: SLRay.h:134
static SLuint intersections
NO. of intersection.
Definition: SLRay.h:133
static SLuint primaryRays
NO. of primary rays shot.
Definition: SLRay.h:126
bool reflectMC(SLRay *reflected, const SLMat3f &rotMat) const
Definition: SLRay.cpp:332
SLfloat lightDist
Distance to light for shadow rays.
Definition: SLRay.h:93
static SLuint tirRays
NO. of TIR refraction rays.
Definition: SLRay.h:131
SLRayType type
PRIMARY, REFLECTED, REFRACTED, SHADOW.
Definition: SLRay.h:92
SLint srcTriangle
Points to the triangle at ray origin.
Definition: SLRay.h:99
SLCol4f backgroundColor
Background color at pixel x,y.
Definition: SLRay.h:100
static SLuint tests
NO. of intersection tests.
Definition: SLRay.h:132
SLMesh * hitMesh
Points to the intersected mesh.
Definition: SLRay.h:106
SLbool isOutside
Flag if ray is inside of a material.
Definition: SLRay.h:95
static SLuint reflectedRays
NO. of reflected rays.
Definition: SLRay.h:127
SLint depth
Recursion depth for ray tracing.
Definition: SLRay.h:78
SLfloat hitAO
Ambient occlusion factor at intersection point.
Definition: SLRay.h:113
SLVec3f dir
Direction vector of ray in WS.
Definition: SLRay.h:76
SLVec3f invDirOS
Inverse ray dir for fast AABB hit in OS.
Definition: SLRay.h:117
void normalizeNormal()
Definition: SLRay.h:175
SLMesh * srcMesh
Points to the mesh at ray origin.
Definition: SLRay.h:98
void refract(SLRay *refracted)
Definition: SLRay.cpp:197
SLbool hitMatIsReflective() const
Returns true if the hit material specular color is not black.
Definition: SLRay.h:194
SLfloat tmin
min. dist. of last AABB intersection
Definition: SLRay.h:120
static SLint maxDepthReached
max. depth reached for all rays
Definition: SLRay.h:135
static SLuint refractedRays
NO. of refracted rays.
Definition: SLRay.h:128
SLfloat length
length from origin to an intersection
Definition: SLRay.h:77
void print() const
Definition: SLRay.cpp:139
SLNode * hitNode
Points to the intersected node.
Definition: SLRay.h:105
void setDirOS(const SLVec3f &Dir)
Setter for the rays direction in object space also setting the inverse direction.
Definition: SLRay.h:159
static SLuint ignoredRays
NO. of ignore refraction rays.
Definition: SLRay.h:129
SLfloat contrib
Current contribution of ray to color.
Definition: SLRay.h:79
static SLuint subsampledPixels
NO. of of subsampled pixels.
Definition: SLRay.h:138
SLbool isInsideVolume
Flag if ray is in Volume.
Definition: SLRay.h:96
SLVec3f hitPoint
Point of intersection.
Definition: SLRay.h:110
void diffuseMC(SLRay *scattered) const
Definition: SLRay.cpp:429
static SLuint totalNumRays()
Total NO. of rays shot during RT.
Definition: SLRay.h:84
SLNode * srcNode
Points to the node at ray origin.
Definition: SLRay.h:97
void setDir(const SLVec3f &Dir)
Setter for the rays direction in world space also setting the inverse direction.
Definition: SLRay.h:146
SLVec3f dirOS
Direction vector of ray in OS.
Definition: SLRay.h:81
static SLfloat avgDepth
average depth reached
Definition: SLRay.h:136
static SLfloat minContrib
Min. contibution to color (1/256)
Definition: SLRay.h:125
void reflect(SLRay *reflected) const
Definition: SLRay.cpp:156
SLint signOS[3]
Sign of invDir for fast AABB hit in OS.
Definition: SLRay.h:119
static SLuint subsampledRays
NO. of of subsampled rays.
Definition: SLRay.h:137
SLCol4f hitTexColor
Color at intersection for texture or color attributes.
Definition: SLRay.h:112
SLfloat y
Pixel position for primary rays.
Definition: SLRay.h:94
SLVec3f invDir
Inverse ray dir for fast AABB hit in WS.
Definition: SLRay.h:116
SLVec3f originOS
Vector to the origin of ray in OS.
Definition: SLRay.h:80
SLfloat x
Definition: SLRay.h:94
void refractMC(SLRay *refracted, const SLMat3f &rotMat) const
Definition: SLRay.cpp:384
static SLuint shadowRays
NO. of shadow rays.
Definition: SLRay.h:130
SLVec3f hitNormal
Surface normal at intersection point.
Definition: SLRay.h:111
SLSceneView * sv
Pointer to the sceneview.
Definition: SLRay.h:101
SLbool hitMatIsDiffuse() const
Returns true if the hit material diffuse color is not black.
Definition: SLRay.h:216
SLbool isShaded() const
Returns true if a shadow ray hits an object on the ray to the light.
Definition: SLRay.h:187
static SLint maxDepth
Max. recursion depth.
Definition: SLRay.h:124
SLfloat hitV
barycentric coords in hit triangle
Definition: SLRay.h:104
SLfloat hitU
Definition: SLRay.h:104
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43
T lengthSqr() const
Definition: SLVec3.h:123