[vtkusers] Texture Mapping PolyData Broke In VTK 8.0??
Ken Martin
ken.martin at kitware.com
Thu Aug 3 11:32:13 EDT 2017
You can fix the issue by using 2D texture coordinates with a 2D texture map
(better memory and performance as well). Apparently the old version
accepted 3D tcoords even when the texturemap was 2D.
On Wed, Aug 2, 2017 at 3:43 PM, Donny Zimmerman <zmanvortex at gmail.com>
wrote:
> *The following code works in VTK 7.1 but does not work in VTK 8.0 using
> the same PC and compiled with Visual Studio 2015. I have attached the XML
> .vti image file and also the .txt file used for the lookup table.*
>
> #include "stdafx.h"
>
> #include "vtkAutoInit.h"
> #define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle,
> vtkRenderingFreeType,vtkRenderingOpenGL2)
> #define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)
>
> #include "vtkXMLImageDataReader.h"
> #include "vtkImageData.h"
> #include "vtkImageViewer2.h"
> #include "vtkLookupTable.h"
> #include "vtkRenderer.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkRenderWindow.h"
> #include "vtkImageResize.h"
> #include "vtkImageMapToWindowLevelColors.h"
> #include "vtkTransformPolyDataFilter.h"
> #include "vtkFloatArray.h"
> #include "vtkMath.h"
> #include "vtkTriangle.h"
> #include "vtkPointData.h"
> #include "vtkTexture.h"
> #include "vtkTransform.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkProperty.h"
>
> #include <iostream> // std::cout
> #include <sstream> // std::istringstream
> #include <string> // std::string
> #include <math.h>
>
> int main()
> {
> double imgfact = 2.0;
>
> vtkSmartPointer<vtkLookupTable> lookuptable = vtkSmartPointer<
> vtkLookupTable>::New();
> lookuptable->GetTable()->Reset();
> lookuptable->SetNumberOfColors(256);
> lookuptable->SetRange(0, 255.0);
>
> std::ifstream infile;
> std::string line;
>
> int ind = 0;
> double r = 0, g = 0, b = 0, a = 0;
> std::string sind, sr, sg, sb, sa;
>
> infile.open("lutable.txt", ifstream::in);
> if (infile.is_open())
> {
> while (infile.good() && ind < 255)
> {
> std::getline(infile, line);
> std::istringstream is(line);
> std::getline(is, sind, ',');
> std::getline(is, sr, ',');
> std::getline(is, sg, ',');
> std::getline(is, sb, ',');
> std::getline(is, sa);
> ind = std::stoi(sind);
> r = std::stod(sr);
> g = std::stod(sg);
> b = std::stod(sb);
> a = std::stod(sa);
>
> lookuptable->SetTableValue(ind, r, g, b, a);
>
> }
> infile.close();
> }
> else
> {
> cout << "Error opening file";
> }
>
> lookuptable->Build();
>
> vtkSmartPointer<vtkXMLImageDataReader> imgdat = vtkSmartPointer<
> vtkXMLImageDataReader>::New();
> imgdat->SetFileName("testimage.vti");
> imgdat->ReleaseDataFlagOn();
> imgdat->Update();
>
> int dims[3];
> imgdat->GetOutput()->GetDimensions(dims);
>
> int img_res_x = dims[0];
> int img_res_y = dims[1];
>
> vtkSmartPointer<vtkImageResize> resize1 = vtkSmartPointer<
> vtkImageResize>::New();
> resize1->SetInputConnection(imgdat->GetOutputPort());
> resize1->SetOutputDimensions(img_res_x / imgfact, img_res_y / imgfact, 1);
> resize1->SetResizeMethodToOutputDimensions();
> resize1->ReleaseDataFlagOn();
>
> vtkSmartPointer<vtkImageResize> resize2 = vtkSmartPointer<
> vtkImageResize>::New();
> resize2->SetInputConnection(resize1->GetOutputPort());
> resize2->SetOutputDimensions(img_res_x, img_res_y, 1);
> resize2->SetResizeMethodToOutputDimensions();
> resize2->ReleaseDataFlagOn();
>
> vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
> vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>:
> :New();
> vtkSmartPointer<vtkFloatArray> textureCoordinates =
> vtkSmartPointer<vtkFloatArray>::New();
> textureCoordinates->SetNumberOfComponents(3);
> textureCoordinates->SetName("TextureCoordinates");
>
> float xval, yval, x, y, z;
>
> double sinang = sin(vtkMath::RadiansFromDegrees(19.0));
> z = sinang * 1000.0;
>
> int ptIdIndex = 0;
>
> for (int rrad = 0; rrad < 360; rrad++)
> {
> vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::
> New();
> triangle->GetPointIds()->SetNumberOfIds(3);
>
> // Point 1
> points->InsertNextPoint(1000.0, 1000.0, 0.0);
> textureCoordinates->InsertNextTuple3(.5, .5, 0);
> triangle->GetPointIds()->SetId(0, ptIdIndex++);
>
> // Point 2
> xval = (float)(sin(((float)rrad * vtkMath::Pi()) / 180.0));
> yval = (float)(cos(((float)rrad * vtkMath::Pi()) / 180.0));
> x = 1000.0 + (1000.0 * xval);
> y = 1000.0 + (1000.0 * yval);
> points->InsertNextPoint(x, y, z);
> textureCoordinates->InsertNextTuple3(x / 2000.0, y / 2000.0, 0);
> triangle->GetPointIds()->SetId(1, ptIdIndex++);
>
> // Point 3
> xval = (float)(sin(((float)(rrad + 1) * vtkMath::Pi()) / 180.0));
> yval = (float)(cos(((float)(rrad + 1) * vtkMath::Pi()) / 180.0));
> x = 1000.0 + (1000.0 * xval);
> y = 1000.0 + (1000.0 * yval);
> points->InsertNextPoint(x, y, z);
> textureCoordinates->InsertNextTuple3(x / 2000.0, y / 2000.0, 0);
> triangle->GetPointIds()->SetId(2, ptIdIndex++);
>
> triangles->InsertNextCell(triangle);
>
> }
>
> vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::
> New();
> polyData->SetPoints(points);
> polyData->SetPolys(triangles);
> polyData->GetPointData()->SetTCoords(textureCoordinates);
>
> vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
> texture->MapColorScalarsThroughLookupTableOn();
> texture->SetLookupTable(lookuptable);
> texture->InterpolateOff();
> texture->SetInputConnection(resize2->GetOutputPort());
>
> vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<
> vtkTransformPolyDataFilter>::New();
> tf->SetInputData(polyData);
> double xfmscale = 250.0;
>
> vtkSmartPointer<vtkTransform> pdxfm = vtkSmartPointer<vtkTransform>:
> :New();
> pdxfm->Scale(xfmscale, xfmscale, xfmscale);
> pdxfm->Translate(-img_res_x / 2, -img_res_y / 2, 0);
> tf->SetTransform(pdxfm);
>
> vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<
> vtkPolyDataMapper>::New();
> mapper->SetInputConnection(tf->GetOutputPort());
> mapper->ScalarVisibilityOff();
>
> vtkSmartPointer<vtkActor> radarActor = vtkSmartPointer<vtkActor>::New();
> radarActor->SetMapper(mapper);
> radarActor->SetTexture(texture);
> radarActor->GetProperty()->LightingOff();
> radarActor->GetProperty()->ShadingOff();
>
> vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::
> New();
> vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<
> vtkRenderWindow>::New();
> renderWindow->AddRenderer(renderer);
> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
> renderWindowInteractor->SetRenderWindow(renderWindow);
>
> renderer->AddActor(radarActor);
>
> renderWindow->Render();
> renderWindowInteractor->Start();
>
> return 0;
> }
>
> *In 7.1 I see the texture rendered on the polydata correctly, but in 8.0 I
> only see the white shaded polydata. Any idea what may have changed in 8.0
> that would prevent this from working.*
>
> *Thanks.*
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>
--
Ken Martin PhD
Distinguished Engineer
Kitware Inc.
28 Corporate Drive
Clifton Park NY 12065
This communication, including all attachments, contains confidential and
legally privileged information, and it is intended only for the use of the
addressee. Access to this email by anyone else is unauthorized. If you are
not the intended recipient, any disclosure, copying, distribution or any
action taken in reliance on it is prohibited and may be unlawful. If you
received this communication in error please notify us immediately and
destroy the original message. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170803/39fce0e1/attachment.html>
More information about the vtkusers
mailing list