In this project, a program that can load, and render OBJ files with precomputed ambient occlusion was developed.

During the development OpenGL 3.3 and GLSL are used. For loading OBJ files, and rendering, OpenGL Development Cookbook, and Braynzar Soft, Lesson 21: Direct3D 11 Loading Static 3D Models (.obj format) were referenced. Models were taken from Principia Mathematica, Inc.

Loading OBJ File

General information, and algorithm for parsing OBJ files can be found in this page.

For rendering the models, interleaved buffer is used. Vertex, normal, UV coordinates, and AO values are kept, and sent to the GPU together. Additionally, before the values are put together, vertices are sorted with respect to their material for increasing the GPU performance.

Rendering and Light Calculation

Rendering process starts by reading models, and materials from the related files. After the whole necessary files are read, the textures related with materials are generated.

In the vertex shader, conversion from object space to eye/camera space is done. Also UV and AO information is sent to the fragment shader. In the fragment shader, color calculation is done. A movable point light is used as light source.

Averaging the Normals

If two faces reference the same vertex, the last face may overwrite the normal for that vertex. Thus, the look of the object can be different depending on the face order. By averaging the normals, this problem is solved.

The easiest way for averaging the normals is done in a straight forward way. Whole normals related with the current vertex are summed, and divided with the length of the whole normals. However, averaging the normals with respect to their area (as a weight) will produce better results.

If the normals are normalized before this operation, total number of the summed normals can be used directly for the division instead of calculating the length. For example, for 3 normalized vector, the formula turns into

For finding the normals that will be summed, we need to find whether the vertex is shared with other faces or not. For this task, a new data structure that keeps vertex, and related normal indices can be used. After we have this vertex, we can calculate averaged normals by looping through this data structure.

A more efficient way for finding adjacent vertices will be investigate in the future.

Ambient Occlusion

AO  gives perceptual clues to depth, curvature and spatial proximity. It is simply a simulation of the shadowing caused by objects blocking the ambient light. Because ambient light is environmental, ambient occlusion does not depend on light direction, so it can be precomputed for static objects.

In traditional Ray Tracing AO, ambient occlusion is simulated by sampling rays from a certain point, which takes a shape of a hemisphere, and then the rays are checked for intersection with the scene. The percentage of the rays that do no hit any geometry (at a distance r ≤ R) is returned as occlusion factor.

For example, if we are using cosine-weighted hemisphere sampling, and launch 100 rays, and 60 of them do not hit the other polygons of the objects/models, then the occlusion factor will be 60/100 = 0.6.

Result of Raytracing AO, which is preprocessed for static objects, can be stored as occlusion (texture) map or in any other formats that we need. This result can be used to compute a fast approximation to diffuse shading in the environment at runtime.

AO was originally developed by Hayden Landis (2002) and colleagues at Industrial Light & Magic.

Precomputing AO

In this project, I used per vertex, per triangle calculation. The rays are shot from each vertex of the model(s) in the direction of cosine-weighted hemisphere. Occlusion factor is saved for each vertex of the model(s).

AO is calculated by solving the integral below over the hemisphere:

AO Integral

Figure-1: AO Integral where V is the visibility of point x with direction w, and nDOTw is the angle between the direction, and the normal at point x. PI is used for normalizing the result.

For approximating AO integral, I used Monte Carlo estimation (Figure-2). f(x) is chosen as AO function (Figure-3). For sampling the direction, PDF was chosen as cosine weighted hemisphere sampling (Figure-4), and the final equations has become as Figure-5:

Figure-2: Monte Carlo Estimator. N is the sample size. f(x) is function of the integral that will be estimated. pdf(x) is used for distributing the points (Sampling).

Figure-2: Monte Carlo Estimator. N is the sample size. f(x) is function of the integral that will be estimated. pdf(x) is used for distributing the points (Sampling).

Figure-3: f(x) (Ambient Occlusion)

Figure-3: f(x) (Ambient Occlusion)

Figure-4: pdf(x) is chosen as cosine-weighted hemisphere. Cosine-weighted hemisphere generates directions with respect to the normal. PI is used for normalizing.

Figure-4: pdf(x) is chosen as cosine-weighted hemisphere. Cosine-weighted hemisphere generates directions with respect to the normal. PI is used for normalizing.

Figure-5: Final MC for AO. After simplifying the f(x) with pdf(x), the estimator turns into picking directions, and tracing them for visibility.

Figure-5: Final MC for AO. After simplifying the f(x) with pdf(x), the estimator turns into picking directions, and tracing them for visibility.

Algorithm

  1. Get the point x
  2. Sample the direction
  3. Convert the direction into the world space
  4. Determine the length of the array
  5. Create the ray by using the point x, and the direction in world space
  6. Shoot the ray, and check for visibility. If it is visible, increase the AO factor

In the project, for calculating AO, for each vertex of each triangle, a direction is chosen, visibility of the ray in this direction at the vertex is traced, occlusion factor is calculated, and the results are saved into a file with a relation between the vertices.

For the next rendering, before calculating AO, first this file is checked. If the file exists, the values are read from it.

Generating Directions and Converting Them into the World Space

The code below is used for sampling a direction cosine-weighted hemisphere.

For converting the direction into the world space, tangent, and bitangent vectors are need. For calculating the tangent vectors, a new vector which is orthogonal to the current normal is generated. Then, the other tangent vector is generated by cross product of the normal, and new vector.

The code below referenced from Nori renderer can be seen for this operation:

Ray-Triangle Intersection

For ray-triangle intersection, Möller–Trumbore intersection algorithm is used. This algorithm is simple, and fast because of not precomputing the plane equation.

The algorithm first check whether the ray is parallel to the triangle or not. This test is followed by checking whether the intersection lies outside of the triangle or not.

If the ray passes all these tests, the intersection point is calculated and returned.

Results

Resultant images for can be seen below. As expected, the results with AO give better result. The quality can be increased by subdividing the triangles.

Figure-6: Cruiser model without AO

Figure-6: Cruiser model without AO

Figure-7: Cruiser with AO

Figure-7: Cruiser model with AO

Figure-8: Cruiser without AO

Figure-8: Cruiser model without AO (Top view)

Figure-9: Cruiser with AO

Figure-9: Cruiser model with AO (Top view)

Figure-11: Only ambient occlusion

Figure-10: Ambient occlusion for cruiser model

Figure-10: Only ambient occlusion

Figure-11: Ambient occlusion for cruiser model

References