#!/usr/bin/perl

print "#VRML V2.0 utf8\n";

$flnm = $ARGV[0];

open(PLATFORM,$flnm);
#Read title
$line = <PLATFORM>;
print "#$line";

#Blank line
$line = <PLATFORM>;

$line = <PLATFORM>;
@pcs = split(' ',$line);
$Radius = $pcs[0];
$Height = $pcs[1];
$StairWidth = $pcs[2];
$Intervals = $pcs[3];
$HeightAboveGround = $pcs[4];

$StepDeg = 90/$Intervals;
$Step = $StepDeg*3.1415/180.0;

$ERadius = $Radius - $StairWidth;
$d = 0;
$xc = 0.0;
$zc = 0.0;
for $i (0 .. $Intervals) {
    $x = $ERadius * cos($d);
    $z = $ERadius * sin($d);
    $xc += $x;
    $zc += $z;
    $Point[$i][0] = $x;
    $Point[$i][1] = $z;
    $CPoint[$i][0] = $x/100.0;
    $CPoint[$i][1] = $z/100.0;
    $d += $Step;
}
$xc /= (2*$Intervals);
$yc = $Height;
$zc /= (2*$Intervals);


print "DEF Q1 Transform {\n";
print "children[\n";
print " Transform {\n";
print "  translation $StairWidth $HeightAboveGround $StairWidth\n";
print "  children LOD {\n";
print "   center $xc $yc $zc\n";
print "   range [15 30]\n";
print "   level [\n";
print "    Shape {\n";
print "      appearance BRICK2560 { }\n";
print "      geometry DEF PLATFORM IndexedFaceSet {\n";
print "        solid FALSE\n";
print "        coord Coordinate {\n";
print "          point [\n";
for $i (0 .. $Intervals) {
    print "                 $CPoint[$i][0] $Height $CPoint[$i][1],\n";
}
for $i (0 .. $Intervals) {
    print "                 $Point[$i][0] $Height $Point[$i][1],\n";
}
for $i (0 .. $Intervals) {
    print "                 $Point[$i][0] 0.0 $Point[$i][1],\n";
}
print "                ]\n";
print "        }\n";
print "        coordIndex [";
for $i (0 .. $Intervals-1) {
    $i0 = $Intervals+1+$i; $i1 = $i0+1; $i2 = $i+1; $i3 = $i;
    print "                   $i0, $i1, $i2, $i3, -1,\n";
}
for $i ($Intervals+1 .. 2*$Intervals) {
    $i0 = $Intervals+1+$i; $i1 = $i0+1; $i2 = $i+1; $i3 = $i;
    print "                   $i0, $i1, $i2, $i3, -1,\n";
}
$i0 = 0; $i1 = $Intervals+1; $i2 = 2*$Intervals+2;
print "                   $i0, $i1, $i2, -1,\n";
$i0 = $Intervals; $i1 = 2*$Intervals+1; $i2 = 3*$Intervals+2;
print "                   $i0, $i1, $i2, -1,\n";
print "                   ]\n";
print "        texCoord TextureCoordinate {\n";
print "          point [\n";
for $i (0 .. $Intervals) {
    $S = $i;
    $T = 2.0;
    print "                 $S $T\n";
}
for $i (0 .. $Intervals) {
    $S = $i;
    $T = 0.5;
    print "                 $S $T\n";
}
for $i (0 .. $Intervals) {
    $S = $i;
    $T = 0.0;
    print "                 $S $T\n";
}
print "                ]\n";
print "        }\n";
print "      }\n";
print "    }\n";
print "    Shape {\n";
print "      appearance BRICK1280 { }\n";
print "      geometry USE PLATFORM\n";
print "    }\n";
print "    Shape {\n";
print "      appearance BRICK320 { }\n";
print "      geometry USE PLATFORM\n";
print "    }\n";
print "  ]\n";
print " }\n";
print "}\n";
print "]}\n\n";

print "Transform {\n";
print "  rotation 0.0 1.0 0.0 1.571\n";
print "  children [\n";
print "    USE Q1\n";
print "    Transform {\n";
print "      rotation 0.0 1.0 0.0 1.571\n";
print "      children [\n";
print "        USE Q1\n";
print "        Transform {\n";
print "        	 rotation 0.0 1.0 0.0 1.571\n";
print "        	 children [\n";
print "        	   USE Q1\n";
print "          ]\n";
print "        }\n";
print "      ]\n";
print "    }\n";
print "  ]\n";
print "}\n";
