3D Programming: Sixth Chapter lighting Models

Source: Internet
Author: User
Tags constant data structures scalar

Sixth Chapter Lighting Models

In the real world, there is no light that cannot be seen, an object can be seen, either by reflecting light or by itself. When using computer rendering, simulating the interaction of light can make 3D objects more realistic. But the interaction of illumination is a very complex process and cannot be easily replicated between different frame rates (at least for the present stage). Thus, the approximation or illumination model that uses the illumination to interact with the 3D objects to achieve more detail in the scene. This chapter mainly introduces some basic illumination models.

Ambient lighting (ambient light)

In a light environment, ambient light does not seem to be in the distance. For example, you can still see the details of a corner even if there is no light directly to the farthest corner of the table. This ambient light is the result of the interaction of countless rays of light on the surface of an object. When a beam of light hits the surface of the object, all or part of the light is either reflected or absorbed, and lasts. So some of the light hits the bottom corner of the table, even if it's only a little.

A simple approximation of ambient light can be achieved by changing some of the constant values to modify a pixel color value. These constants can be viewed as illumination brightness or intensity coefficients, between 0 and 1, and each pixel is multiplied by this factor to obtain an approximate value of ambient light. The closer the ambient light intensity value is to 0, the darker the object looks. In addition, a color can be included in the ambient light model to simulate a light source that is not pure white. This requires the calculation of a pixel of red, green, and blue channel values. Listing 6.1 lists an ambient light effect code.

List 6.1 ambientlighting.fx

/************* *************/#define FLIP_TEXTURE_Y 1 cbuffer cbufferperframe {float4 Ambientcolor:ambien
		T < string uiname = "Ambient Light";
	String uiwidget = "Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};
	} cbuffer cbufferperobject {float4x4 Worldviewprojection:worldviewprojection < string uiwidget = "None";
>;
	} texture2d colortexture < string resourcename = "Default_color.dds";
	String uiname = "Color texture";
String resourcetype = "2D";

>;
	Samplerstate Colorsampler {Filter = min_mag_mip_linear;
	Addressu = WRAP;
ADDRESSV = WRAP;

};

Rasterizerstate disableculling {cullmode = NONE;
	/************* Data Structures *************/struct Vs_input {float4 objectposition:position;
FLOAT2 Texturecoordinate:texcoord;

};
	struct Vs_output {float4 position:sv_position;
FLOAT2 Texturecoordinate:texcoord;

}; /************* Utility functions *************/float2 get_corrected_texture_coordinate (Float2 texturecoordinate) {#If Flip_texture_y return Float2 (texturecoordinate.x, 1.0-texturecoordinate.y);
#else return texturecoordinate; #endif}/************* Vertex Shader *************/vs_output vertex_shader (vs_input in) {vs_output out = (vs_output) 0
	; Out. Position = Mul (in.
	Objectposition, worldviewprojection); Out.
		Texturecoordinate = Get_corrected_texture_coordinate (in.
	Texturecoordinate);
return out;
	/************* Pixel Shader *************/float4 pixel_shader (vs_output in): sv_target {float4 out = (float4) 0; out = Colortexture.sample (Colorsampler, in.
	Texturecoordinate); Out.rgb *= Ambientcolor.rgb * AMBIENTCOLOR.A;
Color (. RGB) * intensity (. a) return out; }/************* Techniques *************/technique10 Main10 {pass P0 {SetVertexShader (Compileshader, Vs_4_0
		X_shader ()));
		Setgeometryshader (NULL);
		SetPixelShader (Compileshader (Ps_4_0, Pixel_shader ()));
	Setrasterizerstate (disableculling);
 }
}

Ambientcolor Shader Constant

You may have noticed that this code is the same as most of the code in the previous chapter texture map effect. Use the previous effects code, is to better learn the knowledge behind.

The first, unlike the previous chapter, adds a FLOAT4 type of shader constant Ambientcolor that represents the color and intensity of the environment. The color value of the light is stored in the RGB channel, and the strength value is stored in the alpha channel. And Ambientcolor uses a new cbuffer,cbufferperframe. Looking back at chapter fourth, the constant buffers discussed in "hello,shaders" usually chooses the Cbuffers type based on the frequency of updates expected by the data contained in the Cbuffer. In this case, the Ambientcolor value is not only for use in multiple objects, but also for each frame update. This is in contrast to the cbufferperobject used in the Worldviewprojection object, Cbufferperobject represents the ambientlighting effect for each object, not every frame.

The Ambientcolor constant has a corresponding ambient semantics. As with any other shader constant, ambient semantics is optional, but it is a good practice to set semantics on a variable, because an application with a semantic CPU side can access the value of the variable through semantics instead of using hard-coded name.

Finally, it should be noted that the Ambientcolor constants are initialized to {1.0f, 1.0f, 1.0f, 1.0f}. Indicates that this is a pure white, maximum intensity ambient light that does not change the objects output color. Therefore, even ignoring the variable in the CPU-side application has no negative effect on the output.

The rest of the code is the same as the code in the texture map effect until pixel shaderf is different.

Ambient lighting Pixel Shader

The pixel shader of the ambient illumination Model first sample texture color, then calculates the illumination output. Specifically, the RGB channel values for the colors that are exported by the texture sample are multiplied by the product of the AMBIENTCOLOR.RGB * ambientcolor.a. A vector (Ambientcolor.rgb float3 type) is multiplied by a scalar (ambientcolor.a float type) to multiply each component of a vector by a scalar. Therefore, the color of the ambient light is first adjusted by the illumination intensity (that is, the illumination color is multiplied by the illumination intensity), and then the RGB channel value of the sampled output is multiplied with the product.

Ambient lighting Output

Figure 6.1 is based on the texture map effect plus the ambient lighting effect applied to a sphere output result. The pure white light is used in the left image, and the illumination intensity factor is 0.5 (that is, the alpha channel value is 0.5). On the right, the red light (the red channel value is 1.0, the green and blue channel values are 0), and the strength is 1 (the alpha channel value is 1.0).

Figure 6.1 ambientlighting.fx applied to a sphere with the texture of Earth with a pure-white,

Contact Us

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.