2011-11-19 52 views
1

我未能以下聲明移動到我的C++頭:如何移動D3DVERTEXELEMENT9聲明到頭

D3DVERTEXELEMENT9 vertexFormat[] = 
{ 
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,0}, 
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,0}, 
{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,0}, 
{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0}, 
{0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0}, 
{1, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, 
{1, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2}, 
{1, 32, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3}, 
{1, 48, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4}, 
D3DDECL_END() 
}; 

什麼問題呢?我該如何解決這個問題?谷歌給我看到一個有趣的結果(http://www.gamedev.net/topic/551682-about-d3dvertexelement9/),但不知何故我仍然無法編譯它:(

要清楚 - 我想用vertexFormat我那一屆學生中的一員

+0

的問題是,你不這樣做是正確的。首先將情況轉化爲絕對最小的例子,以具有代表性的方式執行您想要做的事情(例如,兩個整數的數組),然後查看您是否可以完成*那*工作。如果你仍然在學習C++的基礎知識,那麼沒有必要去做D3D。隨時發佈任何出現的問題。 –

+0

我完全知道這是一個基本的C++問題。我的頭文件包含D3DVERTEXELEMENT9 vertexFormat [10];現在,我的cpp包含一個像D3DVERTEXELEMENT9 t1 = {...};的每個值的非常醜陋的單獨聲明;接着是vertexFormat [0] = t1;然而,這是一個非常難看的解決方案,我正在拼命尋找更好的方法。你能以適當的方式幫助我做到這一點嗎? – Schifty

+1

我相信你只是錯過了一些簡單的東西,比如不在頭文件中聲明extern並在cpp文件中實現。但是使用這個龐大的blob作爲例子是完全不可能的,這就是爲什麼我建議你原則上先用一個非常小的數組來解決這個問題。一旦你指出,你可以在這裏複製/粘貼你的巨大數組。 –

回答

1

我用這可能是有用的是提供一個CPP文件實現的一種方法: (* 我的例子使用的DirectX10,但對於這個答案的DX版本無關) (.h)頭像:

int getLayoutPosTexNormCInstancedSize(); 
D3D10_INPUT_ELEMENT_DESC* getLayoutPosTexNormCInstanced(); 

(的.cpp)Implentation:

D3D10_INPUT_ELEMENT_DESC layoutPosTexNormCInstanced[] = 
{ 
{"POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}, 
{"TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,  0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}, 
{"NORMAL",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20, D3D10_INPUT_PER_VERTEX_DATA, 0}, 
{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 32, D3D10_INPUT_PER_VERTEX_DATA, 0}, 
{"BINORMAL",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 44, D3D10_INPUT_PER_VERTEX_DATA, 0}, 
{"BLENDINDICES", 0, DXGI_FORMAT_R8G8B8A8_SINT,  0, 56, D3D10_INPUT_PER_VERTEX_DATA, 0 }, //4 
{"mTransform", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, 
{"mTransform", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, 
{"mTransform", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, 
{"mTransform", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, 
}; 


D3D10_INPUT_ELEMENT_DESC* getLayoutPosTexNormCInstanced() { return layoutPosTexNormCInstanced; } 
int getLayoutPosTexNormCInstancedSize() { return sizeof(layoutPosTexNormCInstanced)/sizeof(D3D10_INPUT_ELEMENT_DESC); } 

那麼至少,你引用或需要輸入層次創建頂點緩衝區(S)時,你可以簡單地調用getLayoutPosTexNormCInstanced功能,這些也可以輕鬆被移動到一個類/結構體,甚至是由一個可重用模塊提供,就像一個DLL一樣。

0

難道是你在聲明之前忘了「靜態」關鍵詞嗎?

static D3DVERTEXELEMENT9 vertexFormat[] = 
{ 
{0, 0, ....... 

或者一類:

D3DWhatever.h

class D3DWhatever 
{ 
    static D3DVERTEXELEMENT9 vertexFormat[]; 

}; 

D3DWhatever.cpp

D3DWhatever::D3DVERTEXELEMENT9 vertexFormat[] = 
{ 
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,0}, 
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,0}, 
{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,0}, 
{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0}, 
{0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0}, 
{1, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, 
{1, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2}, 
{1, 32, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3}, 
{1, 48, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4}, 
D3DDECL_END() 
};