The translation of raymondking into introduction to 3D game programming with DirectX 9.0

Source: Internet
Author: User
Tags mul
Today, we can see "Introduction to 3D game programming with DirectX 9.0" Translated by raymondking on csdn. it is a coincidence that it can be used as a supplement to learning glsl.

I tried to analyze the scattering Illumination code implemented by HLSL in section 2.4 and found that the principle is basically the same as that of glsl.

The code corresponding to section 6.2 of OpenGL shading Language

// 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. These 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 {
Vector position: position;
Vector normal: normal;
};

Struct vs_output {
Vector 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.
//
/** // * Coordinates the vertex coordinates to the cropping coordinate system.
In glsl, this operation is generally performed in the last step.
*/
Output. Position = MUL (input. Position, viewprojmatrix );
Gl_position = Gl_modelviewprojectionmatrix * Gl_vertex;

//
// Transform lights and normals to view space. Set W
// Components to zero since we're re transforming Vectors
// Here and not points.
//
Lightdirection. W = 0.0f;
Input. Normal. W = 0.0f;

/** // * Convert the light direction and normal to the eye coordinate system.
In HLSL code, the light direction is passed in by parameters.
In glsl code, the coordinates of the light source are input by parameters, and the direction vector of the light is calculated.
*/
Lightdirection = MUL (lightdirection, viewmatrix );
Input. Normal = MUL (input. Normal, viewmatrix );
Vec3 ecposition = Vec3 (gl_modelviewmatrix * Gl_vertex );
Vec3 lightvec = Normalize (lightposition- Ecposition );
Vec3 tnorm = Normalize (gl_normalmatrix * Gl_normal );

//
// Compute cosine of the angle between light and normal.
//
/** // * Use the line direction and normal to obtain the cosine of the angle between the two.
*/
Float S = dot (lightdirection, input. Normal );
Float diffuse = max (dot (lightvec, tnorm), 0.0 );

//
// Recall that if the angle between the surface and light
// Is greater than 90 degrees the surface has es no light.
// Thus, if the angle is greater than 90 degrees we set
// S to zero so that the surface will not be empty.
//
If (S <0.0f)
S = 0.0f;

//
// Ambient light reflected is computed by sort Ming
// Component-wise multiplication with the ambient Material
// Vector and the ambient light intensity vector.
//
// Diffuse light reflected is computed by sort Ming
// 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 encoded ed
// From the light source.
//
// The sum of both the ambient and diffuse components give
// Us our final vertex color.
//

/** // * Effect of ambient light and scattered light on the illumination effect
The effects of materials are ignored in glsl code, while those of mirror light are ignored in HLSL code.
*/
Output. Diffuse = (ambientmtrl * ambientlightintensity) +
(S * (diffuselightintensity * diffusemtrl ));
Lightintensity = Diffusecontribution * diffuse + specularcontribution * Spec;

Return output;
}

Raymondking's blog address is: http://blog.csdn.net/raymondking/

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.