Reprinted please indicate the source: http://blog.csdn.net/tianhai110
Illumination Model
In 3D rendering, the formula for calculating the illumination of the object surface is:
I = iambient + idiffuse + ispecular );
The calculation formula of ambient light is as follows:
Iambient = aintensity * acolor; (aintensity indicates the ambient light intensity, and acolor indicates the ambient light color)
The formula for calculating diffuse is as follows:
Idiffuse = dintensity * dcolor * n. L; (dintensity indicates the diffuse reflection intensity, dcolor indicates the color of the diffuse reflection light, N indicates the normal vector of the point, and l indicates the light source vector)
The formula for calculating the mirrored illumination (specular) is:
Ispecular = sintensity * scolor * (R. v) N; (sintensity indicates the intensity of the mirror light, scolor indicates the color of the mirror light, r indicates the reflection vector of the light, and V indicates the observer vector)
In summary, the entire illumination formula is:
I = aintensity * acolor + dintensity * dcolor * n. L + sintensity * scolor * (R. v) N;
The preceding formula can be simplified:
I = a + D * n. L + (R. v) N
Environment lighting:
1. Open rendermonkey, right-click the workspace's effect workspace node, and select Add default effect-> DirectX;
2. The created effect is an environment lighting effect. You can double-click pixel shader to modify your favorite color as the environment light.
The effect is as follows:
Diffuse Reflection:
1. Open rendermonkey, right-click the workspace's effect workspace node, and select Add default effect-> DirectX;
2. Add an object location matrix: Right-click the effect node and choose add variable-> matrix-> predefined-> matworld.
3. Add a light source vector: Right-click the effect node and choose add variable-> float-> float4. Change the name to veclightdir and double-click it to set its value.
4. Modify stream mapping and add a normal vector form.
5. Write vertex shader:
View plaincopy to clipboardprint?
- Float4x4 matviewprojection;
- Float4x4 matworld;
- Float4 veclightdir;
- Struct vs_input
- {
- Float4 position: position0;
- Float3 norm: normal0;
- };
- Struct vs_output
- {
- Float4 position: position0;
- Float3 light: texcoord0;
- Float3 norm: texcoord1;
- };
- Vs_output vs_main (vs_input input)
- {
- Vs_output output;
- Output. Position = MUL (input. Position, matviewprojection );
- Output. Light = normalize (veclightdir );
- Output. Norm = normalize (MUL (input. Norm, matworld ));
- Return (output );
- }
6. Compile pixel shader:
View plaincopy to clipboardprint?
- Float4 ps_main (float3 light: texcoord0, float3 norm: texcoord1): color0
- {
- Float4 diffuse = {1.0f, 1.0f, 1.0f, 1.0f };
- Float4 ambient = {0.3, 0.2, 0.1, 1.0 };
- Return ambient + DIFFUSE * saturate (dot (light, Norm ));
- }
7. Compile and preview the results:
Mirror reflection light:
You also need to add a position for the view based on the diffuse reflection of the mirror light;
1. Open rendermonkey and add a rendering node. Add default effect-> DirectX;
2. Add the camera matrix, add variable-> matrix-> predefined-> matview;
3. Add variable-> float-> float4 to the light source direction. Change the name to veclightdir and set the value.
4. Add variable-> float-> float4; rename it veceye; and set the value );
5. add texture-> Add 2D texture-> 2D texture; select a texture;
6. Right-click pass0 and add a texture object; add texture object-> my2dtexture; and change it to basemap;
7. Modify steam mapping as follows:
8. Write vertex shader;
View plaincopy to clipboardprint?
- Float4x4 matviewprojection;
- Float4x4 matview;
- Float4 veclightdir;
- Float4 veceye;
- Struct vs_input
- {
- Float4 position: position0;
- Float3 normal: normal0;
- Float2 texcoord: texcoord0;
- };
- Struct vs_output
- {
- Float4 position: position0;
- Float2 texc: texcoord0;
- Float3 light: texcoord1;
- Float3 norm: texcoord2;
- Float3 view: texcoord3;
- };
- Vs_output vs_main (vs_input input)
- {
- Vs_output output;
- Output. Position = MUL (input. Position, matviewprojection );
- Output. Light = veclightdir;
- Float3 posworld = normalize (MUL (input. Position, matview ));
- Output. view = veceye-posworld;
- Output. Norm = MUL (input. Normal, matview );
- Output. texc = input. texcoord;
- Return (output );
- }
9. Write pixel shader;
View plaincopy to clipboardprint?
- Sampler2d basemap;
- Float4 ps_main (float2 texc: texcoord0, float3 light: texcoord1,
- Float3 norm: texcoord2, float3 view: texcoord3): color0
- {
- Float4 ambient = {0.00006f, 0.00006f, 0.00006f, 1.0f };
- Float4 diffuse = {0.88f, 0.88f, 0.88f, 1.0f };
- Float3 normal = normalize (Norm );
- Float3 lightdir = normalize (light );
- Float3 viewdir = normalize (View );
- Float4 diff = saturate (dot (normal, lightdir ));
- Float3 reflect = normalize (2 * diff * normal-lightdir );
- Float4 specular = POW (saturate (dot (reflect, viewdir), 8 );
- Float4 fvbasecolor = tex2d (basemap, texc );
- Float4 fvtotalambient = ambient * fvbasecolor;
- Float4 fvtotaldiffuse = diffuse * diff * fvbasecolor;
- Return fvtotalambient + fvtotaldiffuse + specular;
- }
10. Running result:
This instance can be automatically generated by adding default effect-> DirectX-> texturedphong;