Thread: DirectX10 friendly vertex declarations

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    DirectX10 friendly vertex declarations

    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
    Last edited by VirtualAce; 11-26-2006 at 06:47 AM.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Except how likely is it game developers will switch to using just DirectX 10? Remember how long it took Windows XP to catch on? I'm sure most companies for a while will be doing both so as to get the most sales. Have fun keeping up with both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple (?) problem rendering a vertex buffer
    By Dark_Phoenix in forum Game Programming
    Replies: 4
    Last Post: 08-11-2007, 07:32 PM
  2. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  3. Vertex buffer hell
    By cboard_member in forum Game Programming
    Replies: 2
    Last Post: 12-03-2006, 06:07 AM
  4. DX9 visualization problem
    By darcome in forum Game Programming
    Replies: 4
    Last Post: 03-05-2003, 12:40 PM
  5. Pixel Shaders.
    By Cheeze-It in forum Game Programming
    Replies: 1
    Last Post: 05-21-2002, 01:16 AM