Game Development Reference
In-Depth Information
Note: To reiterate, the D3DXCompileShaderFromFile is the func-
tion that would return the compiled shader code ( shader ).
17.3.3 Setting a Vertex Shader
After we have obtained a pointer to an IDirect3DVertexShader9
interface that represents our vertex shader, we can enable it using the
following method:
HRESULT IDirect3DDevice9::SetVertexShader(
IDirect3DVertexShader9* pShader
);
The method takes a single parameter where we pass a pointer to the
vertex shader that we wish to enable. To enable the shader we created
in section 17.3.2, we would write:
Device->SetVertexShader(ToonShader);
17.3.4 Destroying a Vertex Shader
As with all Direct3D interfaces, to clean them up we must call their
Release method when we are finished with them. Continuing to use
the vertex shader we created in section 17.3.2, we have:
d3d::Release<IDirect3DVertexShader9*>(ToonShader);
17.4 Sample Application: Diffuse Lighting
As a warm-up to creating and using vertex shaders, we write a vertex
shader that does standard diffuse lighting per vertex with a directional
(parallel) light source. As a recap, diffuse lighting calculates the amount
of light that a vertex receives based on the angle between the vertex
normal and the light vector (which points in the direction of the light
source). The smaller the angle, the more light the vertex receives, and
the larger the angle, the less light the vertex receives. If the angle is
greater than or equal to 90 degrees, the vertex receives no light. Refer
back to section 13.4.1 for a more complete description of the diffuse
lighting algorithm.
We begin by examining the vertex 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. These variables are
// initialized from the application.
//
Search WWH ::




Custom Search