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

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
m (vtkglut.cxx)
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page shows an example of how to use the new method vtkRenderWindow::InitializeFromCurrentContext() to associate a vtkRenderWindow instance to an existing window and activated OpenGL context.
+
<div class="floatright">[[File:Vtkglut.png]]</div>
By using this method, VTK do not create the rendering container nor the OpenGL context so a vtkRenderWindow and thus VTK can be easily embedded in an existing graphical application.
+
This page illustrates how to use the new method '''vtkRenderWindow::InitializeFromCurrentContext()''' to associate a vtkRenderWindow instance to an existing window and activated OpenGL context.
  
==XGMLReader.cxx==
+
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.
 +
 
 +
It has been successfully tested under Linux, Windows & MacOS X.
 +
 
 +
The interaction part is not shown here but it works as usual, you just need to pass the GLUT events to your VTK interactor.
 +
 
 +
 
 +
==vtkglut.cxx==
 
<source lang="cpp">
 
<source lang="cpp">
 
+
#ifndef __APPLE_CC__
#include <gl/glut.h>
+
#include <GL/glut.h>
 +
#else
 +
#include <glut.h>
 +
#endif
 
#include "vtkConeSource.h"
 
#include "vtkConeSource.h"
 
#include "vtkPolyDataMapper.h"
 
#include "vtkPolyDataMapper.h"
Line 12: Line 26:
 
#include "vtkActor.h"
 
#include "vtkActor.h"
 
#include "vtkRenderer.h"
 
#include "vtkRenderer.h"
 +
#include "vtkNew.h"
 +
 +
vtkNew<vtkRenderer> ren;
 +
vtkNew<vtkRenderWindow> renWin;
  
vtkRenderer *ren = NULL;
+
void Reshape( int width, int height )
vtkRenderWindow *renWin = NULL;
 
 
void Reshape(int width, int height)
 
 
{  
 
{  
   renWin->SetSize(width, height);
+
   renWin->SetSize( width, height );
 
}  
 
}  
  
Line 28: Line 43:
 
}
 
}
  
int main( int argc, char **argv)
+
int main( int argc, char **argv )
{
+
{
   glutInit(&argc, argv);
+
  // GLUT initialization
   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
+
   glutInit( &argc, argv );
  glutInitWindowSize(640,480);
+
   glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
   glutCreateWindow("VTK-GLUT test");
+
   glutCreateWindow( "VTK-GLUT Example") ;
 +
  glutReshapeFunc( Reshape );
 +
  glutDisplayFunc( Draw );
  
   vtkConeSource *cone = vtkConeSource::New();
+
   // Creation of a simple VTK pipeline
   cone->SetHeight( 3.0 );
+
   vtkNew<vtkConeSource> cone;
  cone->SetRadius( 1.0 );
 
  cone->SetResolution( 10 );
 
  
   vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
+
   vtkNew<vtkPolyDataMapper> coneMapper;
 
   coneMapper->SetInputConnection( cone->GetOutputPort() );
 
   coneMapper->SetInputConnection( cone->GetOutputPort() );
  
   vtkActor *coneActor = vtkActor::New();
+
   vtkNew<vtkActor> coneActor;
   coneActor->SetMapper( coneMapper );
+
   coneActor->SetMapper( coneMapper.GetPointer() );
  
  ren = vtkRenderer::New();
+
   ren->AddActor( coneActor.GetPointer() );
   ren->AddActor( coneActor );
+
   renWin->AddRenderer( ren.GetPointer() );
   ren->SetBackground( 0.1, 0.2, 0.4 );
 
  
   renWin = vtkRenderWindow::New();
+
   // Here is the trick: we ask the RenderWindow to join the current OpenGL context created by GLUT
  renWin->InitializeFromCurrentContext(); // associate the RenderWindow with the current OpenGL context
+
   renWin->InitializeFromCurrentContext();
   renWin->AddRenderer( ren );
 
  renWin->SetSize( 300, 300 );
 
 
  glutReshapeFunc(Reshape);
 
  glutDisplayFunc(Draw);
 
 
    
 
    
 +
  // Let's start the main GLUT rendering loop
 
   glutMainLoop();
 
   glutMainLoop();
 
   
 
   
  cone->Delete();
+
   return EXIT_SUCCESS;
  coneMapper->Delete();
 
  coneActor->Delete();
 
  ren->Delete();
 
  renWin->Delete();
 
 
   return 0;
 
 
}
 
}
 +
</source>
 +
 +
==CMakeLists.txt==
 +
<source lang="cmake">
 +
project(VtkGlut CXX)
 +
cmake_minimum_required(VERSION 2.8)
 +
 +
find_package(VTK REQUIRED)
 +
find_package(GLUT REQUIRED)
 +
 +
include(${VTK_USE_FILE})
 +
 +
include_directories(${GLUT_INCLUDE_DIR})
 +
 +
add_executable(${PROJECT_NAME} vtkglut.cxx)
 +
 +
target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES} ${GLUT_LIBRARIES})
 
</source>
 
</source>

Latest revision as of 08:57, 21 November 2012

Vtkglut.png

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.

It has been successfully tested under Linux, Windows & MacOS X.

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


vtkglut.cxx

#ifndef __APPLE_CC__
#include <GL/glut.h>
#else
#include <glut.h>
#endif
#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()
{ 	
  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

project(VtkGlut CXX)
cmake_minimum_required(VERSION 2.8)

find_package(VTK REQUIRED)
find_package(GLUT REQUIRED)

include(${VTK_USE_FILE})

include_directories(${GLUT_INCLUDE_DIR})

add_executable(${PROJECT_NAME} vtkglut.cxx)

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