Shadder and Effect - 2.4 Sample Application: Scatter Light (Part)

zhaozj2021-02-16  50

2.4 Sample Application: Scattering Light (on)

Read this article shows that you have agreed to the statement

As the warm-up of the vertex shader, we write a vertex shader that uses a direction (parallel) to perform standard scatter light for each vertex. Briefly, the scattered light calculates the number of light received by the vertex based on the vertex normal and light vectors (which point to the light source direction). The smaller the angle, the more the light received by the vertex; the greater the angle, the less the light received by the vertex. If the angle is greater than or equal to 90 degrees, the vertices are not received by the light.

We start with the shader code:

// file: Diffuse.txt

// DESC: Vertex Shader That Does Diffuse Lighting.

//

// Global Variables We use to hold the view matrix, projection matrix,

// Ambient Material, Diffuse Material, And The Light Vector That

// Describes The Direction to the Light Source. Thase Variables Are

// Initialized from the application.

//

Matrix viewmatrix;

Matrix ViewProjmatrix;

Vector ambientmtrl;

Vector diffusemtrl;

Vector LightDirection;

//

// Global Variables Used to Hold The Ambient Light Intensity (Ambient

// light the light source EMITS) AND THE DIFFUSE LIGHT

// Intensity (Diffuse Light The Light Source EMITS). THESE

// Variables Are Initialized here in the shader.

//

Vector DiffuseLightintensity = {0.0F, 0.0F, 1.0F, 1.0F};

Vector ambientlightintensity = {0.0F, 0.0F, 0.2F, 1.0F};

//

// Input and output structures.

//

Struct vs_INPUT

{

Position: position;

VECTOR NORMAL: NORMAL;

}

Struct vs_output

{

Position: position;

Vector Diffuse: Color;

}

//

// main

//

Vs_output main (vs_input input)

{

// Zero Out All Members of the Output INSTANCE.

Vs_output output = (vs_output) 0;

//

// Transform Position to Homogeneous Clip Space

// and store in the output.position member.

//

Output.position = MUL (Input.position, ViewProjmatrix);

//

// Transform Lights and Normals to View Space. Set W

// Components to Zero Since We're Transforming VECTORS

// Here and not points.//

LightDirection.w = 0.0f;

Input.NORMAL.W = 0.0F;

LightDirection = MUL (LightDirection, ViewMatrix);

Input.Normal = MUL (Input.Normal, ViewMatrix);

//

// compute cosine of the Angle Between Light and Normal.

//

FLOAT S = Dot (LightDirection, Input.Normal);

//

// recall what if the Angle Between the Surface and Light

// is Greater Than 90 Degrees The Surface Receives No Light.

// thus, if the Angle Is Greater Than 90 Degrees We set

// s To Zero So That The Surface Will Not Be LIT.

//

IF (S <0.0F)

S = 0.0F;

//

// Ambient Light Reflected Is Comput By Performing A

// Component-Wise Multiplication with the Ambient Material

// Vector and the Ambient Light Intensity Vector.

//

// Diffuse Light Reflected Is Comput By Performing A

// Component-Wise Multiplication with the Diffuse Material

// Vector and the diffuse light intensity vector. further

// We Scale Each Component by The Shading Scalar S, Which

// shades the color based on how much light the vertex received

// from the light source.

//

// the sum of Both the Ambient and Diffuse Components Give

// US ou final vertex color.

//

Output.diffuse = (Ambientmtrl * AmbientlightIntensity)

(S * (DiffuseLightIntensity * Diffusemtrl);

Return Output;

}

[Declaration]: Introduction to 3D Game Programming With DirectX 9.0 "in this article, it is limited to the translator level, and it is inevitable that there is a mistake in the text. Welcome all netizen criticism; this article is only used for learning exchange and reference usage, not to use In any form of commercial use; if you need to reprint the author's consent of the author and the translator, maintain the integrity of the article, indicate the author, translator and source, the author retains all rights to translation. For the consequences of violating the above terms, the translator is not responsible for this. My MSN is raymond_king123@hotmail.com, welcome 3D graphics and games, and friends with a certain graphic programming exchange.

转载请注明原文地址:https://www.9cbs.com/read-23173.html

New Post(0)