vtk textured polygon

Ref: https://vtk.org/Wiki/VTK/Examples/Cxx/Visualization/TextureMapQuad

// Read the image which will be the texture
  vtkSmartPointer<vtkJPEGReader> jPEGReader =
    vtkSmartPointer<vtkJPEGReader>::New();
  jPEGReader->SetFileName ( inputFilename.c_str() );
  
  // Create a plane
  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 2.0, 0.0);
  
  vtkSmartPointer<vtkCellArray> polygons =
    vtkSmartPointer<vtkCellArray>::New();
  vtkSmartPointer<vtkPolygon> polygon =
    vtkSmartPointer<vtkPolygon>::New();
  polygon->GetPointIds()->SetNumberOfIds(4); //make a quad
  polygon->GetPointIds()->SetId(0, 0);
  polygon->GetPointIds()->SetId(1, 1);
  polygon->GetPointIds()->SetId(2, 2);
  polygon->GetPointIds()->SetId(3, 3);
  
  polygons->InsertNextCell(polygon);
vtk, 
  vtkSmartPointer<vtkPolyData> quad =
    vtkSmartPointer<vtkPolyData>::New();
  quad->SetPoints(points);
  quad->SetPolys(polygons);
  
  vtkSmartPointer<vtkFloatArray> textureCoordinates =
    vtkSmartPointer<vtkFloatArray>::New();
  textureCoordinates->SetNumberOfComponents(3);
  textureCoordinates->SetName("TextureCoordinates");
  
  float tuple[3] = {0.0, 0.0, 0.0};
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 1.0; tuple[1] = 0.0; tuple[2] = 0.0;
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 1.0; tuple[1] = 1.0; tuple[2] = 0.0;
  textureCoordinates->InsertNextTuple(tuple);
  tuple[0] = 0.0; tuple[1] = 2.0; tuple[2] = 0.0;
  textureCoordinates->InsertNextTuple(tuple); 
 
  textureCoordinates->SetNumberOfComponents(2);
  textureCoordinates->InsertTuple2(0, 0.0, 0.0); 
  textureCoordinates->InsertTuple2(1, 1.0, 0.0);
  textureCoordinates->InsertTuple2(2, 1.0, 1.0);
  textureCoordinates->InsertTuple2(3, 0.0, 1.0);
   
 quad->GetPointData()->SetTCoords(textureCoordinates);
  
  // Apply the texture
  vtkSmartPointer<vtkTexture> texture =
    vtkSmartPointer<vtkTexture>::New();
  texture->SetInputConnection(jPEGReader->GetOutputPort());
  
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  mapper->SetInput(quad);
#else
  mapper->SetInputData(quad);
#endif

  vtkSmartPointer<vtkActor> texturedQuad =
    vtkSmartPointer<vtkActor>::New();
  texturedQuad->SetMapper(mapper);
  texturedQuad->SetTexture(texture);

  // Visualize the textured plane
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(texturedQuad);
  renderer->SetBackground(1,1,1); // Background color white
  renderer->ResetCamera();

Commenti

Post popolari in questo blog

Indovinello: Soluzioni!!!

Gli indovinelli che ci avete mandato 10