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

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(Created page with "This page shows an example of how to use the new method vtkRenderWindow::InitializeFromCurrentContext() to associate a vtkRenderWindow instance to an existing window and activate...")
 
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.
 
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.
 
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.
 
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.
 +
 +
==XGMLReader.cxx==
 +
<source lang="cpp">
 +
 +
#include <gl/glut.h>
 +
#include "vtkConeSource.h"
 +
#include "vtkPolyDataMapper.h"
 +
#include "vtkRenderWindow.h"
 +
#include "vtkCamera.h"
 +
#include "vtkActor.h"
 +
#include "vtkRenderer.h"
 +
 +
vtkRenderer *ren = NULL;
 +
vtkRenderWindow *renWin = NULL;
 +
 +
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)
 +
{
 +
  glutInit(&argc, argv);
 +
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
 +
  glutInitWindowSize(640,480);
 +
  glutCreateWindow("VTK-GLUT test");
 +
 +
  vtkConeSource *cone = vtkConeSource::New();
 +
  cone->SetHeight( 3.0 );
 +
  cone->SetRadius( 1.0 );
 +
  cone->SetResolution( 10 );
 +
 +
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
 +
  coneMapper->SetInputConnection( cone->GetOutputPort() );
 +
 +
  vtkActor *coneActor = vtkActor::New();
 +
  coneActor->SetMapper( coneMapper );
 +
 +
  ren = vtkRenderer::New();
 +
  ren->AddActor( coneActor );
 +
  ren->SetBackground( 0.1, 0.2, 0.4 );
 +
 +
  renWin = vtkRenderWindow::New();
 +
  renWin->InitializeFromCurrentContext(); // associate the RenderWindow with the current OpenGL context
 +
  renWin->AddRenderer( ren );
 +
  renWin->SetSize( 300, 300 );
 +
 +
  glutReshapeFunc(Reshape);
 +
  glutDisplayFunc(Draw);
 +
 
 +
  glutMainLoop();
 +
 +
  cone->Delete();
 +
  coneMapper->Delete();
 +
  coneActor->Delete();
 +
  ren->Delete();
 +
  renWin->Delete();
 +
 +
  return 0;
 +
}
 +
</source>

Revision as of 04:48, 21 November 2012

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. 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.

XGMLReader.cxx

#include <gl/glut.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"

vtkRenderer *ren = NULL;
vtkRenderWindow *renWin = NULL;
 
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)
{ 	 
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowSize(640,480);
  glutCreateWindow("VTK-GLUT test");

  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );

  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );

  ren = vtkRenderer::New();
  ren->AddActor( coneActor );
  ren->SetBackground( 0.1, 0.2, 0.4 );

  renWin = vtkRenderWindow::New();
  renWin->InitializeFromCurrentContext(); // associate the RenderWindow with the current OpenGL context
  renWin->AddRenderer( ren );
  renWin->SetSize( 300, 300 );
 
  glutReshapeFunc(Reshape);
  glutDisplayFunc(Draw);
  
  glutMainLoop();
 
  cone->Delete();
  coneMapper->Delete();
  coneActor->Delete();
  ren->Delete();
  renWin->Delete();
 
  return 0;
}