スポンサーリンク

vtk 9.0 で表示(5) ポリゴンの頂点色を付けて表示

//VTK_MODULE_INITに必要
#include <vtkAutoInit.h>


#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

#include <vtkPoints.h> // vtkPoints用
#include <vtkPolygon.h> // vtkPolygon用
#include <vtkPolyData.h> //vtkPolyData用
#include <vtkPolyDataMapper.h>

#include <vtkUnsignedCharArray.h> //色データ用
#include <vtkPointData.h> // 頂点データ用


//必須
#pragma comment(lib,"vtkCommonCore-9.0.lib")
#pragma comment(lib,"vtkRenderingCore-9.0.lib")
#pragma comment(lib,"vtkInteractionStyle-9.0.lib")
#pragma comment(lib,"vtkFiltersSources-9.0.lib")
#pragma comment(lib,"vtkCommonExecutionModel-9.0.lib")
#pragma comment(lib,"vtkCommonDataModel-9.0.lib ") // ポリゴン用

#pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib")


//必須
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);


int main(int /*argc*/, char ** /*argv*/)
{


  //////////////////////////////////////
  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, 0.4, 0.0);


  //////////////////////////////////////
  vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
  polygon->GetPointIds()->SetNumberOfIds(4);//4頂点
  polygon->GetPointIds()->SetId(0, 0);
  polygon->GetPointIds()->SetId(1, 1);
  polygon->GetPointIds()->SetId(2, 2);
  polygon->GetPointIds()->SetId(3, 3);




  //////////////////////////////////////
  vtkSmartPointer<vtkCellArray> polylist = vtkSmartPointer<vtkCellArray>::New();
  polylist->InsertNextCell(polygon);

  //////////////////////////////////////
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
  polydata->SetPolys(polylist);

  ////////////////////////////////////////////
  ////////////////////////////////////////////
  vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
  colors->SetNumberOfComponents(3);
  colors->SetName("Colors");

  colors->InsertNextTuple3(255, 0, 0);
  colors->InsertNextTuple3(0, 255, 0);
  colors->InsertNextTuple3(0, 0, 255);
  colors->InsertNextTuple3(255, 255, 0);

  polydata->GetPointData()->SetScalars(colors);

  ////////////////////////////////////////////
  ////////////////////////////////////////////



  //////////////////////////////////////
  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputData(polydata);
  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  //////////////////////////////////////
  auto renderer = vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);
  renderer->ResetCamera();

  //////////////////////////////////////
  auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  //////////////////////////////////////
  auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  renderWindow->SetInteractor(interactor);
  renderWindow->Render();

  interactor->Start(); //イベントループへ入る

  return 0;
}

コメントを残す

メールアドレスが公開されることはありません。

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: