Yes it's time to get rid of the flexible vertex format flags in D3D. They won't be supported in DX10 so if you code for it now, you will hate yourself later.
It also may be a wise idea to move away from D3DMATERIAL9, D3DLIGHT9 and other fixed function functionality like SetTextureStageState(), SetFVF(), and other functions that utilize the fixed function pipeline.
Here are my newest vertex formats. This is similar to what I read in my new book about DirectX by Frank Luna. I just took what he had and expanded it a bit to suit my own needs.
These vertex declarations can be used later in vertex and pixel shaders.
VertexTypes.h
Code:
#include "d3d9.h"
struct VertexPNT1
{
D3DXVECTOR3 vecPos;
D3DXVECTOR3 vecNormal;
D3DXVECTOR2 tex0;
VertexPNT1():vecPos(0.0f,0.0f,0.0f),
vecNormal(0.0f,0.0f,0.0f),
tex0(0.0f,0.0f) { }
VertexPNT1(float x,float y,float z,float u,float v):
vecPos(x,y,z),
vecNormal(0.0f,0.0f,0.0f),
tex0(u,v) { }
static IDirect3DVertexDeclaration9 *pDecl;
};
struct VertexTLT1
{
D3DXVECTOR3 vecPos;
float rhw;
D3DXVECTOR2 tex;
VertexTLT1():vecPos(0.0f,0.0f,0.0f),rhw(0.0f),tex(0.0f,0.0f) { }
VertexTLT1(float x,float y,float z,float u,float v):
vecPos(x,y,z),rhw(1.0f),tex(u,v) { }
static IDirect3DVertexDeclaration9 *pDecl;
};
struct VertexBBT1
{
D3DXVECTOR3 vecPos;
D3DCOLOR dwDiffuse;
D3DCOLOR dwSpecular;
D3DXVECTOR2 tex;
VertexBBT1():vecPos(0.0f,0.0f,0.0f),
dwDiffuse(0),
dwSpecular(0),
tex(0.0f,0.0f) { }
VertexBBT1(float x,float y,float z,float u,float v,D3DCOLOR dwDiffuse=0xFFFFFFFF,
D3DCOLOR dwSpecular=0xFFFFFFFF):
vecPos(x,y,z),dwDiffuse(dwDiffuse),dwSpecular(dwSpecular),
tex(u,v) { }
static IDirect3DVertexDeclaration9 *pDecl;
};
void CreateVertexDecls(IDirect3DDevice9 *pDevice);
void DestroyVertexDecls();
VertexTypes.cpp
Code:
#include "VertexTypes.h"
#include "dxutil.h"
IDirect3DVertexDeclaration9 *VertexPNT1::pDecl=NULL;
IDirect3DVertexDeclaration9 *VertexTLT1::pDecl=NULL;
IDirect3DVertexDeclaration9 *VertexBBT1::pDecl=NULL;
void CreateVertexDecls(IDirect3DDevice9 *pDevice)
{
D3DVERTEXELEMENT9 VertexPNT1_Elements[] =
{
{0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0},
{0,12,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0},
{0,24,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
D3DDECL_END()
};
D3DVERTEXELEMENT9 VertexTLT1_Elements[] =
{
{0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0},
{0,12,D3DDECLTYPE_FLOAT1,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
{0,16,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
D3DDECL_END()
};
D3DVERTEXELEMENT9 VertexBBT1_Elements[]=
{
{0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0},
{0,12,D3DDECLTYPE_D3DCOLOR,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR,0},
{0,16,D3DDECLTYPE_D3DCOLOR,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_COLOR,0},
{0,20,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
D3DDECL_END()
};
pDevice->CreateVertexDeclaration(VertexPNT1_Elements,&VertexPNT1::pDecl);
pDevice->CreateVertexDeclaration(VertexBBT1_Elements,&VertexBBT1::pDecl);
pDevice->CreateVertexDeclaration(VertexTLT1_Elements,&VertexTLT1::pDecl);
}
void DestroyVertexDecls()
{
SAFE_RELEASE(VertexPNT1::pDecl);
SAFE_RELEASE(VertexTLT1::pDecl);
SAFE_RELEASE(VertexBBT1::pDecl);
}
Also all objects now have Shader ID's as well as Texture ID's and my own material ID's. The material is passed onto the shader as a series of constants, the textures are grabbed from the texture manager using the ID's, and the actual ID3DXEffect that encapsulates the shader is grabbed and ran from the effect manager.
Once I get through this transition I hope to have some screenshots of the new system in action as well as some post-processing shaders I'm anxious to try out.
Many of you ask how hard is it to make a game like this. Well we are not even to the game yet and here is a list of the source files.
CBillBoard.cpp CBillBoard.h CCamera.cpp CCamera.h CD3DApp.cpp CD3DApp.h CD3DXMesh.cpp CD3DXMesh.h
CDXAudio.cpp CDXAudio.h CDXInput.cpp CDXInput.h CDXShowWnd.cpp CDXShowWnd.h CEngineException.h
CFileLog.h CGameObject.cpp CGameObject.h CHUDGauge.h CHUDPipper.cpp CHUDPipper.h CHUDSpeedGauge.cpp
CHUDSpeedGauge.h CHUDSys.cpp CHUDSys.h CKeyBinder.h CKeyboard.cpp CKeyboard.h CMouse.cpp CMouse.h
CObjectMgr.h common.h COrient3D.cpp COrient3D.h CParticleObject.h CPixelShader.h CPlanet.cpp CPlanet.h
CResMgr.h CResource.h CShaderMgr.h CShaderRes.h CShip.cpp CShip.h CShipEngine.h CShipMgr.cpp CShipMgr.h
CShipSystem.h CSkyBox.cpp CSkyBox.h CStars.cpp CStars.h CStarXApp.cpp CStarXApp.h CSun.cpp CSun.h
CTexMgr.h CTexture.h CTimer.h CVertexShader.cpp CVertexShader.h CWeapon.h GameControls.h Glare.fx
ScriptCmn.h StarXMain.cpp VertexTypes.cpp VertexTypes.h