Home › Forums › GAMES在线课程(现代计算机图形学入门)讨论区 › 两个三角形的覆盖次序问题 › Reply To: 两个三角形的覆盖次序问题
仔细看了看源码解决了,其实pdf里也讲了,是depth_buff初始化的问题,框架里是初始化成了最大值INF,我们要做的就是把初始化改回负的INF即可,然后深度检测就按部就班,按正常的来,即正常地把z看作是负数
具体改法:
1.在rasterizer.cpp的clear函数
把
void rst::rasterizer::clear(rst::Buffers buff)
{
if ((buff & rst::Buffers::Color) == rst::Buffers::Color)
{
std::fill(frame_buf.begin(), frame_buf.end(), Eigen::Vector3f{0, 0, 0});
}
if ((buff & rst::Buffers::Depth) == rst::Buffers::Depth)
{
std::fill(depth_buf.begin(), depth_buf.end(), std::numeric_limits<float>::infinity());
}
}
改成
void rst::rasterizer::clear(rst::Buffers buff)
{
if ((buff & rst::Buffers::Color) == rst::Buffers::Color)
{
std::fill(frame_buf.begin(), frame_buf.end(), Eigen::Vector3f{0, 0, 0});
}
if ((buff & rst::Buffers::Depth) == rst::Buffers::Depth)
{
std::fill(depth_buf.begin(), depth_buf.end(),,std::numeric_limits<float>::infinity()*(-1.0f));//*这里改动*
}
}
2.在写入z-buffer前的判断应该是:目标像素的当前深度值大于z-buffer里的对应位置的深度->写入z-buffer
(因为我们这时候是把各点的z值看作是负的来看,当然实际上也是负的,所以z越大越接近摄像机)
This post has received 2 votes up.