截图:
LPDIRECT3DVERTEXSHADER9 g_pVertexShader = NULL;
LPD3DXCONSTANTTABLE g_pConstantTable = NULL;
LPDIRECT3DVERTEXDECLARATION9 g_pVertexDeclaration = NULL;
OnCreateDevice
D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
D3DDECL_END()
};
V_RETURN( pd3dDevice->CreateVertexDeclaration( decl, &g_pVertexDeclaration ) );
创建vertex shader
// Assemble the vertex shader from the file
V_RETURN( D3DXCompileShaderFromFile( strPath, NULL, NULL, "Ripple",
"vs_2_0", dwShaderFlags, &pCode,
NULL, &g_pConstantTable ) );
// Create the vertex shader
hr = pd3dDevice->CreateVertexShader( ( DWORD* )pCode->GetBufferPointer(),
&g_pVertexShader );
每帧设置shader里面的常量
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
// Update the camera's position based on user input
g_Camera.FrameMove( fElapsedTime );
// Set up the vertex shader constants
D3DXMATRIXA16 mWorldViewProj;
D3DXMATRIXA16 mWorld;
D3DXMATRIXA16 mView;
D3DXMATRIXA16 mProj;
mWorld = *g_Camera.GetWorldMatrix();
mView = *g_Camera.GetViewMatrix();
mProj = *g_Camera.GetProjMatrix();
mWorldViewProj = mWorld * mView * mProj;
g_pConstantTable->SetMatrix( DXUTGetD3D9Device(), "mWorldViewProj", &mWorldViewProj );
g_pConstantTable->SetFloat( DXUTGetD3D9Device(), "fTime", ( float )fTime );
}
Vertex Shader代码
HLSLwithoutEffects.vsh
float4x4 mWorldViewProj; // World * View * Projection transformation
float fTime; // Time parameter. This keeps increasing
//-----------------------------------------------------------------------------
// Vertex shader output structure
//-----------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 Position : POSITION; // vertex position
float4 Diffuse : COLOR0; // vertex diffuse color
};
//-----------------------------------------------------------------------------
// Name: Ripple
// Type: Vertex shader
// Desc: This shader ripples the vertices
//-----------------------------------------------------------------------------
VS_OUTPUT Ripple( in float2 vPosition : POSITION )
{
VS_OUTPUT Output;
float fSin, fCos;
float x = length( vPosition ) * sin( fTime ) * 15.0f;
// This HLSL intrinsic computes returns both the sine and cosine of x
sincos( x, fSin, fCos );
// Change the y of the vertex position based on a function of time
// and transform the vertex into projection space.
Output.Position = mul( float4( vPosition.x, fSin * 0.1f, vPosition.y, 1.0f ), mWorldViewProj );
// Output the diffuse color as function of time and
// the vertex's object space position
Output.Diffuse = 0.5f - 0.5f * fCos;
return Output;
}
posted on 2008-11-18 14:33
七星重剑 阅读(937)
评论(0) 编辑 收藏 引用 所属分类:
Game Graphics 、
HLSL&ShaderMonkey