Hello, I’m new to VTK and have been trying to work on a small project in c++ to familiarize my self.
I create an actor in a scene and I want to take a screenshot of the window when I want using a press key event, I followed some examples and ended up using a .h file with an observer and an observer function in the main file.
Here is the header file, please ignore the whole resolution thing, it’s just for testing and it does work fine, the screenshot however does not.
#include <vtkCommand.h>
#include<vtkConeSource.h>
#include <vtkPNGWriter.h>
#include <vtkWindowToImageFilter.h>
class Observer : public vtkCommand {
public:
static Observer* New() { return new Observer; }
void Execute(vtkObject*, unsigned long, void*);
void setConeSource(vtkConeSource *coneSource) {
this->ConeSource = coneSource;
resolution = coneSource->GetResolution();
radius= coneSource->GetRadius();
up = true;
}
void windowOb(vtkWindow *window) {
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
vtkSmartPointer<vtkPNGWriter> pngWriter =
vtkSmartPointer<vtkPNGWriter>::New();
this->Window = window;
window->Render();
if (shot = true) {
windowToImageFilter->SetInput(window);
windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
windowToImageFilter->Update();
windowToImageFilter->GetOutputPort();
pngWriter->SetFileName("screenshot.png");
pngWriter->SetInputConnection(windowToImageFilter->GetOutputPort());
pngWriter->Write();
Window->Render();
}
}
void SetResolutionMin(int res) { this->resolutionMin = res; }
void SetResolutionMax(int res) { this->resolutionMax = res; }
void SetRadiusMin(int radius) { this->radMin = radius; }
void SetRadiusMax(int radius) { this->radMax = radius; }
private:
vtkConeSource *ConeSource;
//vtkWindowToImageFilter *WindowToImageFilter;
//vtkPNGWriter *PNGWriter;
vtkWindow *Window;
int resolution,resolutionMin,resolutionMax, radius, radMax, radMin;
bool up, shot;
} ;
Here is the function on the main cpp, again while testing the observer I tested resolution changing which works, not sure how to add screenshot to it though, since I want to take a screenshot everytime I want using a key press, what I’m getting now is one screenshot saved as soon as I run the code and that’s all.
void Observer::Execute(vtkObject caller, unsigned long eventID, void) {
vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::SafeDownCast(caller);
if (interactor->GetKeyCode() == ‘s’) {
shot = true;
cout << “s key pressed” << endl;
}
//R pressed?
if (interactor->GetKeyCode() == ‘r’) {
//Changer resolution of cone
cout << "r key pressed"<<endl;
if (resolution > resolutionMax) up = false;
else if (resolution > resolutionMax) up = true;
//resolution++;
if (up) resolution++, radius++;
else resolution--, radius--;
ConeSource->SetResolution(resolution);
ConeSource->SetRadius(radius);
}
}
Here is the last part of the code where I cann the observer
vtkSmartPointer observer = vtkSmartPointer::New();
observer->setConeSource(coneSource);
observer->windowOb(window);
renderer->ResetCamera();
observer->SetResolutionMax(50);
observer->SetResolutionMin(4);
observer->SetRadiusMin(1);
observer->SetRadiusMax(10);
interactor->AddObserver(vtkCommand::KeyPressEvent, observer);
window->Render();
interactor->Start();