# ) Basic illumination model Formula

Source: Internet
Author: User
Tags mul

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?
1. Float4x4 matviewprojection;
2. Float4x4 matworld;
3. Float4 veclightdir;
4. Struct vs_input
5. {
6. Float4 position: position0;
7. Float3 norm: normal0;
8. };
9. Struct vs_output
10. {
11. Float4 position: position0;
12. Float3 light: texcoord0;
13. Float3 norm: texcoord1;
14. };
15. Vs_output vs_main (vs_input input)
16. {
17. Vs_output output;
18. Output. Position = MUL (input. Position, matviewprojection );
19. Output. Light = normalize (veclightdir );
20. Output. Norm = normalize (MUL (input. Norm, matworld ));
21. Return (output );
22. }

6. Compile pixel shader:

View plaincopy to clipboardprint?
1. Float4 ps_main (float3 light: texcoord0, float3 norm: texcoord1): color0
2. {
3. Float4 diffuse = {1.0f, 1.0f, 1.0f, 1.0f };
4. Float4 ambient = {0.3, 0.2, 0.1, 1.0 };
5. Return ambient + DIFFUSE * saturate (dot (light, Norm ));
6. }

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?
1. Float4x4 matviewprojection;
2. Float4x4 matview;
3. Float4 veclightdir;
4. Float4 veceye;
5. Struct vs_input
6. {
7. Float4 position: position0;
8. Float3 normal: normal0;
9. Float2 texcoord: texcoord0;
10. };
11. Struct vs_output
12. {
13. Float4 position: position0;
14. Float2 texc: texcoord0;
15. Float3 light: texcoord1;
16. Float3 norm: texcoord2;
17. Float3 view: texcoord3;
18. };
19. Vs_output vs_main (vs_input input)
20. {
21. Vs_output output;
22. Output. Position = MUL (input. Position, matviewprojection );
23. Output. Light = veclightdir;
24. Float3 posworld = normalize (MUL (input. Position, matview ));
25. Output. view = veceye-posworld;
26. Output. Norm = MUL (input. Normal, matview );
27. Output. texc = input. texcoord;
28. Return (output );
29. }

9. Write pixel shader;

View plaincopy to clipboardprint?
1. Sampler2d basemap;
2. Float4 ps_main (float2 texc: texcoord0, float3 light: texcoord1,
3. Float3 norm: texcoord2, float3 view: texcoord3): color0
4. {
5. Float4 ambient = {0.00006f, 0.00006f, 0.00006f, 1.0f };
6. Float4 diffuse = {0.88f, 0.88f, 0.88f, 1.0f };
7. Float3 normal = normalize (Norm );
8. Float3 lightdir = normalize (light );
9. Float3 viewdir = normalize (View );
10. Float4 diff = saturate (dot (normal, lightdir ));
11. Float3 reflect = normalize (2 * diff * normal-lightdir );
12. Float4 specular = POW (saturate (dot (reflect, viewdir), 8 );
13. Float4 fvbasecolor = tex2d (basemap, texc );
14. Float4 fvtotalambient = ambient * fvbasecolor;
15. Float4 fvtotaldiffuse = diffuse * diff * fvbasecolor;
16. Return fvtotalambient + fvtotaldiffuse + specular;
17. }

10. Running result:

This instance can be automatically generated by adding default effect-> DirectX-> texturedphong;

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.