VTK  9.1.0
Renderer.h
Go to the documentation of this file.
1#pragma once
2
3#include "vtkLogger.h"
4
5#include "../Types.h"
6#include "Camera.h"
7#include "Data.h"
8#include "FrameBuffer.h"
9#include "Light.h"
10#include "World.h"
11#include "Object.h"
12#include "Texture.h"
13
14#include <VisRTX.h>
15#include <limits>
16
17namespace RTW
18{
19 class Renderer : public Object
20 {
21 public:
22 Renderer(const char* /*type*/)
24 {
25 VisRTX::Context* rtx = VisRTX_GetContext();
26 this->renderer = rtx->CreateRenderer();
27
28 this->renderer->SetToneMapping(false);
29 }
30
32 {
33 this->renderer->Release();
34 }
35
36 void Commit() override
37 {
38 }
39
40 float RenderFrame(FrameBuffer* frameBuffer, Camera *camera, World *world)
41 {
42 if (!frameBuffer)
43 return 0.0f;
44
45
46 // Camera
47 if (camera)
48 {
49 this->renderer->SetCamera(camera->camera);
50 }
51
52 Data *map_backplate = GetObject<Data>({"map_backplate"});
53 Light bgLight("hdri");
54 bgLight.SetVec3f("dir", 1.0, 0.0, 0.0);
55 bgLight.SetVec3f("up", 0.0, 1.0, 0.0);
56 bgLight.SetObject("map", map_backplate);
57 bgLight.Commit();
58
59 bool removeTemp = false;
60
61 // World
62 if (world)
63 {
64 //World
65 VisRTX::Model *model = world->model;
66 this->renderer->SetModel(model);
67
68 // Lights
69 for (VisRTX::Light* light : this->lastLights)
70 this->renderer->RemoveLight(light);
71
72 this->lastLights.clear();
73
74
75 Data *lightData = world->GetObject<Data>({"light"});
76
77 if (lightData &&
78 lightData->GetDataType() == RTW_DATA &&
79 lightData->GetElementDataType() == RTW_LIGHT)
80 {
81 Light** lights = reinterpret_cast<Light**>(lightData->GetData());
82 for (size_t i = 0; i < lightData->GetNumElements(); ++i)
83 {
84 Light* lightHandle = lights[i];
85 if (lightHandle)
86 {
87 VisRTX::Light* light = lightHandle->light;
88 this->renderer->AddLight(light);
89 this->lastLights.push_back(light);
90 }
91 }
92 }
93
94 if(map_backplate)
95 {
96 removeTemp = true;
97 this->renderer->AddLight(bgLight.light);
98 }
99
100 }
101
102 // Samples per pixel
103 int32_t spp;
104 if (this->GetInt({ "pixelSamples" }, &spp))
105 {
106 this->renderer->SetSamplesPerPixel(spp);
107 }
108
109 // Epsilon
110 float epsilon;
111 if (this->GetFloat({ "epsilon" }, &epsilon))
112 {
113 this->renderer->SetEpsilon(epsilon);
114 }
115
116 // Max ray recursion depth
117 int32_t minBounces = this->GetInt({ "rouletteDepth" }, 5);
118 int32_t maxBounces = this->GetFloat({ "maxPathLength" }, 10.0f);
119 this->renderer->SetNumBounces(minBounces, maxBounces);
120
121 // Denoiser
122 int denoise = this->GetInt({ "denoise" });
123 this->renderer->SetDenoiser(denoise > 0 ? VisRTX::DenoiserType::AI : VisRTX::DenoiserType::NONE);
124
125
126 try
127 {
128 this->renderer->Render(frameBuffer->frameBuffer);
129 }
130 catch (VisRTX::Exception& e)
131 {
132 vtkLogF(ERROR, "VisRTX internal error: \"%s\"", e.what());
133 }
134
135 if(removeTemp)
136 this->renderer->RemoveLight(bgLight.light);
137
138 // VisRTX does not use a variance buffer
139 return std::numeric_limits<float>::infinity();
140 }
141
142 private:
143 VisRTX::Renderer* renderer = nullptr;
144
145 std::vector<VisRTX::Light*> lastLights;
146 };
147}
@ RTW_RENDERER
Definition: Types.h:145
@ RTW_LIGHT
Definition: Types.h:143
@ RTW_DATA
Definition: Types.h:134
Definition: Data.h:10
size_t GetNumElements() const
Definition: Data.h:116
RTWDataType GetElementDataType() const
Definition: Data.h:136
void * GetData() const
Definition: Data.h:146
RTWDataType GetDataType() const
Definition: Object.h:306
int32_t GetInt(const std::vector< std::string > &ids, int32_t defaultValue=0, bool *found=nullptr) const
Definition: Object.h:116
T * GetObject(const std::vector< std::string > &ids, T *defaultValue=nullptr, bool *found=nullptr) const
Definition: Object.h:98
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition: Object.h:133
void Commit() override
Definition: Renderer.h:36
float RenderFrame(FrameBuffer *frameBuffer, Camera *camera, World *world)
Definition: Renderer.h:40
Renderer(const char *)
Definition: Renderer.h:22
Definition: Backend.h:6
#define vtkLogF(verbosity_name,...)
Add to log given the verbosity level.
Definition: vtkLogger.h:484