TCL/TK Tutorial
Part 5

TCL/TK Part 5: Using primitive geometric shapes and texturing

You can download the sphere.tcl
or just copy this code into a text doc with a tcl extention.

classvtkSphereSource reference link.

########################################################################
# Sample code for sphere
#

package require vtk
package require vtkinteraction

#--------------------------------------------------------
# create sphere code
vtkSphereSource sphere
sphere SetRadius 1.0
sphere SetThetaResolution 18
sphere SetPhiResolution 18

# map to graphics library
vtkPolyDataMapper sphereMap;
sphereMap SetInput [sphere GetOutput]

# actor coordinates geometry, properties, transformation
vtkActor sphereActor
sphereActor SetPosition 0.0 0.0 0.0
sphereActor SetMapper sphereMap
[sphereActor GetProperty] SetColor 0 0 1; # red

#-------------------------------------------------------

# create a window to render into
vtkRenderWindow renWin
vtkRenderer ren1
renWin AddRenderer ren1

# create an interactor
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin

# add the sphere
ren1 AddActor sphereActor
ren1 SetBackground 1 1 1;# Background color white

# Render an image; since no lights/cameras specified, created automatically
renWin Render

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

After you have copied code check that it works

Now let us add a cylinder.
Add this code under the sphere code

classvtkCylinderSource reference link.

#-------------------------------------------------------
# Set up cylinder
vtkCylinderSource cylinder
cylinder SetHeight 1.6
cylinder SetRadius 0.2
cylinder SetCenter 0 0.8 0

vtkTransform t1

vtkTransformPolyDataFilter f1
f1 SetInput [cylinder GetOutput]
f1 SetTransform t1

vtkDataSetMapper cylinderMap
cylinderMap SetInput [f1 GetOutput]

vtkActor cylinderActor
cylinderActor SetPosition 1.0 1.0 1.0
cylinderActor SetMapper cylinderMap
[cylinderActor GetProperty] SetColor 1 0 0; # Color Red

#-------------------------------------------------------

Now near the bottom of the original code you should see a
ren1 AddActor sphereActor

below it add
ren1 AddActor cylinderActor

Now test it to see if it works if iot dont work here is file to show you what it looks like sphereCylinder.tcl

Now let's add a cone.
Add this code below the cylinder code

classvtkConeSource reference link.

#-------------------------------------------------------
#Cones

vtkConeSource cone
cone SetRadius 0.125
cone SetHeight 1.5
cone SetResolution 30

vtkPolyDataMapper coneMapper

coneMapper SetInput [cone GetOutput]

vtkActor coneActor
coneActor SetMapper coneMapper
coneActor SetPosition -0.5 1.3 0
coneActor RotateZ 115.5
[coneActor GetProperty] SetColor 0 1 0; # Green

#-------------------------------------------------------

Now add
ren1 AddActor coneActor
below ren1 AddActor cylinderActor

Check to see that it works if it does not work here is the file that it should look like sphereCylinderCone.tcl

Now lets add a cube
Add this code below the cone code

classvtkCubeSource reference link.

#-------------------------------------------------------
vtkCubeSource cube
cube SetXLength 1
cube SetYLength 1
cube SetZLength 1

vtkPolyDataMapper cubeMapper
cubeMapper SetInput [cube GetOutput]
vtkActor cubeActor
cubeActor SetMapper cubeMapper
cubeActor SetPosition -1 1 1
[cubeActor GetProperty] SetColor 0 .25 .25
#-------------------------------------------------------

Now add
ren1 AddActor cubeActor
below ren1 AddActor coneActor

Check to see that it works if it does not work here is the file that it should look like sphereCylinderConeCube.tcl

Now let's texure a sphere
in this code you use the vtkTexturedSphereSource object instead of the vtkSphereSource object
Here is the code or download sphereTexture.tcl

classvtkTextureMapToSphere reference link.

###############################################################
package require vtk
package require vtkinteraction

# Create the RenderWindow, Renderer and both Actors
#
vtkRenderer ren1
vtkRenderWindow renWin

renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin

vtkTexturedSphereSource tss
tss SetThetaResolution 18
tss SetPhiResolution 9
vtkPolyDataMapper earthMapper
earthMapper SetInput [tss GetOutput]
vtkActor earthActor
earthActor SetMapper earthMapper

# load 2 differenttexture map
#
vtkBMPReader bmpReader
bmpReader SetFileName "$VTK_DATA_ROOT/Data/masonry.bmp"

vtkPNMReader pnmReader
pnmReader SetFileName "$VTK_DATA_ROOT/Data/earth.ppm"

vtkTexture atext
atext SetInput [bmpReader GetOutput]
#atext SetInput [pnmReader GetOutput]
atext InterpolateOn


earthActor SetTexture atext

# Add the actors to the renderer, set the background and size
#
ren1 AddActor earthActor
ren1 SetBackground 0 0 0.1
renWin SetSize 300 300

# render the image
#
iren AddObserver UserEvent {wm deiconify .vtkInteract}
set cam1 [ren1 GetActiveCamera]
$cam1 Zoom 1.4
iren Initialize

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

###############################################################

Now check to see if code works
In code I have imported 2 textures a vtkBMPReader and vtkPNMReader respectfully bmpReader and pnmReader.
I have commented out the pnmReader I want you to uncomment it and comment out the bmpReader
now check that it works.

Learn about VTK Class Hierarchy: http://www.vtk.org/doc/release/4.0/html/hierarchy.html
The site also has more explanation of all the objects and how to use them.


Homework
1) Create 3 diffrent pictures in each picture change position, color, and resolution on the cube, sphere, cone, and cylinder.
2) Add a texture to the cylinder. Hint goto
http://www.vtk.org/doc/release/4.0/html/classvtkTextureMapToCylinder.html for more info

 

Home
C++ Tutorial
Python Tutorial
TCL/TK Tutorial

Downloads
Homework
About Us
Contact
Links

TCL/TK Tutorial
Part 1: Installation
Part 2: Molecules
Part 3: Terrain
Part 4: Interactive
Part 5: Shapes