#!/usr/bin/perl

print "#VRML V2.0 utf8\n\n";

print "PROTO BRICK [ ] {\n";
print "	Appearance {\n";
print "	  texture     ImageTexture { url \"../IMG/256/brick17.jpg\" }\n";
print "	  textureTransform    TextureTransform { scale 1 2}\n";
print "	}\n";
print "}\n";

print "PROTO BUDDHA [ ] {\n";
print "	 Billboard {\n";
print "	   children Shape {\n";
print "	     appearance Appearance {\n";
print "	       texture     ImageTexture { url \"../IMG/256/buddha1.png\" }\n";
print "	       material Material {\n";
print "		 diffuseColor 0.3 0.3 0.2\n";
print "		 shininess 1\n";
print "	       }\n";
print "	     }\n";
print "	     geometry IndexedFaceSet {\n";
print "	       solid FALSE\n";
print "	       coord Coordinate {\n";
print "		 point [\n";
print "		  -2.0 0.0 0.0,\n";
print "		   2.0 0.0 0.0,\n";
print "		   2.0 7.0 0.0,\n";
print "		  -2.0 7.0 0.0 \n";
print "		 ]\n";
print "	       }\n";
print "	       coordIndex [ 0, 1, 2, 3, -1 ]\n";
print "	       texCoord TextureCoordinate {\n";
print "		 point [ 0 0, 1 0, 1 1, 0 1 ]\n";
print "	       }\n";
print "	     }\n";
print "	   }\n";
print "	 }\n";
print "}\n";
print "Transform {\n";
print "	translation 0.0 1.0 0.0\n";
print "	children BUDDHA { }\n";
print "}\n\n";



$flnm = $ARGV[0];

open(STUPA,$flnm);
$line = <STUPA>;
$line = <STUPA>;
@pcs = split(' ',$line);
$Radius = $pcs[0];
$Steps = $pcs[1];
$LineSet = $pcs[2];
$nsp = -1;
do {
    $line = <STUPA>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$nsp ++;
	$spine[$nsp] = $pcs[0];
	$scale[$nsp] = $pcs[1];
	$hole[$nsp] = $pcs[2];
    }
} while $#pcs > 0 ;

$Astep = 1.57079 / $Steps;

print "DEF Q1 Transform {\n";
print " children [\n";
for $spine (0 .. $nsp-1) {
    $TopRadius = $Radius*$scale[$spine+1];
    $BottomRadius = $Radius*$scale[$spine];
    $MidRadius = ($TopRadius + $BottomRadius)/2.0;
    $Height = $spine[$spine+1] - $spine[$spine];
    $HoleStep = $hole[$spine];
    print "Transform {\n";
    print "  translation 0.0 $spine[$spine] 0.0\n";
    print "  children [\n";
    print "    Shape {\n";
    print "      appearance BRICK { }\n";
    if ($LineSet != 1) {
	print "      geometry IndexedFaceSet {\n";
	print "        solid FALSE\n";
    }
    else {
	print "      geometry IndexedLineSet {\n";
    }
    print "        coord Coordinate {\n";
    print "          point [\n";
    for $i (1 .. $Steps) {
	$ya = 0.0;
	$xTa = $TopRadius*sin(($i-1)*$Astep);
	$zTa = $TopRadius*cos(($i-1)*$Astep);
	$xBa = $BottomRadius*sin(($i-1)*$Astep);
	$zBa = $BottomRadius*cos(($i-1)*$Astep);
	$yb = $Height;
	$xTb = $TopRadius*sin($i*$Astep);
	$zTb = $TopRadius*cos($i*$Astep);
	$xBb = $BottomRadius*sin($i*$Astep);
	$zBb = $BottomRadius*cos($i*$Astep);
	if ($i%$HoleStep == 0) {
	    $yab = $Height/2.0;
	    $xTab = $TopRadius*sin($i*$Astep-$Astep/2.0);
	    $zTab = $TopRadius*cos($i*$Astep-$Astep/2.0);
	    $xBab = $BottomRadius*sin($i*$Astep-$Astep/2.0);
	    $zBab = $BottomRadius*cos($i*$Astep-$Astep/2.0);
	    $xM1 = $MidRadius*sin(($i-1)*$Astep);
	    $zM1 = $MidRadius*cos(($i-1)*$Astep);
	    $xM2 = $MidRadius*sin($i*$Astep);
	    $zM2 = $MidRadius*cos($i*$Astep);
	    
	    printf "           %7.2f %7.2f %7.2f,\n",$xBa,$ya,$zBa;
	    printf "           %7.2f %7.2f %7.2f,\n",$xBab,$ya,$zBab;
	    printf "           %7.2f %7.2f %7.2f,\n",$xM1,$yab,$zM1;
	    
	    printf "           %7.2f %7.2f %7.2f,\n",$xBb,$ya,$zBb;
	    printf "           %7.2f %7.2f %7.2f,\n",$xM2,$yab,$zM2;
	    printf "           %7.2f %7.2f %7.2f,\n",$xBab,$ya,$zBab;
	    
	    printf "           %7.2f %7.2f %7.2f,\n",$xTb,$yb,$zTb;
	    printf "           %7.2f %7.2f %7.2f,\n",$xTab,$yb,$zTab;
	    printf "           %7.2f %7.2f %7.2f,\n",$xM2,$yab,$zM2;
	    
	    printf "           %7.2f %7.2f %7.2f,\n",$xTa,$yb,$zTa;
	    printf "           %7.2f %7.2f %7.2f,\n",$xM1,$yab,$zM1;
	    printf "           %7.2f %7.2f %7.2f,\n",$xTab,$yb,$zTab;
	    
	}
	else {
	    printf "           %7.2f %7.2f %7.2f,\n",$xBa,$ya,$zBa;
	    printf "           %7.2f %7.2f %7.2f,\n",$xBb,$ya,$zBb;
	    printf "           %7.2f %7.2f %7.2f,\n",$xTb,$yb,$zTb;
	    printf "           %7.2f %7.2f %7.2f,\n",$xTa,$yb,$zTa;
	}
    }
    print "          ]\n";
    print "        }\n";
    print "        coordIndex [ \n";
    $pt = -1;
    for $i (1 .. $Steps) {
	if ($i%$HoleStep == 0) {
	    print "            ",$pt+1,",",$pt+2,",",$pt+3," -1,\n";
	    print "            ",$pt+4,",",$pt+5,",",$pt+6," -1,\n";
	    print "            ",$pt+7,",",$pt+8,",",$pt+9," -1,\n";
	    print "            ",$pt+10,",",$pt+11,",",$pt+12," -1,\n";
	    $pt += 12;
	}
	else {
	    print "            ",$pt+1,",",$pt+2,",",$pt+3,",",$pt+4," -1,\n";
	    $pt += 4;
	}
    }
    print "          ]\n";
    print "        }\n";
    print "    }\n";
    print "  ]\n";
    print "}\n";
}
print "] }\n\n";
#--------------------------------------------------------

# Replicate the object in remaining quadrants
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";

