简体   繁体   中英

D3D11 Depth Rendering Issue

yet another depth buffer question ;-) I read the others that have been asked, and just can't seem to figure out what I am doing wrong. Basically my issue is a classic "last drawn objects render over earlier drawn objects" one. But why?

I'm using D3D11, C++, following some basic tutorials and I'm half-way through abstracting object specific code out of my overall D3D rendering class and into a separate object class. So there is a bunch of temp code in here just to help me figure what I can stick in my object class and what is needed for each frame render at a higher level.

Here is what I have in my render function of my D3D class (code below).

I feel like I'm missing something obvious... Should I being using a separate constant buffer or buffer pointer for each object drawn, or is it something else? I've been banging my head on this for hours - and its probably obvious to someone more advanced than I ;-)

void D3DClass::RenderFrame(void)
{
    //Create constant buffer object to be passed
    CBUFFER cBuffer;
    cBuffer.LightVector = XMFLOAT4(-1.0f, 1.0f, 0.0f, 0.0f);
    cBuffer.LightColor = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
    cBuffer.AmbientColor = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);

    CBUFFER cBuffer2;
    cBuffer2.LightVector = XMFLOAT4(-1.0f, 1.0f, 0.0f, 0.0f);
    cBuffer2.LightColor = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
    cBuffer2.AmbientColor = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);

    //Matrices for camera and view
    XMMATRIX matView, matProjection;

    //Camera Object Variables and View Matrix
    matView = XMMatrixLookToLH(objCam->Position, objCam->vFwd, objCam->vUp);

    //Projection matrix (3d->2d transform on the camera)
    matProjection = XMMatrixPerspectiveFovLH(
        XMConvertToRadians(45),             // field of view
        float(rws1.Width / rws1.Height),    // aspect ratio
        0.0001,                             // near view-plane
        500.0);                             // far view-plane

    //////////////////////
    //Per frame updates

        //Clear both the back and depth buffers
        float clearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };  //Clear the back buffer to black
        devcon->ClearRenderTargetView(backbuffer, clearColor);  //Back buffer
        devcon->ClearDepthStencilView(zbuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);  //Depth buffer

        //Update constant buffer per frame variables
        //devcon->UpdateSubresource(pCBuffer[1], 0, 0, &PerFrame, 0, 0);    // update cbuffer 1


    /////////////////////
    //Per object updates

        //OBJECT 1
            //Matrices for object scaling, rotation, translation, and the final
            XMMATRIX matScale, matRotate, matTranslate, matFinal;  

            //Update object info (set per object)
            objModel1->RotateRightObject(); //TEMP JUST TO GET SOME MOVEMENT
            objModel1->RotateVectors();

            //Object matrix data (set per object)
            matScale = XMMatrixScaling(objModel1->Scale.x, objModel1->Scale.y, objModel1->Scale.z);
            matRotate = XMMatrixRotationRollPitchYaw(objModel1->RollPitchYawABS[PITCH], objModel1->RollPitchYawABS[YAW], objModel1->RollPitchYawABS[ROLL]);
            matTranslate = XMMatrixTranslationFromVector(objModel1->Position);

            //Combined final transforms (set per object)
            matFinal = matScale * matRotate * matTranslate * matView * matProjection;  //Load matrices into the constant buffer
            cBuffer.Final = matFinal;
            cBuffer.Rotation = matRotate;

            //Set states  (set per object)
            devcon->RSSetState(pRSDefault);                 //Rasterizer state
            devcon->PSSetSamplers(0, 1, &pSS[0]);           //Set the sampler state
            devcon->OMSetBlendState(pBS, 0, 0xffffffff);    //Set the blend state (for transparency)

            //Select which vertex buffer to display (use the object's vertex buffer)
            UINT stride = sizeof(VERTEX);
            UINT offset = 0;
            devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
            devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0);

            //Select which primtive type we are using
            devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

            //Draw the object
            devcon->UpdateSubresource(pCBuffer, 0, 0, &cBuffer, 0, 0);
            devcon->PSSetShaderResources(0, 1, &pTexture);
            devcon->DrawIndexed(CubeNumTriangles * 3, 0, 0);  //NUMBER OF TRIANGLES * 3 (so it knows how many logical vertices it is making)


        //OBJECT 2
            //Matrices for object scaling, rotation, translation, and the final
            XMMATRIX matScale2, matRotate2, matTranslate2, matFinal2;  //scale, rotate, and final are specific to objects

            //Update object info (set per object)
            objModel2->RotateRightObject(); //TEMP JUST TO GET SOME MOVEMENT
            objModel2->RotateVectors();

            //Object matrix data (set per object)
            matScale2 = XMMatrixScaling(objModel2->Scale.x, objModel2->Scale.y, objModel2->Scale.z);
            matRotate2 = XMMatrixRotationRollPitchYaw(objModel2->RollPitchYawABS[PITCH], objModel2->RollPitchYawABS[YAW], objModel2->RollPitchYawABS[ROLL]);
            matTranslate2 = XMMatrixTranslationFromVector(objModel2->Position);

            //Combined final transforms (set per object)
            cBuffer.Final = matScale2 * matRotate2 * matTranslate2 * matView * matProjection;  //Load matrices into the constant buffer
            cBuffer.Rotation = matRotate2;

            //Set states  (set per object)
            devcon->RSSetState(pRSDefault);                 //Rasterizer state
            devcon->PSSetSamplers(0, 1, &pSS[0]);           //Set the sampler state
            devcon->OMSetBlendState(pBS, 0, 0xffffffff);    //Set the blend state (for transparency)

            //Select which vertex buffer to display (use the object's vertex buffer)
            stride = sizeof(VERTEX);
            offset = 0;
            devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
            devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0);

            //Select which primtive type we are using
            devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

            //Draw the object
            devcon->UpdateSubresource(pCBuffer, 0, 0, &cBuffer, 0, 0);
            devcon->PSSetShaderResources(0, 1, &pTexture);
            devcon->DrawIndexed(CubeNumTriangles * 3, 0, 0);  //NUMBER OF TRIANGLES * 3 (so it knows how many logical vertices it is making)


    //Done with updates - switch the back buffer and the front buffer
    swapchain->Present(0, 0);
}

Thank you!!!

Ok, I figured it out - and it was a really dumb mistake - I just needed to update the output-merger stage render target (it was originally set to null before I had depth testing enabled). So the following function just needed updating to include my depth buffer as the 3rd paramater:

//Set the render target
devcon->OMSetRenderTargets(1, &backbuffer, zbuffer);

Can't believe I missed that (and then spent almost a full day trying to solve it!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM