3D Programming: Environment Mapping

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

Environment Mapping

Another application of texture cubes is environment mapping. Environment mapping, also known as reflection mapping, is used to approximate reflective surfaces (reflective surfaces), such as a metal bumper plated on a car.

The processing of environment mapping is slightly more complex than skybox because of the need to compute the reflection vectors of light and surface. The reflection vector is mainly computed by the view direction (limit angle vector) and the surface's normal vector. The specific formula is as follows:

R = i–2 * n * (I N)

Wherein, I represents the amount of the shot, n refers to the normal vector of the surface. Listing 8.2 lists a environment mapping effect code.

List 8.2 environmentmapping.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 Envcolor:color < string uiname = "Environment COLOR";
	String uiwidget = "Color";
	
	> = {1.0f, 1.0f, 1.0f, 1.0f}; FLOAT3 Cameraposition:cameraposition < string uiwidget= "None";
>;
	} cbuffer cbufferperobject {float4x4 worldviewprojection:worldviewprojection < string uiwidget= ' None '; >; float4x4 World:world < string uiwidget= "None";
	
	>;
		Float Reflectionamount < String uiname = "Reflection Amount";
		String uiwidget = "Slider";
		float uimin = 0.0;
		float Uimax = 1.0;
	float uistep = 0.05;
> = {0.5f};
	} texture2d colortexture < string resourcename = "Default_color.dds";
	String uiname = "Color texture";
String resourcetype = "2D";

>; Texturecube Environmentmap < String uiname =  "Environment Map";
String resourcetype = "3D";

>;

Samplerstate Trilinearsampler {Filter = min_mag_mip_linear;};

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

};
	struct Vs_output {float4 position:sv_position; 
	FLOAT2 texturecoordinate:texcoord0;
FLOAT3 Reflectionvector:texcoord1;

};
	
	/************* 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); FLOAT3 worldposition = Mul (in.
	Objectposition, World). XYZ;
	Float3 incident = normalize (worldposition-cameraposition); FLOAT3 normal = normalize (Mul float4 (in.
	
	Normal, 0), world). xyz); Reflection Vector for cube map:r = i-2*n * (I.N) out. Reflectionvector = Reflect (incident, normal);
return out;
	
	/************* Pixel Shader *************/float4 pixel_shader (vs_output in): sv_target {float4 out = (float4) 0; FLOAT4 color = colortexture.sample (Trilinearsampler, in.
	Texturecoordinate);	
	FLOAT3 ambient = get_vector_color_contribution (Ambientcolor, COLOR.RGB); FLOAT3 environment = Environmentmap.sample (Trilinearsampler, in.
	reflectionvector). RGB;
	
	FLOAT3 reflection = get_vector_color_contribution (Envcolor, Environment);
	Out.rgb = Lerp (ambient, reflection, Reflectionamount);

	OUT.A = COLOR.A;
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);
 }
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.