3D Programming: Specular Highlights (mirror light)

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

Specular Highlights (mirror light)

When simulating a diffuse surface, a smooth, matte object is used. This method is suitable for most objects in the scene, and many illumination models are based on this method. However, it is sometimes simulated to simulate the surface of a flash model, such as polished metal or marble floors. Specular highlights is used to simulate these shiny object surfaces.

Phong

There are two ways to simulate specular reflection. The first is the specular reflection part of the Phong reflection model, which is named after the Bui Tuong Phong of the University of Utah, the Phong model. Unlike diffuse shading, specular light is related to the position of the observer (i.e. camera) relative to the surface. As long as you look at a glowing object, observe this yourself, and notice how the gloss of the object changes when you look at object from different directions. The Phong model indicates that the specular light is determined by the angle between the observed direction and the reflected vector of the light. The formula is:

where r is the reflection vector, V represents the direction of observation, and s represents the size of the bright spot (illuminated area). The larger the specular exponent (specular index), the smaller the highlights. The reflection vector can be calculated using the following formula:

R = 2* (N L) *n-l

where n represents the normal vector of the surface, l refers to the light vector.

Listing 6.5 lists a Phong effect code. As before, copy the code to the Nvidia FX composer and assign the corresponding material to an object in the render panel. Phong effect is described in detail below.

List 6.5 phong.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 Lightdirection:direction < string Object = "DirectionalLight0";
		String uiname = "Light Direction";
	String space = "the World";
	
	> = {0.0f, 0.0f, -1.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;

};
	
    /************* 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); Out.	
		
	Lightdirection = normalize (-lightdirection); FLOAT3 worldposition = Mul (in.
	Objectposition, World). XYZ; 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); FLOAT3 viewdirection = Normalize (in.
	Viewdirection);

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

	FLOAT3 ambient = Ambientcolor.rgb * AMBIENTCOLOR.A * COLOR.RGB;
	FLOAT3 diffuse = (FLOAT3) 0;

	FLOAT3 specular = (FLOAT3) 0;

		if (n_dot_l > 0) {diffuse = Lightcolor.rgb * LIGHTCOLOR.A * n_dot_l * COLOR.RGB; R = 2 * (N.L) * N-L FLOAT3 Reflectionvector = Normalize (2 * n_dot_l * normal-lightdirection); Specular = R.v^n with gloss map stored in color texture ' s alpha channel specular = Specularcolor.rgb * Specularcolor		
	. A * min (Pow (Saturate (dot (reflectionvector, viewdirection), specularpower), COLOR.W);
	} Out.rgb = ambient + diffuse + specular;
	
	OUT.A = 1.0f;
return out;
		} technique10 main10 {pass P0 {SetVertexShader (Compileshader (Vs_4_0, Vertex_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.