Hello there, pretty new to the forum and VTK
I’m working on a project that need advance rendering in Unity. I’ve thought this would be a achievable through VTK and i still think it can’t be that hard but i’m stuck.
There is the VTK External Rendering module and Unity provides a low level plugin interface where C++ can be used. There’s an example that uses OpenGL calls to manipulate vertex buffers and textures in Unity’s OpenGL context. I’m following the approach described in this paper Virtual Interaction and Visualisation of 3D Medical Imaging Data with VTK and Unity
So, I made a Unity script that uses a command buffer that calls a vtk rendering function every frame at (I think) the correct state of OpenGL rendering. This function is imported from a dynamic library which has the VTK code.
Unity code:
[DllImport(“RenderingPlugin”)]
private static extern IntPtr GetRenderVTKFunc ();CommandBuffer buf = null;
// Start is called before the first frame update
IEnumerator Start() {
buf = new CommandBuffer(); buf.name = "VTK CommandBuffer"; Camera.main.AddCommandBuffer(CameraEvent.BeforeImageEffects, buf); yield return StartCoroutine("callPluginEndOfFrame"); } private IEnumerator callPluginEndOfFrame() { while (true) { yield return new WaitForEndOfFrame(); //SetTimeFromUnity(Time.timeSinceLevelLoad); buf.IssuePluginEvent(GetRenderVTKFunc(), 1); } }
Fragment of VTK code:
//Definitions
vtkNew cs;
vtkNew mapper;mapper->SetInputConnection(cs->GetOutputPort());
m_CubeActor->SetMapper(mapper.GetPointer());
m_CubeActor->GetProperty()->SetColor(1, 1, 0.2);
m_CubeActor->GetProperty()->SetOpacity(0.5);
m_CubeActor->SetPosition(0.5, 0, 0);// create the VTK external renderer
vtkNew renWin;
m_ExternalVTKWidget->SetRenderWindow(renWin.GetPointer());
m_ExternalVTKWidget->GetRenderWindow()->AddRenderer(m_renderer.GetPointer());
m_renderer->AddActor(m_CubeActor.GetPointer());
m_renderer->ResetCamera();
m_externalVtkCamera = vtkSmartPointer(
dynamic_cast<vtkExternalOpenGLCamera*>(m_renderer->GetActiveCamera()));//Function that is called by Unity
void RenderAPI_OpenGLCoreES::UpdateVtk(const double viewMatrix[16], const double projectionMatrix[16]) //std::string &debugStr)
{
m_externalVtkCamera->SetViewTransformMatrix(viewMatrix);
m_externalVtkCamera->SetProjectionTransformMatrix(projectionMatrix);m_CubeActor->RotateX(1.0f);
m_CubeActor->RotateY(1.0f);m_ExternalVTKWidget->GetRenderWindow()->Render();
}
When executing this the following OpenGL error pop up
OPENGL NATIVE PLUG-IN ERROR: GL_INVALID_OPERATION: Operation illegal in current state
Any thoughts would be appreciated
Thanks in advance!