vtkTextureMapToSphere可以用于将纹理贴在球体上的场景。
vtkTextureMapToSphere作为filter在mapper之前进行数据处理,将二维纹理数据映射到球体上。

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkActor2D.h>
#include <vtkTextureMapToSphere.h>
#include <vtkStripper.h>
#include <vtkTexture.h>
#include <vtkPNGReader.h>
using namespace std;

int main()
{
    vtkSmartPointer<vtkSphereSource> sphere =
            vtkSmartPointer<vtkSphereSource>::New();
    sphere->SetRadius( 1 );

    vtkSmartPointer<vtkTextureMapToSphere> textureMap =
            vtkSmartPointer<vtkTextureMapToSphere>::New();
    textureMap->SetInputConnection( sphere->GetOutputPort() );

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection( textureMap->GetOutputPort() );

    vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName( "/Users/weiyang/Desktop/Untitled.png" );

    vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
    texture->SetInputConnection( reader->GetOutputPort() );
    texture->InterpolateOn();

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper( mapper );
    actor->SetTexture( texture );

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注