My Blog

Personal
  About Me
  Friends/Family
  Contact

Writing/Opinions

Job Experience
  Portfolio
  Resume

Creativity
  Photo Gallery
  “Digitally Altered”
  Video Editing
  Making Music
  Programming

Interests
  Investments
  Books
  Traveling
  Skiing
  Urban Exploration

Misc.
  Jokes Collection
  Sound Clips
  Links



Political Essays
  The NSA's Domestic Spying
  U.S. Foreign Policy Flaws
  Noam Chomsky Lecture
  Howard Zinn Interviewed
  Why Invade Iraq?
  The Problem of Pres. Bush
  Japanese Government
  Gun Control Laws

Essays of Experience
  My Feelings About Cars
  Tour of a Nuclear Plant
  E. Abbey on Nature
  House Moving Story
  A Balloon Ride

Science Essays
  Baseball Physics
  Evidence of Paranormal
  Was Time Created?
  The Big Bang
  Fish Evolution
  Ocean Currents
  Dinosaur Meteor Impact
  Universe Expansion
  Quantum Chance
  Handwriting Recognition
  Recovery from Smoking

Other Essays
  Investments for Everyone
  Macs vs. PC's
  The Matrix, & Fight Club

All Essays


Recreational Programming Projects
With source code! Java 3D, C, OpenGL, and more.

One of my oldest hobbies has been programming computers, especially graphics. Trapped in the 80's with BASIC on an Apple IIe probably wasn't the best place to learn graphics, but at least computers of that time were more accessible to beginning programming. Back then you could sit down at any computer, turn it on, and write a program, usually something simple like "10 PRINT "HELLO": GOTO 10". Anyway, it wasn't until entering college and learning stuff like OpenGL and Java 3D that I actually had something to show for my greatest programming ambition, modeling natural behavior and mathematical functions through computer graphics.


Not technical enough?

For details of my professional programming experience check out my Professional Portfolio which contains in-depth explanations of what I actually do for a living.

This page sketches some of my non-work-related graphics programs. I'll describe things so anyone can understand what's going on without having any programming experience. And I'll include lots of screenshots, since what fun is a program if you can't see what it does?

Just in case you are a programmer, I do provide all the source code for these projects at the bottom of this page. Let me know if you actually use any of it!


On This Page:

Projects with screen shots and source code:
      Modeling Trees in Java
      L-Systems: Modeling any Plant
      OpenGL Sierpinski Sponge
      Neural Networks
      Ray Tracing
      Assembly Language Space Invaders!
      Fun with QBASIC

Projects with source code only:
      A Unix e-mail client in C
      A JPEG data compression program

See all the source code


 
Modeling Trees
Using a simple hard-coded randomized algorithm

During my job at NCSA in 1999 I was given an assignment for a short time to simply goof around with Java 3D (3D graphics API's for Java) and familiarize myself with the language. What could I do given free reign over a modern high-level 3D programming language? How about trying to make a tree in three dimensions? Here are two photos of my program's crude approximation of a tree.

    
The tree starts out small. Branches randomly split as they grow.

My simple trees were made up of cylindrical segments, since cylinders are a basic object in Java 3D and are rather easy to create. During each iteration, each branch would grow by one cylinder length. Each cylinder was oriented slightly randomly in space, to make the tree look natural. Each cylinder also had approximately a one in three chance of splitting into two cylinders, allowing the tree to branch off. As the tree got older, the oldest cylinder segments would grow fatter, just as the oldest parts of a tree are the fattest. The screen shot on the right shows the extent of what this program could do. (The source code is available below.)

The biggest challenge of the project probably was the 3D math. When a cylinder is instantiated in Java, it's laying along the Y axis—always. I would imagine that most of the time when you need a cylinder, you need it to extend between two points, such as from the origin (0, 0, 0) to some other point, like (3, 2, 7). Java 3D ignorantly does not provide any method for easily placing your cylinder into the desired orientation! So I had to write a 3D transform procedure which took two endpoints and figured out the rotation matrices required for putting the cylinder into that orientation. Since it took me several days to code this (and boy was I excited when I got it to work!), I've posted the source code for it below.

At any rate, the trees my program was making still appeared rather unnatural (besides the fact that they're made out of cylinders, and they don't have leaves). One reason for this is because the branches' orientations are too random. My boss pointed me in the direction of one method which can produce natural-looking plants, trees, and bushes: L-Systems.

 
L-Systems
Making more natural-looking models

Loosely speaking, an L-System is a list of rules for moving, say, a pencil (or a turtle in Logo). Following the rules draws a picture. Some examples of rules are: "move forward one unit," "turn X degrees," or "repeat everything you just did except do it at half the scale and oriented 20 degrees to the right." The "repeat" rule allows an L-System to go on indefinitely, possibly drawing the same pattern recursively on a smaller and smaller scale. This would create a fractal!

So I started from scratch and wrote a completely new program which would interpret a simple two-dimensional L-System (and render it in 3-D space, just cause I could). This page (mirrored here) describes everything you need to know about simple L-Systems (in relation to a mathematical program called FRACTINT), and I used it as my sole reference. I finally reached my goal of producing a simple plant.

     Once I had written my L-System interpreter, all I had to do was feed the program the following L-System, and it would draw a weed!
Weed {
  Angle 50
  Axiom +++++++++++++x
  x=f[@.5+++++++++x]-
    f[@.4-----------!x]@.6x
}

I had hoped to feed more complex L-Systems to my program, but I ran out of time and had to move on. I've posted the source code below so others can use it and improve on it.


 
OpenGL
Programming 3-D graphics in C

As a grad student in computer science, I got to take a real exciting computer graphics class where I learned how to program in OpenGL, a lower-level language than Java 3D.

The picture at right is of a Sierpinski cube, created by taking a cube, hollowing out the center of each of its six faces, and then repeating the process recursively for each of the cubes remaining in the corners and on the sides. If you repeat that infinitely, it becomes a fractal. Source code available below.

    

 
Neural Networks
Making a computer learn by trial and error

This is a snapshot of the interface to my artificial neural network program—an extremely simplified mathematical model of the brain. Each circle represents a neuron. A signal (such a math problem like 2 + 2) is fed into the input neurons at the top of the diagram. The signal is then propagated down through all the connections (which are initially weighted random amounts), and the values present at the output neurons are the "answer." If the answer is incorrect, the connection weights are adjusted according to a rather complex formula (the hardest part of the program to write), and then the input signal is repeated (or one similar to it, like 2 + 1).
      After a neural network has been sufficiently trained on a set of problems, it may be able to generalize answers to other (similar) problems which hadn't been part of its training set. My neural network barely seemed to improve, though, even after lots of training. My program didn't want to learn anything, so I gave up too. The source code is available below.

 
Ray Tracing
Accurately representing the world, using numbers, is a lot of work

These pictures were the result of a fun little project I did for a computer graphics class at the University of Toledo. I defined several objects in 3-D space, specified their surface qualities (color, texture, various reflectivity properties, etc.), positioned some light sources, and positioned the camera (your eye).
    

      Then to produce this picture, I would run a program (RayShade) which would do all the calculations.The computer would churn through a lot of numbers, and I mean churn. For each pixel in this image, a ray of light had to be traced back to its light source to determine what it had done during its trip. In other words, each pixel's color ultimately was determined by what its corresponding light ray had bounced off of. There are a lot of calculations involved in determining what happens when a light ray hits a curved object (like a ball), or a textured object, or a translucent object. And if there's a mirror, the entire scene might be reflected in it, effectively doubling the number of calculations. This picture contained 160,000 pixels (it's 400 pixels wide and 400 pixels high), so the paths of at least 160,000 tiny rays of light had to be calculated. The final result is a picture which shows exactly what your eye would see if that scene had actually existed. And that's called ray tracing.


 
Assembly Language Space Invaders
I wrote Space Invaders using the most painful language available

    
Introduction screen Game screen

In 1995 I took a PC assembly language class at U.T., and for our final project we had to write Space Invaders—purely in assembly! I never would have done something that crazy on my own (true to form, I had to stay up the entire night before in order to finish it, just in time). However, I'm pretty happy with the finished product. Thank goodness it works, because I'd never dream of trying to fix it. It's probably too fast to run on today's machines, but it still runs nicely in the SoftWindows PC emulator on my Mac. The source code is available below.

Download the PC executable here!

 
Fun with QuickBASIC
No big accomplishment here, just some fun physics

    
Solar system simulation Fireworks

I had some spare time at a PC a couple summers ago, so I learned QBASIC and wrote a few fun little programs (source code available). One of them lets you choose sizes of planets and their starting positions and velocities, and then lets them go. They fly around each other according to Newton's law of gravity. However, the planets quickly interfere with each other and their orbits decay. For such a simple little program, it does a good job of demonstrating how finely tuned our solar system is (a byproduct of the way it formed).
      I also wrote a fireworks simulation. One firework at a time explodes on the screen and then shimmers as it drifts down and disappears. It'd look really nice on a fast computer, I bet!
      A third program I wrote was supposed to simulate a flock of birds (you know, how birds tend to stay in a loose grouping while they're flying somewhere), but the program turned out producing results closer to a swarm of insects. That's fine, but I couldn't quite figure out what quality differentiates the two behaviors. Anyway, this would also look great on a fast computer.
      These are exactly the types of programs I liked to write on my Apple IIe back when I was in middle and high school. I always have a list in the back of my mind of fun programs I could write, when I have the time and resources, like I did with my tree modeling program. (My Macintosh at home isn't the ideal platform for writing these kind of things.) If you're a beginning programmer and want some ideas, let me know. I can help you get started and offer advice. Producing realistic models that you could see and interact with was always what made programming fun for me.

 
The Source Code

Here's the source code in its entirety for all of the projects outlined above, plus a few more that have no screen shots available. If you have trouble getting them to work, feel free to drop me a line.

Terms of usage: Feel free to use any of this source code in any project, as long as my name and web site address are present in the source code ("Scott Teresi, www.teresi.us") and listed as a small footnote in your About box (if you're application is extensive enough to have one)! I also would love hearing from you about how you found the source code useful. Thanks!

Java 3D L-Systems Interpreter. This program is designed to create natural-looking plants from L-Systems. (Javadoc documentation for the program is available here!) One L-System which generates a weed is provided. You will need the Java 3D API for either Windows, Solaris Sparc, or SGI Irix in order to compile and run this application. See Sun's Java 3D page.

         Download all the files here, or browse the individual files below:
Javadoc - explanation of all classes
CylinderCreator.java - creates a Java3D cylinder extending between two endpoints.
DrawLsystem.java - run this to draw your L-System (provide your L-System file on the command line).
Interpreter.java - does the actual interpreting of the L-System, positioning the cylinder segments.
Lsystem.java - front-end for the L-System interpreter. Returns a BranchGroup containing the interpreted L-System.
LsystemsReader.java - class for reading in an L-System file.
Rules.java - a class for storing the rules read in from the L-System file.
State.java - class containing state information for the interpreter's "turtle."
weed.lsys - an example L-System (screenshot above). It's the only L-System this program has been tested on, but hey, that's a start
http://spanky.triumf.ca/www/fractint/lsys/plants.html contains help and more L-Systems you can use.
Java 3D hard-coded randomized tree modeler. Generates a scraggly tree (with no leaves) in 3-D space and rotates it.
        Tree.java - main application
        SegmentBehavior.java - behavior class for segments of the tree (the whole tree rotates, by the way)
        BranchBehavior.java - behavior class for entire branches of the tree
        README - a couple notes
Java 3D cylinder creator. Instantiates a cylinder, in the orientation YOU want!
        CylinderCreator.java - creates a Java3D cylinder extending between two endpoints.
C and OpenGL Sierpinski cube (and tetrahedron). Produces a Sierpinski object with varying amounts of detail. Mouse can be used to rotate the object in 3-D space.
        Source code available here. Here's the monster makefile for HP-UX, and one for SGI.
C Unix mail reader. Works like a very bare-bones version of Pine. Displays two window panes, one containing a list of e-mails in your Inbox and the other the current message's contents. Requires the "curses" screen windowing library.
        Here's the source code to mail.c.
Java Neural Net application. Implementation of a plain vanilla backpropagating neural network, with graphical display of neurons and synapses. I'm not sure if it learns very well. Sample data file and results file included.
        Download it here, or see a directory of the files.
Rayshade 3-D graphics scenes. Refer to screenshots above. Requires Rayshade.
        The big one: bigscene.ray
        One of the smaller ones: scene.ray
Matlab JPEG data compression. The JPEG algorithm implemented before your very eyes. The real magic is in the discrete cosine transformation (DCT). Take a look! Matlab is very English-like, as far as math goes.
        The Matlab program: jpeg.m
        Sample image file, raw data format specially for Matlab: lenna.mat (may be binary format)
        Data from some trial runs: jpeg.results
        Help using Matlab: matlab.help
Some fun with QBASIC. Swarm of flies, a model solar system, and fireworks.
        A bunch of dots swarming like flies: swarm.bas    And a slightly modified version: racers.bas
        A model solar system, planets orbiting a larger planet: planets.bas (all settings customizable).
        Fireworks: fireworks.bas
Space Invaders in PC assembly! Text-based (no graphics, but it's colorful).
        The executable for your PC: space.exe
        The source code: mp4.asm and other necessary source files: bintoasc.asm and string.asm
        A batch file to compile and link the program using the MASM compiler: com.bat
• Remember, screen shots are available above for most of these programs, and drop me an e-mail if you have any questions at all about the source code or even programming in general.


     


Home  |  Contact Page
Professional Portfolio  |  Resume

Essay/Opinion  |  Photo Gallery  |  Digitally Altered  |  Video Editing  |  Making Music  |  Programming
Traveling  |  Skiing  |  Urban Exploration

About Me  |  Friends/Family
Jokes  |  Sound Clips  |  Links