#!/bin/sh
# filename:  sphere.sh
# author:    Marvin Simkin
# date:      2004-02-16
# purpose:   demonstrate how to draw a simple sphere

# --------------------------------------------------------------------------
# this shows the lines that are drawn,
# but the resulting shape is flat not round
drawwrl.pl < sphere.draw > flatgrid.wrl

# --------------------------------------------------------------------------
# this takes the previous flat diagram
# and wraps it into a ball

# first we have to "chop" the long straight lines
# into little short ones,
# so that they can be wrapped around a globe.
# The more often you chop, the smoother the lines, but the bigger your files.
# If you chop too often, it can take a very long time to process.
# This chops lines very coarsely (so you can see it) every 30 degrees
# in the X and Y dimensions,
# but there is no need to chop in the Z dimension.
drawchop.pl x=30+30 y=30+30 < sphere.draw |

  # this does the conversion from spherical coordinates to cartesian
  drawball.pl |

  # and produce a VRML file we can view
  drawwrl.pl > ballgrid.wrl

# --------------------------------------------------------------------------
# here is our original flat grid, but with an annotated "bounding box"
# so that the X and Y dimensions will have labels
# to help us see what's where
# First, make the bounding box red, so it will stand out.
echo 'color=1 0 0' > boundbox.draw

# Next, generate a default bounding box and write it to the same file.
# discard color from sphere.draw so it does not override what we set above
grep -v 'color *=' < sphere.draw |
  drawbbox.pl >> boundbox.draw

# Take both the original drawing, and the bounding box,
# and make them into a single VRML file.
cat sphere.draw boundbox.draw |
  drawwrl.pl > flatbbox.wrl


# --------------------------------------------------------------------------
# the same thing but rendered as a sphere instead of flat
cat sphere.draw boundbox.draw |
  drawchop.pl x=30+30 y=30+30 |

  # this does the conversion from spherical coordinates to cartesian
  drawball.pl > ballbbox.draw

  # The previous drawbbox.pl chose a default speed based on flat geometry
  # but that is not fast enough for a sphere.
  # Discard the old value and generate a better one from the sphere file.
  grep -v speed < ballbbox.draw |
    drawbbox.pl |
    grep speed >> ballbbox.draw

  # and produce a VRML file we can view
  drawwrl.pl < ballbbox.draw > ballbbox.wrl

# --------------------------------------------------------------------------
# this time it is chopped much more often to see the difference
cat sphere.draw boundbox.draw |
  drawchop.pl x=5+5 y=5+5 |

  # this does the conversion from spherical coordinates to cartesian
  drawball.pl > ballx5y5.draw

  # The previous drawbbox.pl chose a default speed based on flat geometry
  # but that is not fast enough for a sphere.
  # Discard the old value and generate a better one from the sphere file.
  grep -v speed < ballx5y5.draw |
    drawbbox.pl |
    grep speed >> ballx5y5.draw

  # and produce a VRML file we can view
  drawwrl.pl < ballx5y5.draw > ballx5y5.wrl
