Version 5 of 3dcanvas

Updated 2004-01-05 17:31:53

What: 3dcanvas

 Where: http://www.ida.liu.se/~petlo/3D_canvas/
 Description: This widgets implement a three dimensional space in which structured 
 graphics can be rendered. A 3dcanvas displays any number of items, which may
 be things like spheres, polygons, lines and prisms. Items may be manipulated 
 (e.g. moved or re-colored) and commands may be associated with items in much 
 the same way that the bind command allows commands to be bound to widgets.
 The 3dcanvas also contains a powerful grouping mechanism for manipulating 
 several spatially related objects as one.
 The objects in a 3dcanvas are stored in a hierarchical database together with 
 associated relations and attributes. This hierarchy is constructed using a special 
 item type called the group item, which is capable of containing other items 
 including group items. 
 In the 3dcanvas, all items have their own coordinate system, which is relative to 
 that of it’s parent group (this is the group in which the item is currently placed). 
 All transformations such as scaling or rotating is performed on this local coordinate 
 system. This means that if a group is rotated, it’s coordinate system will be warped, 
 and so will that of every contained item as their coordinate systems are specified 
 in terms of the groups coordinate system.
 3dcanvas is an old project (Tk4.2) introduced by Peter Loborg. The small graphics 
 kernel doesn't depend on a third party library.
 License: LGPL
 Updated: 12/1996 (but v1.0 released 12/2003)
 Contact: mailto:[email protected]

GS (040105) With a few modification in the Makefile, I have compiled 3dcanvas for Tk8.4.x on Linux. This shared library (libdddcanvas.so) is downloadable at: http://gersoo.free.fr/wiki/w10519/index.html .

If someone is able to compile a DLL for windows this could be nice.

Here is a simple demo of 3dcanvas capabilities. It implements an .obj (Alias-Wavefront graphics file format [L1 ]) reader and makes a display in wireframe mode:

http://gersoo.free.fr/wiki/w10519/objviz.jpg http://gersoo.free.fr/wiki/w10519/x29-al.jpg

.obj file samples and source code below are also available at http://gersoo.free.fr/wiki/w10519/index.html .

 # objviz-3dc.tcl 
 # Author: Gerard Sookahet
 # Date: 2004-01-04
 # Description: Simple .obj (Alias-Wavefront graphics file format) viewer

 load ./libdddcanvas.so

 bind all <Escape> { exit }

 # Parse .obj file format
 proc ObjReader { f } {
     global G

  if {$f == ""} then return

  .c delete all
  set nv 0
  set nf 0
  array set tcoords  {}
  array set tconnect {}

  set fp [open $f r]
     set data [read $fp [file size $f]]
  close $fp

  set data [split $data "\n"]
  foreach line $data {
         if {[lindex $line 0] == "v"} then {
           incr nv
           set tcoords($nv) [lrange $line 1 end]
         } elseif {[lindex $line 0] == "f"} then {
           incr nf
           set tconnect($nf) [lrange $line 1 end]
         }
  }

 # Create group items and display lines
  set G [.c create group]

  for {set i 1} {$i <= $nf} {incr i} {
     set lcoords {}
     foreach j $tconnect($i) { lappend lcoords $tcoords($j) }
     .c addgroup $G items [eval .c create line [join [concat $lcoords] " "] -fill blue]
  }

 }

 # Simple rotation animation for phi and theta angle
 proc Animate {} {
     global G

  .c phirot $G 2
  .c thetarot $G 2
  after 50 Animate
 }

 proc BrowseFile { dir filter } {

  switch $filter {
        obj  { set file_types {
                    { {Obj files} {.obj} }
                    { {all files}  * }
             }
        }
  }

  set file [tk_getOpenFile -filetypes $file_types \
                           -initialdir $dir \
                           -title "$filter File"]
  if {$file != ""} { return $file } else { return ""}
 }

 proc Main {} {
     global scx scy scz
     global somega sphi stheta
     global vdist
     global G

  set scx 0
  set scy 0
  set scz -20
  set vdist 400

  wm title . "Objviz with 3dcanvas"
  3dcanvas .c -bg black -width 700 -height 500
  pack .c -side top

  set f0 [frame .f0]
  pack $f0 

  set f1 [frame $f0.f1]
  scale $f1.scx -from 200 -to -200 -length 400 -label "X translation" -orient horiz \
                -showvalue true -variable scx -command {.c configure -ox}
  scale $f1.scy -from 200 -to -200 -length 400 -label "Y translation" -orient horiz \
                -showvalue true -variable scy -command {.c configure -oy}
  scale $f1.scz -from 1000 -to -1000 -length 400 -label "Z translation" -orient horiz \
                -showvalue true -variable scz -command {.c configure -oz}
  scale $f1.vd -from 3000 -to -1000 -length 400 -label "View distance" -orient horiz \
                -showvalue true -variable vdist -command {.c configure -viewdistance}
  eval pack [winfo children $f1]

  set f2 [frame $f0.f2]
  scale $f2.scomega -from 180 -to -180 -length 180 -label "Omega angle" -orient horiz \
                -showvalue true -variable somega -command {.c configure -omegaangle}
  scale $f2.sphi -from 180 -to -180 -length 180 -label "Phi angle" -orient horiz \
                -showvalue true -variable sphi -command {.c configure -phiangle}
  scale $f2.stheta -from 180 -to -180 -length 180 -label "Theta angle" -orient horiz \
                -showvalue true -variable stheta -command {.c configure -thetaangle}
  eval pack [winfo children $f2] 

  set f3 [frame $f0.f3]
  button $f3.bloadobj -text "Load obj" -width 9 -command {ObjReader [BrowseFile [pwd] obj]}
  button $f3.bromega -text "Omega rotate" -width 9 -command {.c omegarot $G 8}
  button $f3.brphi -text "Phi rotate" -width 9 -command {.c phirot $G 8}
  button $f3.brtheta -text "Theta rotate" -width 9 -command {.c thetarot $G 8}
  button $f3.banim -text Animate -width 9 -command {Animate}
  button $f3.babout -text About -width 9 -bg grey -command {About}
  button $f3.bquit -text Quit -width 9 -bg grey -command exit
  eval pack [winfo children $f3]

  pack $f1 $f2 $f3 -side left
 }

 proc About {} {
  set w .about
  catch {destroy $w} ; toplevel $w
  wm title $w "About this demo"
  message $w.msg -justify center -aspect 250 -relief sunken \
                 -text "3dcanvas demo: a simple .obj viewer\n\nGerard Sookahet\n\
                        2004-01-04"
  button $w.bquit -text Quit -command {destroy .about}
  eval pack [winfo children $w]
 }

 Main

[Category Graphics] | [Category Widget]