bwise 3D graphics viewer block

TV Jan 7 ' 09

A bit challenging, not yet as widely usable, and of course fun, like having some of the old AVS package I knew in FOS, and with more possibilities: a 3d viewer in Bwise!

It is NOT YET FINISHED, but this I have working thus far:

Image Bwise bw2dviewer.gif

 package require tcl3d
        # Font to be used in the Tk listbox.
        set listFont {-family {Courier} -size 10}
        
        # Rotation of the box
        set g_RotAngle 0.0
        
        # Offset of the Togl window.
        set g_OffWidth 0
        set g_OffHeight 0
      proc bgerror { msg } {
            tk_messageBox -icon error -type ok -message "Error: $msg\n\n$::errorInfo"
            exit
        }
      proc tclCreateFunc { toglwin } {
            glClearColor 0.5 0.5 0.5 0.5
            glShadeModel GL_SMOOTH
            glClearDepth 1.0
            glEnable GL_DEPTH_TEST
        }
      proc tclDisplayFunc { toglwin } {
            glClear [expr $::GL_COLOR_BUFFER_BIT | $::GL_DEPTH_BUFFER_BIT] 
        
            # Viewport command is not really needed, but has been inserted for
            # Mac OSX. Presentation framework (Tk) does not send a reshape event
            # when switching from one demo to another.
            glViewport 0 0 $::g_WinWidth $::g_WinHeight
        
            glLoadIdentity
            glTranslatef 0.0 0.0 -7.0
            glRotatef $::g_RotAngle 1.0 1.0 1.0
        
            glBegin GL_QUADS
                glColor3f 0.0 1.0 0.0
                glVertex3f  1.0  1.0 -1.0
                glVertex3f -1.0  1.0 -1.0
                glVertex3f -1.0  1.0  1.0
                glVertex3f  1.0  1.0  1.0
                glColor3f 1.0 0.5 0.0  
                glVertex3f  1.0 -1.0  1.0
                glVertex3f -1.0 -1.0  1.0
                glVertex3f -1.0 -1.0 -1.0
                glVertex3f  1.0 -1.0 -1.0
                glColor3f 1.0 0.0 0.0   
                glVertex3f  1.0  1.0  1.0
                glVertex3f -1.0  1.0  1.0
                glVertex3f -1.0 -1.0  1.0
                glVertex3f  1.0 -1.0  1.0
                glColor3f 1.0 1.0 0.0   
                glVertex3f  1.0 -1.0 -1.0
                glVertex3f -1.0 -1.0 -1.0
                glVertex3f -1.0  1.0 -1.0
                glVertex3f  1.0  1.0 -1.0
                glColor3f 0.0 0.0 1.0    
                glVertex3f -1.0  1.0  1.0
                glVertex3f -1.0  1.0 -1.0
                glVertex3f -1.0 -1.0 -1.0
                glVertex3f -1.0 -1.0  1.0
                glColor3f 1.0 0.0 1.0    
                glVertex3f  1.0  1.0 -1.0
                glVertex3f  1.0  1.0  1.0
                glVertex3f  1.0 -1.0  1.0
                glVertex3f  1.0 -1.0 -1.0
            glEnd
            set ::g_RotAngle [expr $::g_RotAngle -0.5]
            
            $toglwin swapbuffers
        }
        
        proc tclReshapeFunc { toglwin  w h } {
            if { $h == 0 } {
                set h 1 
            }
            glViewport 0 0 $w $h
            glMatrixMode GL_PROJECTION
            glLoadIdentity
        
            gluPerspective 45.0 [expr double($w)/double($h)] 0.1 100.0
        
            glMatrixMode GL_MODELVIEW
            glLoadIdentity
        
            set ::g_WinWidth  $w
            set ::g_WinHeight $h
        }
        
        proc ReshapeCanvas { c } {
            set w [winfo width $c]
            set h [winfo height $c]
            set cw [expr $w / 2]
            set ch [expr $h / 2]
            $c coords TOGLWIN [expr $cw + $::g_OffWidth] [expr $ch + $::g_OffHeight]
            $c itemconfigure TOGLWIN -width $w -height $h
            $c coords SE $w $h
            $c coords SW 0  $h 
            $c coords NE $w 0
            $c coords NW 0  0
            $c coords TEXT1 $cw 20
            $c coords TEXT2 $cw [expr $h -20]
        }
 set viewerindex 1

 proc create3dviewer { {name {viewer}} } {
    global mc viewerindex
    
    if {$name == "viewer"} {set name $name$viewerindex;
       set viewerindex [expr $viewerindex+1]
    }
    global $name.bfunc_init $name.bfunc $name.in
    newblock $name 0 0 180 180 {in trigger}
    frame $mc.$name
    eval $mc create window 10 10 -anchor nw -height 160 -width 160  -tags "{$name crb viewerwindow}" -window $mc.$name
 
    canvas $mc.$name.c -height 10 -width 10 -bg gray10
    pack $mc.$name.c -expand y -fill both

    set $name.bfunc_init ""
    uplevel #0  "eval \${$name.bfunc_init}"

 #    eval scrollbar  $mc.$name.s -orient horiz -command "{$mc.$name.c xview}"
 #    eval $mc.$name.c conf -xscrollcommand "{$mc.$name.s set}" -scrollregion  "{0 0 1000 100}"
 #    pack $mc.$name.s -side bottom -expand 0 -fill x
    set $name.in {}
    set $name.bfunc ""
 } 
 
 create3dviewer
 create3dviewer
 create3dviewer
 
 togl $mc.viewer3.c.toglwin -width 160 -height 160 -swapinterval 1 -double true -depth true -createproc tclCreateFunc -displayproc tclDisplayFunc -reshapeproc tclReshapeFunc
 $mc.viewer3.c  create window 80 80 -window $mc.viewer3.c.toglwin -anchor center -tags "TOGLWIN"
 bind $mc.viewer3.c <Configure> "ReshapeCanvas $mc.viewer3.c"

 set g_RotAngle 90
 $mc.viewer3.c.toglwin postredisplay

I need a nice way to deal with the callbacks and the bookkeeping of the interactors, and of course the pins must be used for the right purpose: currently they do nothing.

Also, I'd like the objectviewer approach which can load in some scene and then rotate it with the mouse, that's more cool.

The above is possible ONLY when you have tcl3d (and Img available, and such that they can be 'package require'-d in. I experiment with this at the moment on Fedora 10, where I compiled/installed those packages without too many troubles, although I'd not say it is laymanswork to do so. I only tried Ogl examples till now, which work but not really nice, and it's a shame the callbacks or something else makes the response of the applications often jerky.

I did use the cuda prepared tcl executable (see Tcl on Cuda) so in principle I could link Cuda with for instance an openGL texture.