2015-04-30 65 views
0

我正在使用DirectX Toolkit加載FBX模型的項目。據我所知,DXTK不支持模型的HLSL着色器,所以我必須從模型對象中獲取模型信息(頂點緩衝區,索引緩衝區等),並實現一個標準的Direct3D繪圖,如果我想使用HLSL着色器爲了渲染。帶定製HLSL着色器的DirectX Toolkit加載模型頂點着色器輸入簽名

我的問題是,我無法達到頂點着色器的紋理座標。爲了測試,我設置頂點着色器穿過座標像素着色器,其中,I與紋理顏色整個對象座標這樣的:

float4(input.texCoord, 0.0f, 1.0f); 

在結果中,整個對象是黑色的,所以texcoords在任何地方都是(0.0,0.0)。我用DXTK Model :: Draw(...)函數檢查了模型,並且它具有紋理正確的方式,所以我的模型和我的模型加載代碼似乎是正確的。

我瞭解到,DXTK模型加載使用下面的頂點緩衝聲明:

const D3D11_INPUT_ELEMENT_DESC VertexPositionNormalTangentColorTexture::InputElements[] = 
{ 
    { "SV_Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "NORMAL",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "TANGENT",  0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "COLOR",  0, DXGI_FORMAT_R8G8B8A8_UNORM,  0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,  0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
}; 

於是,我就符合這樣我的頂點着色器輸入結構:

struct VertexShaderInput 
{ 
    float3 pos : SV_Position; 
    float3 normal : NORMAL; 
    float4 tangent : TANGENT; 
    float4 color : COLOR; 
    float2 texCoord : TEXCOORD; 
}; 

這是我如何加載型號:

void SceneObject::LoadMesh(
    std::shared_ptr<DX::DeviceResources> deviceResources, 
    const wchar_t* modelFile) 
{ 
    auto device = deviceResources->GetD3DDevice(); 
    EffectFactory fx(device); 
    this->model = Model::CreateFromCMO(device, modelFile, fx, true); 
} 

這裏是我的繪圖功能:

void SceneObject::Draw(std::shared_ptr<DX::DeviceResources> deviceResources) 
{ 
    auto device = deviceResources->GetD3DDevice(); 
    auto context = deviceResources->GetD3DDeviceContext(); 
    CommonStates states(device); 

    context->UpdateSubresource(
     this->vsConstantBuffer.Get(), 
     0, 
     NULL, 
     &this->vsConstantBufferData, 
     0, 
     0); 

    context->UpdateSubresource(
     this->psConstantBuffer.Get(), 
     0, 
     NULL, 
     &this->psConstantBufferData, 
     0, 
     0); 

    //model->Draw(context, states, local, view, proj); 
    XMVECTOR qid = XMQuaternionIdentity(); 
    const XMVECTORF32 scale = { 1.f, 1.f, 1.f }; 
    const XMVECTORF32 translate = { 0.f, 0.f, 0.f }; 
    //XMVECTOR rotate = XMQuaternionRotationRollPitchYaw(0, XM_PI/2.f, -XM_PI/2.f); 
    XMVECTOR rotate = XMQuaternionRotationRollPitchYaw(0, 0, 0); 
    XMMATRIX worldMatrix = XMLoadFloat4x4(&this->vsConstantBufferData.model); 
    XMMATRIX local = XMMatrixMultiply(worldMatrix, XMMatrixTransformation(g_XMZero, qid, scale, g_XMZero, rotate, translate)); 
    //this->model->Draw(context, states, local, XMLoadFloat4x4(&vsConstantBufferData.view), XMLoadFloat4x4(&vsConstantBufferData.projection), false); 

    XMStoreFloat4x4(&this->vsConstantBufferData.model, local); 

    for each(auto& mesh in model->meshes) 
    { 
     for each (auto& part in mesh->meshParts) 
     { 
      context->IASetVertexBuffers(
       0, 
       1, 
       part->vertexBuffer.GetAddressOf(), 
       &part->vertexStride, 
       &part->vertexOffset 
       ); 

      context->IASetIndexBuffer(
       part->indexBuffer.Get(), 
       part->indexFormat, 
       0 
       ); 

      context->IASetPrimitiveTopology(part->primitiveType); 

      //context->IASetInputLayout(inputLayout.Get()); 
      context->IASetInputLayout(part->inputLayout.Get()); 

      // Attach our vertex shader. 
      context->VSSetShader(
       vertexShader.Get(), 
       nullptr, 
       0 
       ); 

      // Send the constant buffer to the graphics device. 
      context->VSSetConstantBuffers(
       0, 
       1, 
       vsConstantBuffer.GetAddressOf() 
       ); 

      // Attach our pixel shader. 
      context->PSSetShader(
       pixelShader.Get(), 
       nullptr, 
       0 
       ); 

      // Send the constant buffer to the graphics device. 
      context->PSSetConstantBuffers(
       1, 
       1, 
       psConstantBuffer.GetAddressOf() 
       ); 

      context->PSSetShaderResources(0, 1, diffuseTexture.GetAddressOf()); 
      context->PSSetSamplers(0, 1, linearSampler.GetAddressOf()); 

      // Draw the objects. 
      context->DrawIndexed(
       part->indexCount, 
       part->startIndex, 
       0 
       ); 
     } 
    } 
} 

如果你需要更多的代碼,你可以在這裏查看我的整個項目:https://github.com/GiGu92/WaterRenderingDemo

什麼我搞亂?

+0

我設法暫時解決了這個問題,因爲我實現了程序生成的平面網格,但是您仍然可以在上面的github鏈接上瀏覽我的項目。看到提交「小的變化(texcoords仍然不工作)」,這是我仍然試圖用我的着色器繪製加載模型的地方。 –

回答

1

上面的代碼並不表示您在哪裏創建頂點着色器。

對於CMO,請看Src\Shaders\DGSLEffect.fx瞭解它們如何與自定義着色器一起使用。

默認EffectFactoryDGSLEffectFactory正在建立一個標準頂點&像素着色器用於呈現Model作爲BasicEffectSkinnedEffect,或DGSLEffect。有關自定義渲染的詳細信息,請參閱wiki,但我建議您首先使用默認效果渲染它,如您所期望的那樣。請參閱tutorial

如果需要,通過多種方式,您可以覆蓋整個管道:

  • 實現自己的IEffect *接口和IEffectFactory而直接與ModelMesh渲染與型號裝載機用
  • 覆蓋着色器/ ModelMeshParts

對於CMO文件,頂點格式確實是要麼VertexPositionNormalTangentColorTextureVertexPositionNormalTangentColorTextureSkinning,雖然SDKMESH文件它是一個多一點VA可變結構。

一如既往,確保啓用了debug device並檢查任何返回一個Direct3D函數的HRESULT,以確保您不會遺漏某些配置或API使用問題。