[vtkusers] how to get a colorful cube from vtkCubeSource?

Toron J. ji_wi at yahoo.com
Fri Nov 16 12:19:01 EST 2007


Hi,

I want to use vtkCubeSource to create a colorful box (I just modify the VTK example Cube.cxx). But I can not see a colorful cube but only white one.  Does anyone can help me?

-------- My code -----------------------------------------------------------
#include "vtkCubeSource.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"

void main ()
{
    vtkFloatArray *scalars = vtkFloatArray::New();
    for (int i=0; i<8; i++) 
        scalars->InsertValue(i, i);    // Set color
 
    vtkCubeSource *box = vtkCubeSource::New();
    
    vtkPolyData *cube = vtkPolyData::New();    // convert to polydata
        cube = box->GetOutput();

        cube->GetPointData()->SetScalars(scalars);    // assign color to vertex

    vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
        cubeMapper->SetInput(cube);
        cubeMapper->SetScalarRange(0,7);    // set hue range

    vtkActor *cubeActor = vtkActor::New();
        cubeActor->SetMapper(cubeMapper);

    vtkRenderer *renderer = vtkRenderer::New();
        renderer->AddActor(cubeActor);
        renderer->SetBackground(0,0,0);
  
    vtkRenderWindow *renWin = vtkRenderWindow::New();
        renWin->SetSize(500,500);
        renWin->AddRenderer(renderer);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
        iren->SetRenderWindow(renWin);

    renWin->Render();
    iren->Start();
}
---------------------------------------------------------------------------

Thanks a lot!

Toron


       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20071116/aa7ad17d/attachment.htm>


More information about the vtkusers mailing list