Difference between revisions of "VTK/Examples/Cxx/GLUT"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
Line 9: Line 9:
<source lang="cpp">
<source lang="cpp">
#ifndef __APPLE_CC__
#ifndef __APPLE_CC__
#include <gl/glut.h>
#include <GL/glut.h>
#include <glut.h>
#include <glut.h>

Revision as of 09:43, 21 November 2012

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 do not create the rendering container nor the OpenGL context but inherit 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.


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

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

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

void Draw()
  ren->GetActiveCamera()->Azimuth( 1 );

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

  // 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->InitializeFromCurrentContext(); // here is the trick: associate the RenderWindow with the current OpenGL context created by GLUT
  renWin->AddRenderer( ren.GetPointer() );
  // Let's start the main GLUT rendering loop
  return EXIT_SUCCESS;


project(VtkGlut CXX)
cmake_minimum_required(VERSION 2.8)

find_package(VTK REQUIRED)
find_package(GLUT REQUIRED)



add_executable(${PROJECT_NAME} vtkglut.cxx)

target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES} ${GLUT_LIBRARIES})