[vtkusers] Colour a triangle
Naoko TAKAYA
takaya at cv.cs.ritsumei.ac.jp
Mon Nov 11 04:42:21 EST 2002
Dear VTK-users,
I have a simple problem.
I just want to colour the triangle with specified
rgb values specified at each vertices.
It only gets coloured in blue and red and nothing else.
My brief code is the following. Please tell me what I'm doing wrong...
Any advice is very much appreciated ;-)
Naoko.
==================================================================
float points[3][3];
int pts[3] = {0,1,2};
int i;
float x1, y1, z1, x2, y2, z2, x3, y3, z3;
int NumOfPoints = 0;
int PointOfTriangle[3];
// Coordinates of triangle vertices
x1=0.0f; y1=0.0f; z1=0.0f;
x2=2.0f; y2=2.0f; z2=0.0f;
x3=4.0f; y3=0.0f; z3=0.0f;
points[0][0] = x1;
points[0][1] = y1;
points[0][2] = z1;
points[1][0] = x2;
points[1][1] = y2;
points[1][2] = z2;
points[2][0] = x3;
points[2][1] = y3;
points[2][2] = z3;
vtkFloatArray *vScalars = vtkFloatArray::New();
vScalars -> SetNumberOfComponents(3);
vScalars -> SetNumberOfTuples(3);
vtkPoints *vPts = vtkPoints::New();
vPts -> SetDataTypeToFloat();
vtkCellArray *vCellArray = vtkCellArray::New();
// Insert triangle vertices
for(i=0; i<3; i++){
vPts -> InsertPoint(i, points[i]);
}
PointOfTriangle[0] = NumOfPoints;
PointOfTriangle[1] = NumOfPoints + 1;
PointOfTriangle[2] = NumOfPoints + 2;
vCellArray -> InsertNextCell(3, PointOfTriangle);
// Specify RGB values at each vertice of the triangle
vScalars -> SetComponent( 0, 0, 255 );
vScalars -> SetComponent( 0, 1, 0 );
vScalars -> SetComponent( 0, 2, 0 );
vScalars -> SetComponent( 1, 0, 0 );
vScalars -> SetComponent( 1, 1, 255 );
vScalars -> SetComponent( 1, 2, 0 );
vScalars -> SetComponent( 2, 0, 0 );
vScalars -> SetComponent( 2, 1, 0 );
vScalars -> SetComponent( 2, 2, 255 );
vtkPolyData *vData = vtkPolyData::New();
vData -> SetPoints(vPts);
vData -> SetPolys(vCellArray);
vData -> GetPointData() -> SetScalars(vScalars);
vData -> Squeeze();
// mapper
vtkPolyDataMapper *vMapper = vtkPolyDataMapper::New();
vMapper -> ImmediateModeRenderingOn();
vMapper -> ScalarVisibilityOn();
vMapper -> SetInput(vData);
vMapper -> Update();
// property
vtkProperty *vProp = vtkProperty::New();
vProp -> SetInterpolationToGouraud();
vProp -> SetOpacity(0.8f);
// actor
vtkActor *vActor = vtkActor::New();
vActor -> SetMapper(vMapper);
vActor -> SetProperty(vProp);
// render
vRenderer -> AddActor(vActor);
==================================================================
More information about the vtkusers
mailing list