Hello. I created a CustomActor
class which inherits from vtkOpenGLActor
and overrides the common rendering methods. The idea is to use OpenGL calls directly within these methods to render with custom shaders.
I managed to get the code below compiling and running. The issue is: GLEW fails to initialize and throws an error from vtkOpenGLRenderWindow::OpenGLInit()
. Trying to call any OpenGL functions result in a segmentation fault because of that.
ERR| vtkXOpenGLRenderWindow (0x5623a0c95ee0): GLEW could not be initialized: Missing GL version
Despite the error, the program runs flawlessly and renders the transparent objects (with the logs from the overridden methods), but I’m unable to write OpenGL code for rendering. Any advice on this?
I’m on VTK 9.0.3, built from source on Ubuntu 20.04 LTS.
Result is correct:
hpp
file:
#include <vtkOpenGLActor.h>
#include <vtkObjectFactory.h>
#include <iostream>
class CustomActor : public vtkOpenGLActor
{
public:
static CustomActor *New();
vtkTypeMacro(CustomActor, vtkOpenGLActor);
virtual void PrintSelf(ostream& os, vtkIndent indent);
// Override standard render methods.
int RenderOverlay(vtkViewport *viewport);
int RenderOpaqueGeometry(vtkViewport *viewport);
int HasTranslucentPolygonalGeometry();
int RenderTranslucentPolygonalGeometry(vtkViewport* viewport);
protected:
CustomActor();
~CustomActor();
private:
// Not implemented.
CustomActor(const CustomActor&);
void operator=(const CustomActor&);
};
cpp
file:
#include "custom_actor.hpp"
#include <vtk_glew.h>
#include <vtkConeSource.h>
#include <vtkCubeSource.h>
#include <vtkInteractorObserver.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkOpenGLRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPolyDataMapper.h>
/* --- CustomActor implementation --- */
vtkStandardNewMacro(CustomActor);
CustomActor::CustomActor() {}
CustomActor::~CustomActor() {}
void CustomActor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
};
int CustomActor::RenderOverlay(vtkViewport *viewport)
{
std::cout << "CustomActor::RenderOverlay" << std::endl;
return this->Superclass::RenderOverlay(viewport);
}
int CustomActor::RenderOpaqueGeometry(vtkViewport *viewport)
{
std::cout << "CustomActor::RenderOpaqueGeometry" << std::endl;
return this->Superclass::RenderOpaqueGeometry(viewport);
}
int CustomActor::HasTranslucentPolygonalGeometry()
{
std::cout << "CustomActor::HasTranslucentPolygonalGeometry" << std::endl;
return this->Superclass::HasTranslucentPolygonalGeometry();
}
int CustomActor::RenderTranslucentPolygonalGeometry(vtkViewport* viewport)
{
std::cout << "CustomActor::RenderTranslucentPolygonalGeometry" << std::endl;
return this->Superclass::RenderTranslucentPolygonalGeometry(viewport);
}
// Setup custom actors.
void _setupCubeActor(CustomActor* cube_actor);
void _setupConeActor(CustomActor* cone_actor);
// Main
int main(int argc, char* argv[])
{
vtkNew<CustomActor> cubeActor;
_setupCubeActor(cubeActor);
vtkNew<CustomActor> coneActor;
_setupConeActor(coneActor);
// Create renderer, render window and an OpenGL context.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
vtkOpenGLRenderWindow* context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
// vtkOpenGLRenderWindow::OpenGLInit() calls glewInit(), which fails:
// ERROR: vtkOpenGLRenderWindow.c:569 ERR| vtkXOpenGLRenderWindow (0x5623a0c95ee0): GLEW could not be initialized: Missing GL version
context->OpenGLInit();
context->MakeCurrent();
context->AddRenderer(renderer);
context->SetSize(800, 800);
unsigned int vao;
//glGenVertexArrays(1, &vao); // -> Segmantation fault!
vtkNew<vtkNamedColors> colors;
renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
renderer->AddActor(cubeActor);
renderer->AddActor(coneActor);
// Setup interactor.
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(context);
context->Render();
context->SetWindowName("Transparent Objects");
// Interact with the data
iren->Start();
return EXIT_SUCCESS;
}
void _setupCubeActor(CustomActor* cube_actor)
{
vtkNew<vtkNamedColors> colors;
// Cube object.
vtkNew<vtkCubeSource> cubeSource;
cubeSource->SetXLength(4.0);
cubeSource->SetYLength(9.0);
cubeSource->SetZLength(1.0);
cubeSource->SetCenter(0.0, 0.0, -6.0);
vtkNew<vtkPolyDataMapper> cubeMapper;
cubeMapper->SetInputConnection(cubeSource->GetOutputPort());
cube_actor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("DarkGreen").GetData()
);
cube_actor->GetProperty()->SetOpacity(0.5);
cube_actor->SetMapper(cubeMapper);
}
void _setupConeActor(CustomActor* cone_actor)
{
vtkNew<vtkNamedColors> colors;
// Cone object.
vtkNew<vtkConeSource> coneSource;
coneSource->SetCenter(0.0, 0.0, 0.0);
coneSource->SetHeight(4.0);
coneSource->SetRadius(2.0);
coneSource->SetDirection(0.0, 1.0, 0.0);
coneSource->SetResolution(60);
coneSource->CappingOn();
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(coneSource->GetOutputPort());
cone_actor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("DarkTurquoise").GetData()
);
cone_actor->GetProperty()->SetOpacity(0.5);
cone_actor->SetMapper(coneMapper);
}