#--------------------------------------------------------------------
#   observerlib.tcl
#   ===============
#   Support-Funktionen fuer Gfx-Server-Ersatz.
#
#   tod3d6
#   tod3d7hal
#   tod3d7emu
#   togl
#   orig
#   map_input
#   init_observer
#
#   26-Nov-99   floh    Input-Mappings mit .beginmap/.endmap
#   15-Feb-00   floh    + win2k aware (needs get_platform from nsh.tcl)
#
#   (C) 1999 A.Weissflog
#--------------------------------------------------------------------

#--------------------------------------------------------------------
#   init_gfxserver
#   Stellt die gewuenschten Gfx-Server-Parameter ein. Wird z.B.
#   beim Wechsel des Gfxserv per tod3d oder togl aufgerufen.
#   26-Jul-99   floh    created
#   28-Jul-99   bernd   
#--------------------------------------------------------------------
proc init_gfxserver { } {
    /sys/servers/gfx.setviewvolume -0.1 +0.1 -0.075 +0.075 +0.1 +2500
    /sys/servers/gfx.setclearcolor 0 0 0 0
    /sys/servers/gfx.opendisplay
}

#--------------------------------------------------------------------
#   tod3d6
#   Schaltet nach Direct3D V6
#   26-Jul-99   floh    created
#--------------------------------------------------------------------
proc tod3d6 { } {
    if {[exists /sys/servers/gfx]} { delete /sys/servers/gfx }
    new nd3dserver /sys/servers/gfx
    /sys/servers/gfx.setdisplaymode dev(0)-w(1024)-h(768)
    init_gfxserver
}

#--------------------------------------------------------------------
#   tod3d7hal
#   Schaltet nach Direct3D V7 HAL Device
#   13-Jan-00   floh    created   
#--------------------------------------------------------------------
proc tod3d7hal { } {
    if {[exists /sys/servers/gfx]} { delete /sys/servers/gfx }
    new nd3d7server /sys/servers/gfx
    /sys/servers/gfx.setdisplaymode dev(0)-w(1024)-h(768)
    init_gfxserver
}

#--------------------------------------------------------------------
#   tod3d7emu
#   Schaltet nach Direct3D V7 RGB Emulation
#   13-Jan-00   floh    created
#--------------------------------------------------------------------
proc tod3d7emu { } {
    if {[exists /sys/servers/gfx]} { delete /sys/servers/gfx }
    new nd3d7server /sys/servers/gfx
    /sys/servers/gfx.setdisplaymode dev(20)-w(1024)-h(768)
    init_gfxserver
}

#--------------------------------------------------------------------
#   togl
#   Schaltet nach OpenGL
#   26-Jul-99   floh    created
#--------------------------------------------------------------------
proc togl { } {
    if {[exists /sys/servers/gfx]} { delete /sys/servers/gfx }
    new nglserver /sys/servers/gfx/
    /sys/servers/gfx.setdisplaymode dev(0)-w(1024)-h(768)
    init_gfxserver
}

#--------------------------------------------------------------------
#   orig
#   Stellt Kamera und Lookat-Node auf Ausgangs-Position ein
#   26-Jul-99   floh    created
#   28-Jul-99   bernd   Standard-Settings von Rechts Oben
#   10-Nov-99   bernd   Camera Positioning currently is somehow strange /
#                       may not work as expected.
#                       Camera Animation will not work.
#                       Will be fixed later.
#--------------------------------------------------------------------
proc orig { } {
    /usr/lookat.txyz   0    0    0
    /usr/lookat.rxyz  -25   45   0
    /usr/camera.tz     5 
}

#--------------------------------------------------------------------
#   map_input
#   Input-Mapping fuer nobserver Klasse.
#   26-Jul-99   floh    created
#   26-Nov-99   floh    .beginmap/.endmap
#   20-Dec-99   floh    + Konsole an/aus
#   22-Dec-99   floh    + an neues Input-Mapping angepasst   
#--------------------------------------------------------------------
proc map_input { } {
    set cwd [psel]
        sel /sys/servers/input
        .beginmap
        .map keyb0:shift.pressed    pan
        .map keyb0:ctrl.pressed     orbit
        .map mouse0:btn0.pressed    pan
        .map mouse0:btn1.pressed    orbit
        .map mouse0:btn2.pressed    dolly
        .map keyb0:space.down       "script:orig"
        .map keyb0:esc.down         "script:/sys/servers/console.toggle"
        .endmap
    sel $cwd
}

#--------------------------------------------------------------------
#   init_observer
#   26-Jul-99   floh    created
#   27-Jul-99   bernd   Plattformabhängige Server
#   12-Aug-99   bernd   Zwar wird immer noch die Unterscheidung
#                       zwischen den Plattformen vorgenommen, aber
#                       eigentlich verwenden alle die selben Server.
#   18-Aug-99   bernd   Wenn 3ddevice da ist, dann so initialisieren
#                       wie es dort steht.
#   15-Feb-00   floh    + Win2k aware (needs nsh.tcl however)
#   01-Apr-00   floh    + added particle server
#--------------------------------------------------------------------
proc init_observer { } {
global 3ddevice

    set platform [get_platform]
    switch [get_platform] {
        win9x -
        win2k {
            puts "Using Win9x/Win2k settings."
            if  [ catch { set test $3ddevice } err ] {
                set 3ddevice " OpenGL "
            }    
            if { $3ddevice==" OpenGL "   }        { togl }
            if { $3ddevice==" Direct3D V6 " }     { tod3d6 }
            if { $3ddevice==" Direct3D V7 HAL " } { tod3d7hal }
            if { $3ddevice==" Direct3D V7 EMU " } { tod3d7emu }
	}

        winnt {
            puts "Using WinNT settings."
            togl
        }

        unix {
            puts "Using Unix settings."
            togl
        }

        default {
            puts "Unknown platform."
            exit
        }
    }

    map_input

    new n3dnode /usr/scene
    new n3dnode /usr/camera
    new n3dnode /usr/lookat

    orig
    new nobserver /observer
}

#--------------------------------------------------------------------
#   EOF
#--------------------------------------------------------------------
