According to the light model in the book, the type of light is divided into 3 kinds: diffuse light, ambient light, mirror light.
The type of light source is also three kinds: parallel light, Spot light, spotlight.
Other required data: material, normal direction (illumination angle).
Now, take the parallel light source as an example, because it is the simplest, does not need to calculate the distance, the angle to the light effect:
Define the light source first:
// all the definitions here relate only to the color of the light, that is, the color of the light. Dirlight.ambient = XMFLOAT4 (0.2f0.2f0.2f1.0f); // Ambient light Dirlight.diffuse = XMFLOAT4 (0.5f0.5f0.5f1.0f); // Diffuse reflection Light Dirlight.specular = XMFLOAT4 (1.0f1.0f1.0f0.0f); // Mirror Light
1. Ambient light:
The so-called ambient light, it can be thought that there is a light filled with the entire irradiation area, no matter in which location, can be covered. His role is to simulate the diffuse effect in the real world.
Implement the formula: A=la?ma, explains that the ambient light obtained is equal to the ambient light of the input (input Ambient three-dimensional vector) by multiplying the ambient light factor of the material (Material Ambient three-dimensional vector)
HLSL code: ambient = Mat. Ambient * l.ambient;
Example: Light (0.2,0.2,0.2) * material (0.5,0.5,0.5) = (0.1,0.1,0.1)
In other words, the ambient light is only related to the material, the light itself, regardless of the angle.
(It's so depressing here.) Why? The notation is multiplied by item .... I remember that this symbol in our textbook indicates cross-multiplication ... And the vector has this definition ... All of the following will be multiplied by item)
2. Diffuse reflection Light:
Very simply, the light on the rough surface will have a uniform reflection in all directions, so no matter in that direction, we see the intensity of the light is the same.
Implementation formula: cd = kd ld? MD Explanation: The next half and the front of the ambient light, the material and light are multiplied, the previous KD represents an "angle", in the book 7.1 of the normal theorem and 7.2 in the definition of Lambert cosine has a detailed explanation, the radiation point of the normal and the cosine of the angle of the light.
The code is along with the mirror light behind it.
3. Specular light (also known as highlights):
For some smooth materials, the light will form a specular reflection, which can only be seen at some special angles.
Implementation formula: cs = ks LS? The KS here refers to the angle between the direction of observation and the direction of the reflected light (the strongest angle is 0 o'clock)
Full code:
The definition of a variable:
//-------------------------------------------------------------------------------------//Defining lighting Structures//----------------------------------------------------------------------------------------structDirectionalLight////Parallel Light{FLOAT4 Ambient; FLOAT4 Diffuse; FLOAT4 specular; FLOAT3 Direction; floatpad;};//--------------------------------------------------------------------------------------//constant Buffer definition//--------------------------------------------------------------------------------------Cbuffer Constantbuffer:register (B0)//World, projected coordinate transformation{matrix world; Matrix View; Matrix Projection;} Cbuffer Cbperframelight//The light structure is packaged and a parallel light is temporarily{directionallight gdirlight; //Parallel Light//Pointlight gpointlight; //Point Light//SpotLight gspotlight; //SpotlightFLOAT4 GEYEPOSW;//Eye coordinates};//--------------------------------------------------------------------------------------//Other structure Definitions//--------------------------------------------------------------------------------------structmaterial{float4 Ambient; FLOAT4 Diffuse; FLOAT4 specular; //w = specpowerfloat4 Reflect;};
The implementation of the function:
//--------------------------------------------------------------------------------------//function Function Implementation//--------------------------------------------------------------------------------------//realization of parallel light computedirectionallight (material, parallel light structure, reflection point normal, unit vector pointing to the eye, return 1, return 2, return 3)voidComputedirectionallight (Material mat, directionallight L, Float3 Normal, Float3 Toeye, outfloat4 Ambient, outfloat4 Diffuse, outFLOAT4 Spec) { //Initialize the output variableAmbient = FLOAT4 (0.0f,0.0f,0.0f,0.0f); Diffuse= FLOAT4 (0.0f,0.0f,0.0f,0.0f); Spec= FLOAT4 (0.0f,0.0f,0.0f,0.0f); //the light vector is in the opposite direction of light propagation.FLOAT3 Lightvec =-l.direction; //Add ambient lightAmbient = Mat. Ambient *l.ambient; //add diffuse and specular light, note that Normal,lightvec is a unit vector (normalized) floatDiffusefactor = dot (Lightvec, normal);//Calculate Cosa//Flatten Avoid dynamic branching[Flatten]if(Diffusefactor >0.0f)//if the cosine value of the angle is greater than 0,{FLOAT3 v= Reflect (-lightvec, normal);//reflect calculation of reflection vectors by incident light and normal floatSpecfactor = POW (max (dot (v, toeye),0.0f), Mat. SPECULAR.W);//POW is used to calculate the line reflectivity of a material's lightDiffuse= Diffusefactor * Mat. Diffuse *L.diffuse; Spec= Specfactor * Mat. Specular *L.specular; }}
I made more comments than the code in the original book, but I would like to explain it again:
Ambient light calculations already described: Ambient = mat. Ambient * l.ambient;
The vector direction of light in this light is the opposite of the direction of the Ray incident, that is, the direction of the reflection point to the light source, the parallel light directly opposite can be: float3 Lightvec =-l.direction;
Calculates the cosine of the angle between the reflected normals and the ray vectors: float diffusefactor = dot (Lightvec, normal), which is normal, Lightvec above, they are unit vectors, only such points multiply as the Cos value, Also, the calculation of normals can be read in verse 7.2.
Then there is an if statement, about the support control statements in HLSL look at MSDN's introduction:
Https://msdn.microsoft.com/en-us/library/bb509600.aspx
The explanation of flatten and branch in this is totally not clear to me. Besides, ...
Calculating reflected light using incident light and normal vector: FLOAT3 v = reflect (-lightvec, normal);
Calculates the cosine of a reflected light with a vector pointing to the eye (observation vector, point to point of view) dot (V, toeye), judging if it is greater than 0:max (dot (v, toeye))
Finally, the material is analyzed using POW, which is used to distinguish the reflective ability of different materials.
This section is relatively simple and does not give the code. The other 2 kinds of light sources are similar, no longer detailed analysis of ...
Good trouble.
Append: About flatten and branch
https://msdn.microsoft.com/en-us/library/windows/desktop/bb509610
http://www.gamedev.net/topic/650462-branch-and-flatten-attributes-in-sm-5/
My understanding: The GPU is to avoid branching, so when the program is compiled using a heuristic intelligent optimization method to remove the branch, this is the same program in different branches of the case can have the same running time, to avoid the waste of time? But we know that sometimes some branches are necessary, so he provides branch and flatten to let us decide whether to use dynamic branching. The examples in the book use flatten to avoid dynamic branching.
------above for me to read the documents and comments concluded that I have no practical way to verify.
DIRECTX11 Note 4: Light