Dienstag, 18. November 2014

Generating a Single Cloud


As I described in my previous post, I set out to procedurally generate a sky that reflects the current weather conditions over Stockholm. At first, it was hard to grasp the project to the whole extent. For that reason and to become accustomed to OpenGL in general, I started off with basic sphere as can be seen in Figure 1. Afterwards, the surface was adapted to obtain a realistic-looking shape with irregularities. Finally, the shape is colored and blended.
 

The cloud starts as a sphere

The first step towards a complex cloud shape is a geometrical primitive – a sphere. To construct a sphere from a given number of rings and sectors, sphere coordinates were used. Additionally, each four adjacent vertices are connected to two triangles which are then rendered to the screen by OpenGL. The more vertices the sphere includes the smoother is the outline. The sphere shown in Figure 1 has 100 rings and 100 sectors.

Figure 1 A sphere decomposed into triangles

Adding noise

In order to distort the primitive shape, some variation has to be applied to the surface. The basic idea is to apply a displacement factor D thus:
Pnew = P + PC * D
Where P is the original vertex position on the outline of the sphere, PC is the vector from the center of the sphere to P and Pnew is the resulting displaced vector. Different approaches that determine the displacement will have different impacts on the shape. To get a feeling for the implications of the displacement, a random value was assigned to the displacement factor D. Figure 2 shows a sphere with randomly displaced vertices. Obviously, this technique does not yield satisfying results. The surface is very edgy and the impact of single extreme displacements diminishes with the number of total vertices. Roughly speaking, the high number of vertices that is needed for a smoothly curved sphere counterbalances the randomness.

Figure 2 Distorted surface with random noise

A noise function ought to achieve better results because it regulates the displacement factor in a more controlled manner. Moreover, another function is incorporated that creates a turbulence-like approximation as described by G. Taxén in Cloud Modeling for Computer Graphics:
float Turbulence (vector p) {
   t = 0;
   scale = 1;
   while (scale > pixelsize) {
      t += abs (Noise (p / scale) * scale);
      scale /= 2;
   }
   return t;
}

Together the noise and the turbulence function create a more sophisticated displacement factor D such D = turbulence + noise. The turbulence function takes a 4-dimensional input consisting of the vertex normal and a time variable to achieve animated fluctuations over time. The noise function comes from the implementation of Perlin Noise 4D available at GitHub (https://github.com/ashima/webgl-noise). The improved result can be seen in Figure 4 (whereas Figure 3 shows a test run with Perlin Noise 3D which generated slightly worse displacements).

Figure 3 Distorted surface with 3D Perlin Noise and turbulence

Figure 4 Distorted surface with 4D Perlin Noise and turbulence

Finally, the parameters of both functions were tweaked in many weary tests to achieve a cloud-like shape (Figure 7 displays a snapshot from the final cloud including the adapted parameters).

Fading the edges

So far, the surface of the cloud has begun to look realistic with the irregular turbulence. Problematic is that authentic clouds are to some extent translucent which the current model does not simulate. It is not sufficient to simply apply a transparency value to the whole shape but instead the outer edges can be faded out towards the visible background. To create the illusion of fading edges two imaginary layers are created orthogonal to the view direction of the camera. The normal of the created planes are parallel to the view direction and point towards the camera. Figure 5 shows a schematic depiction of how to select vertices for fading. The distance to the plane of each point on the sphere can be calculated from the plane equations using the normals. Each vertex on the sphere that is between the camera and the dashed plane is visible, each vertex between the planes will be faded and the rest is invisible.

Figure 5 Fading schema with two planes orthogonal to the camera that determine the visibility of vertices

A percentage value L that describes the location of a vertex between the planes can be derived from both of the previously calculated distances. These percentages are mapped to alpha values on a scale from zero to one for blending. Therefore, a simple linear blending function b(x) = x was used but did not result in a natural appearance of the cloud due to the curved shape. Instead, the fading was enhanced with a composed cubical blending function:
   b(x) = 1 - 2 * p^2  if x < 0.5
or b(x) = 2 * p^2 - 4 * p + 2 otherwise
(Mind that this function is continuous and differentiable in the considered range.)
Figure 6 shows the obtained results on a greyscale where 1 is white and 0 is black. The final version in Figure 7 shows the fading effect in colored version.

Figure 6 Cubical fading for the outer vertices of the cloud (the higher the determined transparency the darker the area)

Adding color

To colorize the cloud, two different approaches where chosen. The top of the cloud ought to be a little brighter than the bottom so depending on the vertical position of a fragment the color is interpolated between the two defaults which were picked from a picture of a real cloud. Additionally, the color is based on the noise that transformed a specific realm of the shape. Therefore, the noise value between zero and one was incorporated into the process. Figure 7 and Figure 8 show the resulting cloud at different times.

Figure 7 Final cloud including turbulence, fading and color

Figure 8 Final cloud including turbulence, fading and color (this image was captured slightly after Figure 7 showing how the cloud transforms over time)

The cloud evolved from a geometric primitive to a somewhat realistic model. In the next step, several basic cloud models are combined to obtain more complex structures.

References

  • Cloud Modeling for Computer Graphics, G. Taxén (1999)
  • Realistic and Fast Cloud Rendering, Niniane Wang (2003)

Keine Kommentare:

Kommentar veröffentlichen