3D Programming: Fog

Source: Internet
Author: User
Tags bool data structures include mul

Fog

The next few effects are not related to texture cubes, but can help you learn more about different graphics techniques. The first one to discuss is fog, which increases the distance from object to camera, and this effect makes object blur until it disappears completely in the background color.

You can use a color, a distance from camera to module fog effect, which is used to indicate how far from the beginning of the fog, and how far the fog can extend until the color of the object is completely filled by the color of the fog. To apply the final color to an object, first determine the proportion of fog in the color and the proportion of the normal light. As with environment mapping, the process uses linear interpolation, which can be used to get the scale value for interpolation calculations:

Where V is the view vector from the camera to the surface. You can then use the following method to interpolate the final color:

In pixel shader, any illumination model can be used to compute the Litcolor value, and the flog interpolation is performed at the end. A small flog effect code is listed in Listing 8.3.

List 8.3 abbreviated Fog Effect

#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 Lightdirection:direction < string Object = "DirectionalLight0";
		String uiname = "Light Direction";
	String space = "the World";
	
	> = {0.0f, 0.0f, -1.0f};
		FLOAT3 Fogcolor < string uiname = "Fog Color";
	String uiwidget = "Color";

	> = {0.5f, 0.5f, 0.5f};
	Float Fogstart = {20.0f};
	
	Float Fogrange = {40.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;
	FLOAT3 Lightdirection:texcoord1;
	FLOAT3 Viewdirection:texcoord2; Float Fogamount:TEXCOORD3;

}; /************* Utility Functions *************/float get_fog_amount (float3 viewdirection, float fogstart, float Fograng

E) {return saturate (length (viewdirection)-Fogstart)/(Fogrange);} /************* Vertex Shader *************/vs_output vertex_shader (vs_input in, uniform bool fogenabled) {VS_OUTPUT OU
	
	T = (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); Out.

	Lightdirection = normalize (-lightdirection); FLOAT3 worldposition = Mul (in.
	Objectposition, World). XYZ;
	FLOAT3 viewdirection = cameraposition-worldposition; Out.

	Viewdirection = normalize (viewdirection); if (fogenabled) {out.
	Fogamount = Get_fog_amount (viewdirection, Fogstart, Fogrange);
} return out;
}/************* Pixel Shader *************/float4 pixel_shader (vs_output in, uniform bool fogenabled): Sv_target {	FLOAT4 out = (float4) 0; FLOAT3 normal = normalize (in.
	Normal); FLOAT3 viewdirection = Normalize (in.
	Viewdirection); FLOAT4 color = colortexture.sample (Colorsampler, in.
	Texturecoordinate);
	
	FLOAT3 ambient = get_vector_color_contribution (Ambientcolor, COLOR.RGB);
	Light_contribution_data Lightcontributiondata;
	Lightcontributiondata.color = Color;
	Lightcontributiondata.normal = Normal;
	Lightcontributiondata.viewdirection = viewdirection; Lightcontributiondata.lightdirection = Float4 (in.
	Lightdirection, 1);
	Lightcontributiondata.specularcolor = Specularcolor;
	Lightcontributiondata.specularpower = Specularpower;
	Lightcontributiondata.lightcolor = Lightcolor;

	FLOAT3 light_contribution = get_light_contribution (Lightcontributiondata);
	Out.rgb = ambient + light_contribution;	

	OUT.A = 1.0f; if (fogenabled) {Out.rgb = Lerp (Out.rgb, Fogcolor, in.
	Fogamount);
} return out; } technique10 fogenabled {pass P0 {SetVertexShader compileshader (Vs_4_0, Vertex_shader (True));
		Setgeometryshader (NULL);                
			
		SetPixelShader (Compileshader (Ps_4_0, Pixel_shader (true));
	Setrasterizerstate (disableculling);
		} technique10 fogdisabled {pass P0 {SetVertexShader (Compileshader, Vs_4_0 (false));
		Setgeometryshader (NULL);                
			
		SetPixelShader (Compileshader (Ps_4_0, Pixel_shader (false));
	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.