Xna DX11中的不可见几何体

Xna DX11中的不可见几何体,xna,alignment,directx,directx-11,xna-math-library,Xna,Alignment,Directx,Directx 11,Xna Math Library,在花了好几个小时来理解和弥补微软在Windows8中对D3DX的反对之后,我遇到了一个无法摆脱的问题 我第一次启动程序时,由于访问冲突,程序变得不朽。重新启动我的计算机,重新编译并运行相同的代码后,它没有崩溃,但我应该看到的多维数据集不在那里 我还收到一条警告,“堆上声明的对象可能未对齐”。根据我的研究,这通常是因为XNA数学。因此,在发现后,我几乎尝试了所有建议。除了XMFLOAT4X4解决方案外,其他一切都正常工作,但我的多维数据集仍然不可见。以下是我认为相关的代码部分: class IEG

在花了好几个小时来理解和弥补微软在Windows8中对D3DX的反对之后,我遇到了一个无法摆脱的问题

我第一次启动程序时,由于访问冲突,程序变得不朽。重新启动我的计算机,重新编译并运行相同的代码后,它没有崩溃,但我应该看到的多维数据集不在那里

我还收到一条警告,“堆上声明的对象可能未对齐”。根据我的研究,这通常是因为XNA数学。因此,在发现后,我几乎尝试了所有建议。除了XMFLOAT4X4解决方案外,其他一切都正常工作,但我的多维数据集仍然不可见。以下是我认为相关的代码部分:

class IEGame : public DX11InfernalEngineBase
{
public:
    //...
bool LoadContent()
{
    //...
    Vertex vertices[] =
    {
        { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) }
    };

    D3D11_BUFFER_DESC vertexDesc;
    ZeroMemory(&vertexDesc, sizeof(vertexDesc));
    vertexDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexDesc.ByteWidth = sizeof(VertexPos)*24;

    D3D11_SUBRESOURCE_DATA resourceData;
    ZeroMemory(&resourceData, sizeof(resourceData));
    resourceData.pSysMem = vertices;

    try{
        d3dresult = d3dDevice_->CreateBuffer(&vertexDesc, &resourceData, &vertexBuffer_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Vertex Buffer", MB_OK);
        return false;
    }

    WORD indices[] = {
        3, 1, 0, 2, 1, 3,
        6, 4, 5, 7, 4, 6,
        11, 9, 8, 10, 9, 11,
        14, 12, 13, 15, 12, 14,
        19, 17, 16, 18, 17, 19,
        22, 20, 21, 23, 20, 22
    };

    D3D11_BUFFER_DESC indexDesc;
    ZeroMemory(&indexDesc, sizeof(indexDesc));
    indexDesc.Usage = D3D11_USAGE_DEFAULT;
    indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexDesc.ByteWidth = sizeof(WORD)* 36;
    indexDesc.CPUAccessFlags = 0;
    resourceData.pSysMem = indices;

    try{
        d3dresult = d3dDevice_->CreateBuffer(&indexDesc, &resourceData, &indexBuffer_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Index Buffer", MB_OK);
        return false;
    }

    try{
        std::vector<byte> textureFile = LoadFile("C:\\Users\\Marcus\\documents\\visual studio 2013\\Projects\\infernalEngine\\Debug\\guide.png");
        d3dresult = CreateWICTextureFromMemory(d3dDevice_, d3dContext_, textureFile.data(), textureFile.size(), nullptr, &colorMapView_, 0);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Loading Texture", MB_OK);
        return false;
    }

    D3D11_SAMPLER_DESC colorMapDesc;
    ZeroMemory(&colorMapDesc, sizeof(colorMapDesc));
    colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;

    try{
        d3dresult = d3dDevice_->CreateSamplerState(&colorMapDesc, &colorMapSampler_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Sampler State", MB_OK);
        return false;
    }

    D3D11_BUFFER_DESC constDesc;
    ZeroMemory(&constDesc, sizeof(constDesc));
    constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    constDesc.ByteWidth = sizeof(XMMATRIX);
    constDesc.Usage = D3D11_USAGE_DEFAULT;

    try{
        d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &viewCB_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating View Matrix", MB_OK);
        return false;
    }
    try{
        d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &projCB_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Projection Matrix", MB_OK);
        return false;
    }
    try{
        d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &worldCB_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating World Matrix", MB_OK);
        return false;
    }

    viewMatrix_ = XMMatrixIdentity();
    projMatrix_ = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

    viewMatrix_ = XMMatrixTranspose(viewMatrix_);
    projMatrix_ = XMMatrixTranspose(projMatrix_);

    return true;
}

void UnloadContent()
{
    if (colorMapSampler_) colorMapSampler_->Release();
    if (colorMapView_) colorMapView_->Release();
    if (solidColorVS_) solidColorVS_->Release();
    if (solidColorPS_) solidColorPS_->Release();
    if (inputLayout_) inputLayout_->Release();
    if (vertexBuffer_) vertexBuffer_->Release();
    if (viewCB_) viewCB_->Release();
    if (projCB_) projCB_->Release();
    if (worldCB_) worldCB_->Release();

    colorMapSampler_ = 0;
    colorMapView_ = 0;
    solidColorVS_ = 0;
    solidColorPS_ = 0;
    inputLayout_ = 0;
    vertexBuffer_ = 0;
    viewCB_ = 0;
    projCB_ = 0;
    worldCB_ = 0;
}

void Render()
{
    if (d3dContext_ == 0)
        return;

    float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
    d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);
    d3dContext_->ClearDepthStencilView(depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0);

    unsigned int nStride = sizeof(VertexPos);
    unsigned int nOffset = 0;

    d3dContext_->IASetInputLayout(inputLayout_);
    d3dContext_->IASetVertexBuffers(0, 1, &vertexBuffer_, &nStride, &nOffset);
    d3dContext_->IASetIndexBuffer(indexBuffer_, DXGI_FORMAT_R16_UINT, 0);
    d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    d3dContext_->VSSetShader(solidColorVS_, 0, 0);
    d3dContext_->PSSetShader(solidColorPS_, 0, 0);
    d3dContext_->PSSetShaderResources(0, 1, &colorMapView_);
    d3dContext_->PSSetSamplers(0, 1, &colorMapSampler_);

    XMMATRIX rotationMatrix;
    rotationMatrix = XMMatrixRotationRollPitchYaw(0.0f, 0.7f, 0.7f);
    XMMATRIX translationMatrix;
    translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.6f);

    XMMATRIX worldMatrix;
    worldMatrix = rotationMatrix * translationMatrix;
    worldMatrix = XMMatrixTranspose(worldMatrix);

    d3dContext_->UpdateSubresource(worldCB_, 0, nullptr, &worldMatrix, 0, 0);
    d3dContext_->UpdateSubresource(viewCB_, 0, nullptr, &viewMatrix_, 0, 0);
    d3dContext_->UpdateSubresource(projCB_, 0, nullptr, &projMatrix_, 0, 0);

    d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
    d3dContext_->VSSetConstantBuffers(1, 1, &viewCB_);
    d3dContext_->VSSetConstantBuffers(2, 1, &projCB_);

    d3dContext_->DrawIndexed(36, 0, 0);

    swapChain_->Present(0, 0);
}

private:
ID3D11VertexShader * solidColorVS_;
ID3D11PixelShader * solidColorPS_;

ID3D11InputLayout * inputLayout_;
ID3D11Buffer * vertexBuffer_;
ID3D11Buffer * indexBuffer_;

ID3D11ShaderResourceView * colorMapView_;
ID3D11SamplerState * colorMapSampler_;

ID3D11Buffer * viewCB_;
ID3D11Buffer * projCB_;
ID3D11Buffer * worldCB_;
XMMATRIX viewMatrix_;
XMMATRIX projMatrix_;
};

更新:我的XMMatrix现在已正确对齐。但立方体仍然是不可见的。我也找不到Visual Studio图形调试器。根据我的研究,我知道它应该在哪里,只是它不在那里

这里的关键问题是,您正在使用
XMMATRIX
,它需要16字节的对齐方式作为堆分配的类成员,但是x86(32位)上的
new
默认情况下只提供8字节对齐方式

这正是
XMFLOAT4X4
XMLoadFloat4x4
XMStoreFloat4x4
要解决的问题

XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

XMStoreFloat4x4( &viewMatrix_, XMMatrixTranspose(vm) );
XMStoreFlaot4x4( &projMatrix_, XMMatrixTranspose(pm) );

XMFLOAT4X4 viewMatrix_;
XMFLOAT4X4 projMatrix_;
或者您可以切换到x64(64位)本机编码,默认情况下16字节对齐,并且您可以使用
XMMATRIX
XMVECTOR
作为类成员,在大多数情况下不必担心对齐问题

这在MSDN中有详细说明。看

您还应该查看中的包装器。这些类型用C++“魔术”处理这个加载/存储行为:

#包括
XMMATRIX vm=XMMatrixIdentity();
XMMATRIX pm=xmmatrixpspectivefovlh(XM_PIDIV4,800.0f/600.0f,0.01f,100.0f);
viewMatrix=矩阵传输(vm);
projMatrix_xM=MatrixTranspose(pm);
DirectX::SimpleMath::矩阵视图矩阵;
DirectX::SimpleMath::Matrix projMatrix;
如果您尚未看到这些,请务必阅读:


编辑对于Windows桌面DirectX开发,如果尚未使用VS 2013 Professional+,则应使用Visual Studio 2013社区版。它包括VS图形调试器,而VS 2013 Express for Windows Desktop没有。好吧,我终于找到了解决方案。由于VS图形调试器没有给出任何错误,我不得不做一些实际有效的随机操作

我必须先创建光栅化器状态,然后才能看到任何东西。原来我的立方体也是由内而外的,所以我将D3D11_光栅化器_DESC的前逆时针字段设置为false。我还通过在Render()中设置以下行来缩小摄影机:

我还创建了一个剪式矩形;我不知道这是否与解决方案有关

这里是我找到所有相关资料的地方:


感谢大家的帮助,我也用内联函数清除了try-catch的废话。

EDIT:我没有尝试过_-aligned_-malloc()解决方案,因为我不知道如何使用它,也找不到任何合适的例子。你试过打开调试层(D3D11\u-CREATE\u-DEVICE\u-Debug)吗或者使用Visual Studio图形调试器查看出了什么问题?使用异常作为基本错误处理机制在很多级别上都是不好的。只需测试HRESULT,然后抛出异常。通常,当DX功能失败时(您这边没有错误)。。。真的很糟糕,很难恢复。除非它是一个设备(丢失了):这里有一个例子。@Adam Miles:没有,我会马上去做。@CodeAngry:try-catch混乱帮我调试:它让我从函数失败中得到一个可以理解的错误。一旦一切正常,我就会删除。我已经尝试过XMFLOAT4X4解决方案,但它在启动时给了我一个错误(我记不起来与“pDestination”有关)。无论如何,我使用_aligned_malloc()解决了对齐问题。不过,我的问题仍然存在:立方体是看不见的!至于D3DX,我已经做了很多研究。谢谢你给我指点VS图形调试器。
XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

XMStoreFloat4x4( &viewMatrix_, XMMatrixTranspose(vm) );
XMStoreFlaot4x4( &projMatrix_, XMMatrixTranspose(pm) );

XMFLOAT4X4 viewMatrix_;
XMFLOAT4X4 projMatrix_;
#include <SimpleMath.h>

XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

viewMatrix_ = XMMatrixTranspose(vm);
projMatrix_ = XMMatrixTranspose(pm); 

DirectX::SimpleMath::Matrix viewMatrix_;
DirectX::SimpleMath::Matrix projMatrix_;
 translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 4.0f);