#!/bin/sh
#\
exec ${GOOD_ROOT_DIR}/bin/vr_scene $0 $*

# set the guy in front of the house:
VR_position { -2 0 8 }

VR_msg {Welcome. This is a trivial scene containing a simple house. Try to open/close the door.}

set WINDOW {{1 1 1} {0.1 0.1 0.4} {0.2 0.2 0.2} {0 0 0} 1 1 1 ""}

Tcl_Primitive Door {} angle Door String {Creates a {ARG 1 Door}.} {
    $THIS -diffuse { .6 0.4 0} -ambient { .6 0.4 0} 
    Quader $THIS.1 1 2.31 0.05
    $THIS.1 -father $THIS -translate { 0.5 0 0} -pickmode 2
    Cylinder $THIS.2 0.05 0.1
    $THIS.2 -father $THIS -diffuse {1 1 1} -ambient {1 1 1} -translate { 0.85 0 0}

    set $THIS->angle 0
    $THIS -open [set $THIS->angle 0]
}

Tcl_Method Door -open newAngle Double {Open the door with a certain {ARG 1 Angle} in radiants. 0 means that the door is closed.} {
    $THIS -rotate "0 [set $THIS->angle] 0"
    set $THIS->angle $newAngle
    $THIS -rotate "0 -[set $THIS->angle] 0"
} 

Tcl_Method Door -close {} {} {Close the door.} {
    $THIS -open 0
} 

Tcl_Method Door -get_knob {} {} {Get the door's knob.} {
    return $THIS.2	
} 

Tcl_Primitive Table {} {} Name String {Creates a {ARG 1 Table}.} {
    $THIS -diffuse {0 0 1} -ambient { 0 0 1}
    Cylinder $THIS.c 0.5 0.05
    $THIS.c -father $THIS -translate { 0 0.925 0} -diffuse {0.2 0.2 0.8} \
	    -specular { 0.3 0.3 0.3} -ambient {0.2 0.2 0.8} -transmission 1 -refraction 1.33 -pickmode 2
    Cylinder $THIS.c1 0.02 0.7
    $THIS.c1 -father $THIS -translate { 0.2 0.55 0.2} -pickmode 2
    Cylinder $THIS.c2 0.02 0.7
    $THIS.c2 -father $THIS -translate { -0.2 0.55 0.2} -pickmode 2
    Cylinder $THIS.c3 0.02 0.7
    $THIS.c3 -father $THIS -translate { 0.2 0.55 -0.2} -pickmode 2
    Cylinder $THIS.c4 0.02 0.7
    $THIS.c4 -father $THIS -translate { -0.2 0.55 -0.2} -pickmode 2
    foreach c [$THIS -get_children] { $c -rotate { -1.57 0 0} }
}

Tcl_Primitive Sit {} {} Sit String {Creates a {ARG 1 Sit}.} {
    $THIS -diffuse {0 1 0} -ambient { 0 1 0}
    Cylinder $THIS.c 0.2 0.05
    $THIS.c -father $THIS -translate { 0 0.7 0}  -pickmode 2
    $THIS.c -rotate { -1.57 0 0}
    Quader $THIS.q 0.4 0.6 0.05
    $THIS.q -father $THIS -translate { 0 1 0.2}  -pickmode 2
    $THIS.q -rotate { 0.1 0 0}
    Cylinder $THIS.c2 0.02 0.7
    $THIS.c2 -father $THIS -translate { 0 0.35 0}  -pickmode 2
    $THIS.c2 -rotate { -1.57 0 0}
}

Tcl_Primitive House {} {} Name String {Creates a new {ARG 1 House}.} {
    global WINDOW
    
    # front:

    Quader $THIS.qf1 0.5 3 0.2
    $THIS.qf1 -translate { -2.25 1.7 2} -father $THIS -pickmode 2

    Door $THIS.qf21
    $THIS.qf21 -father $THIS -translate { -2 1.35 2.125} -pickmode 2	
    Quader $THIS.qf22 1 0.7 0.2
    $THIS.qf22 -translate { -1.5 2.85 2} -father $THIS  -pickmode 2
    Quader $THIS.qf23 1 0.2 0.5
    $THIS.qf23 -translate { -1.5 0.1 2.2} -father $THIS  -pickmode 2

    Quader $THIS.qf3 1 3 0.2
    $THIS.qf3 -translate { -0.5 1.7 2} -father $THIS -pickmode 2

    Quader $THIS.qf41 2 1.0 0.2
    $THIS.qf41 -translate { 1 0.7 2} -father $THIS -pickmode 2
    Quader $THIS.qf42 2 1.3 0.1
    $THIS.qf42 -translate { 1 1.85 2} -father $THIS -surface $WINDOW -pickmode 2
    Quader $THIS.qf43 2 0.7 0.2
    $THIS.qf43 -translate { 1, 2.85 2} -father $THIS  -pickmode 2

    Quader $THIS.qf5 0.5 3 0.2
    $THIS.qf5 -translate { 2.25 1.7 2} -father $THIS -pickmode 2

    # back front: 

    Quader $THIS.qh1 1 3 0.2
    $THIS.qh1 -translate { -2 1.7 -2} -father $THIS -pickmode 2

    Quader $THIS.qh21 1 1.0 0.2
    $THIS.qh21 -translate { -1 0.7 -2} -father $THIS -pickmode 2
    Quader $THIS.qh22 1 1.3 0.1
    $THIS.qh22 -translate { -1 1.85 -2} -father $THIS -surface $WINDOW -pickmode 2
    Quader $THIS.qh23 1 0.7 0.2
    $THIS.qh23 -translate { -1 2.85 -2} -father $THIS  -pickmode 2

    Quader $THIS.qh3 1 3 0.2
    $THIS.qh3 -translate { 0 1.7 -2} -father $THIS -pickmode 2

    Quader $THIS.qh41 1 1.0 0.2
    $THIS.qh41 -translate { 1 0.7 -2} -father $THIS -pickmode 2
    Quader $THIS.qh42 1 1.3 0.1
    $THIS.qh42 -translate { 1 1.85 -2} -father $THIS -surface $WINDOW -pickmode 2
    Quader $THIS.qh43 1 0.7 0.2
    $THIS.qh43 -translate { 1 2.85 -2} -father $THIS  -pickmode 2

    Quader $THIS.qh5 1 3 0.2
    $THIS.qh5 -translate { 2 1.7 -2} -father $THIS -pickmode 2

    # left side:

    Quader $THIS.ql1 0.2 3 3.8
    $THIS.ql1 -translate { -2.4 1.7 0} -father $THIS -pickmode 2

    # right side:

    Quader $THIS.qr1 0.2 3 3.8
    $THIS.qr1 -translate { 2.4 1.7 0} -father $THIS -pickmode 2

    # bottom:

    Quader $THIS.b1 4.8 0.2 4
    $THIS.b1 -translate { 0 0.1 0} -father $THIS -diffuse { 0.1 0.3 0.5} -ambient { 0.1 0.3 0.5} -pickmode 2
    Quader $THIS.b2 4.8 0.01 4
    $THIS.b2 -translate { 0 0.2 0} -father $THIS -diffuse { 1 0 0} -ambient {1 0 0} -pickmode 2

    # roof:
    Quader $THIS.r 5.4 0.3 4.4
    $THIS.r -translate { 0 3.3 0} -father $THIS -diffuse { 1 0 0} -ambient {1 0 0} -pickmode 2
    
    # table:
    Table $THIS.ta
    $THIS.ta -father $THIS -translate {1 0 0.2} -pickmode 2

    # sits:
    Sit $THIS.sit1
    $THIS.sit1 -father $THIS -translate {2 0 0.2} -pickmode 2
    $THIS.sit1 -rotate { 0 1.7 0}
    Sit $THIS.sit2
    $THIS.sit2 -father $THIS -translate {0.2 0 0.4} -pickmode 2
    $THIS.sit2 -rotate { 0 -1.5 0}
    Sit $THIS.sit3
    $THIS.sit3 -father $THIS -translate {1 0 -0.8} -pickmode 2
    $THIS.sit3 -rotate { 0 -3 0}
    Sit $THIS.sit4
    $THIS.sit4 -father $THIS -translate {1 0 1.2} -pickmode 2

    # lamp:
    Cone $THIS.co 0.5 0.2
    $THIS.co -father $THIS -translate { 0 3.2 0} -pickmode 2
    $THIS.co -rotate { 1.57 0 0} -open
}

Tcl_Method House -get_door {} {} {Returns the door of the house} {
    return $THIS.qf21
} 

House h
PointLight h.li
h.li -origin { 0 2.9 0} -color { 1 1 0}

Tcl_Primitive XLamp {} {} Name String {Creates a new {ARG 1 Lamp}.} {
    Cylinder $THIS.c 0.05 4
    $THIS.c -father $THIS -translate { 0 2 0} -pickmode 2
    $THIS.c -rotate {-1.57 0 0 }
    Cylinder $THIS.cc 0.05 1
    $THIS.cc -father $THIS -translate { 0 4 0.5} -pickmode 2
    Cone $THIS.co 0.4 0.3
    $THIS.co -father $THIS -translate { 0 4 1} -pickmode 2
    $THIS.co -rotate {1.57 0 0 } -open
}

XLamp l
l -translate {3 0 3}
PointLight l.li 
l.li -color { 0.8 0.8 0.8} -origin { 3 3.8 4} -off

AmbientLight al 
al -color { 0.2 0.2 0.2}

Quader ground 1000 0.01 1000
ground -translate { 0 -0.005 0} -diffuse { 0.2 1 0.2} -ambient {0.2 1 0.2}

sc -background  {0.1 0 0.4} -insert h h.li l l.li ground al

XLamp ax

set vr_DoorOpen 0

proc VR_pick {uid obj} {
    if {$obj == [[h -get_door] -get_knob]} {
	global vr_DoorOpen 
	if {$vr_DoorOpen} {
	    VR_evalAndBroadcast "[h -get_door] -close;l.li -off"
	    set vr_DoorOpen 0
	} {
	    VR_evalAndBroadcast "[h -get_door] -open 2;l.li -on"
	    set vr_DoorOpen 1
	}
    } 
}
