Programming Assignment 4
CS 45500 / CS 51580
Computer Graphics
Fall, 2024

This assignment makes use of the files contained in this zip file. This assignment is due Friday, December 6.

This assignment has two parts. The first part is based on renderer_7.zip and specifically on the sub-folder "clients_r7\aspect_ratio_examples". The second part is based on renderer_9.zip and specifically on the sub-folder "basic_transformation_examples".

For the first part of this assignment you will write code that uses the Camera and Viewport APIs to handle changes in the program window's size and aspect ratio. For the second part of this assignment you will use linear transformations from renderer_9 to create an animation of the letter models from hw2.

In the zip file there is an executable jar file, hw4_demo.jar, that you can run. Also in the zip file is a program file, Hw4.java, that you need to complete so that it runs the same way as the demo program.

For the first part of this assignment you need to write code that reacts to the user changing the size and aspect ratio of the program's window. Play around with the demo program to get a sense of what your code should implement.

When the demo program starts up, the dimensions of the window are 800 pixels wide and 400 pixels high, so the program begins with an aspect ratio of 2.0. Start the demo program and drag the bottom edge of the program window down to decrease the window's aspect ratio to be less than 2.0. Notice that the program begins to letterbox the scene into a viewport. Also notice that there is a scrollbar along the right edge of the program window. Use the scrollbar to move the letterboxed viewport up and down within the framebuffer.

Next, move the bottom edge of the window up so that there is no letterbox. Then drag the right edge of the program window to the right to increase the aspect ratio to be more than 2.0. Notice that the program scales the scene horizontally and begins to crop the scene vertically. You can use the scrollbar to pan the cropped area up and down within the framebuffer.

in general, when the user makes the program window's aspect ratio less than 2.0, than the program should letterbox a maximally sized viewport with aspect ratio 2.0 within the framebuffer. The scrollbar lets the user move the letterbox up and down within the framebuffer.

When the user makes the program window's aspect ratio greater than 2.0, than the program should crop the scene in the camera's image-plane with a view-rectangle that has the same aspect ratio as that of the framebuffer. The scrollbar lets the user move the view-rectangle up and down within the view-plane.

When the program starts up, the dimensions of the framebuffer (and its viewport) are 800 pixels wide by 400 pixels high and the view-rectangle in the camera's image plane has the boundaries left = -2, right = 2, bottom = -1, and top = 1. Both the initial viewport and the initial view-rectangle have aspect ratio 2.0. We will refer to this initial view-rectangle as the reference view-rectangle. When the framebuffer's aspect ratio is greater than 2.0, you should crop the reference view-rectangle, so that its aspect ratio is the same as the framebuffer's, by moving reference view-rectangle's top edge down and its bottom edge up, but leaving its left and right edges at -2 and 2, respectively. The scrollbar value determines how much each of the top and bottom edges are moved.

A JScrollBar component represents a user adjustable value between 0 and 100. A JScrollBar generates AdjustmentEvents which must be handled by an AdjustmentListener object. The adjustmentValueChanged() method in the AdjustmentListener object can call the getValue() method from the AdjustmentEvent object to find out the current (user set) value of the scrollbar.

Since the scrollbar value is a number between 0 and 100, you can think of it as the percentage of the empty letterbox space that is above the viewport (when the program window's aspect ratio is less than 2.0) or you can think of the scrollbar value as the percentage of the reference view-rectangle that is being cropped off from the top (when the aspect ratio is greater than 2.0).

For a simple example that uses a JScrollBar, look at Watch_Adjustment.java from the sub-folder watch-events from JavaGuiEvents.zip.

As the user changes the program's window, the scene's image should always remain undistorted; the circle should always appear circular and never elliptical.

For the second part of this assignment you need to use transformation matrices on the P, N, and W models to create an animation.

In the renderer_9 zip file there is a sub folder "basic transformation examples" containing several examples using translations and rotations to create animations. Build the Javadoc for that folder and open the html sub-folder's index.html file using a web browser. The Javadocs combine each example's explanation with it animated gif file.

Also look at RotationExample.java from the clients_r9 sub-folder of renderer_9.zip.

Here is the most important thing to remember from those examples. If you want to rotate a model around the point (a, b, c) in the model's coordinate system, and you want this rotation to happen at the point (u, v, w) in the world coordinate system, then you would do that with the following three transformations (where position is the Position object holding the Model object).

      position.transform( Matrix.translate(u, v, w)
                  .times( Matrix.rotate_(theta) ) // choose any axis
                  .times( Matrix.translate(-a, -b, -c) ) );

In the demo program, the P model is rotating around the x-axis. The N model is rotating around the y-axis. The W model is rotating around both the y-axis and the z-axis. Quickly start and stop the animation many times so that you can figure out what the points (a, b, c) and (u, v, w) are for each model.

The two parts of this assignment are independent of each other. You can solve them in ether order.

For the first part, I think you should first get it working for windows that have an aspect ratio less than 2.0. That uses letterboxing without any cropping. So you only need to adjust the viewport. Then do windows with aspect ratio greater than 2.0. The viewport will be all of the framebuffer, but you will need to adjust the view-rectangle.

For the second part, the rotations of the three letters are independent of each other. So work on one letter at a time.

Turn in a zip file called CS455Hw4Surname.zip (where Surname is your last name) containing only your version of Hw4.java.

This assignment is due Friday, December 6.

Here are references to some relevant classes in the Java API.