Direct X 11 问题

Direct X 11 Problems

所以我正在用 DX11 制作游戏引擎,这是一个奇怪的问题,我已经有一段时间了。

http://gfycat.com/MerryLikableGreyhounddog

我以为是背面法线,但这没什么区别。 我尝试了剔除模式。运气不好

.gif 中要注意的是手榴弹的别针。请注意它并非始终半隐身,而是仅当它位于手榴弹的相机侧时才隐身。

我会 post 一些代码。请问还想看吗

这是从 .obj 文件加载数据的函数。

bool CModel::LoadModel(char* filename)
{
struct Coord
{
    float x, y, z;
};

struct UV
{
    float u, v;
};

struct Face
{
    int v1, t1, n1;
    int v2, t2, n2;
    int v3, t3, n3;
};

//Check to see if the file exists
bool result = CheckFile(filename);
if(!result) return false;

std::ifstream file;

char line, slash;

Coord*  vertex  = new Coord[m_VertexCount];
UV*     uvs     = new UV[m_TextureCount];
Coord*  normals = new Coord[m_NormalCount];
Face*   face    = new Face[m_FaceCount];

m_VertexCount = m_TextureCount = m_NormalCount = m_FaceCount = 0;

file.open(filename);

//Read in the file
file.get(line);
while(!file.eof())
{
    if(line == 'v')
    {
        file.get(line);

        //Data is describing the vertex coordinates
        if(line == ' ')
        {
            file>>vertex[m_VertexCount].x>>vertex[m_VertexCount].y>>vertex[m_VertexCount].z;

            //Invert z axis
            vertex[m_VertexCount].z = vertex[m_VertexCount].z * -1.0f;
            m_VertexCount++;
        }

        //Data is describing the UV coordinates
        if(line == 't')
        {
            file>>uvs[m_TextureCount].u>>uvs[m_TextureCount].v;

            //Invert y axis
            uvs[m_TextureCount].v = 1.0f - uvs[m_TextureCount].v;
            m_TextureCount++;
        }

        //Data is describing the normal coordinates
        if(line == 'n')
        {
            file>>normals[m_NormalCount].x>>normals[m_NormalCount].y>>normals[m_NormalCount].z;

            //Invert z axis
            normals[m_NormalCount].z = normals[m_NormalCount].z * -1.0f;
            m_NormalCount++;
        }
    }

    //Data is describing the face coordinates
    if(line == 'f')
    {
        file.get(line);
        if(line == ' ')
        {
            file>>face[m_FaceCount].v3>>slash>>face[m_FaceCount].t3>>slash>>face[m_FaceCount].n3
                >>face[m_FaceCount].v2>>slash>>face[m_FaceCount].t2>>slash>>face[m_FaceCount].n2
                >>face[m_FaceCount].v1>>slash>>face[m_FaceCount].t1>>slash>>face[m_FaceCount].n1;
            m_FaceCount++;
        }
    }

    //Read rest of line
    while(line != '\n')
        file.get(line);

    //Next line
    file.get(line);
}
file.close();

//Put data into model structure
int vIndex, tIndex, nIndex = 0;

std::ofstream fout;
fout.open("model.txt");
//Finally put all the data into the model
//Since the faces are stored in threes it gets pretty messy
fout<<"Vertex Count : "<<m_FaceCount*3<<std::endl;

for(int i=0; i<m_FaceCount; i++)
{
    vIndex = face[i].v1 - 1;
    tIndex = face[i].t1 - 1;
    nIndex = face[i].n1 - 1;

    fout<< vertex[vIndex].x << ' ' << vertex[vIndex].y << ' ' << vertex[vIndex].z << ' '
        << uvs[tIndex].u << ' ' << uvs[tIndex].v << ' '
        << normals[nIndex].x << ' ' << normals[nIndex].y << ' ' << normals[nIndex].z << std::endl;

    vIndex = face[i].v2 - 1;
    tIndex = face[i].t2 - 1;
    nIndex = face[i].n2 - 1;

    fout<< vertex[vIndex].x << ' ' << vertex[vIndex].y << ' ' << vertex[vIndex].z << ' '
        << uvs[tIndex].u << ' ' << uvs[tIndex].v << ' '
        << normals[nIndex].x << ' ' << normals[nIndex].y << ' ' << normals[nIndex].z << std::endl;

    vIndex = face[i].v3 - 1;
    tIndex = face[i].t3 - 1;
    nIndex = face[i].n3 - 1;

    fout<< vertex[vIndex].x << ' ' << vertex[vIndex].y << ' ' << vertex[vIndex].z << ' '
        << uvs[tIndex].u << ' ' << uvs[tIndex].v << ' '
        << normals[nIndex].x << ' ' << normals[nIndex].y << ' ' << normals[nIndex].z << std::endl;
}

深度缓冲区代码:

//Initialise the depth buffer description
ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));

depthBufferDesc.Width = width;
depthBufferDesc.Height = height;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;

//Create texture for the depth buffer
result = m_Device->CreateTexture2D(&depthBufferDesc, NULL, &m_DepthStencilBuffer);
if(FAILED(result)) return false;

//Initialise the stencil state description
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));

depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;

depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;

//Operations if pixel is front-facing.
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

//Operations if pixel is back-facing.
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

//Create  depth stencil state
result = m_Device->CreateDepthStencilState(&depthStencilDesc, &m_DepthStencilState);
if(FAILED(result)) return false;

//Set depth stencil state
m_DeviceContext->OMSetDepthStencilState(m_DepthStencilState, 1);

//Initailse the depth stencil view.
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

//Set up depth stencil view description.
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;

//Create the depth stencil view.
result = m_Device->CreateDepthStencilView(m_DepthStencilBuffer, &depthStencilViewDesc, &m_DepthStencilView);
if(FAILED(result)) return false;

//Bind render target view and depth stencil buffer to render pipeline
m_DeviceContext->OMSetRenderTargets(1, &m_RenderTargetView, m_DepthStencilView);

光栅状态代码:

//Setup the raster description
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;

//Create the rasterizer state
result = m_Device->CreateRasterizerState(&rasterDesc, &m_RasterState);
if(FAILED(result)) return false;

//Set the rasterizer state.
m_DeviceContext->RSSetState(m_RasterState);

我很笨。 当我初始化 D3D 设备时,我不小心将近距离剪辑距离设置为远距离剪辑,反之亦然,所以一切都被渲染到前面。

记住孩子们总是会检查您传递给函数的内容!