Department of Computer Science Worcester Polytechnic Institute |
---|
Objective: | In this assignment, you will learn how to create GPU shaders by implementing a simple shader using GLSL. |
---|---|
Assignment: |
Create a single shader effect and run it on a programmable GPU. The shader will exhibit a specular reflection model called the Banks Anisotropic Reflection (explained below). You will write/modify two shader files and run them on a provided GLSL Demo application. To make things easier, the GLSL Demo application provides numerous examples of other GLSL shaders. You can use the provided example shaders to help you understand how to write your own shaders for the assignment. The assignment consists of the following steps:
|
Background: |
Anisotropic Specular Highlights
Anisotropic Reflections are just like regular reflections, except stretched or blurred based on the orientation of small grooves (bumps, fibers or scratches) that exist on a reflective surface. Anisotropic specular highlights can be seen on objects with very small grooves, for example in brushed metal, old LP records, silk and even hair (as a whole, not per strand). Below are two rendered examples of brushed or grooved metal that exhibit anisotropic reflection.
In the graphics literature, there are several formulas for modeling anisotropic reflection. One in particular is called the Banks model. As you might suspect, modeling these types of specular reflections can be complicated. In the above examples, a different coordinate frame, called Tangent Space, is used to render these reflection functions. Tangent space consists of three unit vectors oriented according to the surface; the first you are already familiar with is the normal N. The other two are called the Tangent T and Bi-normal B (sometimes called Bi-tangent). The three vectors are orthogonal and form a coordinate system based on the surface of the object (per vertex). Below is a diagram showing a different tangent space for each vertex of a triangle.
The formula we've implemented for this assignment is a simplified version of the Banks formula. The modified formula is similar to the Phong reflection model, in that there is an ambient, diffuse, and specular component. The diffuse and specular components themselves differ significantly from Phong while the ambient constant remains the same. The major difference is the use of the tangent vector T within the formulas. This indicates that the Banks reflection function has a relationship to how the surface is changing, similar to that of the normal N. Once the tangent space vectors are determined (OpenGL gives you N and gives you hints on how to get T & B), but before you can use them, they need to be in the same space as the light vector L and the view vector V. You can do this by transforming them to the appropriate space or coordinate system just as you would the normal N.
The kd and ks are the same material values specified in the Phong Illumination model. Just as in the Phong model, there is an exponent on the specular component q. The difference here is that there is also an exponent on the diffuse term p as well. The exponent p helps to keep the shading from getting too bright (like a canceling effect). Generally p will range from [2.0 - 5.0]. The exponent q provides a way to change the size of the specular highlight and has the same range of values as the Phong model. In the shader you write for the assignment, ambient, diffuse, and specular components have all been pre-rendered and stored in a texture. To give you a hint, all you need to do is look up the total reflection value from the provided texture map in your Fragment Shader.
|
Writing Your Shaders: |
The very first thing you need to do is determine if your video card can support GPU
programming. If you know your card's make and model, look up the specs here
(http://www.delphi3d.net/hardware/listreports.php).
Specifically, you are looking for support for the following two hardware extensions ARB_vertex_shader &
ARB_fragment_shader. If your video card has these two extensions listed then your
video card should have the minimal capabilities you need. If you need a program to determine
your card's capabilities for you, try the Glinfo application located here:
(http://www.delphi3d.net/hardware/glinfo2.zip). This application will give a list of
extensions your card supports. Again we are interested in ARB_vertex_shader & ARB_fragment_shader.
The shaders you are going to be writing are just small text files resembling C/C++ code in the form of a function (called main). The files, once written, can be loaded into the pre-compiled application that will render them on the GPU. The application, called GLSLdemo, is capable of handling all the setup details you would normally have to do when writing your own C/C++ application for GPU rendering. All that you are required to do is write a single Vertex and Fragment shader for the previously mentioned effects (Banks Anisotropic Specular = 2 shader files + Modify 1 XML file). The rendering application that will be used to run your shaders is located here GLSLDemo.zip. Unzip the application to a computer with a programmable GPU. The executable file is located in the "bin" directory of the demo application. Also, there are example shader files for you to look at within the demo application located in the "shaders" directory. There are two sub-directories under the main shaders directory (default & OrangeBook). Both directories contain examples from the GLSL Orange book. There are two configuration XML files located in the "shaders" directory. This file tells the application which shaders to load into the application and how to configure the parameters for the shaders (uniform, varying, and texture variables). You need to create an entry in one of these files for your shaders called banks. The name of the entries in the configuration file has to match the filename of your shaders. We suggest using (banks.vert, banks.frag). For the assignment, we have pre-processed the Banks reflection model and stored it in two texture files with different values for the parameter q (rainbow.jpg, rainbow2.jpg) for you to look up within your shader. Due to a limitation in the rendering application, the only textures the application will accept are those that have been provided with the original application. Therefore, you must replace an existing texture with one that has the same name (that is why the files I provided are named rainbow.jpg). Your shader should deal with vectors in World and Tangent space, which can be tricky to deal with. To aid in dealing with the Tangent space, an example shader provided with the application and demonstrated in class uses the tangent space as well. So you may use this other examples as a guide for writing the shaders for your assignment. When you have finished, your objects should display specular reflections similar to the images below. The Banks reflection model depends on two variables, the light vector & view vector. These vectors need to be transformed into Tangent space. The application will provide the Tangent, Bi-Tangent (Bi-normal), and Normal vectors for you per vertex as long as the configuration files are setup properly (again look at the provided examples for how to do this). Once the light vector & view vector are in Tangent space you can use these vectors as the texture coordinates for looking up into the provided texture map.
Since we only need two values for texture coordinates, you will use L'.x & V'.x. Keep in mind that these are vectors and when normalized their components will range from [-1, 1], but the texture coordinates are only valid from [0,1]. Also, before calculating L' and V', make sure everything is in the same coordinate system. For our implementation we made sure everything was in object space before converting to tangent space. We believe this will work equally well in other spaces like eye-space. One last detail regarding the code, the authors of the book Real-time Shading suggest multiplying the value returned from the texture look up with a cos α not shown in the derivation. Essentially, this boils down to multiplying the returned value by L'.x. Try it and see if you get better results (closer to the pictures in this document). The values I used for the exponents when producing the texture maps make the specular highlights thin and dominate the reflection during rendering. Notice the long and narrow specular highlights in the rendered examples above. As q grows the highlight become thinner simulating a quick drop off with high exponent values (similar to Phong).
|
Debugging: | The application has two facilities for debugging your shader. The first is the "Information Log" located under the pull-down menu "View". The log will give you feedback regarding your shaders after you click on them (which causes the application to compile and run your shaders). The most important feedback the log will provide are the errors your shaders are producing (including line numbers, error code, and a guess at the problem). The second form of feedback the application will give you is that the model you are trying to render with your shader, if there are errors in you shaders, will be covered in a striped line pattern, and/or your shader will be highlighted in red on the side menu. |
Alternatives for Running GLSLDemo: |
GLSLDemo is a Win32-only application. Although the source code is provided on the
authors Website, we cannot guarantee it will work on other platforms. If you do not have
a Windows computer that will run the application, the machines in the CCC should
work. Also, last year we had the machines in the EMechanical ngineering Department available to use
in the Higgins Labs building, RM 230. These machines are used to run the various
graphics related applications like AutoCAD and will run the GLSLDemo application.
Fortunately, the GLSLDemo application provides the Tangent space vectors for us, which are not easy to calculate. If you decide to develop GLSL shaders for the assignment on another platform or application, that application may not provide the Tangent space vectors. This would make the assignment very difficult to finish, as the effect we are rendering requires Tangent space vectors. As an alternative, you might be able to connect to a Win32 computer using remote desktop and develop the application there. If this is required, email Cliff (clindsay [at] wpi.edu) if you have problems and we may be able to work out a solution. |
Documentation: | Please comment your shaders as best you can, and provide a README of any information we might need to understand your work. |
What to Turn in: |
Submit everything you need to run your shaders.
You will need to submit three program files, at least two screen shots (large enough
to see the reflections please), and your README. Your files should be
free of compiling errors and bugs. Please be sure to test the file prior to emailing them.
The command to ZIP everything, assuming your code is in a directory "proj5", is: zip -r FirstName_LastName_proj5.zip proj5 To submit your work, email your ZIP file to Rob.
|
Academic Honesty: |
Remember the policy on Academic Honesty: You may discuss the assignment with others, but you are to do your own work. The official WPI statement for Academic Honesty can be accessed HERE. |