3D Programming: 7th Chapter Additional Lighting Models

Source: Internet
Author: User
Tags data structures include mul

7th Chapter Additional Lighting Models

This chapter introduces point lights,spotlights on the basis of the previous chapter illumination model and adds a variety of light sources to the scene. The HLSL and effect framework will be further studied in the course of the tutorial.

Point Light

A point light in a scene is like a light bulb somewhere around you, and the light from the bulb radiates to all directions. This is exactly the opposite of directional light, where directional light is infinitely far away and light comes from the same direction. Directional lights does not have a "moving light" concept, and point lights does not have the concept of "rotating light source."

As with directional lights, point lights uses the same diffuse and specular illumination models. In point lights, however, you need to provide light position and compute the light vector. The Light vector is calculated simply by subtracting the position of the object with the Light postion (all two positions are located in the world). Figure 7.1 Depicts a point light and a light vector calculation method.

Figure 7.1 An illustration's a point light and the computed light vector. (Stone wall Texture by Nick

Zuccarello, Florida Interactive Entertainment Academy.)

In addition, because point lights has a specific coordinate position, the illumination can be attenuated according to the distance between the light source and the surface. The farther away the light is from the surface, the less brightness it produces. Listing 7.1 lists the code for a simple point light effect.

List 7.1 pointlight.fx

#include ". \\include\\Common.fxh "/************* Resources *************/cbuffer cbufferperframe {float4 ambientcolor:ambient
		< string uiname = "Ambient Light";
	String uiwidget = "Color";

	> = {1.0f, 1.0f, 1.0f, 0.0f};
		FLOAT4 Lightcolor:color < string Object = "LightColor0";
		String uiname = "Light Color";
	String uiwidget = "Color";

	> = {1.0f, 1.0f, 1.0f, 1.0f};
		FLOAT3 Lightposition:position < string Object = "PointLight0";
		String uiname = "Light Position";
	String space = "the World";

	> = {0.0f, 0.0f, 0.0f};
		Float Lightradius < String uiname = "Light Radius";
		String uiwidget = "Slider";
		float uimin = 0.0;
		float Uimax = 100.0;
	float Uistep = 1.0;
	
	> = {10.0f}; FLOAT3 Cameraposition:cameraposition < string uiwidget= "None";
>;
	} cbuffer cbufferperobject {float4x4 worldviewprojection:worldviewprojection < string uiwidget= ' None '; >; float4x4 World:world < string uiwidget= "None";
	
	>; Float4 Specularcolor:specular < String uiname = "Specular Color";
	String uiwidget = "Color";

	> = {1.0f, 1.0f, 1.0f, 1.0f};
		Float Specularpower:specularpower < String uiname = "specular power";
		String uiwidget = "Slider";
		float Uimin = 1.0;
		float Uimax = 255.0;
	float Uistep = 1.0;
> = {25.0f};
	} 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;
FLOAT3 Normal:normal;

};
	struct Vs_output {float4 position:sv_position;
	FLOAT3 Normal:normal;	
	FLOAT2 texturecoordinate:texcoord0;
	FLOAT4 Lightdirection:texcoord1;
FLOAT3 Viewdirection:texcoord2;

}; /************* 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); Out. Normal = Normalize (Mul (FLOAT4) (in. 
	
	Normal, 0), world). xyz); FLOAT3 worldposition = Mul (in.
	Objectposition, World). XYZ;
	FLOAT3 lightdirection = lightposition-worldposition; Out.
	LIGHTDIRECTION.XYZ = normalize (lightdirection); Out. LIGHTDIRECTION.W = Saturate (1.0f-(Length (lightdirection)/Lightradius)); Attenuation out.
	
	Viewdirection = normalize (cameraposition-worldposition);
return out;
		
	/************* Pixel Shader *************/float4 pixel_shader (vs_output in): sv_target {float4 out = (float4) 0; FLOAT3 normal = normalize (in.
	Normal); FLOAT3 lightdirection = Normalize (in.
	LIGHTDIRECTION.XYZ); FLOAT3 viewdirection = Normalize (in.
	Viewdirection);	
	float n_dot_l = dot (normal, lightdirection); FLOAT3 Halfvector = NormaliZe (lightdirection + viewdirection);

	float N_dot_h = dot (normal, halfvector); FLOAT4 color = colortexture.sample (Colorsampler, in.
	Texturecoordinate);

	Float4 lightcoefficients = Lit (n_dot_l, n_dot_h, specularpower);
	FLOAT3 ambient = get_vector_color_contribution (Ambientcolor, COLOR.RGB); FLOAT3 diffuse = get_vector_color_contribution (Lightcolor, LIGHTCOEFFICIENTS.Y * color.rgb) * in.
	LIGHTDIRECTION.W; FLOAT3 specular = get_scalar_color_contribution (Specularcolor, Min (lightcoefficients.z, COLOR.W)) * in.

	LIGHTDIRECTION.W;
	Out.rgb = ambient + diffuse + specular;
	
	OUT.A = 1.0f;
return out; }/************* Techniques *************/technique10 Main10 {pass P0 {SetVertexShader (Compileshader, Vs_4_0
		Ex_shader ()));
		Setgeometryshader (NULL);
			
		SetPixelShader (Compileshader (Ps_4_0, Pixel_shader ()));
	Setrasterizerstate (disableculling);
 }
}

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.