Reputation: 1006
I have vtkAppendPolyData which contains 4 vtkConeSource. I want to color these 4 cone with different colors. Is there any way in vtk to implement this. If you have any other suggestion please let me know.
vtkConeSource *cone1 = vtkConeSource::New();
cone1->SetHeight(6.0);
cone1->SetRadius(3.0);
cone1->SetCenter(0, 0, 0);
cone1->SetResolution(10);
vtkPolyData *coneData1 = cone1->GetOutput();
unsigned char red[3] = {255, 0, 0};
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
colors->InsertNextTupleValue(red);
coneData1->GetCellData()->SetScalars(colors);
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInput(coneData1);
mapper->Update();
mapper->StaticOn();
vtkActor *coneActor = vtkActor::New();
coneActor->SetMapper( mapper );
vtkRenderer *ren1= vtkRenderer::New();
ren1->AddActor( coneActor );
ren1->SetBackground( 0.1, 0.2, 0.4 );
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer( ren1 );
renWin->SetSize( 300, 300 );
vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
renWin->SetInteractor(interactor);
renWin->Render();
interactor->Start();
This is my code I have created cone and i want to color it Even though i have set coneData1->GetCellData()->SetScalars(colors) its not showing cone in Red color.
Upvotes: 0
Views: 1690
Reputation: 1006
vtkConeSource *cone1 = vtkConeSource::New();
cone1->SetHeight(6.0);
cone1->SetRadius(3.0);
cone1->SetCenter(0, 0, 0);
cone1->SetResolution(10);
vtkPolyData *coneData1 = cone1->GetOutput();
unsigned char red[3] = {255, 0, 0};
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
colors->InsertNextTupleValue(red);
// Before setting color to cell data
// Upadte coneSource
coneData1->GetCellData()->Update();
// This will give celldata Other wise Number of cell data will be zero
// Insert tuples equal to number of Cell present in Polydata
coneData1->GetCellData()->SetScalars(colors);
Similarly for other cone I added color and this solves my problem
Upvotes: 1
Reputation: 10273
You'd have to attach a color array to each vtkConeSource output before connecting them to the append filter. You'd do that something like this:
unsigned char red[3] = {255, 0, 0};
vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
colors->InsertNextTupleValue(red);
polydata->GetCellData()->SetScalars(colors);
(here is a complete example: http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/TriangleSolidColor )
Here is a description of the different ways to color an object (coloring the data directly, versus coloring the actor) that might also be worth looking at: https://docs.google.com/present/edit?id=0AcyIfGqnlfSoZGdqaGhnMnJfMjc0Z3EybnNkZzQ&hl=en
Upvotes: 2