欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vtk 纹理贴图

程序员文章站 2022-06-11 10:53:38
...

环境 vtk8.2 + linux

直接上代码

#include <vtkCylinderSource.h>
#include <vtkLineSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkTubeFilter.h>
#include <vtkBMPReader.h>
#include <vtkJPEGReader.h>
#include <vtkTexture.h>
#include <vtkTextureMapToCylinder.h>
#include <vtkTransformTextureCoords.h>

int main(int, char *[])
{
    // Create a Cylinder
    vtkSmartPointer<vtkCylinderSource> cylinderSource =
        vtkSmartPointer<vtkCylinderSource>::New();
    cylinderSource->SetHeight(10.0);
    cylinderSource->SetCenter(0.0, 0.0, 0.0);
    cylinderSource->SetRadius(2.0);
    cylinderSource->SetResolution(50);
    
	//vtkSmartPointer<vtkSTLReader> cylinderSource = vtkSmartPointer<vtkSTLReader>::New();  加载模型

    // Load a bmp file
    vtkSmartPointer<vtkBMPReader> bmpReader =
        vtkSmartPointer<vtkBMPReader>::New();
    bmpReader->SetFileName("/root/workSpace/SimpleView/gothic03.bmp");

    // or Load a jpeg file
    vtkSmartPointer<vtkJPEGReader> jpegReader =
        vtkSmartPointer<vtkJPEGReader>::New();
    jpegReader->SetFileName("/root/workSpace/SimpleView/BH5A7920.jpg");

    vtkSmartPointer<vtkTexture> texture =
        vtkSmartPointer<vtkTexture>::New();
    texture->SetInputConnection(bmpReader/*jpegReader*/->GetOutputPort());
    texture->InterpolateOn();

    // map two-dimensional to three-dimensional, use cylinder
    vtkSmartPointer<vtkTextureMapToCylinder> textureMap =
        vtkSmartPointer<vtkTextureMapToCylinder>::New();
    textureMap->SetInputConnection(cylinderSource->GetOutputPort());

    vtkSmartPointer<vtkTransformTextureCoords> transformTextureCoords =
        vtkSmartPointer<vtkTransformTextureCoords>::New();
    transformTextureCoords->SetInputConnection(textureMap->GetOutputPort());
    //transformTextureCoords->SetScale(1, 1, 0);

    vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection(/*textureMap*/transformTextureCoords->GetOutputPort());

    vtkSmartPointer<vtkActor> cylinderActor =
        vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper(cylinderMapper);
    cylinderActor->SetTexture(texture);

    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(600, 600);
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(cylinderActor);

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}