3#include <vtkInformation.h>
5#include <vtkDataArray.h>
7#include <vtkPointData.h>
10#include <vtkOpenGLPolyDataMapper.h>
11#include <vtkOpenGLRenderWindow.h>
12#include <vtkOpenGLRenderer.h>
13#include <vtkPlaneSource.h>
14#include <vtkRenderWindow.h>
15#include <vtkWindowToImageFilter.h>
17#include <vtkFloatArray.h>
18#include <vtkUnsignedCharArray.h>
20#include <vtkOpenGLTexture.h>
21#include <vtkProperty.h>
22#include <vtkShaderProperty.h>
23#include <vtkTextureObject.h>
25#include <vtkFramebufferPass.h>
26#include <vtkRenderStepsPass.h>
28#include <boost/algorithm/string.hpp>
39 this->SetNumberOfInputPorts(1);
40 this->SetNumberOfOutputPorts(1);
46 vtkInformation *info) {
48 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),
"vtkImageData");
55 vtkInformation *info) {
64 return std::string(R
"(
66//VTK::System::Dec // always start with this line in your VS
68attribute vec4 vertexMC;
72 vPos = vertexMC/2. + vec4(0.5,0.5,0.5,0);
73 gl_Position = vertexMC;
80 return std::string(R
"(
82//VTK::System::Dec // always start with these lines in your FS
83//VTK::Output::Dec // always start with these lines in your FS
88 gl_FragData[0] = vec4(1,0,0,1);
96 std::string result = source;
98 for(
const auto &it : this->Replacements)
99 boost::replace_all(result, it.first, it.second.toString());
105 const std::vector<double> &values,
107 auto it = this->Replacements.find(name);
108 if(it == this->Replacements.end()) {
109 this->Replacements.emplace(std::piecewise_construct,
110 std::forward_as_tuple(name),
111 std::forward_as_tuple(values, isInt));
113 it->second.values = values;
121 ps->SetOrigin(-1, -1, 0);
122 ps->SetPoint1(1, -1, 0);
123 ps->SetPoint2(-1, 1, 0);
127 this->FullScreenQuad->ShallowCopy(ps->GetOutput());
131 mapper->SetInputData(this->FullScreenQuad);
132 this->FullScreenQuadActor->SetMapper(mapper);
140 this->Renderer->AddActor(this->FullScreenQuadActor);
141 this->Renderer->SetBackground(0, 0, 0);
145 camera->SetParallelProjection(
true);
146 camera->SetClippingRange(0, 2);
147 camera->SetPosition(0, 0, 1);
148 camera->SetFocalPoint(0, 0, 0);
149 camera->SetParallelScale(
151 this->Renderer->SetActiveCamera(camera);
160 outputImage->GetDimensions(dim);
162 this->
AddReplacement(
"cResolution", {(double)dim[0], (
double)dim[1]});
165 int *size = this->RenderWindow->GetSize();
166 if(size[0] != dim[0] || size[1] != dim[1]) {
168 this->
printMsg(
"Initializing Renderer (" + std::to_string(dim[0]) +
"x"
169 + std::to_string(dim[1]) +
")",
173 this->RenderWindow->AddRenderer(this->Renderer);
174 this->RenderWindow->SetMultiSamples(0);
175 this->RenderWindow->OffScreenRenderingOn();
177 auto windowAsOGL = vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow);
178 if(windowAsOGL ==
nullptr) {
181 windowAsOGL->SetSize(dim[0], dim[1]);
182 windowAsOGL->Initialize();
184 this->
printMsg(
"Initializing Renderer (" + std::to_string(dim[0]) +
"x"
185 + std::to_string(dim[1]) +
")",
196 image->GetDimensions(dim);
198 auto inputArray = this->GetInputArrayToProcess(arrayIdx, image);
199 if(!inputArray || this->GetInputArrayAssociation(arrayIdx, image) != 0) {
200 this->
printErr(
"Unable to retrieve input point data array "
201 + std::to_string(arrayIdx) +
".");
205 std::string
const textureName =
"tex" + std::to_string(textureIdx);
207 auto properties = this->FullScreenQuadActor->GetProperty();
209 if(properties->GetTexture(textureName.data()))
210 properties->RemoveTexture(textureName.data());
216 textureObj->SetContext(
217 vtkOpenGLRenderWindow::SafeDownCast(this->RenderWindow));
218 textureObj->SetWrapT(vtkTextureObject::ClampToEdge);
219 textureObj->SetWrapS(vtkTextureObject::ClampToEdge);
220 textureObj->Create2DFromRaw(
221 dim[0], dim[1], inputArray->GetNumberOfComponents(),
223 texture->SetTextureObject(textureObj);
224 texture->InterpolateOn();
227 properties->SetTexture(textureName.data(), texture);
233 const std::string &name) {
236 image->GetDimensions(dim);
238 "Rendering (" + std::to_string(dim[0]) +
"x" + std::to_string(dim[1]) +
")",
241 this->FullScreenQuadActor->GetShaderProperty()->SetVertexShaderCode(
243 this->FullScreenQuadActor->GetShaderProperty()->SetFragmentShaderCode(
247 buffer->SetName(name.data());
248 buffer->SetNumberOfComponents(4);
249 buffer->SetNumberOfTuples(dim[0] * dim[1]);
251 this->RenderWindow->Render();
252 this->RenderWindow->GetRGBACharPixelData(
253 0, 0, dim[0] - 1, dim[1] - 1, 1, buffer);
255 image->GetPointData()->AddArray(buffer);
256 image->GetPointData()->SetActiveScalars(buffer->GetName());
259 "Rendering (" + std::to_string(dim[0]) +
"x" + std::to_string(dim[1]) +
")",
static vtkInformationIntegerKey * SAME_DATA_TYPE_AS_INPUT_PORT()
Base Class for all CinemaDarkroom Shaders.
virtual int Render(vtkImageData *image, const std::string &name)
int FillInputPortInformation(int port, vtkInformation *info) override
~ttkCinemaDarkroomShader() override
ttkCinemaDarkroomShader()
int InitRenderer(vtkImageData *outputImage)
int AddTexture(vtkImageData *image, int arrayIdx, int textureIdx)
int FillOutputPortInformation(int port, vtkInformation *info) override
int AddReplacement(const std::string &name, const std::vector< double > &values, const bool &isInt=false)
virtual std::string GetFragmentShaderCode()
std::string PerformReplacements(const std::string &input)
int CreateFullScreenQuad()
virtual std::string GetVertexShaderCode()
static void * GetVoidPointer(vtkDataArray *array, vtkIdType start=0)
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
vtkStandardNewMacro(ttkCinemaDarkroomShader)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)