#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRotationalExtrusionFilter.h>
#include <vtkStripper.h>
#include <vtkTubeFilter.h>
#include <vtkSmartPointer.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int, char* [])
{
// Create the RenderWindow, Renderer and both Actors
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer.GetPointer());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.GetPointer());
// create bottle profile
//
vtkNew<vtkPoints> points;
points->InsertPoint(0, 0.01, 0.0, 0.0);
points->InsertPoint(1, 1.5, 0.0, 0.0);
points->InsertPoint(2, 1.5, 0.0, 3.5);
points->InsertPoint(3, 1.25, 0.0, 3.75);
points->InsertPoint(4, 0.75, 0.0, 4.00);
points->InsertPoint(5, 0.6, 0.0, 4.35);
points->InsertPoint(6, 0.7, 0.0, 4.65);
points->InsertPoint(7, 1.0, 0.0, 4.75);
points->InsertPoint(8, 1.0, 0.0, 5.0);
points->InsertPoint(9, 0.2, 0.0, 5.0);
vtkNew<vtkCellArray> lines;
lines->InsertNextCell(10); // number of points
lines->InsertCellPoint(0);
lines->InsertCellPoint(1);
lines->InsertCellPoint(2);
lines->InsertCellPoint(3);
lines->InsertCellPoint(4);
lines->InsertCellPoint(5);
lines->InsertCellPoint(6);
lines->InsertCellPoint(7);
lines->InsertCellPoint(8);
lines->InsertCellPoint(9);
vtkNew<vtkPolyData> profile;
profile->SetPoints(points.GetPointer());
profile->SetLines(lines.GetPointer());
// extrude profile to make bottle
//
vtkNew<vtkRotationalExtrusionFilter> extrude;
extrude->SetInputData(profile.GetPointer());
extrude->SetResolution(60);
vtkNew<vtkPolyDataMapper> map;
map->SetInputConnection(extrude->GetOutputPort());
vtkNew<vtkActor> bottle;
bottle->SetMapper(map.GetPointer());
bottle->GetProperty()->SetColor(colors->GetColor3d("Mint").GetData());
// display the profile
vtkNew<vtkStripper> stripper;
stripper->SetInputData(profile.GetPointer());
vtkNew<vtkTubeFilter> tubes;
tubes->SetInputConnection(stripper->GetOutputPort());
tubes->SetNumberOfSides(11);
tubes->SetRadius(0.05);
vtkNew<vtkPolyDataMapper> profileMapper;
profileMapper->SetInputConnection(tubes->GetOutputPort());
vtkNew<vtkActor> profileActor;
profileActor->SetMapper(profileMapper.GetPointer());
profileActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
// Add the actors to the renderer, set the background and size
//
renderer->AddActor(bottle.GetPointer());
renderer->AddActor(profileActor.GetPointer());
renderer->SetBackground(colors->GetColor3d("Burlywood").GetData());
renWin->SetSize(640, 480);
renWin->SetWindowName("Bottle");
renWin->Render();
renderer->GetActiveCamera()->SetPosition(1, 0, 0);
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
// render the image
//
iren->Start();
return EXIT_SUCCESS;
}
My guess is that your camera is not looking at the data or the data is being clipped away.
Just before iren->Start
, add
renderer->ResetCameraClippingRange();
renWin->Render()
These two pieces of code exist.
But I found that I am unable to use vtkNamedFors.
I got an exception “Microsoft C++ Exception: std::length_error”, when i use the code GetColor3d("Mint")
.
vtkNew<vtkNamedColors> colors;
vtkColor3d color = colors->GetColor3d("Mint");
double* a = color.GetData();
Sometime no exception, but a is [0.0f,0.0f,0.0f] when no exception.
Here is my solution.
Change to
bottle->GetProperty()->SetColor(0,0,1);
Change to
profileActor->GetProperty()->SetColor(1,0,0);
Change to
renderer->SetBackground(1,1,1);
But I want to know why. why can not use vtkNamedColors
?
Have a look at Bottle. Check your CMakeLists.txt files against the one there. Also ensure that if you built VTK as Release make sure your build is also Release.