CS40 Lab 4: 3D Coordinate transforms

Due 11:59pm Monday 21 February 2011

New: Trouble getting started with CMake? See the tips section below.

You may work with one partner on this assignment. For this lab, you will be writing code from scratch, but you can use the planet.c example in w04-transforms as a possible starting point. You will be modeling an entire solar system, using coordinate transformations. Mostly you will be making judicious use of glTranslatef, glRotatef, glPopMatrix, glPushMatrix, to work in different coordinate systems.

Data
The file solarData.txt in your labs/04 folder contains some basic info about our solar system (and Pluto, which has recently been disqualified). The information contains the radius of each object (in thousands of km), the distance from the sun (a, in millions of km), the period of rotation around the sun (tau, in earth years), the eccentricity of the elliptical orbit, and the inclination of the orbit with respect to a plane through the center of the sun. You should use this information to model a solar system as a collection of rotating spheres.

You can parse this data file using code similar to that in parseData.cpp which you can compile and run using g++ parseData.cpp -o parseData && ./parseData

Basic Requirements
You will need to find an acceptable coordinate system in which to draw your system. I recommend scaling the radii of the objects so they look visually appealing and convey some sense of relative scale. Feel free to adjust the sun (and perhaps the larger outer planets) differently than the rest. The sun is really big. After adjusting the radius field, you should be able to use the rest of the units as they are.

You must create a planet class or planet struct to organize necessary planet info.

You must draw and animate all the planets listed. All planets should revolve in the same direction around the sun, but not at the same rate. You may assume the planets revolve in circular orbits, but the eccentricity is given if you want to model elliptical orbits. You can use glutSolidSphere to model the planets

Assign a color to each of the planets. You may want to add these parameters to the data file, so you can refresh them without recompiling.

You must model at least one moon around one of the planets. You can choose the size of the moon, radius of orbit, and period of rotation, or consult a source for reasonable defaults.

You should enable double buffering as well as the depth buffer. Code samples will demonstrate how to enable these features.

Add keyboard controls to pan and zoom. You can decide how this is implemented. Remember you can change either the projection or model view.

Hints/Tips
Work incrementally. Get your data file parser working and then print out the info each planet to ensure you parsed the data correctly. Make subtle adjustments to the GL_PROJECTION matrix using gluPerspective, glOrtho, or glFrustrum until you understand what you are looking out and how the scene is effected.

To get a working build system, you may start as follows.

cd cs40/labs/04
cp ../../class/w04-3d/planet.c ./solarSystem.cpp
For the purposes of this example, I implemented my planet class in planet.h and planet.cpp and my test code is in solarSystem.cpp. My CMakeLists.txt looks like this

#create a new library called planet
#a library consists of compiled cpp code, but
#does not have a main function

#put all the .h files needed for your library here
#planet_hdrs is just a variable name, not a header file
set(planet_hdrs planet.h ) 

#put all the .cpp files needed for your library here
set(planet_src planet.cpp)

#tell cmake that the new "planet" library depends on
#the hdrs and src defined above
add_library(planet ${planet_hdrs} ${planet_src})

#stuff any openGL app usually needs. 
set(CORELIBS ${GLUT_LIBRARY} ${OPENGL_LIBRARY} m)

#create a new solarSystem executable based on
#solarSystem.cpp. This code has a main function
add_executable(solarSystem solarSystem.cpp)

#link compiled solarSystem code with openGL libraries
#and your new planet library
target_link_libraries(solarSystem ${CORELIBS} planet)

#tell cmake that the solarSystem executable depends
#on some data files too and symlink them
symlink_data(solarSystem solarData.txt)

#parseData is just a simple cpp program and doesn't need
#to link against anything
add_executable(parseData parseData.cpp)
Remember to edit the CMakeLists.txt file in the parent directory and add add_subdirectory(04). Then go into cs40/labs/build and type make. If all goes well, it should create a 04 subdirectory, build and link solarSystem, and symlink your solarData.txt file. If solarData.txt does not appear in your build directory, you may need to symlink the file yourself using

ln -s ../../04/solarData.txt ./

Optional features
The following are optional extensions and are not required for full credit.
Submit
Once you are satisfied with your programs, hand them in by typing handin40 at the unix prompt. You may run handin40 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.