What: 3dcanvas Where: http://www.ida.liu.se/~petlo/3D_canvas/ 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 Updated: 12/1996 (but v1.0 released 12/2003) Contact: mailto:plo@ida.liu.se ---- The 3dcanvas shared library for Tcl8.4.x is available under: - Linux [http://gersoo.free.fr/inform/tcl/3dcanvas/dddcanvas10.so] - Windows [http://gersoo.free.fr/inform/tcl/3dcanvas/dddcanvas10.dll] (compiled by [EB]) Here is a simple demo of 3dcanvas capabilities. It implements an .obj (Alias-Wavefront graphics file format [http://astronomy.swin.edu.au/~pbourke/geomformats/obj/]) 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 c:/tcl/lib/dddcanvas/dddcanvas.dll bind all { 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 [http://mini.net/tcl/11832] ---- [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? ---- [Category Package] | [Category Graphics] | [Category 3D Graphics] | [Category Widget]