0

I'm a new D3D programmer.

When I tried to render a model, I got a strange problem.!

you can see the picture, some part of the model always in front of the others.

kT8hz.png

the model vertex only contains the following data

{
float x, y, z;
float r, g, b;
float u, v;
}

I tried to render it in opengl and webgl ( http://nalol.azurewebsites.net/ ), it works well. but in D3D11, I got this strange problem.

I tried Google and find something about depth, but i don't know how to deal with it.

the following are some part of my code:

HLSL file

struct vout
{
    float4 position : SV_POSITION;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD;
};

vout vshader(float3 position : POSITION, float3 normals : NORMAL, float2 texcoords : TEXCOORD)
{
    vout output;

    output.position = float4(position, 1);
    output.normal = normals ;
    output.texcoord = texcoords;

    return output;
}

Texture2D shaderTexture;
SamplerState SampleType;
float3 pshader(float3 position : POSITION, float3 normals  : NORMAL, float2 texcoords : TEXCOORD) : SV_TARGET
{
    return shaderTexture.Sample(SampleType, texcoords);
}

vertex struct

struct lol_skn_vertex {
    float position[3];
    char bone_index[4]; // for bones and animation, not used here
    float bone_weights[4]; // for bones and animation, not used here
    float normals[3];
    float texcoords[2];
};

input layout object

D3D11_INPUT_ELEMENT_DESC ied[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0,  0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"NORMAL",   0, DXGI_FORMAT_R32G32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

render function

void RenderFrame(void)
{
    FLOAT ColorRGBA[4] = {0.0f, 0.2f, 0.4f, 1.0f};

    d3d11_device_context->ClearRenderTargetView(d3d11_view_rt_backbuffer, ColorRGBA);
    d3d11_device_context->ClearDepthStencilView(d3d11_view_ds,D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL,1.f,0); 

    update();

    UINT stride = sizeof(lol_skn_vertex);
    UINT offset = 0;

    d3d11_device_context->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset);
    d3d11_device_context->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R16_UINT, 0);
    d3d11_device_context->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    d3d11_device_context->DrawIndexed(skn.num_indices, 0, 0);
    // switch the back buffer and the front buffer
    dxgi_swapchain->Present(0, 0);
}

buffer update function

void update() {

    // copy the vertices into the buffer
    D3D11_MAPPED_SUBRESOURCE ms;
    d3d11_device_context->Map(vertex_buffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);    // map the buffer
    memcpy(ms.pData, skn_vertex_buffer, sizeof(lol_skn_vertex) * skn.num_vertices);                 // copy the data
                                        // unmap the buffer

    SYSTEMTIME SystemTime;
    GetSystemTime(&SystemTime);

    float angle = (float)SystemTime.wMilliseconds/1000+SystemTime.wSecond;

    D3DXMATRIX x;
    D3DXMatrixRotationY(&x, angle);
    D3DXVec4TransformArray((D3DXVECTOR4 *)ms.pData, sizeof(lol_skn_vertex), (D3DXVECTOR4 *)ms.pData, sizeof(lol_skn_vertex), &x, skn.num_vertices);
    // use D3DXVECTOR4 for Transform

    d3d11_device_context->Unmap(vertex_buffer, NULL);  
}
Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • 3
    Did you ensured, that you are using a DepthStencilState with Z-Buffering enabled? And did you created a DepthStencilSurface? Maybe this link helps: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205074%28v=vs.85%29.aspx#Create_Depth_Stencil_State – Gnietschow May 22 '13 at 17:42
  • Thanks for reply, but i still a bit confused about the concept of "Depth, Stencil" and "z". In d3d9, what i need to do is SetRenderState(D3DRS_ZENABLE, TRUE), then how to create a DepthStencilState with Z-Buffering? Thanks – yylang1987-old May 23 '13 at 06:01
  • Just read the link, there is the whole creation explained. Or google a little bit there are many examples or tutorials in the web, if the link is too difficult. A little effort from your side would be nice. – Gnietschow May 23 '13 at 07:55
  • Thanks again, Actually I have read the link. And if I understand it right, what I need to do is configure DepthEnable to true, DepthFunc to D3D11_COMPARISON_LESS And StencilEnable to false (which means use z for cliping, but seems not working in my code). Is that right? If so, at least I'm on the right dirction. – yylang1987-old May 23 '13 at 09:44
  • Can you update your question with the new code? The drawcall and the setup would be interesting. – Gnietschow May 23 '13 at 09:50

1 Answers1

0

At last I solved the problem.

I make 2 very stupid mistake.

first: in "input layout object", i use DXGI_FORMAT_R32G32_FLOAT for position, which only contain x and y. so the shader always get 0 on z.

second: my model data is not normalized, which ranged from -50 to 50, so i use D3D11_RASTERIZER_DESC to disable DepthClip and I forgot about it.

Fix this 2 problems and everything works. And great thank to Gnietschow :)