Hello,
I am working on replacing the Visualization in our legacy application that earlier used OpenGL and trying to use VTK as a replacement.
When applying a texture to a polygon as shown below, the texture image does not appear as I am expecting and gets distorted as shown in the below image
Here is my VTK code
```
// Step 1: Read Image Data
vtkSmartPointer<vtkImageReader2Factory> readerFactory = vtkSmartPointer<vtkImageReader2Factory>::New();
vtkSmartPointer<vtkImageReader2> imageReader = readerFactory->CreateImageReader2("D:\\Temp\\MyTestImage2.jpg");
imageReader->SetFileName("D:\\Temp\\MyTestImage2.jpg");
imageReader->Update();
// Step 2: Convert Image Data to Image Mapper...
m_imageMapper = vtkSmartPointer<vtkImageMapToColors>::New();
m_imageMapper->SetInputConnection(imageReader->GetOutputPort());
m_imageMapper->SetLookupTable(m_lookupTable);
m_imageMapper->Update();
// Step 3: Create Texture from image mapper....
m_texture = vtkSmartPointer<vtkTexture>::New();
m_texture->SetInputConnection(m_imageMapper->GetOutputPort());
m_texture->RepeatOff();
m_texture->InterpolateOn();
// Step 4: Initialize Points
m_points = vtkSmartPointer<vtkPoints>::New();
m_points->InsertNextPoint( 0.0, 0.0, 0.0);
m_points->InsertNextPoint(-0.5, 0.0, 0.0);
m_points->InsertNextPoint(-2.0, -0.5, 0.0);
m_points->InsertNextPoint(-1.0, -1.0, 0.0);
unsigned int numPoints = 4;
// Step 5: Create a polygon
m_polygon = vtkSmartPointer<vtkPolygon>::New();
m_polygon->GetPointIds()->SetNumberOfIds(numPoints);
for (unsigned int i = 0; i < numPoints; i++)
{
m_polygon->GetPointIds()->SetId(i, i);
}
m_polygons = vtkSmartPointer<vtkCellArray>::New();
m_polygons->InsertNextCell(m_polygon);
m_polyData = vtkSmartPointer<vtkPolyData>::New();
m_polyData->SetPoints(m_points);
m_polyData->SetPolys(m_polygons);
// Step 6: Add texture coordinates
vtkSmartPointer<vtkFloatArray> textureCoordinates = vtkSmartPointer<vtkFloatArray>::New();
textureCoordinates->SetNumberOfComponents(2);
textureCoordinates->SetName("TextureCoordinates");
textureCoordinates->InsertNextTuple2(0.0, 0.0);
textureCoordinates->InsertNextTuple2(1.0, 0.0);
textureCoordinates->InsertNextTuple2(1.0, 1.0);
textureCoordinates->InsertNextTuple2(0.0, 1.0);
m_polyData->GetPointData()->SetTCoords(textureCoordinates);
// Step 7: Map Texture to Polygon
m_polyDataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
m_polyDataMapper->SetInputData(m_polyData);
// Step 8: Render
m_sector = vtkSmartPointer<vtkActor>::New();
m_sector->SetMapper(m_polyDataMapper);
m_sector->SetTexture(m_texture);
m_sector->VisibilityOn();
m_renderer1->AddActor(m_sector);
I am new to VTK and come from an OpenGL background.
I could be missing some VTK understanding on texture mapping and greatly appreciate
if you could help with some pointers to get the texture mapping right.
Thanks for your time.
Regards
Balaji