This assignment makes use of the files contained in this zip file and the files from the Java renderer (ver 4). This assignment is due Friday, May 9.
For this assignment you will write two "fragment shader" pipeline stages.
We did some example fragment shaders in class, 6. Fragment Shaders.zip. You shoukld study those examples as you work on this assignment.
Your first shader should implement "Phong Shading". Phong shading is a lighting algorithm that works on fragments instead of on vertices. The idea for Phong shading is to do the exact same lighting calculation we did in pipeline stage P4_LightingSmooth.java
, but do the calculation at every fragment instead of (only) at every vertex. In other words, do the Phong Lighting Model calculation at the center point of every fragment (instead of at each vertex).
Since Phong shading does the lighting calculation at every fragment, and there are a lot more fragments than there are vertices, Phong shading is a more computationally intensive than the regular Phong lighting model (early versions of OpenGL could not do Phong shading and older OpenGL graphics cards cannot implement it). But Phong shading is easy to describe, and easy to implement as a fragment shader.
We make three changes to the renderer to convert from Phong (smooth) lighting to Phong shading, First, delete the lighting stage P4_LightingSmooth.java
, since we will no longer need lighting information at the vertices. Second, create a new stage, PhongShader.java
that goes after the rasterizer stage. The PhongShader.java
stage will do the lighting calculation at every fragment. Third, in the actual lighting calculation we make just one small change. A lighting calculation is done at a point that has a normal vector attached to it. In the original Phong lighting model, the point was always a vertex. In Phong shading, the point where we do the lighting calculation is a fragment's center point. We use the fragment's barycentric coordinates to interpolate its center from the triangle's three vertices. And we use the fragment's barycentric coordinates to interpolate the normal vectors from the three vertices to the fragment's center. Once we have interpolated a point p and a normal vector n for a fragment, then we do the standard red, green and blue lighting calculations (using the view vector, the light vector, the material properties, the light properties, etc). We use the result of the three lighting calculations to determine the fragment's color.
In the zip file there is a sketch of PhongShader.java
. You need to complete this file. Also in the zip file there is a completed client for this shader, PhongShaderClient.java
. You can see the images produced by this client by running the demo program in the sub folder Flat vs Smooth vs Phong
. Also in the sub folder are demos of flat and smooth lighting, so you can see how Phong shading improves on Phong lighting (which improves on flat lighting).
For the third part of this assignment, try to come up with a fragment shader idea and implement it. Provide a client program that demonstrates your shader. Call your shader and its client MyShader.java
and MyShaderClient.java
.
Turn in a zip file called CS590Hw3Surname.zip
(where Surname
is your last name) containing your versions of PhongShader.java
and MyShader.java
, MyShaderClient.java
.
This assignment is due Friday, May 9.