Assignment 2: Fur Rendering - PUC-Riowebserver2.tecgraf.puc-rio.br/~andrei/docs/fur.pdf ·...

6
Assignment 2: Fur Rendering Andrei Alhadeff Monteiro Programming Massively Parallel Processors Introduction The second assignement focuses on developing shader for OpenGL programs. The shader language (GLSL) used in this work functions on OpenGL 2.1. This report briefly describes the implementation techniques used to develop a fur shader. The fur shader is based on a texture technique that mimics a mesh fur with “fake” inter-fur shadows. Wind simulation, not necessarily real-based physics, is also applied to the fur to see its movement. Apart from drawing any mesh with fur, this technique can also be used to simulate a grass-like appearance, like a soccer field as shown at the end of the report. Fur Technique The implementation of fur is based on textures. The fur is composed of several layers called shells that simulate the fins. The mesh in which the fur is applied is drawn N times, where N L is the number of layers of the fur. Therefore, if we have 20 layers, we must have 20 textures, one for each layer. The more external the layer is (that is, farthest from the mesh surface), the more transparent the fin is. To simulate inter- fur shadows, we use a “fake” implementation (non-based lighting) in which the fins are darker as we move closer to the mesh´s surface. When rendering the mesh, it has to be done N L times. For each render, we apply a different texture layer by binding the texture relative to the current layer, and blending the results. Depending on the layer, the vertices of the mesh are offset by a value dependent on the fur length in the vertex shader. To simulate physics on the fur, the fins are bended according to a cubic function relative to the vertex tangents. Wind forces are also applied on the fins by varying the fin direction relative to a spring harmonic equation. The triangle vertices are then interpolated to form the final results in the fragment shader. Implementation Texture construction The textures are alpha-based only, and the color will be of the fins will be based on a texture provided by the user. The textures simulate the fins in a way that each texel is a fin. If the user needs more or less fins, it is necessary to change the texture resolution or repeat it in a pattern. In order to build the textures, we must perform two steps. The first step is to make sure that not all then fins of the texture are the same size. See the figure below.

Transcript of Assignment 2: Fur Rendering - PUC-Riowebserver2.tecgraf.puc-rio.br/~andrei/docs/fur.pdf ·...

Assignment 2: Fur Rendering

Andrei Alhadeff Monteiro

Programming Massively Parallel Processors

Introduction

The second assignement focuses on

developing shader for OpenGL programs. The

shader language (GLSL) used in this work

functions on OpenGL 2.1. This report briefly

describes the implementation techniques used

to develop a fur shader. The fur shader is based

on a texture technique that mimics a mesh fur

with “fake” inter-fur shadows. Wind simulation,

not necessarily real-based physics, is also

applied to the fur to see its movement. Apart

from drawing any mesh with fur, this technique

can also be used to simulate a grass-like

appearance, like a soccer field as shown at the

end of the report.

Fur Technique

The implementation of fur is based on textures.

The fur is composed of several layers called

shells that simulate the fins. The mesh in which

the fur is applied is drawn N times, where NL is

the number of layers of the fur. Therefore, if

we have 20 layers, we must have 20 textures,

one for each layer. The more external the layer

is (that is, farthest from the mesh surface), the

more transparent the fin is. To simulate inter-

fur shadows, we use a “fake” implementation

(non-based lighting) in which the fins are darker

as we move closer to the mesh´s surface. When

rendering the mesh, it has to be done NL times.

For each render, we apply a different texture

layer by binding the texture relative to the

current layer, and blending the results.

Depending on the layer, the vertices of the

mesh are offset by a value dependent on the

fur length in the vertex shader. To simulate

physics on the fur, the fins are bended

according to a cubic function relative to the

vertex tangents. Wind forces are also applied

on the fins by varying the fin direction relative

to a spring harmonic equation. The triangle

vertices are then interpolated to form the final

results in the fragment shader.

Implementation

Texture construction

The textures are alpha-based only, and the

color will be of the fins will be based on a

texture provided by the user. The textures

simulate the fins in a way that each texel is a

fin. If the user needs more or less fins, it is

necessary to change the texture resolution or

repeat it in a pattern. In order to build the

textures, we must perform two steps. The first

step is to make sure that not all then fins of the

texture are the same size. See the figure below.

Figure 1 Fins in different texture layers are in

different sizes. Figure A shows a schematic

representation, and Figure B shows the layered

approximation.

In order to simulate different sizes, for

each texel, we generate a random value

from 0 to 1 meaning that it will be the

maximum length of the fin. Multiplying it

by the constant fur length will give the

maximum fur height. We set the alpha of

all fins to 1 until each fin´s maximum

length. Above this length, alphas will be 0,

or completely transparent.

The next step is to make sure that the fins

become more transparent as they reach

their maximum length. This is simply done

by linearly interpolating the alpha from 1

to 0 as the fin grows.

Figure 2 Final resulto of the texture.

Rendering

We must render the mesh NL times, each

time applying the texture relative to the

current layer in the mesh, blending the

results using the blend function

(SRC_ALPHA, ONE_MINUS_SRC_ALPHA).

In the vertex shader, we offset the vertices

with the amount value proportional to the

fur height relative to the current layer in

the direction of the vertex normal.

Figure 3 Vertices offset according to each layer

índex and fur length in the direction of the surface

normal.

We add an extra offset to “bend” the fins

like the realistic hairs by using a cubic

function (pow(layer, 3)). To add movement

to the fur, we add a vector in the direction

of the wind in the equation. The wind

vector varies like a harmonic movement,

using the equation:

�� = −��� cos���

��+= ������∆�

����+= ����∆�

The equation used to offset the vertices is

as follows:

� = ��

�´ = � + ���� + ����� ��

Where

� = ������� �� ��

� = ������� �����!

��� = �����! �"�#��

���� = �$�% %$����$"�

�� = ������� &�� ���'�ℎ

In the fragment shader, we fetch the alpha

from the current layered texture and fetch

the color from the texture provided by the

user. The color is modulated with the

diffuse component of the light (like

traditional lighting). However, as the fin is

closer to the mesh´s surface and farthest

from the last layer, the blacker it is. So

before applying lighting to the fin´s color,

we multiply the fin color by a value ranging

from 0 (closer to mesh surface) to 1

(further). To increase faster so as to

simulate inter-fur shadows, we use a

pow(1.0-alpha, 3) function.

Program Usage

The program dependencies are glut32.lib

(windows 32 bit), glew32.lib, opengl32.lib,

glu32.lib, and im.lib.

Additional include files are glut.h, glew.h,

and im.h.

To run the program, the file data paths for

the mesh and texture associated to the

model has to be passed as parameter. For

instance:

>> prog.exe “data/venus” “data/star.jpg”

The mouse is used to manipulate the

model. Also, keys are used to activate the

following modes in the rendering pipeline.

f/F: Decrease/Increase fur length

dynamically.

w: Pause/resume wind animation.

l: Pause/resume light animation.

q: Quit application.

Conclusion

This work made us possible to understand

the developing of a shader and allowe us to

develop a ‘fake’ (non-physically based)

animation and rendering of fur in arbitrary

meshes. We leave as future work the use

of a 3-dimensional texture to avoid binding

each texture each time we render a

different layer.

Reference:

[1] Papaioannou, G. “A Simple and Fast

Technique for Fur Rendering”, Technical

Report, University of Athens.

Figure 4 Soccer Field Grass using the fur application.