Vtkglut

VTKExamples/Cxx/Untested/HasBugs/vtkglut

Description

This page illustrates how to use the new method '' vtkRenderWindow::InitializeFromCurrentContext()''' to associate a vtkRenderWindow instance to an existing window and activated OpenGL context.

By using this method, VTK does not create the rendering container nor the OpenGL context but inherits from the one created somewhere else. This allows a vtkRenderWindow and thus VTK to be easily embedded in an existing graphical application.

The following code shows how to embed a VTK RenderWindow in a very simple GLUT based application which shows a vtkCone in rotation.

The interaction part is not shown here but it works as usual, you just need to pass the GLUT events to your VTK interactor.

Code

vtkglut.cxx

#ifndef __APPLE_CC__
#include <GL/glut.h>
#else
#include <glut.h>
#endif
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>

vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> renWin;

void Reshape(int width, int height) { renWin->SetSize(width, height); }

void Draw() {
  renWin->Render();
  ren->GetActiveCamera()->Azimuth(1);
  glutPostRedisplay();
}

int main(int argc, char **argv) {
  // GLUT initialization
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow("VTK-GLUT Example");
  glutReshapeFunc(Reshape);
  glutDisplayFunc(Draw);

  // Creation of a simple VTK pipeline
  vtkNew<vtkConeSource> cone;

  vtkNew<vtkPolyDataMapper> coneMapper;
  coneMapper->SetInputConnection(cone->GetOutputPort());

  vtkNew<vtkActor> coneActor;
  coneActor->SetMapper(coneMapper.GetPointer());

  ren->AddActor(coneActor.GetPointer());
  renWin->AddRenderer(ren.GetPointer());

  // Here is the trick: we ask the RenderWindow to join the current OpenGL
  // context created by GLUT
  renWin->InitializeFromCurrentContext();

  // Let's start the main GLUT rendering loop
  glutMainLoop();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(vtkglut)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping vtkglut: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(vtkglut MACOSX_BUNDLE vtkglut.cxx )
  target_link_libraries(vtkglut PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(vtkglut MACOSX_BUNDLE vtkglut.cxx )
  target_link_libraries(vtkglut PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS vtkglut
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build vtkglut

Click here to download vtkglut and its CMakeLists.txt file. Once the tarball vtkglut.tar has been downloaded and extracted,

cd vtkglut/build 

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./vtkglut

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.