# Learn OpenGL. Lesson 6.1. PBR or Physically-correct rendering

## Physically correct rendering

3r31295. PBR, or physically-correct rendering (physically-based rendering), is a set of visualization techniques based on a theory that agrees fairly well with the real theory of light propagation. Since the goal of PBR is a physically accurate imitation of light, it looks much more realistic than the Phong and Blinna-Phong lighting models we used earlier. It not only looks better, but also gives a good approximation to real physics, which allows us (and, in particular, artists) to create materials based on the physical properties of surfaces, without resorting to cheap tricks to make the lighting look realistic. The main advantage of this approach is that the materials we create will look as intended regardless of the lighting conditions, which is not the case with other non-PBR approaches. 3r31296.

Hello Triangle 3r31338.

3r350. Shaders

3r31338.

3r3355. Textures

3r31338.

3r360. Transformations

3r31338.

3r3365. Coordinate systems 3r31337. 3r31338.

Camera 3r31337. 3r31338.

Part 2. Basic lighting 3r31301.

3r3384. Colors 3r31338.

3r33575. The Basics of Lighting 3r31337. 3r31338.

3r3394. Materials

3r31338.

3r399. Texture maps

3r31338.

3r3104. Light sources

3r31338.

Multiple light sources 3r31337. 3r31338.

Part 3. Downloading 3D models

3r3128. Assimp library 3r31338.

3r3128. Mesh mesh class is

3r31338.

Class of the 3D model 3r31338.

Part 4. Advanced features of OpenGL

3r3147. Depth test 3r313337. 3r31338.

3r3152. Test stencil

3r31338.

Mixing colors 3r31337. 3r31338.

3r3162. Trimming faces 3r31338.

3r3303. Frame buffer

3r31338.

3r3305. Cube maps 3r31337. 3r31338.

3r33177. Advanced work with data

3r31338.

3r3182. Advanced GLSL

3r31338.

3r3187. Geometric shader

3r31338.

3r3192. Instancing

3r31338.

3r3197. Smoothing 3r31337. 3r31338.

Part 5. Advanced lighting 3r31301.

3r33333. Advanced lighting. Model Blinna-Phong. 3r31337. 3r31338.

3r3307. Gamma Correction

3r31338.

Shadow maps 3r31337. 3r31338.

Omnidirectional Shadow Maps 3r31338.

3r31272. Normal Mapping

3r31338.

Parallax Mapping 3r31338.

3r3309. HDR

3r31338.

Bloom 3r31337. 3r31338.

Postponed rendering 3r31338.

SSAO 3r31337. 3r31338.

Part 6. PBR

Theory

3r31295. However, PBR still remains an approximation of reality (based on the laws of physics), so it is called physical-correct rendering, not physical rendering. In order for a lighting model to be called physically correct, it must meet 3m conditions (don't worry, we'll get to them soon): 3r3131301.

Be based on the

reflective micro-facet model.

Obey the law of energy conservation

Use the two-beam reflectance function (eng. Bidirectional reflectance distribution function - BRDF) 3r31313.

In this series of tutorials, we will focus on the PBR approach, originally developed at Disney and adapted for real-time visualization by Epic Games. Their approach is based on

*metal-dielectric workflow 3r31131. (English metallic workflow, 3r31130. I didn’t find a better translation - approx. ed. 3r31131.), well documented, widely accepted in many popular engines and looks amazing. At the end of this section, we get something like this:*

3r31258.

3r31295. Keep in mind that the articles in this section are quite advanced, so it is recommended to have a good understanding of OpenGL and shader lighting. Here are some of the knowledge you will need to study this section:

frame buffer 3r31337. , 3r3305. cubic cards

3r31258.

3r31295. Keep in mind that the articles in this section are quite advanced, so it is recommended to have a good understanding of OpenGL and shader lighting. Here are some of the knowledge you will need to study this section:

frame buffer 3r31337. , 3r3305. cubic cards

*, 3r3307. gamma correction*

, 3r3309. HDR

and 3r31272.

normal maps. . We also delve a bit into mathematics, but I promise to do everything possible to explain everything as clearly as possible. 3r31296.

3r31246. Model of reflective microgrone

3r31295. All PBR techniques are based on the theory of microfaces. This theory says that each surface with a strong magnification can be represented as a set of microscopic mirrors, called 3r31290. microfaces [/b] . Due to the surface roughness of these micro-mirrors can be oriented in different directions: 3r31296.

3r31258.

3r31295. The more rough the surface, the more chaotically oriented its microgranes. The result of this arrangement of these small mirrors is (in particular when it comes to specular highlights and reflections) that the incident rays of light are scattered in different directions on rough surfaces, which leads to a broader specular reflectance. And vice versa: on smooth surfaces, the incident rays are more likely to reflect in one direction, which will give a smaller and sharper highlight:

3r31258. 3r33336.

3r31295. At the microscopic level, there are no absolutely smooth surfaces, but given the fact that the micro-edges are small enough and we cannot distinguish between them within our pixel space, we statistically approximate the surface roughness by introducing the coefficient 3r31290. roughness [/b] . Using this coefficient, we can calculate the fraction of micro-faces oriented in the direction of some vector 3r31236. 3r31238. . This vector is

3r31238. nothing more than a median vector lying in the middle between the direction of the incident light

3r33358. 3r31238. and the direction of the observer 3r31236. 3r33333. 3r31238. . We talked about it earlier, in a lesson dedicated to 3r33355. advanced lighting r3r31337. where defined it as the ratio of the sum of the vectors

3r33358. 3r31238. and 3r31236. 3r33333. 3r31238. to the length of the resulting vector:

3r31295. 3r31296. 3r31295. 3r31236. 3r31238. 3r31296.

3r31295. The more microfaces are oriented in the direction of the median vector, the sharper and more bright the specular highlight will be. Thanks to the roughness coefficient, which lies between 0 and ? we can statistically approximate the orientation of the micro-facets: 3r31296.

3r31258. 3r33939.

3r31295. As can be seen, a higher value of the roughness coefficient gives a larger specular highlight stain, compared with a small and sharp spot on smooth surfaces. 3r31296.

3r31246. Energy saving. 3r31247.

3r31295. The use of the approximation taking into account the microface already carries some form of energy conservation: the energy of the reflected light will never exceed the energy of the incident (if the surface does not glow by itself). Looking at the image above, we see that with an increase in the surface roughness, the spot of the reflected light increases, but at the same time its brightness decreases. If the intensity of the reflected light were the same for all pixels, regardless of the size of the spot, then rougher surfaces would emit much more energy, which would violate the law of conservation of energy. Therefore, specular reflections are brighter on smooth surfaces and dim on rough ones. 3r31296.

3r31295. In order for energy to be conserved, we must make a clear separation between the diffuse and specular components. The moment the light beam reaches the surface, it is divided into 3r31130. reflected 3r31131. and 3r31130. refractedcomponents. The reflected component is light that is directly reflected and does not penetrate the surface; we know it as a specular component of light. The refracted component is a light that penetrates into the surface and is absorbed by it — it is known to us as the diffuse component of light. 3r31296.

, 3r3309. HDR

and 3r31272.

normal maps. . We also delve a bit into mathematics, but I promise to do everything possible to explain everything as clearly as possible. 3r31296.

3r31246. Model of reflective microgrone

3r31295. All PBR techniques are based on the theory of microfaces. This theory says that each surface with a strong magnification can be represented as a set of microscopic mirrors, called 3r31290. microfaces [/b] . Due to the surface roughness of these micro-mirrors can be oriented in different directions: 3r31296.

3r31258.

3r31295. The more rough the surface, the more chaotically oriented its microgranes. The result of this arrangement of these small mirrors is (in particular when it comes to specular highlights and reflections) that the incident rays of light are scattered in different directions on rough surfaces, which leads to a broader specular reflectance. And vice versa: on smooth surfaces, the incident rays are more likely to reflect in one direction, which will give a smaller and sharper highlight:

3r31258. 3r33336.

3r31295. At the microscopic level, there are no absolutely smooth surfaces, but given the fact that the micro-edges are small enough and we cannot distinguish between them within our pixel space, we statistically approximate the surface roughness by introducing the coefficient 3r31290. roughness [/b] . Using this coefficient, we can calculate the fraction of micro-faces oriented in the direction of some vector 3r31236. 3r31238. . This vector is

3r31238. nothing more than a median vector lying in the middle between the direction of the incident light

3r33358. 3r31238. and the direction of the observer 3r31236. 3r33333. 3r31238. . We talked about it earlier, in a lesson dedicated to 3r33355. advanced lighting r3r31337. where defined it as the ratio of the sum of the vectors

3r33358. 3r31238. and 3r31236. 3r33333. 3r31238. to the length of the resulting vector:

3r31295. 3r31296. 3r31295. 3r31236. 3r31238. 3r31296.

3r31295. The more microfaces are oriented in the direction of the median vector, the sharper and more bright the specular highlight will be. Thanks to the roughness coefficient, which lies between 0 and ? we can statistically approximate the orientation of the micro-facets: 3r31296.

3r31258. 3r33939.

3r31295. As can be seen, a higher value of the roughness coefficient gives a larger specular highlight stain, compared with a small and sharp spot on smooth surfaces. 3r31296.

3r31246. Energy saving. 3r31247.

3r31295. The use of the approximation taking into account the microface already carries some form of energy conservation: the energy of the reflected light will never exceed the energy of the incident (if the surface does not glow by itself). Looking at the image above, we see that with an increase in the surface roughness, the spot of the reflected light increases, but at the same time its brightness decreases. If the intensity of the reflected light were the same for all pixels, regardless of the size of the spot, then rougher surfaces would emit much more energy, which would violate the law of conservation of energy. Therefore, specular reflections are brighter on smooth surfaces and dim on rough ones. 3r31296.

3r31295. In order for energy to be conserved, we must make a clear separation between the diffuse and specular components. The moment the light beam reaches the surface, it is divided into 3r31130. reflected 3r31131. and 3r31130. refracted

3r31295. But there are some nuances associated with the absorption of light - it does not happen instantly as soon as the light touches the surface. From the physics course, we know that light can be described as a beam of photons with energy that moves in a straight line until it loses all energy as a result of a collision with obstacles. Each material consists of microparticles that can interact with a ray of light, as shown in the figure below. These particles absorb some or all of the light energy at each collision, transforming it into heat. 3r31296.

3r31258. 3r3409.

3r31295. In general, not all of the energy is absorbed, and the light continues to scatter in (mostly) random directions, where it collides with other particles again until it dries up, or it leaves the surface again. Thus, the surface begins to re-emit light rays, making a contribution in the form of the observed (diffuse) color of the surface. Using PBR, we make a simplified assumption that all the refracted light is absorbed and scattered over a small area of impact, ignoring the effect of scattered light that leaves the surface at a distance from this area. Special shader techniques that take this into account are known as 3r31130 techniques. subsurface scattering [/i] , significantly improve the visual quality of materials like leather, marble, wax, but are expensive in terms of performance. 3r31296.

3r31295. Additional subtleties appear with the refraction and reflection of light on 3r31130. metal 3r31131. surfaces. Metal surfaces interact with light differently than

*non-metallic*(i.e. dielectrics). They obey the same laws of refraction and reflection, with one exception: all the refracted light is absorbed by the surface without scattering, only the specularly reflected light remains; in other words, metal surfaces do not have a diffuse color. Because of this obvious difference between metals and dielectrics, they will be processed differently in the PBR conveyor into which we dive further in the course of the article. 3r31296.

3r31295. This difference between reflected and refracted light leads us to another observation regarding the conservation of energy: their magnitudes are mutually exclusive. The energy of the reflected light cannot be absorbed by the material. Therefore, the energy absorbed by the surface in the form of refracted light is the remaining energy after taking into account the reflected.

We use this ratio by first calculating the reflected part as a percentage of the energy of the incident rays reflected by the surface, and then the fraction of the refracted light directly from the reflected light, like:

3r31192. 3r31193. float kS = calculateSpecularComponent (); //reflected /mirrored

float kD = 1.0 - kS; //refracted /diffused part

3r31198. 3r31199.

3r31295. In this way we will know the values of both the reflected and refracted parts due to the law of conservation of energy. With this approach, neither refracted (difffuznaya), nor the reflected part will exceed 1.? ensuring that their total energy does not exceed the value of the energy of the incident light, which we could not take into account in previous lessons. 3r31296.

3r31246. Reflection equation

3r31295. The above brings us to the so-called rendering equation : a complex equation, invented by very smart guys, and today is the best model to simulate lighting. PBR strictly follows a more specific version of this equation, known as

**reflection equation**. In order to understand PBR well, it is important to have a complete understanding of the reflection equation first:

3r31295. 3r31296. 3r31295. 3r31236. 3r33613. 3r31238. 3r31296.

3r31295. At first, it looks frightening, but we will sort it out gradually, piece by piece, and you will see how slowly it will start to make sense. To understand this equation we will have to delve a little into radiometry. 3r31290. Radiometry [/b] - This is the science of measuring electromagnetic radiation (including visible light). There are several radiometric quantities that we can use to measure illumination, but we will only use one relating to the reflection equation, known as Energy Brightness (eng. Radiance) and denoted here by the letter L. HP is used to quantify the magnitude or intensity of light, coming from a certain direction. EH, in turn, is a combination of several physical quantities, and, to make it easier for us to imagine it, we will focus on each of them separately. 3r31296.

3r31299. Radiation flux (English radiant flux) 3r31300.

3r31295. The radiation flux (3r31236. 3r33549.

) Is the power transmitted by light energy, measured in watts. The total light energy is made up of a set of terms for different wavelengths, each of which has its own color spectrum. The energy emitted by the light source, in this case, can be represented as a function of all these wavelengths. Wavelengths from 390nm to 700nm make up the visible part of the spectrum, that is, radiation in this range can be perceived by the human eye. In the image below you can see the energy values for the different wavelengths of daylight:

3r31258.

3r31295. The radiation flux corresponds to the area under the graph of this function from all wavelengths. Directly using the wavelengths of light as input data in computer graphics is impractical, so we resort to a simplified representation of the radiation flux using a triple of colors instead of a function of all wavelengths, known as RGB (or as we usually call it - lighting color). Such a presentation leads to some loss of information, but in general it will slightly affect the final picture. 3r31296.

3r31299. Solid angle (English solid angle)

3r31295. 3r31290. Physical angle [/b] referred to as 3r31236. 3r? 3594. 3r31238. gives us the size or area of a figure projected onto a single sphere. You can think of it as a direction with a volume of: 3r31296.

3r31258.

3r31295. Imagine that you are in the center of a sphere and look in the direction of a figure. The size of the resulting silhouette will be a solid angle. 3r31296.

3r31299. Radiation intensity (eng. Radiance intensity) 3r31300.

3r31295. The radiation power measures the amount of radiation flux per solid angle, or the power of a light source per square of a unit sphere defined by a solid angle. For example, for an omnidirectional light source that radiates equally in all directions, the radiation power means the energy of light per specific area (solid angle): 3r31296.

3r31258. 3r33535.

3r31295. The equation describing the power of the radiation looks like this:

3r31295. 3r31296. 3r31295. 3r31236. 3r33535. 3r31238. 3r31296.

3r31295. , where I is the radiation flux Φ falling on the solid angle

3r33542. 3r31238. 3r31296.

3r31295. Knowing the radiation flux, the force and the solid angle, we can describe the energy brightness equation that describes the total observed energy in area A, limited by the solid angle O for the light of 3r31236. 3r3-3549. 3r31238. 3r31296.

3r31295. 3r31296. 3r31295. 3r31236. 3r33558. 3r31238. 3r31296.

3r31258. 3r33535.

3r31295. Energy brightness is the radiometric amount of the amount of light in an area that depends on the angle of incident light 3r31236. 3r33570. 3r31238. (angle between the direction of the light and the normal to the surface) through 3r31236. 3r31238. : light is weaker when it radiates along the surface and is strongest when it is perpendicular to it. This is similar to our calculations for diffuse light in the tutorial on 3r37575.

lighting basics. since

3r31238. nothing more than a scalar product between the direction of light and the surface normal vector:

3r31296. 3r31192. 3r31193. float cosTheta = dot (lightDir, N); 3r31198. 3r31199.

3r31295. The energy brightness equation is very useful for us, because it contains most of the physical quantities that we are interested in. If we assume that the solid angle ω and the area A are infinitely small, we can use the EW to measure the flux of one ray of light per one point of space. This will allow us to calculate the EL of a single light beam acting on a single point (fragment); we actually translate the solid angle

3r? 3594. 3r31238. in the direction vector 3r31236. 3r? 3594. 3r31238. and 3r31236. 3r? 3597. 3r31238. to point 3r31236. 3r31238. . Thus, we can directly use the EA in our shaders to calculate the contribution of a separate ray of light for each fragment. 3r31296.

3r31295. In fact, when it comes to HE, we are usually interested in all the incoming light falling on the point p, which is the sum of the entire ET, and is known as irradiation (irradiance). Knowing EH and irradiation, we can return to the reflection equation:

3r31295. 3r31296. 3r31295. 3r31236. 3r33613. 3r31238. 3r31296.

3r31295. Now we know that

3r3620. 3r31238. in the rendering equation, the EW for some point of the surface p and some infinitely small solid angle of the incoming light is 3r31236. 3r31238. , which can be considered as an incoming direction vector

3r31238. . Remember that energy is multiplied by

3r31238. - the angle between the direction of light incidence and the surface normal, which is expressed in the reflection equation with the product 3r31236. 3r33232. 3r31238. . The reflection equation calculates the sum of the reflected EH 3r31236. 3r33635. 3r31238. points

3r31238. in the direction of 3r31236. 3r31238. which is the outgoing direction to the observer. Or else: 3r31236. 3r33836. 3r31238. measures the reflected irradiation point

3r31238. if viewed from 3r31236. 3r31238. . 3r31296.

3r31295. Since the reflection equation is based on irradiation, which is the sum of all incoming radiation, we measure the light not only of one incoming light direction, but from all incoming light directions within the hemisphere of 3r31236. 3r33720. 3r31238. , centered at

3r31238. . It can be described as half a sphere oriented along the surface normal

3r3754. 3r31238. :

3r31258. 3r3669.

3r31295. To calculate the sum of all values within a region, or, in the case of a hemisphere, volume, we integrate the equation in all input directions 3r31236. 3r337. 3r31238. within the hemisphere of

3r33720. 3r31238. . Since there is no analytical solution for both the render equation and the reflection equation, we will solve the integral numerically. This means that we will get results for small discrete steps of the reflection equation over the hemisphere 3r31236. 3r33720. 3r31238. and average them by step size. This is called

**the Riemann sum**which we can roughly represent with the following code:

3r31192. 3r31193. int steps = 100;

float sum = 0.0f;

vec3 P = ;

vec3 Wo = ;

vec3 N = ;

float dW = 1.0f /steps;

for (int i = 0; i < steps; ++i)

{

vec3 Wi = getNextIncomingLightDir (i); 3r313346. sum + = Fr (P, Wi, Wo) * L (P, Wi) * dot (N, Wi) * dW ; 3r31346.} 3r31346. 3r31198. 3r31199. 3r31301.

3r31295. dW for each discrete step can be viewed as

3r337. 3r31238. in the reflection equation. Mathematically,

3r337. 3r31238. is the differential over which we calculate the integral, and although it is not the same as dW in the code (since it is a discrete step of the Riemann sum), we can consider it as such for ease of calculation. Keep in mind that using discrete steps will always give us an approximate sum, not an exact value of the integral. The attentive reader will notice that we can improve the accuracy of the Riemann sum by increasing the number of steps. 3r31296.

3r31295. The reflection equation summarizes the emission of all incoming light directions

3r31238. over the hemisphere

3r33720. 3r31238. which reaches the point

3r31238. and returns the sum of the reflected light

3r33836. 3r31238. in the direction of the viewer. Incoming radiation can come from light sources with which we are already familiar, or from environment maps defining the electric motor of each incoming direction, which we will discuss in the IBL tutorial. 3r31296.

3r31295. Now the only unknown to the left is the symbol

3r33737. 3r31238. , known as the

**function. BRDF**or

**3r31291 two-beam reflectance function. which scales (or weights) the value of the incoming radiation based on the properties of the surface material. 3r31296.**

3r31246. BRDF

3r31295. BRDF is a function that takes as input the direction of the incident light

3r31238. direction to the observer 3r31236. 3r31238. , normal to the surface 3r31236. 3r3754. 3r31238. and parameter 3r31236. 3r3757. 3r31238. which is a surface roughness. BRDF approximates how much each individual light beam

3r31238. contributes to the final reflected light of an opaque surface with regard to the properties of its material. For example, if the surface is completely smooth (almost like a mirror), the BRDF function will return 0.0 for all incoming light rays 3r31236. 3r31238. , with the exception of one having the same angle (after reflection) as the beam 3r31236. 3r31238. for which the function will return 1.0. 3r31296.

3r31295. BRDF approximates the reflective and refractive properties of a material based on the previously mentioned theory of microfaces. In order for a BRDF to be physically plausible, it must obey the law of conservation of energy, that is, the total energy of the reflected light must never exceed the energy of the incident light. Technically, the Blinna-Phong model is considered BRDF, accepting the same 3r31236. 3r31238. and 3r31236. 3r31238. at the entrance. However, the Blinna-Phong model is not considered physically correct, since it does not guarantee compliance with the law of conservation of energy. There are several physically correct BRDFs for approximating the response of a surface to illumination. However, almost all real-time graphics pipelines use BRDF, known as 3r312390. BRDF Cook-Torrens(Cook-Torrance). 3r31296.

3r31246. BRDF

3r31295. BRDF is a function that takes as input the direction of the incident light

3r31238. direction to the observer 3r31236. 3r31238. , normal to the surface 3r31236. 3r3754. 3r31238. and parameter 3r31236. 3r3757. 3r31238. which is a surface roughness. BRDF approximates how much each individual light beam

3r31238. contributes to the final reflected light of an opaque surface with regard to the properties of its material. For example, if the surface is completely smooth (almost like a mirror), the BRDF function will return 0.0 for all incoming light rays 3r31236. 3r31238. , with the exception of one having the same angle (after reflection) as the beam 3r31236. 3r31238. for which the function will return 1.0. 3r31296.

3r31295. BRDF approximates the reflective and refractive properties of a material based on the previously mentioned theory of microfaces. In order for a BRDF to be physically plausible, it must obey the law of conservation of energy, that is, the total energy of the reflected light must never exceed the energy of the incident light. Technically, the Blinna-Phong model is considered BRDF, accepting the same 3r31236. 3r31238. and 3r31236. 3r31238. at the entrance. However, the Blinna-Phong model is not considered physically correct, since it does not guarantee compliance with the law of conservation of energy. There are several physically correct BRDFs for approximating the response of a surface to illumination. However, almost all real-time graphics pipelines use BRDF, known as 3r312390. BRDF Cook-Torrens

3r31295. The Cook-Torrens BRDF contains both a diffuse and a mirror part:

3r31295. 3r31296. 3r31295. 3r31236. 3r31238. 3r31296.

3r31295. here

3r31238. - refracted fraction of the incoming light energy, 3r31236. 3r31228. 3r31238. - reflected. The left side of the BRDF contains the diffuse part of the equation, designated here as

3r3804. 3r31238. . This is the so-called Lambert scattering. It is similar to what we used for diffuse illumination, and is constant: 3r31296.

3r31295. 3r31296. 3r31295. 3r31236. 3r31238. 3r31296.

3r31295. where

3r31238. - albedo or surface color (diffuse surface texture). The division by 3r31236. 3r31238. one needs to normalize the diffused light, since the previously designated integral containing BRDF is multiplied by 3r31236. 3r31238. (we'll get to that in the IBL tutorial). 3r31296.

3r31160. You may be surprised at how this Lambert scattering looks like the expression for diffuse illumination that we used before: the color of the surface multiplied by the scalar product between the surface normal and the direction of light. The dot product is still present, but derived from BRDF, since we have

3r33833. 3r31238. in the integral 3r31236. 3r33836. 3r31238. . 3r31163.

3r31295. There are different equations for the diffuse part of the BRDF, which look more realistic, but they are more expensive in terms of performance. In addition, as concluded in Epic Games: Lambert scattering is enough for most real-time rendering purposes. 3r31296.

3r31295. The mirror part of the BRDF Cook-Torrens is slightly improved and is described as:

3r31295. 3r31296. 3r31295. 3r31236. 3r33853. 3r31238. 3r31296.

3r31295. It consists of three functions and a normalization factor.I'm in the denominator. Each of the letters D, F, and G is a specific type of function that approximates a specific part of the reflective properties of a surface. They are known as the normal distribution function (normal

**D**Istribution function, NDF), Fresnel equation (3r31290. F [/b] Resnel equation) and Geometry function (

**G**Eometry function):

3r31296. 3r? 31303.

Normal distribution function: approximates the number of surface microfaces oriented along the median vector, based on the surface roughness; This is the main function approximating micro-boundaries. 3r31338.

Geometry function: describes the self-shadowing property of microfaces. When the surface is rather rough, some microfaces of the surface may overlap others, thereby reducing the amount of light reflected by the surface. 3r31338.

Fresnel equation: describes the coefficient of surface reflection at different angles. 3r31338.

3r313340.

3r31295. Each of these functions is an approximation of their physical equivalent, and for them there are different implementations aimed at a more accurate approximation to the underlying physical model; some give more realistic results, others are more efficient in terms of performance. Brian Karis from Epic Games has done a lot of research on various types of approximations, which you can learn more about 3r3883. here . We will use the same features as in Unreal Engine 4 from Epic Games, namely: 3r31130. Trowbridge-Reitz GGX [/i] for D, approximation

*Fresnel-Schlick*for F and 3r31130. Smith's Schlick-GGX [/i] for G. 3r31296.

3r31246.

normal distribution function.

3r31295. The normal distribution function D statistically approximates the relative surface area of microfaces, precisely oriented along the median vector 3r31236. 3r31238. . There are many NDFs that determine the statistical approximation of the general alignment of microfaces, taking into account a certain roughness parameter. We will use one known as the Trowbridge-Reitz GGX:

3r31295. 3r31296. 3r31295. 3r31236. 3r3909. 3r31238. 3r31296.

3r31295. here

3r31238. - This is the median vector,

3r31238. - the value of surface roughness. If we choose

3r31238. as a median vector between the normal to the surface and the direction of light, then changing the roughness parameter, we get the following picture: 3r31296.

3r31258. 3r33939.

3r31295. When the roughness is small (i.e., the surface is smooth), the micrografts oriented in the direction of the median vector are concentrated in a small radius. Thanks to this high concentration, NDF gives a very bright spot. On the rough surface, where the microgranes are oriented in more random directions, you will find a much larger number of microfaces oriented in the direction of the median vector 3r31236. 3r31238. , but located in a larger radius, which makes the color of the stain more gray. 3r31296.

3r31295. In the GLSL code, the Trowbridge-Reitz GGX normal distribution function will look something like this: 3r31296.

3r31192. 3r31193. float DistributionGGX (vec3 N, vec3 H, float a)

{

float a2 = a * a;

float NdotH = max (dot (N, H), 0.0);

float NdotH2 = NdotH * NdotH;

float nom = a2;

float denom = (NdotH2 * (a2 - 1.0) + 1.0);

denom = PI * denom * denom;

return nom /denom;

} 3r31199.

3r31246. Geometry function

3r31295. The geometry function statistically approximates the relative surface area, where its microscopic irregularities overlap each other, which prevents light rays from penetrating. 3r31296.

3r31258. 3r33939.

3r31295. As in the case of NDF, the geometry function accepts a surface roughness coefficient as input, which in this case means the following: rougher surfaces will have a higher probability of microface shadowing. The geometry function that we will use is a combination of the GGX and Schlick-Beckmann approximation (Schlick-Beckmann), and is known as Schlick-GGX: 3r31296.

3r31295. 3r31296. 3r31295. 3r31236. 3r33981. 3r31238. 3r31296.

3r31295. Here

3r33988. 3r31238. is a re-designation

3r31238. depending on whether we use the geometry function for direct lighting or IBL lighting: 3r3-31296.

3r31295. 3r31296. 3r31295. 3r31236. 3r3-3000. 3r31238. 3r31296.

3r31295. 3r31296. 3r31295. 3r31236. 3r31238. 3r31296.

3r31295. Notice that the value is

3r31238. may differ depending on how your engine converts roughness to

3r31238. . In the following lessons we will discuss in detail how and where this reassignment becomes relevant. 3r31296.

3r31295. To efficiently approximate geometry, we need to take into account both the direction of the view (the overlap of the geometry) and the vector of the direction of the light (the self-shading of the geometry). We can consider both cases using 3r31130. Smith's method of r3r31131. :

3r31295. 3r31296. 3r31295. 3r31236. 3r3-1034. 3r31238. 3r31296.

3r31295. Using Smith's method with Schlick-GGX as 3r31236. 3r3-1041. 3r31238. gives the following picture with different roughness R:

3r31258. 3r31047.

3r31295. The geometry function is a multiplier between[0.0, 1.0]where white (or 1.0) means no shading of the micro-facets, and black (or 0.0) means complete shading of the micro-facets. 3r31296.

3r31295. In GLSL, the geometry function is converted to the following code:

3r31192. 3r31193. float GeometrySchlickGGX (float NdotV, float k)

{

float nom = NdotV;

float denom = NdotV * (1.0 - k) + k;

return nom /denom;

}

float GeometrySmith (vec3 N, vec3 V, vec3 L, float k)

{

float NdotV = max (dot (N, V), 0.0);

float NdotL = max (dot (N, L), 0.0);

float ggx1 = GeometrySchlickGGX (NdotV, k);

float ggx2 = GeometrySchlickGGX (NdotL, k);

return ggx1 * ggx2;

}

3r31198. 3r31199.

3r31246. Fresnel equation

3r31295. The Fresnel equation describes the ratio of reflected and refracted light, which depends on the angle at which we are looking at the surface. When light hits the surface, the Fresnel equation gives us the percentage of reflected light based on the angle at which we see this surface. From this ratio of reflection and energy conservation law, we can directly get the refracted part of the world, which will be equal to the remaining energy. 3r31296.

3r31295. Each surface or material has a level of

*base reflectivity*

1. , observed when looking at the surface directly, but if you look at the surface at an angle, all reflections become more noticeable. You can check it yourself by looking at your probably wooden or metal table, first perpendicularly, and then at an angle close to 90 degrees. You will see that the reflections become much more visible. All surfaces, theoretically, completely reflect the light, if you look at them at an ideal angle of 90 degrees. This effect is given the name Fresnel and is described as

3r31295. The Fresnel equation is rather complicated, but, fortunately, it can be simplified with the help of 3r31290. Fresnel-Schlick approximation [/b] :

3r31295. 3r31296. 3r31295. 3r31236. 3r31110. 3r31238. 3r31296.

3r31295. 3r31236. 3r31183. 3r31238. represents the base reflectivity of the surface, which we calculate using something called the refractive indices or 3r31290. Ior 3r31291. (indices of refraction), and, as you can see on the surface of the sphere, the closer the direction of view to the borders of the visible sphere (the angle between the direction of view and the median vector reaches 90 degrees), the stronger the Fresnel effect and, therefore, the reflection: 3r31296 .

3r31258. 3r3-131125.

3r31295. There are several subtleties associated with the Fresnel equation. The first is that the Fresnel-Schlick approximation is valid only for 3r31130. dielectricor non-metallic surfaces. For surfaces of conductors (metals), the calculation of the base reflectivity using refractive indices will be incorrect, and we need to use a different Fresnel equation for the conductors. Since this is inconvenient, we use the pre-calculated values for the surfaces with a normal incidence (

1. , observed when looking at the surface directly, but if you look at the surface at an angle, all reflections become more noticeable. You can check it yourself by looking at your probably wooden or metal table, first perpendicularly, and then at an angle close to 90 degrees. You will see that the reflections become much more visible. All surfaces, theoretically, completely reflect the light, if you look at them at an ideal angle of 90 degrees. This effect is given the name Fresnel and is described as

**the fresnel equation**. 3r31296.3r31295. The Fresnel equation is rather complicated, but, fortunately, it can be simplified with the help of 3r31290. Fresnel-Schlick approximation [/b] :

3r31295. 3r31296. 3r31295. 3r31236. 3r31110. 3r31238. 3r31296.

3r31295. 3r31236. 3r31183. 3r31238. represents the base reflectivity of the surface, which we calculate using something called the refractive indices or 3r31290. Ior 3r31291. (indices of refraction), and, as you can see on the surface of the sphere, the closer the direction of view to the borders of the visible sphere (the angle between the direction of view and the median vector reaches 90 degrees), the stronger the Fresnel effect and, therefore, the reflection: 3r31296 .

3r31258. 3r3-131125.

3r31295. There are several subtleties associated with the Fresnel equation. The first is that the Fresnel-Schlick approximation is valid only for 3r31130. dielectric

) (0 degrees, as if we were looking directly at the surface) and interpolate this value based on the viewing angle according to the Fresnel approximation -Slick, so that we can use the same equation for metals and non-metals. 3r31296.

3r31295. The response of the surface during normal incidence or base reflectivity can be found in large databases like 3r31139. this

. Some common values listed below are taken from Nati Hoffman course notes: 3r31296.

3r31258. 3r31145.

3r31295. An interesting observation: for all dielectric surfaces, the base reflectivity never rises above 0.1? which is the exception rather than the rule, whereas for conductors the base reflectivity is much higher and (mostly) lies in the range from 0.5 to 1.0. In addition, for conductors or metal surfaces, the base reflectivity is tinted, therefore 3r31236. 3r31183. 3r31238. presented as an RGB triplet (reflectivity with a normal incidence may vary depending on the wavelength). 3r31296.

3r31295. These specific features of metallic surfaces compared to dielectric ones gave rise to something called a metallic workflow: when we create materials of surfaces with an additional parameter, known as metalness, which describes whether the surface is metallic or not. 3r31296.

3r31160. Theoretically, the metallicity of the surface takes only two values: it is either a metal or not; the surface cannot be both. However, most render pipelines allow you to adjust the metallicity of the surface linearly between 0.0 and 1.0. This is mainly due to the lack of material in the texture of sufficient accuracy to create a surface, for example, with small particles of dust and sand, scratches on the metal surface. By changing the metallicity value around these small non-metallic particles and scratches, we get visually more pleasant results.

3r31163.

3r31295. Precomputing 3r31236. 3r31183. 3r31238. for both dielectrics and metals, we can use the same Fresnel-Schlick approximation for both types of surfaces, but we need to tint the base reflectivity if we have a metallic surface. Usually we do it like this:

3r31192. 3r31193. vec3 F0 = vec3 (???);

F0 = mix (F? surfaceColor.rgb, metalness);

3r31198. 3r31199.

3r31295. We determined the base reflectivity, which is approximately the same for most dielectric surfaces. This is another approximation since

3r31183. 3r31238. averaged over most common dielectrics. The base reflectance of ??? is retained for most dielectrics and gives physically plausible results without the need to specify an additional surface parameter. Therefore, depending on the type of surface, we either take the basic reflectance of the dielectric, or assume that F0 is given by the color of the surface. Since metal surfaces absorb all the refracted light, they do not have diffuse reflections, and we can directly use the diffuse surface texture as the base reflectivity. 3r31296.

3r31295. We translate the Fresnel-Schlick approximation into the code:

3r31192. 3r31193. vec3 fresnelSchlick (float cosTheta, vec3 F0)

{

return F0 + (1.0 - F0) * pow (1.0 - cosTheta, 5.0);

}

3r31198. 3r31199.

3r31295. where cosTheta is the result of the scalar product between the surface normal vector and the direction of view. 3r31296.

3r31246. Cook's-Torrens reflection equation

3r31295. Knowing all the components of the BRDF Cook-Torrens, we can include it in the final reflection equation: 3r312296.

3r31295. 3r31296. 3r31295. 3r31236. 3r31218. 3r31238. 3r31296.

3r31295. However, this equation is not completely truethematically. You probably remember that the Fresnel equation F is the coefficient of the amount of light reflected by a surface. That is, in fact, this is our coefficient

3r31228. 3r31238. , which means that the mirror part of the reflection equation already contains

3r31228. 3r31238. . Given this, our final reflection equation is written as:

3r31295. 3r31296. 3r31295. 3r31236. 3r31237. 3r31238. 3r31296.

3r31295. This equation now fully describes the physical rendering model, which we usually call physically correct rendering or PBR. Do not worry if you have not fully understood how we need to arrange all the mathematics under discussion in the form of a code. In the following lessons, we will look at how to use the reflection equation to get much more physically plausible results in our rendering, and all the pieces will be gathered together into a single unit. 3r31296.

3r31246. Creating materials for PBR

3r31295. Now, knowing the basic mathematical model of PBR, we conclude the discussion with a description of how artists usually define the physical properties of a surface, which we can directly transfer to the PBR equations. Each of the surface parameters that we need for the PBR pipeline can be defined or modeled with textures. The use of textures gives us fragmentary control over how each specific point on the surface should react to light: is it metallic, rough or smooth, or how this surface reacts to different wavelengths of light. 3r31296.

3r31295. Below you will see a list of textures that can often be found in the PBR pipeline, as well as the result of their application: 3r31296

3r31258. 3r31259.

3r31295. 3r31290. Albedo [/b] : The albedo texture determines for each texel the surface color or the base reflectivity if this texel is metallic. This is a lot like the diffuse textures we used before, but all the lighting information is removed from the texture. Diffuse textures often have small shadows or darkened cracks inside the image, which is not necessary in the albedo texture; it must contain only the color of the surface. 3r31296.

3r31295. 3r31290. Normals [/b] : texture of the normal map, exactly the same as we used earlier in the tutorial in 3r3-31272. normal maps

. The normal map allows us to set our normal for each fragment in order to create the illusion that the surface is more convex. 3r31296.

3r31295. 3r31290. Metallicity 3r31291. : The metallicity map determines whether the texel is metallic or not. Depending on how the PBR engine is configured, artists can set metallicity in shades of gray or in two colors only: black and white. 3r31296.

3r31295. 3r31290. Roughness [/b] : The roughness map indicates how rough the surface is at the base of the texel. The roughness value selected from the texture determines the statistical orientation of the microfaces of the surface. On a rougher surface, broader and more diffuse reflections are obtained, while on a smooth surface they are focused and clear. Some PBR engines use smoothness maps instead of roughness maps, which some artists find more intuitive, but these values translate into roughness (1.0 - smoothness) at the time of sampling. 3r31296.

3r31295. 3r31290. AO (ambient occlusion) [/b] : Background shading maps or AO define an additional coefficient for shading the surface and surrounding geometry. If we have a brick surface, for example, the albedo texture should not have any information about shading inside the brick gaps. The AO map defines these darkened faces, for which the light is more difficult to penetrate. Given the AO at the end of the lighting stage, we can significantly improve the visual quality of the scene. AO maps for meshes and surfaces are either manually generated or computed in 3D modeling programs. 3r31296.

3r31295. Artists set and customize input values for each texel and can base their textures on the physical properties of the surface of real materials. This is one of the biggest advantages of the PBR conveyor, since these physical properties of the surface remain unchanged from environmental conditions or lighting, which makes it easier for artists to obtain plausible results. Surfaces created in the PBR pipeline can be easily used in various PBR engines, and will look correct regardless of the environment in which they are located, which as a result looks much more natural. 3r31296.

3r31299. Materials on the topic 3r31300.

3r? 31303.

Background: Physics and Math of Shading by Naty Hoffmann: a lot of theory to fit in one article, so the theory hardly touches the basics here; if you want to learn more about the physics of light and how it relates to the PBR theory, this must-read resource is for you. 3r31338.

3r31311. Real shading in Unreal Engine 4

: A discussion of the PBR model adopted by Epic Games in the Unreal Engine 4. The PBR we are talking about in these tutorials is based on this model. 3r31338.

3r31316. Marmoset: PBR Theory

: introduction to PBR. Mainly intended for artists, but nonetheless worth reading. 3r31338.

Coding Labs: Physically based rendering : The basics of the rendering equation and how it relates to PBR. 3r31338.

Coding Labs: Physically Based Rendering - Cook – Torrance : Highlights BRDF Cook-Torrens 3r3r1313.

3r31331. Wolfire Games - Physically based rendering

: introduction to PBR by Lukas Orsvärn. 3r31338.

[SH17C]Physically Based Shading : an excellent interactive shadertoy (warning: can be loaded for a long time) from Krzysztof Narkowi, demonstrates the interaction of light with PBR material. 3r31338.

3r313340.

3r31344. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();