The goal of this assignment is to develop a polygon rendering function that uses a "scan line" algorithm to calculate and display the pixels that are within the polygon.
Assume that you are given a convex polygon with N points. Each point has an (x,y) coordinate and an (r,g,b) color. Your task is to walk the boundary of the polygon to fill in a scan line data structure, and then process each of these scan lines to calculate the appropriate (r,g,b) values for all pixels within the polygon.
Instead of writing (r,g,b) pixel values to the OpenGL display buffer one pixel at a time, it is much faster to create a 2D array of unsigned bytes to store your (r,g,b) pixel values, and then call glDrawPixels to display this buffer on the screen. See the sample program "render.cpp" for an example of this approach.
Bonus 10 points: Extend your program above to perform Phong shading using a scan line approach. In this case, assume that each point on the polygon has an (x,y) coordinate and a surface normal (Nx,Ny,Nz) that must be interpolated along the polygon boundary and along scan lines to obtain the surface normal for every point in the polygon.
The other parameters needed for the Phong shading model are: the unit length viewing direction (Vx,Vy,Vz), the unit length light source direction (Lx,Ly,Lz), the light source color (Lr,Lg,Lb), the polygon material color (Mr,Mg,Mb), and the Phong model coefficients (Ka,Kd,Ks,p).
The weighting term for the diffuse component is calculated as Kd(Lx,Ly,Lz)*(Nx,Ny,Nz) where * denotes dot product. The weighting term for the specular component is calculated as Ks(Rx,Ry,Rz)*(Vx,Vy,Vz)^p where the reflectance direction (Rx,Ry,Rz) = 2[(Lx,Ly,Lz)*(Nx,Ny,Nz)](Nx,Ny,Nz)-(Lx,Ly,Lz). See the wikipedia pages for "phong shading" and "phong reflection model" for additional details.
Since we are restricting ourselves to convex polygons, we know in advance that the polygon can intersect each scan line at most two times. Hence we do not need an array of linked lists to implement scan line rendering. Instead an array of two (x,y,r,g,b) records will suffice. In fact, we do not need to store the y value since the scan line array is indexed by y in the first place. To simplify processing, it is often helpful to sort these two records by their x value.
Since each point on the polygon has its own (r,g,b) color, you must interpolate colors as you walk the boundary of the polygon, and also when you process each scan line. Linear interpolation based on distance from the endpoints is the easiest option. To verify your code is working, you should create test polygons with distinct colors at each point, so you can visually see if the interpolation is working properly. For example, a triangle with colors (255,0,0), (0,255,0) and (0,0,255) should produce a nice range of intermediate colors.
You can implement this program using either a bottom-up approach or a top-down approach. If you go for a bottom-up approach, start by creating basic methods and classes, and test theses methods using a simple main program that calls each method. When this is working, you can create the main program that uses these methods to solve the problem above.
If you go for a top-down approach, start by creating your main program that reads user input, and calls empty methods to pretend to solve the problem. Then add in the code for these methods one at a time. This way, you will get an idea of how the whole program will work before you dive into the details of implementing each method and class.
Regardless of which technique you choose to use, you should develop your code incrementally adding code, compiling, debugging, a little bit at a time. This way, you always have a program that "does something" even if it is not complete.
When you think you are about 1/2 way through the program, upload a copy of your source code and your program output at that point. Be sure to hand in something that compiles even if it does not do much when it runs.
Test your program to check that it operates correctly for all of the requirements listed above. Also check for the error handling capabilities of the code. Try your program on 2-3 input documents, and save your testing output in text files for submission on the program due date.
When you have completed your C++ program, write a short report (less than one page long) describing what the objectives were, what you did, and the status of the program. Does it work properly for all test cases? Are there any known problems? Save this report in a separate text file to be submitted electronically.
In this class, we will be using electronic project submission to make sure that all students hand their programming projects and labs on time, and to perform automatic analysis of all programs that are submitted. When you have completed the tasks above go to the class web site to "submit" your documentation, C++ program, and testing files.
The dates on your electronic submission will be used to verify that you met the due date above. All late projects will receive reduced credit (50% off if less than 24 hours late, no credit if more than 24 hours late), so hand in your best effort on the due date.
You should also PRINT a copy of these files and hand them into your teaching assistant in your next lab. Include a title page which has your name and uaid, and attach your hand written design notes from above.