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