还有另一个深度缓冲问题;-)我读了其他人的问题,但似乎找不出我做错了什么。基本上,我的问题是一个经典的“最后绘制的对象在早期绘制的对象上渲染”的问题。但是为什么呢?
我正在使用D3D11,C++,遵循一些基本的教程,并且我已经将对象特定的代码从我的整个D3D呈现类抽象到一个单独的对象类中。所以这里有一堆临时代码,只是为了帮助我弄清楚我可以在对象类中保留什么,以及在更高级别上渲染每个帧需要什么。
下面是我的D3D类的render函数中的内容(代码如下)。
我觉得我遗漏了一些明显的东西。我应该为每个绘制的对象使用单独的常量缓冲区还是缓冲区指针,或者是其他什么?我已经花了几个小时来研究这个问题--这对比我更高级的人来说可能是显而易见的;-)
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);
}谢谢!
发布于 2015-02-04 00:38:05
好吧,我弄清楚了--这真是个愚蠢的错误--我只需要更新输出合并阶段的渲染目标(在我启用深度测试之前,它最初被设置为null )。所以下面的函数只需要更新就可以包含我的深度缓冲区作为第三个参数:
//Set the render target
devcon->OMSetRenderTargets(1, &backbuffer, zbuffer);不敢相信我错过了(然后花了几乎一整天的时间来解决它!)。
https://stackoverflow.com/questions/28287053
复制相似问题