vtk中的纹理贴图要用 vtkTextureMapToPlane vtkTextureMapToSphere vtkTextureMapToCylinder与 vtkTransformTextureCoords 配合使用才能达到最好的效果。
目前vtk只支持2维纹理,3维纹理似乎并不支持。
那么vtk的纹理是如何工作的呢??
那么我们通过自己的一段代码来解释一下:
我们用vtk的tcl来解释代码了。
首先定义一个面 这是一个竖直的面
vtkPoints points
points InsertPoint 0 0 0 0
points InsertPoint 1 100 100 0
points InsertPoint 2 100 100 11
points InsertPoint 3 0 0 11
定义这个面顶点索引
vtkCellArray lines
lines InsertNextCell 4;#number of points
lines InsertCellPoint 0
lines InsertCellPoint 1
lines InsertCellPoint 2
lines InsertCellPoint 3
将点和多边形加入polydata。
vtkPolyData profile
profile SetPoints points
profile SetPolys lines
计算多边形的发向量。
vtkPolyDataNormals normal
normal SetInput profile
设置多边形的纹理映射模式
设置为plane模式
vtkTextureMapToPlane tmapper
tmapper SetInputConnection [normal GetOutputPort]
设置纹理st坐标系的顶点坐标 和两点坐标,定义了st坐标系
tmapper SetOrigin 0 0 0
tmapper SetPoint1 100 100 0
tmapper SetPoint2 0 0 11
对纹理映射进行计算
vtkTransformTextureCoords xform
xform SetInputConnection [tmapper GetOutputPort]
设置贴图重复比例 第一项是指s 第二项是指t 第三项是指r 一般是1
14:1是根据上面点坐标的数据估算出来的。如果将t坐标定义为1,那么就将纹理向s方向贴14格,也就是说,纹理在这个面片上横着14张
xform SetScale 14 1 1
读取纹理图像
vtkBMPReader bmpReader
bmpReader SetFileName "c://masonry.bmp"
vtkTexture atext
atext SetInputConnection [bmpReader GetOutputPort]
atext InterpolateOn
将数据放入polydata
vtkPolyDataMapper weightedTransMapper
weightedTransMapper SetInputConnection [xform GetOutputPort]
将数据放入actor
vtkActor weightedTransActor
weightedTransActor SetMapper weightedTransMapper
进行显示
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
ren1 AddActor weightedTransActor
ren1 SetBackground 0.1 0.2 0.5
renWin SetSize 300 300
ren1 ResetCamera
[ren1 GetActiveCamera] Azimuth 0
[ren1 GetActiveCamera] Dolly 1
# Get handles to some useful objects
#
iren AddObserver UserEvent {wm deiconify .vtkInteract}
renWin Render
# prevent the tk window from showing up then start the event loop
proc cmd {s} {
rotated Identity
rotated RotateX $s
renWin Render
}
wm withdraw .
目前vtk只支持2维纹理,3维纹理似乎并不支持。
那么vtk的纹理是如何工作的呢??
那么我们通过自己的一段代码来解释一下:
我们用vtk的tcl来解释代码了。
首先定义一个面 这是一个竖直的面
vtkPoints points
points InsertPoint 0 0 0 0
points InsertPoint 1 100 100 0
points InsertPoint 2 100 100 11
points InsertPoint 3 0 0 11
定义这个面顶点索引
vtkCellArray lines
lines InsertNextCell 4;#number of points
lines InsertCellPoint 0
lines InsertCellPoint 1
lines InsertCellPoint 2
lines InsertCellPoint 3
将点和多边形加入polydata。
vtkPolyData profile
profile SetPoints points
profile SetPolys lines
计算多边形的发向量。
vtkPolyDataNormals normal
normal SetInput profile
设置多边形的纹理映射模式
设置为plane模式
vtkTextureMapToPlane tmapper
tmapper SetInputConnection [normal GetOutputPort]
设置纹理st坐标系的顶点坐标 和两点坐标,定义了st坐标系
tmapper SetOrigin 0 0 0
tmapper SetPoint1 100 100 0
tmapper SetPoint2 0 0 11
对纹理映射进行计算
vtkTransformTextureCoords xform
xform SetInputConnection [tmapper GetOutputPort]
设置贴图重复比例 第一项是指s 第二项是指t 第三项是指r 一般是1
14:1是根据上面点坐标的数据估算出来的。如果将t坐标定义为1,那么就将纹理向s方向贴14格,也就是说,纹理在这个面片上横着14张
xform SetScale 14 1 1
读取纹理图像
vtkBMPReader bmpReader
bmpReader SetFileName "c://masonry.bmp"
vtkTexture atext
atext SetInputConnection [bmpReader GetOutputPort]
atext InterpolateOn
将数据放入polydata
vtkPolyDataMapper weightedTransMapper
weightedTransMapper SetInputConnection [xform GetOutputPort]
将数据放入actor
vtkActor weightedTransActor
weightedTransActor SetMapper weightedTransMapper
进行显示
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
ren1 AddActor weightedTransActor
ren1 SetBackground 0.1 0.2 0.5
renWin SetSize 300 300
ren1 ResetCamera
[ren1 GetActiveCamera] Azimuth 0
[ren1 GetActiveCamera] Dolly 1
# Get handles to some useful objects
#
iren AddObserver UserEvent {wm deiconify .vtkInteract}
renWin Render
# prevent the tk window from showing up then start the event loop
proc cmd {s} {
rotated Identity
rotated RotateX $s
renWin Render
}
wm withdraw .