Math 155A - Introduction to Computer Graphics – Fall 2022
Instructor: Sam Buss,  UC San Diego

Project #2 – Modify Solar System program by adding a binary sun, a submoon, a torus, and a tilt.

Due date: Friday, October 14; 10:00pm.

Goals:  Learn more about how to use OpenGL, interrupt-driven programming, animation, and transformations. Program some additions to an animated solar system.  Use OpenGL commands to generate transformations that control the animation. 

Grading will be personalized in a one-on-one in-person session with one of the four TAs or with Professor Sam Buss.  You will meet in the APM B349 (or B432) lab, and run your project either on a lab PC, or cloudlabs in a browser or on your personal laptop. You will have to discuss your source code, run the program, make changes on the spot to your program and recompile as requested by the grader, and be able to explain how your program works and why it renders what it does.  The grading should be completed promptly, preferably no later than the due date for the next programming assignment.


1.      As preparatory work, you can look at the SimpleAnimModern and SolarModern programs, available at the textbook’s website and the accompanying webpages explaining how the code works. (Some of this you do not need to know for Project #2, notably the use of the projection matrix for perspective). Project #2 asks you to modify the program SolarModern.

2.      Download the SolarModern project from the textbook’s web page.

a.      There are three core source files: SolarModern.cpp and two source files ShaderMgrSLR.h and ShaderMgrSLR.cpp. The only source file you will change for Project #2 is SolarModern.cpp. Start by renaming it to SolarProj.cpp. (Please use this name exactly, including the same capitalization.)

b.     Download the five files LinearR3.cpp, LinearR3.h, LinearR4.cpp, LinearR4.h, and MathMisc.h.  There is a link on the textbook’s webpage to download these five (5) files, under GlLinearMath. You will NOT need to change these five files.

c.      Download the six files GlGeomSphere.cpp and GlGeomSphere.h, GlGeomTorus.cpp, GlGeomTorus.h, and GlGeomBase.cpp and GlGeomBase.h.  There is another link on the textbook’s web page to the GlGeomShapes project to download these files. You also will NOT need to change these four files. You do not need to get the other GlGeom* files in the GlGeomShapes project.

d.     The ZIP file contains an executable file SolarProj_Demo_Fa22.exe. This shows, approximately, what your solution for Project #2 should look like.

e.      Form a new Visual Studio C++ project called (say) SolarProject.  Put all 12 source files into this new project.  It should now be able to compile and run.

3.      Understand the code in the SolarModern program. Read the online documentation of the SolarModern program at the page where you downloaded the SolarModern.cpp file. Compile and run the program. You may find the solar program runs way too fast or too slow on your system, so that the earth and moon zip around their orbits many times per second or move very slowly.  If so, you can press up/down arrow keys to adjust the speed. (Or, set the initial value for the variable AnimateIncrement smaller or larger. Test out the keyboard controls:

a.      You will see a very simple solar system (in SolarModern) with a central yellow sun, a blue planet (Earth) rotating on its axis once every 24 hours and revolving around the sun every 365 days, and a moon revolving around the Earth 12 times per year.

b.      Pressing the up and down arrows will change the animation step size. Pressing the up arrow makes the animation run faster by taking bigger time steps each time the solar system is rendered. Pressing the down arrow makes it run more slowly.

c.      Pressing “r” will start and stop the animation.

d.      Pressing “s” will put the program in “single step” mode so that the animation steps one time frame each time “s” is pressed.

e.      Pressing “c” will toggle the culling of backfaces.

f.       Pressing ‘a’ will toggle the view from somewhat above to solar system, to looking straight down, to looking directly from the side.

g.      Examine the code in SolarSModern.cpp, to understand how it makes items a.-e. work. There are variables HourOfDay, DayOfYear, and AnimateIncrement. Find all their occurrences in the source code, and understand how they are being used and updated. Also understand how the spinMode and singleStep variables are used to control the animation.

h.      Compare the starter code in SolarModern.cpp to the reference executable, SolarProj_Demo_Fa22.exe: SolarProj_Demo_Fa22.exe has

                                                    i.     A binary sun,

                                                  ii.     Earth and moon as before (but with orbits and sizes adjusted).

                                                 iii.     The Earth’s moon has its own orbiting satellite, (this is called a “subsatellite” or “submoon” or “moonmoon”,)

                                                 iv.     A second, bright magenta, planet (Planet X) in a retrograde orbit around the sun,  (Colors can be adjusted in for your own project.)

                                                   v.     A thin red torus marking the orbit of the Earth.

                                                 vi.     The earth system (earth, moon, and submoon) is tilted, similar to the tilt of the real Earth that causes the seasons.

i.       You should understand the callback-driven style of the program, and how the keyboard controls are handled by the program, and when myRenderScene is called. But you will not change this part of the code.

j.       The GlGeomSphere objects are used to render the spheres. This handles all the work of creating and drawing the VAO and VBO (and EBO), for the spheres.

k.      Similarly, a GlGeomTorus object will render the thin torus marking the Earth’s orbit. This is not present in the downloaded starter code.

l.       When the program first starts, aliasing causes the planet to appear to not be rotating, but instead always keeping the same face to the sun.   Slow down the animation (down arrow keys) to see the "true" motion.  Figure out why the animation code makes this happen.  Understand what was meant by "aliasing" three sentences ago. (We will discuss this in a class meeting; or you may discuss on piazza.) 

4.      The “demo” executable, “SolarProj_Demo_Fa22.exe, in the zip file shows what your Project #2 should look like (approximately) when you are finished with the steps 4-12 below. It is suggested to add features one-by-one.

5.      Change the sun to be a binary sun.  The two suns revolve around the center of the solar system. You may set their orbital rate to look good, say similar to the orbital rate in the demo code. You should also adjust the distance between the suns and the size of the suns so that they look good.  (Hint: You may wish to add another LinearMapR4 variable or two to your program to control the rotation of the sun.) This change will require both translations and rotation; it may also involve a scaling transformation if you wish to shrink the size of the sun.

6.      Add a “Planet X”, a second planet orbiting the Sun.  Planet X should orbit the Sun once every 500 days (that is, once every 500 Earth days). In the SolarDemo program, Planet X is magenta, but you may use whatever color you like. Planet X does not rotate on its axis.  Hint: Since the PlanetX year is a different length than the earth’s year, Planet X’s “day of year” will be different from the “day of year” of the earth. You will need an extra variable for this; for example, named “DayOfYearX”. You can use the same “Earth” GlGeomSphere to render PlanetX as the earth, but with a different radius and a different color.

7.      Add a submoon revolving around the Earth’s moon. You can use the same “Moon1” GlGeomSphere to render the submoon as the moon, but of course with a smaller radius and a different color.

8.      Add a thin red torus to mark the Earth’s orbit.   For this,

a.      Add a line “#include “GlGeomTorus.h” next to the existing line “#include “GlGeomSphere”.

b.     Where the three GlGeomSphere’s are declared, add a line such as GlGeomTorus MyTorus(40, 10, 0.01);  Look at the online GlGeomShapes or the GlGeomTorus.h header file for documentation for the meaning of the three parameters.  The torus will have major radius 1.0 and minor radius 0.01. The “40,10” control the resolution of the mesh.

c.      Add the line MyTorus.InitializeAttribLocations(vertPos_loc); in the routine mySetupGeometries(). This calls code in GlGeomTorus.cpp that sets up the VAO and VBO for the torus.

d.     Render the torus with MyTorus.Render(); Scale the torus so it shows the orbital path of the earth, and set its color to be red.

9.      Add a tilt to the earth, of about 24 degrees. This tilts the earth and its moon system, but not the orbital path of the earth. It is similar to the tilt that causes the seasons of the real earth. The tilt is always rightward in the scene.  We will discuss in lecture some hints for how to do this; see also item 11 below.
The perspective in the Solar program can make it hard to see the tilt well: The tilt will be easier to see if you use the “a” keystroke command to change the viewAzimuth variable to equal 0.0 instead of 0.25 radians, so that you are viewing the solar system directly from the side. You can watch the orbital plane of the moon as the earth system goes around the sun: it should tilt with the earth’s tilt.

10.   As part of steps 4-9, adjust the orbital radii, the orbital rates, view distance, colors, etc. Make changes to the sizes and colors of the suns, planets and moons and to the radii of the orbits, so as to make viewing the solar system convenient.  (You may need to adjust the values of Xmax, Ymax, Znear, Zfar.)  Choose colors that make all planets and moons, and the torus clearly visible and distinguishable. You do not need to match the supplied demo program.

11.   Hints for the tilt: This probably the hardest part, because the natural way to do this is tilt the earth before revolving it around the sun, but then the revolving around the sun affects the direction of the tilt.  There are several ways to make this work. One way is to make the tilt on the earth’s system be applied along a different axis than the z-axis, chosen to make the final tilt come out right.   A second way, perhaps better, is to instead of using a translation plus a rotation to revolve the earth around the sun, just use a single translation to move the earth to the correct location, as calculated with the aid of sine and cosine functions). (There are other ways to implement the tilt besides these two suggestions.) In any case, implement the tilt so that an inhabitant of the earth still sees 365 days during each earth year. This last part can be a bit tricky! 

12.   The animation speed initially updates a single fixed time step size per frame update. You may notice that when you move the mouse across the screen, the animation goes much faster. This is due the mouse position changing and generating a new frame refresh every time the mouse moves from one pixel the next (even though your program is not paying attention to mouse movement). There is code in the routine myRenderScene() that compensates for this by using a variable UseRealTime and tracking actual elapsed time with calls to glfwGetTime(). This code can be toggled off and on with the “t” keystroke command. You may wish to examine this code to understand its functionality for use in future assignments.

13.   Turn in the project by uploading (only) your source file SolarProj.cpp to gradescope.  The gradescope files are primarily to time-stamp your project; uploading to gradescope does not suffice to get graded.

14.   To get graded, see Professor Buss or (after the due date) one of the TA’s. Grading is drop-on/on-demand (no reservations) in the computer lab hours in the APM basement computers. See the preliminary version of the grading rubric.                 

Program grading: Scale of 0 to 20.  Personal zoom grading session with one of the TAs or Professor Sam Buss. For grading, be ready to discuss any of the above topics, plus be prepared to make small modifications to the source code and recompile.