# planet.tcl
#
# An example of the OpenGL red book modified to work with Tcl3D.
# The original C sources are Copyright (c) 1993-2003, Silicon Graphics, Inc.
# The Tcl3D sources are Copyright (c) 2005-2026, Paul Obermeier.
# See file LICENSE for complete license information.
#
# This program shows how to composite modeling transformations
# to draw translated and rotated models.
# Interaction:  pressing the d and y keys (day and year)
# alters the rotation of the planet around the sun.

package require tcl3d

set year 0
set day  0

# Font to be used in the Tk listbox.
set listFont {-family {Courier} -size 10}

# Show errors occuring in the Togl callbacks.
proc bgerror { msg } {
    tk_messageBox -icon error -type ok -message "Error: $msg\n\n$::errorInfo"
    exit
}

# Print info message into widget a the bottom of the window.
proc PrintInfo { msg } {
    if { [winfo exists .fr.info] } {
        .fr.info configure -text $msg
    }
}

proc CreateCallback { toglwin } {
    glClearColor 0.0 0.0 0.0 0.0
    glShadeModel GL_FLAT
}

proc DisplayCallback { toglwin } {
    glClear GL_COLOR_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 [$toglwin width] [$toglwin height]

    glColor3f 1.0 1.0 1.0

    glPushMatrix
    glutWireSphere 1.0 20 16   ; # draw sun
    glRotatef  $::year 0.0 1.0 0.0
    glTranslatef 2.0 0.0 0.0
    glRotatef  $::day 0.0 1.0 0.0
    glutWireSphere 0.2 10 8    ; # draw smaller planet
    glPopMatrix
    $toglwin swapbuffers 
}

proc ReshapeCallback { toglwin { w -1 } { h -1 } } {
    set w [$toglwin width]
    set h [$toglwin height]

    glViewport 0 0 $w $h 
    glMatrixMode GL_PROJECTION
    glLoadIdentity
    gluPerspective 60.0 [expr double($w)/double($h)] 1.0 20.0
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    gluLookAt 0.0 0.0 5.0 0.0 0.0 0.0 0.0 1.0 0.0
}

proc IncrYear { val } {
    set ::year [expr ($::year + $val) % 360]
    .fr.toglwin postredisplay
}

proc IncrDay { val } {
    set ::day [expr ($::day + $val) % 360]
    .fr.toglwin postredisplay
}

frame .fr
pack .fr -expand 1 -fill both
togl .fr.toglwin -width 500 -height 500 -double true \
                 -createcommand CreateCallback \
                 -reshapecommand ReshapeCallback \
                 -displaycommand DisplayCallback 
listbox .fr.usage -font $::listFont -height 5
label   .fr.info
grid .fr.toglwin -row 0 -column 0 -sticky news
grid .fr.usage   -row 1 -column 0 -sticky news
grid .fr.info    -row 2 -column 0 -sticky news
grid rowconfigure .fr 0 -weight 1
grid columnconfigure .fr 0 -weight 1
wm title . "Tcl3D demo: OpenGL Red Book example planet"

bind . <Key-y> "IncrYear  5"
bind . <Key-Y> "IncrYear -5"
bind . <Key-d> "IncrDay  10"
bind . <Key-D> "IncrDay -10"
bind . <Key-Escape> "exit"

.fr.usage insert end "Key-y      Increase year"
.fr.usage insert end "Key-Y      Decrease year"
.fr.usage insert end "Key-d      Increase day"
.fr.usage insert end "Key-D      Decrease day"
.fr.usage insert end "Key-Escape Exit"
.fr.usage configure -state disabled

PrintInfo [tcl3dOglGetInfoString]
