Updated 2012-01-16 23:57:57 by dkf
 What: 3dcanvas (DRH)
 Where: http://3dcanvas.tcl.tk/
 Description: This widget implements a high-level canvas type interface
        to OpenGL.
        The program describes a scene as a bunch of polygons and lines with
        various properties (color, transparency, etc) and the position of lights
        and the camera.  Simple commands allow the camera to moved to fly
        through the scene.
 License: BSD
 Status: Under active development
 Updated: 2005-08-17
 Contact: mailto:drh@hwaci.com

2006-10-18 Is a precompiled version of the dll for Windows available ?

GS (070907) Here is a precompiled DLL [1] cooked by ulis
 What: Qanim - tclogl Animation Demo 
 Where: [2]
 Description: This code demonstrates a 3d canvas built using Tcl code on top of tclogl.
 License: Free for non commercial purposes.
 Status: Proof of concept, other tclogl code under development.
 Updated: 2005-10-22
 Contact: Philip Quaife

 What: 3dcanvas (Peter Loborg)
 Where: http://www.ida.liu.se/~petlo/3D_canvas/ (dead link)
 Description: This widget implements 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 its 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, its 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
 Status: No longer maintained
 Updated: 12/1996 (but v1.0 released 12/2003)
 Contact: mailto:plo@ida.liu.se

The 3dcanvas (Peter Loborg version) shared library for Tcl8.4.x is available under:

  • Linux [3]
  • Windows [4] (compiled by EB)

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

.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 c:/tcl/lib/dddcanvas/dddcanvas.dll

 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 {
           # --- vertex
           incr nv
           set tcoords($nv) [lrange $line 1 end]
         } elseif {[lindex $line 0] == "f"} then {
           # --- face
           #     JAG,04-Mar-2005
           #     Updated to handle face record containing "vt" and "vn" flags
           #     So, "f 1 2 3" or "f 1/1/1 2/2/2 3/3/4 4/4/3" should now work...
           
           #     next line commented out as it does not handle vt and vn flags
           #set tconnect($nf) [lrange $line 1 end]
           incr nf
           foreach item [lrange $line 1 end] {
                lappend tconnect($nf) [lindex [split $item "/"] 0]
           }
         }
  }

 # 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

GS (040618) Another 3dcanvas demo: the Sierpinski tetrahedron [6]

FW: Does this use any hardware rendering? It almost seems like it.

GS: No. It is based on the same software rendering system as the classical 2D canvas. Apparently, 3dcanvas can be used wherever Tk is used. It just needs to be compiled for the target platform.

FW: It's mentioned on the site that the lack of a lumination model will soon be fixed - that couldn't really be done with Tk's current canvas. Maybe by pixel-by-pixel drawing I suppose. Oh, oh, maybe they mean entire surfaces will change color - that makes sense.

GS: By lumination, I suppose it's the color intensity of a 3D object according the light source direction. The author has said to me that 3dcanvas was a student project. He is currently chaising the results of the follow-up project of 1997 where this widget was rewritten and extended with a lightning model and other nice things. I hope he will find this enhanced version after several years.

JAG, 04-Mar-2005 - Updated OBJ file reader section to handle a "face" record that may contain both "vt" (texture vertex) and "vn" (normal vertex) fields. Now, in addition to a record that looks like "f 1 2 3", the reader will also handle a record that looks like this "f 1/1/1 2/2/2 3/3/3". Some of the sample models provided in the links on this page seem to act very "strangely" when animated in the above program. I didn't dig into it, but I assume this is a bug in the 3d canvas? Anyone else see odd (broken, stretched, etc) geometry?

SZ I managed to compile 3dcanvas with stubs (for Windows this time and not without some manual work). The page [7] allows you to download stub-enabled dll and describes neccessary modifications to source code. I hope this will help someone, as it helped me.

AM (5 september 2007) I downloaded the DLL "dddcanvas.dll" mentioned above, but there was something odd about it: it did not accept the -color option. (And it did not display anything useful, but I had no time to sort out the viewpoint etc., just tried if/how it would work). Is this DLL being maintained?

GS (070905) This version of 3dcanvas was no longer maintained since .... 1996 ! It was a student project. You should try the DRH 3dcanvas (http://3dcanvas.tcl.tk/). It is hardware-dependent (OpenGL) and more advanced. If you really need the Peter Loborg's version of 3dcanvas, I can send you the unique package that I have (source code + man page).

AM (6 september 2007) I was assuming it was DRH's 3dcanvas, so I was slightly surprised to find things not working according to the man page. Well, that serves me right for not reading too thoroughly :). This said, I will have a go at that package (I was just too lazy to try and create a Windows version ...).

GS (070907) See at the top of the page. Another tcler has done it.

SDW - I'm using a version of 3dcanvas for a class I'm teaching. One cheap performance boost I found was to tweak the makefile so that gcc optimized the code better. Bascially change all of the -Os to -O3. Does nice things like unwrap your matrix multiplication loops, re-order things to execute faster, etc. I've watched by framerate almost double.