DirectX 11:无法从Model类呈现模型 [英] DirectX 11: Model not rendering from Model class

查看:197
本文介绍了DirectX 11:无法从Model类呈现模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好!开始。我已经更新了代码。但是,经过数小时的看似完美的代码调试,我仍然无法发现问题。我围绕创建顶点和索引缓冲区以及类绘制调用设置了多个断点。

Ok! Here goes. I've updated my code. However, after hours of debugging seemingly perfect code, I can't spot the problem. I've set up multiple breakpoints around the Vertex and Index buffer creation, and the class draw call.

我已经创建了一个临时的 vtest 结构用于测试。

I've created a temporary vtest struct for the purposes of testing. It carries the definition.

struct vtest{
    XMFLOAT3 Vertex;
    XMFLOAT4 Color;
};

链接到适当的IA抽象:

Linked to a proper IA abstraction:

D3D11_INPUT_ELEMENT_DESC InputElementDesc[] = {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },  
};

(全部返回(HRESULT)S_OK

D3D11_BUFFER_DESC BufferDescription;
ZeroMemory(&BufferDescription, sizeof(BufferDescription));

BufferDescription.Usage = D3D11_USAGE_DYNAMIC;
BufferDescription.ByteWidth = sizeof(vtest) * sz_vBuffer;
BufferDescription.BindFlags = D3D11_BIND_VERTEX_BUFFER;
BufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
BufferDescription.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA SRData;
ZeroMemory(&SRData, sizeof(SRData));
SRData.pSysMem = test;
SRData.SysMemPitch = 0;
SRData.SysMemSlicePitch = 0;

hr = Device->CreateBuffer(&BufferDescription, &SRData, &g_vBuffer);
D3D11_MAPPED_SUBRESOURCE MappedResource;
ZeroMemory(&MappedResource, sizeof(MappedResource));

vtest 结构填充正确,并且:

The vtest struct fills proper, and:

DeviceContext->Map(g_vBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &MappedResource);

成功,也带有(HRESULT)S_OK

索引初始化如下:(一维 DWORD 索引数组。)

Indices initialized as such:(One-dimensional DWORD array of indices.)

D3D11_BUFFER_DESC iBufferDescription;
ZeroMemory(&iBufferDescription, sizeof(iBufferDescription));

iBufferDescription.Usage = D3D11_USAGE_DEFAULT;
iBufferDescription.ByteWidth = sizeof(DWORD)*sz_iBuffer;
iBufferDescription.BindFlags = D3D11_BIND_INDEX_BUFFER;
iBufferDescription.CPUAccessFlags = NULL;
iBufferDescription.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA iSRData;
iSRData.pSysMem = Indices;

hr = direct3D.Device->CreateBuffer(&iBufferDescription, &iSRData, &g_iBuffer);

IA Set ... 调用是在 draw()调用中:

The IA Set... calls are in the draw() call:

DeviceContext->IASetVertexBuffers(0, 1, &g_vBuffer, &stride, &Offset);
DeviceContext->IASetIndexBuffer(g_iBuffer, DXGI_FORMAT_R32_UINT, 0);

其他设置:(编辑:更正值以显示配置。)

Other settings: ( Corrected values to show configuration.)

D3D11_RASTERIZER_DESC DrawStyleState;
DrawStyleState.AntialiasedLineEnable = false;
DrawStyleState.CullMode = D3D11_CULL_NONE;
DrawStyleState.DepthBias = 0;
DrawStyleState.FillMode = D3D11_FILL_SOLID;
DrawStyleState.DepthClipEnable = false;
DrawStyleState.MultisampleEnable = true;
DrawStyleState.FrontCounterClockwise = false;
DrawStyleState.ScissorEnable = false;

我的深度模具代码。

D3D11_TEXTURE2D_DESC DepthStenDescription;
ZeroMemory(&DepthStenDescription, sizeof(D3D11_TEXTURE2D_DESC));

DepthStenDescription.Width = cWidth;
DepthStenDescription.Height = cHeight;
DepthStenDescription.MipLevels = 0;
DepthStenDescription.ArraySize = 1;
DepthStenDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
DepthStenDescription.SampleDesc.Count = 1;
DepthStenDescription.SampleDesc.Quality = 0;
DepthStenDescription.Usage = D3D11_USAGE_DEFAULT;
DepthStenDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL;
DepthStenDescription.CPUAccessFlags = 0;
DepthStenDescription.MiscFlags = 0;

D3D11_DEPTH_STENCIL_VIEW_DESC DSVDesc;
ZeroMemory(&DSVDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
DSVDesc.Format = DSVDesc.Format;
DSVDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
DSVDesc.Texture2D.MipSlice = 0;

最后,我的实体类 draw()方法:

And finally, my entity class draw() method:

void    Entity::Draw(){
    UINT stride = sizeof(vtest);
    UINT Offset = 0;

    ObjectSpace = XMMatrixIdentity();
    m_Scale = Scale();
    m_Rotation = Rotate();
    m_Translate = Translate();

    ObjectSpace = m_Scale*m_Rotation*m_Translate;
    mWVP = ObjectSpace*direct3D.mView*direct3D.mProjection;

    LocalWorld.mWorldVP = XMMatrixTranspose(wWVP);

    DeviceContext->UpdateSubresource(direct3D.MatrixBuffer, 0, NULL, &LocalWorld, 0, 0);
    DeviceContext->VSSetConstantBuffers(0, 1, &direct3D.MatrixBuffer);

    DeviceContext->IASetVertexBuffers(0, 1, &g_vBuffer, &stride, &Offset);
    DeviceContext->IASetIndexBuffer(g_iBuffer, DXGI_FORMAT_R32_UINT, 0);
    DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    DeviceContext->DrawIndexed(e_Asset.sz_Index, 0, 0);
}

代码已编译,并且后缓冲区正确显示,但没有模型。

The code compiles, and the backbuffer presents correctly, but no model.

DirectX的初始化函数似乎也很好...

Initialization of DirectX functions seem to be fine too...

更新根据Banex的建议,使用Visual Studio DirectX调试工具会导致我的 .hlsl 文件可能出错了。

Update From Banex's suggestion, using the Visual Studio DirectX Debugging tools yield that I may have gone wrong in my .hlsl file.

我想我在着色器初始化上也可能出错了,因为我的着色器确实很简单,并且可以作为vert / pix传递文件使用:

I think also I may have gone wrong at shader initialization, since my shader really is simple, and really works as a vert/pix pass-through file:

推荐答案

在检查了 .hlsl 文件并进行进一步调试之后;设置 output.position = position; 而不是与世界矩阵相乘,而是在屏幕上绘制模型,这意味着矩阵计算错误,从而导致极度扭曲或空值,存储在常量缓冲区中。

After examining the .hlsl file and doing further debugging; setting output.position = position; rather than being multiplied by the world matrix, the model was drawn on screen, implying a bad matrix calculation causing an extreme warp, or null values, stored in the constant buffer.

cbuffer ConstantBuffer:register(b0)
{
    float4x4 WVP;
}

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
    VOut output;
    output.position = position;// mul(position, WVP);
    output.color = color;

    return output;
}


float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
    return color;
}

这篇关于DirectX 11:无法从Model类呈现模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆