CMSC 435/634: Introduction to Computer Graphics

Assignment 2


Due March 4, 2008

Last Update:

Tue Mar 4 14:28:12 EST 2008
Wed Feb 27 09:44:11 EST 2008
Fri Feb 15 14:34:47 EST 2008

The Assignment

For this assignment, you will write a program to create and render a 3D tree using an L-system. Your program should be written in either C or C++ program using OpenGL on Linux, or C sharp program using XNA Game Studio 2.0. You should read a text file to define the L-system. The first line should give a starting symbol or string. The remaining lines should define production rules for each non-terminal, so "A=A[+B]" would be a rule to replace A with A[+B]. All of your grammars should use the same set of symbols, including symbols for branch segments, to push and pop the transformation stack, and for various rotations. You should intially render the start string, but include keyboard controls (and game controller, if you do an XBox version) to increase or decrease the number of generations of substitution to create your tree. For keyboard controls, use "+" to increase the level and "-" to decrease it. For the game controller, use the right trigger to increase the level and the left trigger to decrease it.

The tree should be on a large flat plane, and lit by a single directional light source (the sun). For the lighting to work, you will need to compute a surface normal for each vertex, pointing outward from the branch axis. The branches should be rendered as cones or cylinders, and need only intersect with each other where branching occurs. For extra credit: At each branching point, you should keep the same cross sectional area before and after the branch. In other words, a trunk of radius r has area r2, so if it splits into two branches, their radii should be chosen to also give a total area of r2. An even split would result in both new branches having radius r/sqrt(2).

You should create the triangles for your cone or cylinder branches yourself, following the terrain example given, rather than use the standard gluCylinder function. Your can choose any number of triangles around the circumference of your branch, more will look smoother while less will be faster. Six to eight is probably a good compromise. You should have at least a position and normal at each vertex. For cylinders, the normal points directly out from the cylinder axis, in other words equal to the shortest vector from the cylinder axis to the vertex. For cones, the easiest approach is to start with the cylinder normal, then rotate it up by the angle of the cone. You can also optionally include texture coordinates to have a textured branch, but this is not necessary.

634 students (only): Also include options to prune your plant to at least three specific shapes. Each shape should be described by an implicit function in your code that can be called for any point in space. It should return negative for points outside the pruning shape, and positive for points inside the pruning shape. Use this in your L-system rendering to trim the tree to only show branches inside the pruning region. Be careful to also remove dangling branches if their connection to the main tree is pruned.

Nuts and bolts

Two sample programs have been provided in your assn2 CVS directory, sampleXNA and sampleOpenGL. You may need to do a 'cvs update' to get these new files.

OpenGL is a cross-platform API (Application Programming Interface) for graphics that runs on Windows, Linux, Mac and others. You can use C or C++ for your OpenGL project. This sample code also uses GLUT, a widely avalable cross-platform window and input management layer for OpenGL. For major projects, I'd suggest something a bit more sophisticated that GLUT, but for something like this class project, it is perfect. Once set up, GLUT assumes control of your program, and calls your code when there is something to do through a set of callback functions (the ones passed to GLUT through the glut*Func calls). This has two important implications for your code. First, the only way to pass data into or between these functions is using global variables. Second, if you use C++, any callback functions must be declared extern "C". For example,

// declare
extern "C" void draw();
// define
extern "C" void draw {
  // arbitrary C++ code

We have tested the sample program on the OIT Linux systems. If you want to just focus on the project, without having to potentially spend time on installation or other unexpected headaches, this is the one for you. Since this is an interactive program, you will want to actually go into one of the campus computer labs and reboot a machine into Linux. Networked display will be too slow.

XNA is a Microsoft API for graphics that runs on Windows and the XBox 360. You must use C# for XNA program development. XNA is not normally installed on the campus Windows systems. We have a few (10) computers with XNA in the GAIM (Games, Animation and Interactive Media) lab, as well as XBoxes with the necessary "Creators Club" membership to allow you to load XNA programs onto the XBox. Given the limited number of PCs in this lab, if you choose to use XNA, you must install it on your own Windows computer and do most fo your development there. I can provide card access to the GAIM lab for those who want to port their working Windows XNA program to the XBox.

Both of these sample programs show examples of creating and rendering geometry in a 3D environment. You will, of course, have to replace these with your own code and data structures that render models meeting the project description. For best interaction, for either XNA or OpenGL, I recommend generating and storing all relevant vertex data for the tree only when the tree changes (by changing the number of generations), then just using this precomputed data to render each frame.


Think first, then take an incremental approach to your development so you can check your code along the way. I would suggest starting with just text-based testing of your L-system grammar reading and L-system evolution. Then you might try modifying the sample rendering code to show a flat plane, then add a single branch or trunk, then the more complex transformations in the grammar.

I made the background of the sample program blue since problems with lighting will often lead to totally black triangles, which do not show well against a black background. If you do want to have the black background of space, wait until the end to change it. You may find it useful to add additional keys to change the way aspects of your scene are rendered for debugging — say to turn lighting on and off, color triangles random solid colors to more easily see the triangle subdivision, or use a rescaled normal as color to see if the normals are consistent and smooth. If these don't interfere with the required keys, you can leave them in.

Getting started and submitting your work

You will be using the university systems, and using CVS to submit all work in this course. Refer to the class CVS instructions for details, but you will need to first check out a copy of your CVS files from the class repository. This will give you the sample files you need to start work on this assignment. Do all of your work in your checked out copy, then check in your submission by the deadline. DO NOT work directly in the repository, if you do, we may not be able to retrieve your assignment and your work may be lost!

If you are using XNA, you can use CVS directly from your PC using WinCVS. You will need to set it for ssh access to the remote repository.

Turn in this assignment electronically by checking it into your assn2 CVS directory by 11:59 PM on the day of the deadline. We will use a dated checkout for grading, so you will be graded on whatever has been checked in as of 11:59 PM. Submit a readme.txt file telling us about your assignment. What help, if any, did you receive from books, web sites or people other than the instructor and TA?

Also submit all source and data files we need to build and run your submission. For the OpenGL option, this will consist of your readme, Makefile, C or C++ headers and source files, and some sample grammar files. For the XNA option, this includes your readme, .sln, .csproj and .cs files, as well as any Content directory files, including sample grammar files. We should be able to check out and build your project entirely based on the files you check in. Be sure to comment your code! You will not be graded on the presence or quality of your comments, but we will look at your code. Anything that helps us understand what you did (or were trying to do) can only help. In any case, your programs are expected to be robust and easy to understand.