SLProject  4.3.020
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  bool refractMC(SLRay* refracted, const SLMat3f& rotMat) const;
62  void diffuseMC(SLRay* scattered) const;
63  void print() const;
64 
65  //! Rotation matrix that maps a sample drawn around +z onto lobeAxis
66  static SLMat3f lobeToWorld(const SLVec3f& lobeAxis);
67 
68  // Helper methods
69  inline void setDir(const SLVec3f& Dir);
70  inline void setDirOS(const SLVec3f& Dir);
71  inline void normalizeNormal();
72  inline SLbool isShaded() const;
73  inline SLbool hitMatIsReflective() const;
74  inline SLbool hitMatIsTransparent() const;
75  inline SLbool hitMatIsDiffuse() const;
76 
77  // Classic ray members
78  SLVec3f origin; //!< Vector to the origin of ray in WS
79  SLVec3f dir; //!< Direction vector of ray in WS
80  SLfloat length; //!< length from origin to an intersection
81  SLint depth; //!< Recursion depth for ray tracing
82  SLfloat contrib; //!< Current contribution of ray to color
83  SLVec3f originOS; //!< Vector to the origin of ray in OS
84  SLVec3f dirOS; //!< Direction vector of ray in OS
85 
86  //! Total NO. of rays shot during RT
93 
94  // Additional info for intersection
95  SLRayType type; //!< PRIMARY, REFLECTED, REFRACTED, SHADOW
96  SLfloat lightDist; //!< Distance to light for shadow rays
97  SLfloat x, y; //!< Pixel position for primary rays
98  SLbool isOutside; //!< Flag if ray is inside of a material
99  SLbool isInsideVolume; //!< Flag if ray is in Volume
100  SLNode* srcNode; //!< Points to the node at ray origin
101  SLMesh* srcMesh; //!< Points to the mesh at ray origin
102  SLint srcTriangle; //!< Points to the triangle at ray origin
103  SLCol4f backgroundColor; //!< Background color at pixel x,y
104  SLSceneView* sv; //!< Pointer to the sceneview
105 
106  // Members set after at intersection
107  SLfloat hitU, hitV; //!< barycentric coords in hit triangle
108  SLNode* hitNode; //!< Points to the intersected node
109  SLMesh* hitMesh; //!< Points to the intersected mesh
110  SLint hitTriangle; //!< Points to the intersected triangle
111 
112  // Members set before shading
113  SLVec3f hitPoint; //!< Point of intersection
114  SLVec3f hitNormal; //!< Surface normal at intersection point
115  SLCol4f hitTexColor; //!< Color at intersection for texture or color attributes
116  SLfloat hitAO; //!< Ambient occlusion factor at intersection point
117 
118  // Helpers for fast AABB intersection
119  SLVec3f invDir; //!< Inverse ray dir for fast AABB hit in WS
120  SLVec3f invDirOS; //!< Inverse ray dir for fast AABB hit in OS
121  SLint sign[3]; //!< Sign of invDir for fast AABB hit in WS
122  SLint signOS[3]; //!< Sign of invDir for fast AABB hit in OS
123  SLfloat tmin; //!< min. dist. of last AABB intersection
124  SLfloat tmax; //!< max. dist. of last AABB intersection
125 
126  // static variables for statistics
127  static SLint maxDepth; //!< Max. recursion depth
128  static SLfloat minContrib; //!< Min. contibution to color (1/256)
129  static SLuint primaryRays; //!< NO. of primary rays shot
130  static SLuint reflectedRays; //!< NO. of reflected rays
131  static SLuint refractedRays; //!< NO. of refracted rays
132  static SLuint ignoredRays; //!< NO. of ignore refraction rays
133  static SLuint shadowRays; //!< NO. of shadow rays
134  static SLuint tirRays; //!< NO. of TIR refraction rays
135  static SLuint tests; //!< NO. of intersection tests
136  static SLuint intersections; //!< NO. of intersection
137  static SLint depthReached; //!< depth reached for a primary ray
138  static SLint maxDepthReached; //!< max. depth reached for all rays
139  static SLfloat avgDepth; //!< average depth reached
140  static SLuint subsampledRays; //!< NO. of of subsampled rays
141  static SLuint subsampledPixels; //!< NO. of of subsampled pixels
142 };
143 
144 //-----------------------------------------------------------------------------
145 // inline functions
146 //-----------------------------------------------------------------------------
147 //! Setter for the rays direction in world space also setting the inverse direction
148 inline void
150 {
151  dir = Dir;
152  invDir.x = (SLfloat)(1 / dir.x);
153  invDir.y = (SLfloat)(1 / dir.y);
154  invDir.z = (SLfloat)(1 / dir.z);
155  sign[0] = (invDir.x < 0);
156  sign[1] = (invDir.y < 0);
157  sign[2] = (invDir.z < 0);
158 }
159 //-----------------------------------------------------------------------------
160 //! Setter for the rays direction in object space also setting the inverse direction
161 inline void
163 {
164  dirOS = Dir;
165  invDirOS.x = (SLfloat)(1 / dirOS.x);
166  invDirOS.y = (SLfloat)(1 / dirOS.y);
167  invDirOS.z = (SLfloat)(1 / dirOS.z);
168  signOS[0] = (invDirOS.x < 0);
169  signOS[1] = (invDirOS.y < 0);
170  signOS[2] = (invDirOS.z < 0);
171 }
172 //-----------------------------------------------------------------------------
173 /*!
174 SLRay::normalizeNormal does a careful normalization of the normal only when the
175 squared length is > 1.0+FLT_EPSILON or < 1.0-FLT_EPSILON.
176 */
177 inline void
179 {
180  SLfloat nLenSqr = hitNormal.lengthSqr();
181  if (nLenSqr > 1.0f + FLT_EPSILON || nLenSqr < 1.0f - FLT_EPSILON)
182  {
183  SLfloat len = sqrt(nLenSqr);
184  hitNormal /= len;
185  }
186 }
187 //-----------------------------------------------------------------------------
188 //! Returns true if a shadow ray hits an object on the ray to the light
189 inline SLbool
191 {
192  return type == SHADOW && length < lightDist;
193 }
194 //-----------------------------------------------------------------------------
195 //! Returns true if the hit material specular color is not black
196 inline SLbool
198 {
199  if (!hitMesh) return false;
200  SLMaterial* mat = hitMesh->mat();
201  return ((mat->specular().r > 0.0f) ||
202  (mat->specular().g > 0.0f) ||
203  (mat->specular().b > 0.0f));
204 }
205 //-----------------------------------------------------------------------------
206 //! Returns true if the hit material transmission color is not black
207 inline SLbool
209 {
210  if (!hitMesh) return false;
211  SLMaterial* mat = hitMesh->mat();
212  return ((mat->transmissive().r > 0.0f) ||
213  (mat->transmissive().g > 0.0f) ||
214  (mat->transmissive().b > 0.0f));
215 }
216 //-----------------------------------------------------------------------------
217 //! Returns true if the hit material diffuse color is not black
218 inline SLbool
220 {
221  if (!hitMesh) return false;
222  SLMaterial* mat = hitMesh->mat();
223  return ((mat->diffuse().r > 0.0f) ||
224  (mat->diffuse().g > 0.0f) ||
225  (mat->diffuse().b > 0.0f));
226 }
227 //-----------------------------------------------------------------------------
228 #endif
float SLfloat
analog to GLfloat
Definition: SL.h:200
unsigned int SLuint
analog to GLuint
Definition: SL.h:198
bool SLbool
analog to GLbool
Definition: SL.h:202
int SLint
analog to GLint
Definition: SL.h:197
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:148
Ray class with ray and intersection properties.
Definition: SLRay.h:40
SLint signOS[3]
Sign of invDir for fast AABB hit in OS.
Definition: SLRay.h:122
SLfloat tmax
max. dist. of last AABB intersection
Definition: SLRay.h:124
static SLint maxDepth
Max. recursion depth.
Definition: SLRay.h:127
static SLuint shadowRays
NO. of shadow rays.
Definition: SLRay.h:133
SLint hitTriangle
Points to the intersected triangle.
Definition: SLRay.h:110
static SLuint tirRays
NO. of TIR refraction rays.
Definition: SLRay.h:134
SLbool hitMatIsTransparent() const
Returns true if the hit material transmission color is not black.
Definition: SLRay.h:208
SLVec3f origin
Vector to the origin of ray in WS.
Definition: SLRay.h:78
static SLuint primaryRays
NO. of primary rays shot.
Definition: SLRay.h:129
bool reflectMC(SLRay *reflected, const SLMat3f &rotMat) const
Definition: SLRay.cpp:413
SLfloat lightDist
Distance to light for shadow rays.
Definition: SLRay.h:96
SLRayType type
PRIMARY, REFLECTED, REFRACTED, SHADOW.
Definition: SLRay.h:95
SLint srcTriangle
Points to the triangle at ray origin.
Definition: SLRay.h:102
static SLuint intersections
NO. of intersection.
Definition: SLRay.h:136
SLCol4f backgroundColor
Background color at pixel x,y.
Definition: SLRay.h:103
static SLint depthReached
depth reached for a primary ray
Definition: SLRay.h:137
static SLfloat minContrib
Min. contibution to color (1/256)
Definition: SLRay.h:128
SLMesh * hitMesh
Points to the intersected mesh.
Definition: SLRay.h:109
SLbool isOutside
Flag if ray is inside of a material.
Definition: SLRay.h:98
static SLint maxDepthReached
max. depth reached for all rays
Definition: SLRay.h:138
SLint depth
Recursion depth for ray tracing.
Definition: SLRay.h:81
SLfloat hitAO
Ambient occlusion factor at intersection point.
Definition: SLRay.h:116
SLVec3f dir
Direction vector of ray in WS.
Definition: SLRay.h:79
SLVec3f invDirOS
Inverse ray dir for fast AABB hit in OS.
Definition: SLRay.h:120
void normalizeNormal()
Definition: SLRay.h:178
SLMesh * srcMesh
Points to the mesh at ray origin.
Definition: SLRay.h:101
void refract(SLRay *refracted)
Definition: SLRay.cpp:218
static SLMat3f lobeToWorld(const SLVec3f &lobeAxis)
Rotation matrix that maps a sample drawn around +z onto lobeAxis.
Definition: SLRay.cpp:372
SLbool hitMatIsReflective() const
Returns true if the hit material specular color is not black.
Definition: SLRay.h:197
SLint sign[3]
Sign of invDir for fast AABB hit in WS.
Definition: SLRay.h:121
SLfloat tmin
min. dist. of last AABB intersection
Definition: SLRay.h:123
SLfloat length
length from origin to an intersection
Definition: SLRay.h:80
void print() const
Definition: SLRay.cpp:160
SLNode * hitNode
Points to the intersected node.
Definition: SLRay.h:108
void setDirOS(const SLVec3f &Dir)
Setter for the rays direction in object space also setting the inverse direction.
Definition: SLRay.h:162
static SLuint ignoredRays
NO. of ignore refraction rays.
Definition: SLRay.h:132
SLfloat contrib
Current contribution of ray to color.
Definition: SLRay.h:82
SLbool isInsideVolume
Flag if ray is in Volume.
Definition: SLRay.h:99
SLVec3f hitPoint
Point of intersection.
Definition: SLRay.h:113
void diffuseMC(SLRay *scattered) const
Definition: SLRay.cpp:542
static SLuint subsampledPixels
NO. of of subsampled pixels.
Definition: SLRay.h:141
static SLuint totalNumRays()
Total NO. of rays shot during RT.
Definition: SLRay.h:87
SLNode * srcNode
Points to the node at ray origin.
Definition: SLRay.h:100
void setDir(const SLVec3f &Dir)
Setter for the rays direction in world space also setting the inverse direction.
Definition: SLRay.h:149
SLVec3f dirOS
Direction vector of ray in OS.
Definition: SLRay.h:84
bool refractMC(SLRay *refracted, const SLMat3f &rotMat) const
Definition: SLRay.cpp:478
static SLuint tests
NO. of intersection tests.
Definition: SLRay.h:135
void reflect(SLRay *reflected) const
Definition: SLRay.cpp:177
static SLuint reflectedRays
NO. of reflected rays.
Definition: SLRay.h:130
SLCol4f hitTexColor
Color at intersection for texture or color attributes.
Definition: SLRay.h:115
SLfloat y
Pixel position for primary rays.
Definition: SLRay.h:97
SLVec3f invDir
Inverse ray dir for fast AABB hit in WS.
Definition: SLRay.h:119
SLVec3f originOS
Vector to the origin of ray in OS.
Definition: SLRay.h:83
SLfloat x
Definition: SLRay.h:97
SLVec3f hitNormal
Surface normal at intersection point.
Definition: SLRay.h:114
SLSceneView * sv
Pointer to the sceneview.
Definition: SLRay.h:104
static SLfloat avgDepth
average depth reached
Definition: SLRay.h:139
SLbool hitMatIsDiffuse() const
Returns true if the hit material diffuse color is not black.
Definition: SLRay.h:219
SLRay(SLSceneView *sv=nullptr)
default ctor
Definition: SLRay.cpp:65
SLbool isShaded() const
Returns true if a shadow ray hits an object on the ray to the light.
Definition: SLRay.h:190
static SLuint refractedRays
NO. of refracted rays.
Definition: SLRay.h:131
SLfloat hitV
barycentric coords in hit triangle
Definition: SLRay.h:107
static SLuint subsampledRays
NO. of of subsampled rays.
Definition: SLRay.h:140
SLfloat hitU
Definition: SLRay.h:107
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