Failed to inherit vtkRenderer

I want to write a class which inherit from vtkRenderer.

My code is:

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);

#include <vtkSmartPointer.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkCellData.h>
#include <vtkPropAssembly.h>

class MyRenderer : public vtkRenderer
{
public:
	static MyRenderer* New();
	vtkTypeMacro(MyRenderer, vtkRenderer);


protected:

};

vtkStandardNewMacro(MyRenderer);

int main(int, char* [])
{
	vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
	cube->Update();
	vtkSmartPointer<vtkPolyDataMapper> cubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	cubeMapper->SetInputData(cube->GetOutput());
	vtkSmartPointer<vtkActor> cubeActor = vtkSmartPointer<vtkActor>::New();
	cubeActor->SetMapper(cubeMapper);

//======================== experiment code =============================
	vtkSmartPointer<MyRenderer> render = vtkSmartPointer<MyRenderer>::New();
	//vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
//======================== experiment code =============================
	render->AddActor(cubeActor);

	vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
	renWin->AddRenderer(render);
    renWin->Render();

	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(renWin);
	iren->Initialize();
	iren->Start();

	return EXIT_SUCCESS;
}

MyRenderer just inherit from vtkRenderer and do nothing.

After run the above code, the window do not display anything.

However, if I replace MyRenderer with vtkRenderer, the window would display a cube.

I am confuzed about MyRenderer, because it is almost equal to vtkRenderer.

On the other hand, I write a python code as following:

import vtkmodules.all as vtk

class MyRenderer(vtk.vtkRenderer):

    def __init__(self):
        pass

cube = vtk.vtkCubeSource()
cube.Update()
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInputData(cube.GetOutput())
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)

# render = vtk.vtkRenderer()
render = MyRenderer()
render.AddActor(cubeActor)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
iren.Start()

And MyRenderer works.

How can I implement a class to inherit from vtkRenderer in C++? Any suggestion is appreciated~~~

This is because vtkRenderer is a factory class, so vtkRenderer::New() actually creates a vtkOpenGLRenderer object. But when you inherit only vtkRenderer, your MyRenderer class has no OpenGL code and does not render anything.

The solution is to derive your class from vtkOpenGLRenderer. Then it will be able to render.

It works in Python because the constructor MyRenderer() performs a call to vtkRenderer.__new__(), which calls vtkRenderer::New(), which creates a vtkOpenGLRenderer.

1 Like

Thank you for your detail reply~~~