另一个深度缓冲问题;-)我读了其他被问过的问题,似乎无法弄清楚我做错了什么。基本上我的问题是经典的“最后绘制的对象渲染在先前绘制的对象上”一个。但为什么呢?
我正在使用D3D11,C ++,遵循一些基本的教程,我正在将整个D3D渲染类中的对象特定代码抽象到一个单独的对象类中。所以这里有一堆临时代码只是为了帮助我弄清楚我可以在我的对象类中找到什么,以及在更高层次上每个帧渲染需要什么。
以下是我在D3D类的渲染函数中的内容(下面的代码)。
我觉得我错过了一些明显的东西......我应该为每个绘制的对象使用单独的常量缓冲区或缓冲区指针,还是其他的东西?几个小时以来我一直在敲打这个问题 - 这对于比我更先进的人来说可能是显而易见的;-)
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);
}
谢谢!!!
答案 0 :(得分:1)
好吧,我想通了 - 这是一个非常愚蠢的错误 - 我只需要更新输出合并阶段渲染目标(在我启用深度测试之前它最初设置为null)。因此,以下函数只需更新以包含我的深度缓冲区作为第3个参数:
//Set the render target
devcon->OMSetRenderTargets(1, &backbuffer, zbuffer);
无法相信我错过了(然后花了差不多一整天时间试图解决它!)。