0. Intro
In this project, we will be doing a high quality real time rendering scene with OpenGL and GLSL shaders. While the executable will be for Windows only (due to development on Visual Studio), relevant source code and shader files can be used to run on other systems (provided that they have graphics cards supporting OpenGL 3.3 and GLSL 330 or newer).
The executable along with necessary dependencies can be downloaded in part 2 of this blog post, which is to be posted in 2 weeks. Detailed explanations and concepts regarding the final project expectations can be found at the bottom of this post.
The team members are: Hoang Tran and Nikhilesh Sankaranarayanan.
The team members are: Hoang Tran and Nikhilesh Sankaranarayanan.
A short list of rendering techniques and effects in this project include:
- Shadow mapping
- Extended to have soft anti-aliased shadows
- Environment mapping
- Extended to have scene and shadow reflections
- Ashikhmin-Shirley BRDF, with indirect lighting approximated and refined with the above mentioned rendering techniques
- With detailed explanations on the refinements that brought about the final images.
Usage of the program will be described in each section.
1. Shading model
There are two shading models in use in this project, which can be toggled with the "i" key.
The first shading model is the familiar Blinn-Phong shading model. With this model, we can render a variety of different materials, however the drawback is that everything looks like plastic, even with high values of shininess. Below are various samples of a model with material properties of gold:
Add-on shading model
The second shading model is a slightly modified version of the Ashikhmin-Shirley BRDF, which can be read in more detail here. With this shading model, metallic objects will actually look metallic, and the shape of the specular spots can be controlled, however since most object lighting in the Ashikhmin-Shirley BRDF is obtained from indirect lighting, the direct lighting calculation of this BRDF is added purely for aesthetic purposes. Since different specular and diffuse levels can be set, it is also possible with this BRDF model to render a purely diffuse object, however the issue of objects being too dark remains.In summary, the formulas used from the Ashikhmin BRDF paper are described as follows:
This is the formula used for specular lighting.
In this formula, k1 and k2 are the vectors from the illuminated point to the eye and to the light, respectively, h is the halfway vector (normalized k1 + k2), n is the normal, phi is the angle between h and a vector perpendicular to n (to be described in Section 2 of this post), nu and nv are controllers for the shape of the specular spot, manually defined in the code when predefining materials, and F is the Fresnel term. Because of the property of the halfway vector h, either k1 or k2 can be used for k.
The Fresnel term is approximated as the following.
Finally, the diffuse term is calculated as the following.
In this formula, Rd is the level of diffuse (0 being a purely specular object, 1 being a purely diffuse object). The strange factor of 28/(23pi) is, according to Ashikhmin, to ensure the conservation of energy.
This shading model is also added to show how the GLSL shaders can be used to perform calculations that would otherwise take minutes in the CPU (due to the complex formulas used). Below are various samples (direct lighting only) of the same model:
2. Environment mapping
Environment mapping is the process of approximating the appearance of a reflective surface using a texture image. In order to perform environment mapping, we must surround the entire environment with a texture image first, so that there is something to reflect onto the surface of the reflective object. The steps needed to do this are detailed in this section.
2.1.a Skybox
A skybox is essentially an infinitely large box used as the sky (background), as the name implies. Because the skybox is infinitely large, it is near impossible to notice any stitching in the edges of the box. Games in particular use skyboxes for infinitely far away scenery, which gives a good visual approximation of what the user should see at a very low cost, since models won't need to be drawn (as the user is instead shown a 2D approximation).
Below are various samples of the same model as section 1, this time with a skybox added.
2.1.b Minor add-on extension: Ashikhmin-Shirley indirect lighting approximation
Due to the impracticality of sending the entire scene data over to the shader (due to the complexity of models used), and due to the fact that the shader program is only run on pixels in which a fragment occupies, it is not really possible to do high quality ray tracing in the shader program itself. Consequently, approximations must be made for indirect lighting in the Ashikhmin-Shirley BRDF. The first such approximation shall be made using environment reflections.
To obtain a reflection, we must first determine the direction of reflection. This will be done simply with a Monte-Carlo method with hemispherical distribution. First a vector perpendicular u to the normal must be generated by crossing it with (1, 0, 0), or with (0, 1, 0) if the first result gives the zero vector. A second perpendicular vector v will be generated by crossing n and v to obtain a basis for a coordinate system. 2 pseudorandom numbers z1 and z2 are generated in the range [0...1], which then get mapped to [0... 2*pi] and [0... pi/2] through a careful combination of sin, cos, and arctan, in order to obtain a random ray contained in a hemisphere. This hemisphere is reoriented according to the coordinate system formed by u, n, v, as previously mentioned.
The incoming view vector k1 will be reflected about the above hemispherical distributed ray to obtain an outgoing vector out. The scene is redrawn from the perspective of the center of the model, and out will sample the textured skybox cube corresponding to the shifted perspective, in order to obtain a rough approximation of indirect lighting on the model.
Below are various samples showing different intensities indirect lighting added to the model, dependent upon the textures used on the skybox. Compare this to section 1, whereby the model is now much less dark, while the metallic property is still highlighted:
2.2 Environment mapping
As an outline on how reflections should be done have already been implemented in the Ashikhmin-Shirley BRDF shader, environment mapping now becomes very trivial. The scene simply needs to be redrawn from the perspective of the reflecting object in order to obtain an environment mapping.
Pressing "s" will make a floating reflective sphere appear/disappear in the scene.
Below are some sample images of an environment mapped sphere. Due to the complex shape of the bunny model, it is not redrawn & reflected.
3. Future improvements
As can be observed regardless of shading model, the model is brightly lit in unreasonable places, due to the lack of occlusion testing. Shadow mapping will be added in the next part to improve the quality of the model.
No comments:
Post a Comment