SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRayMC.h
Go to the documentation of this file.
1 /**
2  * \file SLRay.h
3  * \date February 2013
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 <stdafx.h>
14 #include <randomc.h> // high qualtiy random generators
15 #include <SLMaterial.h>
16 
17 struct SLFace;
18 class SLShape;
19 
20 //-----------------------------------------------------------------------------
22 {
23  PRIMARY = 0,
24  REFLECTED = 1,
26  SHADOW = 3
27 };
28 #define SL_MAXTRACE 15
29 
30 //-----------------------------------------------------------------------------
31 //! Ray class with ray and intersection properties
32 /*!
33 Ray class for Ray Tracing. It not only holds informations about the ray itself
34 but also about the node hit by the ray. With that information the method
35 reflect calculates a reflected ray and the method transmit calculates a
36 transmitted ray.
37 */
38 class SLRay
39 {
40 public:
41  //! default ctor
42  SLRay();
43 
44  //! ctor for primary rays
45  SLRay(SLVec3f Origin,
46  SLVec3f Dir,
47  SLint X,
48  SLint Y);
49 
50  //! ctor for shadow rays
51  SLRay(SLfloat distToLight,
52  SLVec3f dirToLight,
53  SLRay* rayFromHitPoint);
54 
55  //! ctor for all parameters
57  SLVec3f dir,
59  SLShape* originShape,
60  SLfloat length = SL_FLOAT_MAX,
61  SLint depth = 1);
62 
63  void reflect(SLRay* reflected);
64  void refract(SLRay* refracted);
65  bool reflectMC(SLRay* reflected, SLMat3f rotMat);
66  void refractMC(SLRay* refracted, SLMat3f rotMat);
67  void diffuseMC(SLRay* scattered);
68 
69  // Helper methods
70  inline void setDir(SLVec3f Dir)
71  {
72  dir = Dir;
73  invDir.x = (SLfloat)(1 / dir.x);
74  invDir.y = (SLfloat)(1 / dir.y);
75  invDir.z = (SLfloat)(1 / dir.z);
76  sign[0] = (invDir.x < 0);
77  sign[1] = (invDir.y < 0);
78  sign[2] = (invDir.z < 0);
79  }
80  inline void setDirOS(SLVec3f Dir)
81  {
82  dirOS = Dir;
83  invDirOS.x = (SLfloat)(1 / dirOS.x);
84  invDirOS.y = (SLfloat)(1 / dirOS.y);
85  invDirOS.z = (SLfloat)(1 / dirOS.z);
86  signOS[0] = (invDirOS.x < 0);
87  signOS[1] = (invDirOS.y < 0);
88  signOS[2] = (invDirOS.z < 0);
89  }
90  SLbool isShaded() { return type == SHADOW && length < lightDist; }
91  void print();
93 
94  // Classic ray members
95  SLVec3f origin; //!< Vector to the origin of ray in WS
96  SLVec3f dir; //!< Direction vector of ray in WS
97  SLVec3f originOS; //!< Vector to the origin of ray in OS
98  SLVec3f dirOS; //!< Direction vector of ray in OS
99  SLfloat length; //!< length from origin to an intersection
100  SLint depth; //!< Recursion depth for ray tracing
101  SLfloat contrib; //!< Current contibution of ray to color
102 
103  // Additional info for intersection
104  SLRayType type; //!< PRIMARY, REFLECTED, TRANSMITTED, SHADOW
105  SLfloat lightDist; //!< Distance to light for shadow rays
106  SLfloat x, y; //!< Pixel position for primary rays
107  SLbool isOutside; //!< Flag if ray is inside of a material
108  SLShape* originShape; //!< Points to the shape at ray origin
109  SLFace* originTria; //!< Points to the triangle at ray origin
110  SLMaterial* originMat; //!< Points to appearance at ray origin
111 
112  // Members set after at intersection
113  SLfloat hitU, hitV; //!< barycentric coords in hit triangle
114  SLShape* hitShape; //!< Points to the intersected shape
115  SLFace* hitTriangle; //!< Points to the intersected triangle
116  SLMaterial* hitMat; //!< Points to material of intersected node
117 
118  // Members set before shading
119  SLVec3f hitPoint; //!< Point of intersection
120  SLVec3f hitNormal; //!< Surface normal at intersection point
121  SLCol4f hitTexCol; //!< Texture color at intersection point
122 
123  // Helpers for fast AABB intersection
124  SLVec3f invDir; //!< Inverse ray dir for fast AABB hit in WS
125  SLVec3f invDirOS; //!< Inverse ray dir for fast AABB hit in OS
126  SLint sign[3]; //!< Sign of invDir for fast AABB hit in WS
127  SLint signOS[3]; //!< Sign of invDir for fast AABB hit in OS
128  SLfloat tmin; //!< min. dist. of last AABB intersection
129  SLfloat tmax; //!< max. dist. of last AABB intersection
130 
131  // static variables for statistics
132  static SLint maxDepth; //!< Max. recursion depth
133  static SLfloat minContrib; //!< Min. contibution to color (1/256)
134  static SLuint reflectedRays; //!< NO. of reflected rays
135  static SLuint refractedRays; //!< NO. of transmitted rays
136  static SLuint shadowRays; //!< NO. of shadow rays
137  static SLuint tirRays; //!< NO. of TIR refraction rays
138  static SLuint tests; //!< NO. of intersection tests
139  static SLuint intersections; //!< NO. of intersection
140  static SLint depthReached; //!< depth reached for a primary ray
141  static SLint maxDepthReached; //!< max. depth reached for all rays
142  static SLfloat avgDepth; //!< average depth reached
143  static SLuint subsampledRays; //!< NO. of of subsampled rays
144  static SLuint subsampledPixels; //!< NO. of of subsampled pixels
145 
146  // statistics for photonmapping
147  static SLlong emittedPhotons; //!< NO. of emitted photons from all lightsources
148  static SLlong diffusePhotons; //!< NO. of diffusely scattered photons on surfaces
149  static SLlong reflectedPhotons; //!< NO. of reflected photons;
150  static SLlong refractedPhotons; //!< NO. of refracted photons;
151  static SLlong tirPhotons; //!< NO. of total internal refraction photons
152  static SLbool ignoreLights; //!< flag for gloss sampling
153  static TRanrotBGenerator* random; //!< Random generator
154 
155  ////////////////////
156  // Photon Mapping //
157  ////////////////////
158 
159  SLbool nodeReflectance() { return ((hitMat->specular().r > 0.0f) ||
160  (hitMat->specular().g > 0.0f) ||
161  (hitMat->specular().b > 0.0f)); }
162  SLbool nodeTransparency() { return ((hitMat->transmission().r > 0.0f) ||
163  (hitMat->transmission().g > 0.0f) ||
164  (hitMat->transmission().b > 0.0f)); }
165  SLbool nodeDiffuse() { return ((hitMat->diffuse().r > 0.0f) ||
166  (hitMat->diffuse().g > 0.0f) ||
167  (hitMat->diffuse().b > 0.0f)); }
168 };
169 //-----------------------------------------------------------------------------
170 #endif
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
signed long SLlong
Definition: SL.h:164
int SLint
Definition: SL.h:170
SLRayType
SLRayType enumeration for specifying ray type in ray tracing.
Definition: SLRay.h:22
@ TRANSMITTED
Definition: SLRayMC.h:25
@ SHADOW
Definition: SLRayMC.h:26
@ PRIMARY
Definition: SLRayMC.h:23
@ REFLECTED
Definition: SLRayMC.h:24
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
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
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
SLbool nodeDiffuse()
Definition: SLRayMC.h:165
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
SLCol4f hitTexCol
Texture color at intersection point.
Definition: SLRayMC.h:121
static SLuint tests
NO. of intersection tests.
Definition: SLRay.h:132
SLbool isOutside
Flag if ray is inside of a material.
Definition: SLRay.h:95
SLFace * originTria
Points to the triangle at ray origin.
Definition: SLRayMC.h:109
static SLuint reflectedRays
NO. of reflected rays.
Definition: SLRay.h:127
SLint depth
Recursion depth for ray tracing.
Definition: SLRay.h:78
SLMaterial * hitMat
Points to material of intersected node.
Definition: SLRayMC.h:116
SLVec3f dir
Direction vector of ray in WS.
Definition: SLRay.h:76
SLbool nodeReflectance()
Definition: SLRayMC.h:159
SLShape * originShape
Points to the shape at ray origin.
Definition: SLRayMC.h:108
SLFace * hitTriangle
Points to the intersected triangle.
Definition: SLRayMC.h:115
SLVec3f invDirOS
Inverse ray dir for fast AABB hit in OS.
Definition: SLRay.h:117
void normalizeNormal()
void setDirOS(SLVec3f Dir)
Definition: SLRayMC.h:80
void refract(SLRay *refracted)
SLbool isShaded()
Definition: SLRayMC.h:90
SLfloat tmin
min. dist. of last AABB intersection
Definition: SLRay.h:120
void setDir(SLVec3f Dir)
Definition: SLRayMC.h:70
SLShape * hitShape
Points to the intersected shape.
Definition: SLRayMC.h:114
static SLint maxDepthReached
max. depth reached for all rays
Definition: SLRay.h:135
static SLuint refractedRays
NO. of refracted rays.
Definition: SLRay.h:128
static SLlong emittedPhotons
NO. of emitted photons from all lightsources.
Definition: SLRayMC.h:147
SLfloat length
length from origin to an intersection
Definition: SLRay.h:77
void print() const
Definition: SLRay.cpp:139
SLfloat contrib
Current contribution of ray to color.
Definition: SLRay.h:79
static SLuint subsampledPixels
NO. of of subsampled pixels.
Definition: SLRay.h:138
SLVec3f hitPoint
Point of intersection.
Definition: SLRay.h:110
void diffuseMC(SLRay *scattered) const
Definition: SLRay.cpp:429
static SLlong diffusePhotons
NO. of diffusely scattered photons on surfaces.
Definition: SLRayMC.h:148
SLbool nodeTransparency()
Definition: SLRayMC.h:162
SLVec3f dirOS
Direction vector of ray in OS.
Definition: SLRay.h:81
static SLlong tirPhotons
NO. of total internal refraction photons.
Definition: SLRayMC.h:151
static SLlong refractedPhotons
NO. of refracted photons;.
Definition: SLRayMC.h:150
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
static SLbool ignoreLights
flag for gloss sampling
Definition: SLRayMC.h:152
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
static TRanrotBGenerator * random
Random generator.
Definition: SLRayMC.h:153
SLMaterial * originMat
Points to appearance at ray origin.
Definition: SLRayMC.h:110
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
static SLlong reflectedPhotons
NO. of reflected photons;.
Definition: SLRayMC.h:149
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
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43