[vtkusers] How to insert a mirror in a VTK scene?

Tim Hutton T.Hutton at eastman.ucl.ac.uk
Mon Sep 16 06:32:38 EDT 2002

Hi Pierre,

I was having a go at that a while back, it shouldn't be that hard. Basic 
technique is to work out where the mirrored observer is, render a scene 
from that viewpoint and use the rendering as a texture on the mirror object 
in the final scene. When the observer moves the reflection needs to be 
re-rendered of course.

Spherical mirrors would be a different matter... in theory the principle 
extends to arbitrarily curved shiny surfaces (basically render from a 
different viewpoint).

Attached a tcl script that has some of the framework.

Let us know how you get on!


At 12:21 16/09/2002 +0200, you wrote:
>Hi everybody,
>Could someone explain me how to modelise a mirror in VTK or whether it's 
>possible at a low computational cost?
>Thanks a lot and have a nice day everybody!
>P. Mayenobe
>PhD student in France (and VTK user, indeed...)
-------------- next part --------------
package require vtk
package require vtkinteraction
package require vtktesting

# Create the RenderWindow, Renderer and both Actors
vtkRenderer ren1
vtkRenderer ren2
vtkRenderWindow renWin
    renWin AddRenderer ren1
    renWin AddRenderer ren2
vtkRenderWindowInteractor iren
    iren SetRenderWindow renWin

# create pipeline for ren1
vtkBYUReader cow
  cow SetGeometryFileName "$VTK_DATA_ROOT/Data/Viewpoint/cow.g"
vtkPolyDataNormals normals
    normals SetInput [cow GetOutput]
    normals SetFeatureAngle 45
vtkPolyDataMapper cowMapper
    cowMapper SetInput [normals GetOutput]
    cowMapper ScalarVisibilityOn
vtkActor cowActor
    cowActor SetMapper cowMapper
    eval [cowActor GetProperty] SetColor 1.0 0.3 0.4

vtkPlaneSource plane
  plane SetOrigin -5 -3.6 -5
  plane SetPoint1 -5 -3.6 5
  plane SetPoint2 5 -3.6 -5
  plane SetResolution 30 30
vtkPolyDataMapper planeMapper
 planeMapper SetInput [plane GetOutput]
vtkActor planeActor
 planeActor SetMapper planeMapper
 [planeActor GetProperty] SetRepresentationToWireframe

# Add the actors to the renderer, set the background and size
ren1 AddActor cowActor
ren1 AddActor planeActor
ren1 SetBackground 1 1 1
ren1 SetViewport 0 0 .5 1

renWin SetSize 512 256
ren1 SetBackground .8 .4 .3

vtkSphereSource aSphere
  aSphere SetThetaResolution 18
  aSphere SetPhiResolution 9
  aSphere SetRadius 2
  aSphere SetStartPhi 30
  aSphere SetEndPhi 150
  aSphere SetStartTheta 210
  aSphere SetEndTheta 330

# phi angle must subtend same as viewing angle in cam1 below (I think)

vtkTextureMapToSphere tmap
  tmap SetInput [aSphere GetOutput]

vtkTransformTextureCoords trans
  trans SetInput [tmap GetOutput]
  trans FlipROn
#  trans SetScale 1 0.75 1

vtkPolyDataMapper aSphereMapper 
  aSphereMapper SetInput [trans GetOutput]
  aSphereMapper ImmediateModeRenderingOn
vtkActor aSphereActor
  aSphereActor SetMapper aSphereMapper
  aSphereActor SetPosition 0 -3 -7
  aSphereActor RotateX -90

# create a camera to look along the z-axis from a certain position
set cam1 [ren1 GetActiveCamera]
$cam1 SetClippingRange 3.95297 50
$cam1 SetFocalPoint 0 -3 0 
$cam1 SetPosition 0 -3 -7
$cam1 SetViewUp 0 1 0
$cam1 SetViewAngle 110

ren2 AddActor aSphereActor
ren2 AddActor cowActor
ren2 AddActor planeActor
ren2 SetViewport .5 0 1 1
[ren2 GetActiveCamera] Azimuth 30
[ren2 GetActiveCamera] Elevation 30
ren2 SetBackground .8 .4 .3
ren1 ResetCameraClippingRange
ren2 ResetCameraClippingRange
renWin Render

vtkRendererSource ren1Image
  ren1Image SetInput ren1
  ren1Image DepthValuesOn
vtkTexture aTexture
  aTexture SetInput [ren1Image GetOutput]
  aTexture RepeatOff

aSphereActor SetTexture aTexture

renWin Render

# render the image
iren AddObserver UserEvent {wm deiconify .vtkInteract}

renWin Render

# prevent the tk window from showing up then start the event loop
wm withdraw .

More information about the vtkusers mailing list