2.4 Sample Application: Scatter Light (below)
Read this article shows that you have agreed to the statement
Since we have seen the actual vertex shader code, let us change the way the application is used. This application has the following related global variables:
IDirect3dvertexshader9 * Diffuseshader = 0;
ID3DXCONSTANTTABLE * DIFFUSECONSTTABLE = 0;
ID3DXMESH * TEAPOT = 0;
D3DXHANDLE VIEWMATRIXHANDLE = 0;
D3DXHANDLE ViewProjmaMatrixHandle = 0;
D3DXHANDLE AMBIENTMTRLHANDLE = 0;
D3DXHANDLE DIFFUSEMTRLHANDLE = 0;
D3DXHANDLE LIGHTDIRHANDLE = 0;
D3DXMAMATRIX PROJ;
We have variables to represent the vertex shader and its constant table. We have a grid variable of a teapot, followed by a episode of D3DXHANDLE, whose variable name describes the variables they reference:
The setup function performs the following tasks:
N created a teapot grid
N-compilation vertex shader
n Create a vertex shader according to compiled code
n The handle of several variables in the shader program via a constant table
N initialized these shader variables by constant table
Note: For this application, our vertex structure does not require additional components that cannot be described in any free vertex format. Therefore, in this example, we use a free vertex format instead of the vertex statement. Recall that the free vertex format describes that the internal converted into a vertex statement.
BOOL setup ()
{
HRESULT HR = 0;
//
// CREATE Geometry:
//
D3DXCREATEAPOT (Device, & Teapot, 0);
//
// compile shader
//
ID3DXBUFFER * shader = 0;
ID3DXBUFFER * ERRORBUFFER = 0;
HR = D3DXCompileshaderfromfile
"DIFFUSE.TXT",
0,
0,
"Main", // entry point function name
"vs_1_1",
D3DXSHADER_DEBUG,
& shader,
& ErrorBuffer,
& DIFFUSECONSTTABLE);
// output any error messages
IF (ErrorBuffer)
{
:: MessageBox (0, (char *) errorbuffer-> getBufferPointer (), 0, 0);
D3D: Release
}
IF (Failed (HR))
{
:: MessageBox (0, "D3DXCompileshaderfromFile () - failed", 0, 0);
Return False;
}
//
// CREATE SHADER
//
HR = device-> CreateVertexShader
(DWORD *) Shader-> getBufferPointer (),
& Diffuseshader;
IF (Failed (HR))
{
:: MessageBox (0, "CreatevertexShader - Failed", 0, 0); Return False;
}
D3D :: Release
//
// Get Handles
//
ViewMatrixHandle = DiffuseConstTable-> getConstantByname
0, "ViewMatrix");
ViewProjmatrixHandle = DiffuseConstTable-> getConstantByname
0, "ViewProjmatrix");
AmbientmtrlHandle = DiffuseConstTable-> getConstantByname (
0, "ambientmtrl");
Diffusemtrlhandle = DiffuseConstTable-> getConstantByname
0, "DIFFUSEMTRL");
Lightdirhandle = DiffuseConstTable-> getConstantByname (
0, "LightDirection");
//
// set Shader Constants:
//
// light direction:
D3DXVECTOR4 DIRECTIONTOLIGHT (-0.57F, 0.57F, -0.57F, 0.0F);
DiffuseConstTable-> SetVector (Device, Lightdirhandle,
& directiontolight);
// Materials:
D3DXVECTOR4 Ambientmtrl (0.0F, 0.0F, 1.0F, 1.0F);
D3DXVector4 Diffusemtrl (0.0F, 0.0F, 1.0F, 1.0F);
DiffuseConstable-> setVector (device, ambientmtrlhandle, & ambientmtrl);
DiffuseConstTable-> SetVector (Device, Diffusemtrlhandle, & Diffusemtrl);
DiffuseConstTable-> setDefaults (device);
// Compute Projection Matrix.
D3DXMAMATRIXPERSPECTIVEFOVLH (
& Proj, D3DX PI * 0.25F,
(float) Width / (Float) Height, 1.0F, 1000.0F);
Return True;
}
The Display function is very simple. It detects user input (translator Note: Here is the variable of the incoming shader program input to the user), and update the view matrix. However, because we perform this view matrix transform in the shader, we must also update the view matrix variables in the shader. We complete this matter with a constant table.
Bool Display (Float Timedelta)
{
IF (Device)
{
//
// Update View Matrix Code Snipped ...
//
D3DXMAMATRIX V;
D3DXMAMATRIXLOKATLH (& V, & Position, & Target, & Up);
DiffuseConstTable-> Setmatrix (Device, ViewMatrixHandle, & V);
D3DXMAMATRIX ViewProj = v * proj;
DiffuseConstTable-> SetMatrix (Device, ViewProjmatrixHandle, & ViewProj);
//
// render
//
Device-> Clear (0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
0xffffffff, 1.0F, 0);
Device-> Beginscene ();
Device-> setvertexshader (Diffuseshader);
Teapot-> DrawSubset (0);
Device-> endscene ();
Device-> Present (0, 0, 0, 0);
}
Return True;
}
Also note that before the DrawSubset call, we allow this top shader we want to use.
Cleaning also needs to be completed; we will release this allocated interface:
void cleanup ()
{
D3D :: Release
D3D :: Release
D3D: Release
}
[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.