ZBuffer是像空间消隐算法,首先在三维空间中记住物体的深度信息,将物体投影到二维平面后,用一个全局的缓冲器存储每个像素对应的Z值,这样只绘制距离相机最近的表面。
关键是要有一个缓冲器,然后是计算每个像素对应的Z值,这个Z值是在相机坐标系下的距离。
坐标转换
bool CCamera::getZbufferPoint(CPoint3D& ScreenPoint, CPoint3D& localPoint, double screenHeight, double screenWidth)
{
arma::Mat<double> point3DMat({
localPoint.x,localPoint.y,localPoint.z,1 });
arma::Mat<double> point3DMatTemp = point3DMat * localRotateMat * ModelMat * CameraMat;
arma::Mat<double> point2DMat = point3DMatTemp * perspectiveMat;
CPoint2D viewPoint(point2DMat(0), point2DMat(1));
CPoint2D ScreenPoint2D;
if (viewToScreen(ScreenPoint2D, viewPoint, angle, screenHeight, screenWidth))
{
ScreenPoint.x = ScreenPoint2D.x;
ScreenPoint.y = ScreenPoint2D.y