#!/usr/bin/env python

# This example shows how to apply an vtkImageData texture to an plane
# vtkPolyData object.
# Note: Input jpg file can be located in the VTKData repository.
#
# @author JBallesteros
##

import vtk

jpegfile = "earth_display.jpg"

# Create a render window
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(480,480)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Generate plane polydata
plane = vtk.vtkPlaneSource()

# Read the image data from a file
reader = vtk.vtkJPEGReader()
reader.SetFileName(jpegfile)

# Create texture object
texture = vtk.vtkTexture()
texture.SetInput(reader.GetOutput())

# Map texture coordinates
map_to_plane = vtk.vtkTextureMapToPlane()
map_to_plane.SetInput(plane.GetOutput())

# Create mapper and set the mapped texture as input
mapper = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    mapper.SetInput(map_to_plane.GetOutput())
else:
    mapper.SetInputConnection(map_to_plane.GetOutputPort())

# Create actor and set the mapper and the texture
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.SetTexture(texture)

ren.AddActor(actor)

iren.Initialize()
renWin.Render()
iren.Start()
