SLProject  4.3.020
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 The result is written into an image of an SLGLTexture and that then is rendered
55 with OpenGL in a orthographic projection.
56 */
57 class SLRaytracer : public SLGLTexture
58 {
59 public:
60  SLRaytracer();
61  ~SLRaytracer() override;
62 
63  // ray tracer functions
66  void renderSlices(bool isMainThread, SLuint threadNum);
67  void renderSlicesMS(bool isMainThread, SLuint threadNum);
68  SLCol4f trace(SLRay* ray);
69  SLCol4f shade(SLRay* ray);
70  void sampleAAPixels(bool isMainThread, SLuint threadNum);
71  void renderUIBeforeUpdate();
72 
73  // additional ray tracer functions
74  void setPrimaryRay(SLfloat x, SLfloat y, SLRay* primaryRay);
75  void getAAPixels();
76  SLCol4f fogBlend(SLfloat z, SLCol4f color);
77  virtual void printStats(SLfloat sec);
78  virtual void initStats(SLint depth);
79 
80  // Setters
82  {
83  if (_state != rtBusy) _state = state;
84  }
86  {
87  _maxDepth = depth;
88  state(rtReady);
89  }
91  void doDistributed(SLbool distrib) { _doDistributed = distrib; }
92  void doContinuous(SLbool cont)
93  {
94  _doContinuous = cont;
95  state(rtReady);
96  }
97  void doFresnel(SLbool fresnel)
98  {
99  _doFresnel = fresnel;
100  state(rtReady);
101  }
102  void aaSamples(SLint samples)
103  {
104  _aaSamples = samples;
105  state(rtReady);
106  }
107  void gamma(SLfloat g)
108  {
109  _gamma = g;
110  _oneOverGamma = 1.0f / g;
111  }
112 
113  // Getters
114  SLRTState state() const { return _state; }
115  SLint maxDepth() const { return _maxDepth; }
116  SLbool doDistributed() const { return _doDistributed; }
117  SLbool doContinuous() const { return _doContinuous; }
118  SLbool doFresnel() const { return _doFresnel; }
119  SLint aaSamples() const { return _aaSamples; }
120  static SLuint numThreads() { return Utils::maxThreads(); }
121  SLint progressPC() const { return _progressPC; }
122  SLfloat aaThreshold() const { return _aaThreshold; }
123  SLfloat renderSec() const { return _renderSec; }
124  SLfloat gamma() const { return _gamma; }
125  SLfloat oneOverGamma() const { return _oneOverGamma; }
127  SLint resolutionFactorPC() const { return (SLint)(_resolutionFactor * 100.0f + 0.00001f); }
128  //! Rays per ms of the last completed render, for comparing machines
129  /*! Measured over the whole render, so it includes the window updates that
130  the main thread does every 500ms and the thread barrier at the end of every
131  sample pass. Both are small, but it means the figure describes this renderer
132  on this machine rather than raw ray throughput. */
134 
135  // Render target image
136  virtual void prepareImage();
137  virtual void renderImage(bool updateTextureGL);
138  virtual void saveImage();
139 
140 protected:
141  function<void(bool, SLuint)> renderSlicesAsync;
142  function<void(bool, SLuint)> sampleAAPixelsAsync;
143 
144  SLSceneView* _sv; //!< Parent sceneview
145  SLRTState _state; //!< RT state;
146  SLCamera* _cam; //!< shortcut to the camera
147  SLfloat _resolutionFactor; //!< screen to RT image size factor (default 1.0)
148  SLint _maxDepth; //!< Max. allowed recursion depth
149  SLbool _doContinuous; //!< if true state goes into ready again
150  SLbool _doDistributed; //!< Flag for parallel distributed RT
151  SLbool _doFresnel; //!< Flag for Fresnel reflection
152  SLint _progressPC; //!< progress in %
153  SLfloat _renderSec; //!< Rendering time in seconds
154  AvgFloat _raysPerMS; //!< Rays per ms of the last completed render
155 
156  SLfloat _pxSize; //!< Pixel size
157  SLVec3f _eye; //!< Camera position
158  SLVec3f _la, _lu, _lr; //!< Camera lookat, lookup, lookright
159  SLVec3f _bl; //!< Bottom left vector
160  SLint _nextLine; //!< next line index to render RT in a thread
161  SLVPixel _aaPixels; //!< Vector for antialiasing pixels
162  SLfloat _gamma; //!< gamma correction value
163  SLfloat _oneOverGamma; //!< one over gamma correction value
164 
165  // variables for distributed ray tracing
166  SLfloat _aaThreshold; //!< threshold for anti aliasing
167  SLint _aaSamples; //!< SQRT of uneven num. of AA samples
168 };
169 //-----------------------------------------------------------------------------
170 #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
unsigned short SLushort
analog to GLushort
Definition: SL.h:196
int SLint
analog to GLint
Definition: SL.h:197
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
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:58
void gamma(SLfloat g)
Definition: SLRaytracer.h:107
SLint maxDepth() const
Definition: SLRaytracer.h:115
SLbool doFresnel() const
Definition: SLRaytracer.h:118
SLint resolutionFactorPC() const
Definition: SLRaytracer.h:127
SLfloat _oneOverGamma
one over gamma correction value
Definition: SLRaytracer.h:163
virtual void prepareImage()
SLVPixel _aaPixels
Vector for antialiasing pixels.
Definition: SLRaytracer.h:161
void getAAPixels()
SLbool _doDistributed
Flag for parallel distributed RT.
Definition: SLRaytracer.h:150
SLbool renderClassic(SLSceneView *sv)
Definition: SLRaytracer.cpp:64
SLfloat renderSec() const
Definition: SLRaytracer.h:123
SLint aaSamples() const
Definition: SLRaytracer.h:119
SLCol4f trace(SLRay *ray)
SLSceneView * _sv
Parent sceneview.
Definition: SLRaytracer.h:144
void doContinuous(SLbool cont)
Definition: SLRaytracer.h:92
function< void(bool, SLuint)> renderSlicesAsync
Definition: SLRaytracer.h:141
SLVec3f _eye
Camera position.
Definition: SLRaytracer.h:157
void renderSlices(bool isMainThread, SLuint threadNum)
~SLRaytracer() override
Definition: SLRaytracer.cpp:54
SLRTState state() const
Definition: SLRaytracer.h:114
SLCol4f fogBlend(SLfloat z, SLCol4f color)
SLfloat gamma() const
Definition: SLRaytracer.h:124
virtual void printStats(SLfloat sec)
SLint _nextLine
next line index to render RT in a thread
Definition: SLRaytracer.h:160
void state(SLRTState state)
Definition: SLRaytracer.h:81
void renderSlicesMS(bool isMainThread, SLuint threadNum)
SLRTState _state
RT state;.
Definition: SLRaytracer.h:145
SLCol4f shade(SLRay *ray)
SLfloat _resolutionFactor
screen to RT image size factor (default 1.0)
Definition: SLRaytracer.h:147
SLfloat _renderSec
Rendering time in seconds.
Definition: SLRaytracer.h:153
static SLuint numThreads()
Definition: SLRaytracer.h:120
SLfloat _gamma
gamma correction value
Definition: SLRaytracer.h:162
SLint _aaSamples
SQRT of uneven num. of AA samples.
Definition: SLRaytracer.h:167
SLfloat oneOverGamma() const
Definition: SLRaytracer.h:125
SLbool renderDistrib(SLSceneView *sv)
SLbool _doFresnel
Flag for Fresnel reflection.
Definition: SLRaytracer.h:151
void doFresnel(SLbool fresnel)
Definition: SLRaytracer.h:97
SLVec3f _lu
Definition: SLRaytracer.h:158
SLint _maxDepth
Max. allowed recursion depth.
Definition: SLRaytracer.h:148
function< void(bool, SLuint)> sampleAAPixelsAsync
Definition: SLRaytracer.h:142
virtual void renderImage(bool updateTextureGL)
SLbool doDistributed() const
Definition: SLRaytracer.h:116
void renderUIBeforeUpdate()
Must be called before an inbetween frame updateRec.
SLbool doContinuous() const
Definition: SLRaytracer.h:117
void maxDepth(SLint depth)
Definition: SLRaytracer.h:85
SLint progressPC() const
Definition: SLRaytracer.h:121
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:158
SLfloat aaThreshold() const
Definition: SLRaytracer.h:122
SLfloat _aaThreshold
threshold for anti aliasing
Definition: SLRaytracer.h:166
void resolutionFactor(SLfloat rf)
Definition: SLRaytracer.h:90
void aaSamples(SLint samples)
Definition: SLRaytracer.h:102
SLCamera * _cam
shortcut to the camera
Definition: SLRaytracer.h:146
virtual void initStats(SLint depth)
SLfloat raysPerMS()
Rays per ms of the last completed render, for comparing machines.
Definition: SLRaytracer.h:133
SLbool _doContinuous
if true state goes into ready again
Definition: SLRaytracer.h:149
void sampleAAPixels(bool isMainThread, SLuint threadNum)
SLfloat _pxSize
Pixel size.
Definition: SLRaytracer.h:156
SLint _progressPC
progress in %
Definition: SLRaytracer.h:152
AvgFloat _raysPerMS
Rays per ms of the last completed render.
Definition: SLRaytracer.h:154
SLfloat resolutionFactor() const
Definition: SLRaytracer.h:126
virtual void saveImage()
Saves the current RT image as PNG image.
void doDistributed(SLbool distrib)
Definition: SLRaytracer.h:91
SLVec3f _la
Definition: SLRaytracer.h:158
SLVec3f _bl
Bottom left vector.
Definition: SLRaytracer.h:159
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:1188
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