
#include <windows.h>
#include <TChar.h>
#include <d3d8types.h>
#include <d3dx8.h>

/*
C2 to C5 holds the transposed (WorldxViewxProjection) matrix.
V0 = Input vertex position.
*/

static const char g_szVertexShaderString[] = 
"vs.1.1			; Shader version 1.1	\n"
"dp4 oPos.x  v0, c2	; output to clip space	\n"
"dp4 oPos.y  v0, c3	; output to clip space	\n"
"dp4 oPos.z  v0, c4	; output to clip space	\n"
"dp4 oPos.w  v0, c5	; output to clip space	\n" ;



DWORD		g_VSHandle ;

void YourVertexShaderCreationCode()
{
	DWORD dwDecl0[] =
	{
		D3DVSD_STREAM(0),					// start
		D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ),	// position and it’s size
		D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3 ), 	// vertex normal and it’s size
		D3DVSD_REG(D3DVSDE_DIFFUSE,  D3DVSDT_D3DCOLOR ),	// Diffuse color
		D3DVSD_REG(D3DVSDE_TEXCOORD0,D3DVSDT_FLOAT2 ),	// texture co-ordinates
		D3DVSD_END()						// end
	};

	if(!CreateThisVertexShader(NULL, g_szVertexShaderString, sizeof(g_szVertexShaderString)-1,
		dwDecl0, &g_VSHandle))
	{
		// handle error
	}
}



// common function for creating all vertex shaders.
BOOL CreateThisVertexShader(LPDIRECT3DDEVICE8 device8, const char* szSource, 
							const DWORD dwStringLength, DWORD dwDecl[], 
							DWORD* pdwHandle)
{
	HRESULT				hr ;
	char				szBuffer[128] = { 0 } ;
	LPD3DXBUFFER		pshader0 = 0 ;
	LPD3DXBUFFER		perrors = 0 ;
	DWORD				*pdwFunction = 0 ;
	
	
	// assemble and crete your shader
	// Assemble the shader	
	hr = D3DXAssembleShader(szSource , dwStringLength, 0 , NULL , &pshader0 , &perrors) ;
	if(FAILED(hr))
	{
		LPTSTR lpStr1 = _T("\n\nFailed to assemble CharBlend4VertexShader, errors:\n") ;
		OutputDebugString(lpStr1) ;
		lpStr1 = (char*)(perrors->lpVtbl->GetBufferPointer((IDirect3DVolume8*)perrors)) ;
		OutputDebugString("\n") ;
		OutputDebugString(lpStr1);
		OutputDebugString("\n") ;
		return FALSE ;
	}

	// Create the vertex shader
	pdwFunction = (DWORD*)pshader0->lpVtbl->GetBufferPointer((IDirect3DVolume8*)pshader0) ;
    hr = device8->lpVtbl->CreateVertexShader(device8, dwDecl, pdwFunction,
                                           pdwHandle, 0) ;
	if(FAILED(hr))
	{
		LPTSTR	lpStr1 = _T("\n\nFailed to create VertexShader, errors:\n") ;
		OutputDebugString(lpStr1) ;
		D3DXGetErrorStringA(hr , szBuffer, sizeof(szBuffer)) ;
		OutputDebugString(szBuffer) ;
		OutputDebugString("\n") ;
		return FALSE ;
	}

	return TRUE ;
}