SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRaytracer.h
Go to the documentation of this file.
1 /**
2  * \file SLRaytracer.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 SLRAYTRACER_H
11 #define SLRAYTRACER_H
12 
13 #include <SLEventHandler.h>
14 #include <SLGLTexture.h>
15 #include <SLVec4.h>
16 #include <SLLight.h>
17 #include <Averaged.h>
18 
19 class SLScene;
20 class SLSceneView;
21 class SLRay;
22 class SLMaterial;
23 class SLCamera;
24 
25 //-----------------------------------------------------------------------------
26 //! Ray tracing state
27 typedef enum
28 {
29  rtReady, // RT is ready to start
30  rtBusy, // RT is running
31  rtFinished, // RT is finished
32  rtMoveGL // RT is finished and GL camera is moving
34 //-----------------------------------------------------------------------------
35 //! Pixel index struct used in anti aliasing in ray tracing
37 {
38  explicit SLRTAAPixel(SLushort X = 0, SLushort Y = 0)
39  {
40  x = X;
41  y = Y;
42  }
43  SLushort x; //!< Unsigned short x-pixel index
44  SLushort y; //!< Unsigned short x-pixel index
45 };
46 typedef vector<SLRTAAPixel> SLVPixel;
47 //-----------------------------------------------------------------------------
48 //! SLRaytracer hold all the methods for Whitted style Ray Tracing.
49 /*!
50 SLRaytracer implements the methods render, eyeToPixel, trace and shade for
51 classic Whitted style Ray Tracing. This class is a friend class of SLScene and
52 can access via the pointer _s all members of SLScene. The scene traversal for
53 the ray intersection tests is done within the intersection method of all nodes.
54 */
55 class SLRaytracer : public SLGLTexture
56  , public SLEventHandler
57 {
58 public:
59  SLRaytracer();
60  ~SLRaytracer() override;
61 
62  // ray tracer functions
65  void renderSlices(bool isMainThread, SLuint threadNum);
66  void renderSlicesMS(bool isMainThread, SLuint threadNum);
67  SLCol4f trace(SLRay* ray);
68  SLCol4f shade(SLRay* ray);
69  void sampleAAPixels(bool isMainThread, SLuint threadNum);
70  void renderUIBeforeUpdate();
71 
72  // additional ray tracer functions
73  void setPrimaryRay(SLfloat x, SLfloat y, SLRay* primaryRay);
74  void getAAPixels();
75  SLCol4f fogBlend(SLfloat z, SLCol4f color);
76  virtual void printStats(SLfloat sec);
77  virtual void initStats(SLint depth);
78 
79  // Setters
81  {
82  if (_state != rtBusy) _state = state;
83  }
85  {
86  _maxDepth = depth;
87  state(rtReady);
88  }
90  void doDistributed(SLbool distrib) { _doDistributed = distrib; }
91  void doContinuous(SLbool cont)
92  {
93  _doContinuous = cont;
94  state(rtReady);
95  }
96  void doFresnel(SLbool fresnel)
97  {
98  _doFresnel = fresnel;
99  state(rtReady);
100  }
101  void aaSamples(SLint samples)
102  {
103  _aaSamples = samples;
104  state(rtReady);
105  }
106  void gamma(SLfloat g)
107  {
108  _gamma = g;
109  _oneOverGamma = 1.0f / g;
110  }
111 
112  // Getters
113  SLRTState state() const { return _state; }
114  SLint maxDepth() const { return _maxDepth; }
115  SLbool doDistributed() const { return _doDistributed; }
116  SLbool doContinuous() const { return _doContinuous; }
117  SLbool doFresnel() const { return _doFresnel; }
118  SLint aaSamples() const { return _aaSamples; }
119  static SLuint numThreads() { return Utils::maxThreads(); }
120  SLint progressPC() const { return _progressPC; }
121  SLfloat aaThreshold() const { return _aaThreshold; }
122  SLfloat renderSec() const { return _renderSec; }
123  SLfloat gamma() const { return _gamma; }
124  SLfloat oneOverGamma() const { return _oneOverGamma; }
126  SLint resolutionFactorPC() const { return (SLint)(_resolutionFactor * 100.0f + 0.00001f); }
128 
129  // Render target image
130  virtual void prepareImage();
131  virtual void renderImage(bool updateTextureGL);
132  virtual void saveImage();
133 
134 protected:
135  function<void(bool, SLuint)> renderSlicesAsync;
136  function<void(bool, SLuint)> sampleAAPixelsAsync;
137 
138  SLSceneView* _sv; //!< Parent sceneview
139  SLRTState _state; //!< RT state;
140  SLCamera* _cam; //!< shortcut to the camera
141  SLfloat _resolutionFactor; //!< screen to RT image size factor (default 1.0)
142  SLint _maxDepth; //!< Max. allowed recursion depth
143  SLbool _doContinuous; //!< if true state goes into ready again
144  SLbool _doDistributed; //!< Flag for parallel distributed RT
145  SLbool _doFresnel; //!< Flag for Fresnel reflection
146  SLint _progressPC; //!< progress in %
147  SLfloat _renderSec; //!< Rendering time in seconds
148  AvgFloat _raysPerMS; //!< Averaged rays per ms
149 
150  SLfloat _pxSize; //!< Pixel size
151  SLVec3f _eye; //!< Camera position
152  SLVec3f _la, _lu, _lr; //!< Camera lookat, lookup, lookright
153  SLVec3f _bl; //!< Bottom left vector
154  SLint _nextLine; //!< next line index to render RT in a thread
155  SLVPixel _aaPixels; //!< Vector for antialiasing pixels
156  SLfloat _gamma; //!< gamma correction value
157  SLfloat _oneOverGamma; //!< one over gamma correction value
158 
159  // variables for distributed ray tracing
160  SLfloat _aaThreshold; //!< threshold for anti aliasing
161  SLint _aaSamples; //!< SQRT of uneven num. of AA samples
162 };
163 //-----------------------------------------------------------------------------
164 #endif
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
unsigned short SLushort
Definition: SL.h:169
int SLint
Definition: SL.h:170
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
vector< SLRTAAPixel > SLVPixel
Definition: SLRaytracer.h:46
SLRTState
Ray tracing state.
Definition: SLRaytracer.h:28
@ rtBusy
Definition: SLRaytracer.h:30
@ rtMoveGL
Definition: SLRaytracer.h:32
@ rtFinished
Definition: SLRaytracer.h:31
@ rtReady
Definition: SLRaytracer.h:29
Active or visible camera node class.
Definition: SLCamera.h:54
Virtual Eventhandler class.
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
SLuint depth()
Definition: SLGLTexture.h:220
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
Ray class with ray and intersection properties.
Definition: SLRay.h:40
SLRaytracer hold all the methods for Whitted style Ray Tracing.
Definition: SLRaytracer.h:57
void gamma(SLfloat g)
Definition: SLRaytracer.h:106
SLint maxDepth() const
Definition: SLRaytracer.h:114
SLbool doFresnel() const
Definition: SLRaytracer.h:117
SLint resolutionFactorPC() const
Definition: SLRaytracer.h:126
SLfloat _oneOverGamma
one over gamma correction value
Definition: SLRaytracer.h:157
virtual void prepareImage()
SLVPixel _aaPixels
Vector for antialiasing pixels.
Definition: SLRaytracer.h:155
void getAAPixels()
SLbool _doDistributed
Flag for parallel distributed RT.
Definition: SLRaytracer.h:144
SLbool renderClassic(SLSceneView *sv)
Definition: SLRaytracer.cpp:56
SLfloat renderSec() const
Definition: SLRaytracer.h:122
SLint aaSamples() const
Definition: SLRaytracer.h:118
SLCol4f trace(SLRay *ray)
SLSceneView * _sv
Parent sceneview.
Definition: SLRaytracer.h:138
void doContinuous(SLbool cont)
Definition: SLRaytracer.h:91
function< void(bool, SLuint)> renderSlicesAsync
Definition: SLRaytracer.h:135
SLVec3f _eye
Camera position.
Definition: SLRaytracer.h:151
void renderSlices(bool isMainThread, SLuint threadNum)
~SLRaytracer() override
Definition: SLRaytracer.cpp:46
SLRTState state() const
Definition: SLRaytracer.h:113
SLCol4f fogBlend(SLfloat z, SLCol4f color)
SLfloat gamma() const
Definition: SLRaytracer.h:123
virtual void printStats(SLfloat sec)
SLint _nextLine
next line index to render RT in a thread
Definition: SLRaytracer.h:154
void state(SLRTState state)
Definition: SLRaytracer.h:80
void renderSlicesMS(bool isMainThread, SLuint threadNum)
SLRTState _state
RT state;.
Definition: SLRaytracer.h:139
SLCol4f shade(SLRay *ray)
SLfloat _resolutionFactor
screen to RT image size factor (default 1.0)
Definition: SLRaytracer.h:141
SLfloat _renderSec
Rendering time in seconds.
Definition: SLRaytracer.h:147
static SLuint numThreads()
Definition: SLRaytracer.h:119
SLfloat _gamma
gamma correction value
Definition: SLRaytracer.h:156
SLint _aaSamples
SQRT of uneven num. of AA samples.
Definition: SLRaytracer.h:161
SLfloat oneOverGamma() const
Definition: SLRaytracer.h:124
SLbool renderDistrib(SLSceneView *sv)
SLbool _doFresnel
Flag for Fresnel reflection.
Definition: SLRaytracer.h:145
void doFresnel(SLbool fresnel)
Definition: SLRaytracer.h:96
SLVec3f _lu
Definition: SLRaytracer.h:152
SLint _maxDepth
Max. allowed recursion depth.
Definition: SLRaytracer.h:142
function< void(bool, SLuint)> sampleAAPixelsAsync
Definition: SLRaytracer.h:136
virtual void renderImage(bool updateTextureGL)
SLbool doDistributed() const
Definition: SLRaytracer.h:115
void renderUIBeforeUpdate()
Must be called before an inbetween frame updateRec.
SLbool doContinuous() const
Definition: SLRaytracer.h:116
void maxDepth(SLint depth)
Definition: SLRaytracer.h:84
SLint progressPC() const
Definition: SLRaytracer.h:120
void setPrimaryRay(SLfloat x, SLfloat y, SLRay *primaryRay)
Set the parameters of a primary ray for a pixel position at x, y.
SLVec3f _lr
Camera lookat, lookup, lookright.
Definition: SLRaytracer.h:152
SLfloat aaThreshold() const
Definition: SLRaytracer.h:121
SLfloat _aaThreshold
threshold for anti aliasing
Definition: SLRaytracer.h:160
void resolutionFactor(SLfloat rf)
Definition: SLRaytracer.h:89
void aaSamples(SLint samples)
Definition: SLRaytracer.h:101
SLCamera * _cam
shortcut to the camera
Definition: SLRaytracer.h:140
virtual void initStats(SLint depth)
SLfloat raysPerMS()
Definition: SLRaytracer.h:127
SLbool _doContinuous
if true state goes into ready again
Definition: SLRaytracer.h:143
void sampleAAPixels(bool isMainThread, SLuint threadNum)
SLfloat _pxSize
Pixel size.
Definition: SLRaytracer.h:150
SLint _progressPC
progress in %
Definition: SLRaytracer.h:146
AvgFloat _raysPerMS
Averaged rays per ms.
Definition: SLRaytracer.h:148
SLfloat resolutionFactor() const
Definition: SLRaytracer.h:125
virtual void saveImage()
Saves the current RT image as PNG image.
void doDistributed(SLbool distrib)
Definition: SLRaytracer.h:90
SLVec3f _la
Definition: SLRaytracer.h:152
SLVec3f _bl
Bottom left vector.
Definition: SLRaytracer.h:153
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
unsigned int maxThreads()
Returns in release config the max. NO. of threads otherwise 1.
Definition: Utils.cpp:1191
Pixel index struct used in anti aliasing in ray tracing.
Definition: SLRaytracer.h:37
SLushort y
Unsigned short x-pixel index.
Definition: SLRaytracer.h:44
SLRTAAPixel(SLushort X=0, SLushort Y=0)
Definition: SLRaytracer.h:38
SLushort x
Unsigned short x-pixel index.
Definition: SLRaytracer.h:43