Home Forums GAMES在线课程(现代计算机图形学入门)讨论区 作业1 高阶任务 绕X、Y轴旋转效果图 是否正确? Reply To: 作业1 高阶任务 绕X、Y轴旋转效果图 是否正确?

#10810 Score: 2
炎炎夏日丽丽在目
Participant
2 pts

void rst::rasterizer::set_pixel(const Eigen::Vector3f& point, const Eigen::Vector3f& color)
{
//old index: auto ind = point.y() + point.x() * width;
if (point.x() < 0 || point.x() >= width ||
point.y() < 0 || point.y() >= height) return;
auto ind = (height-point.y())*width + point.x();
// if out of range, return
if (ind >= width * height) return;
frame_buf[ind] = color;
}
闪退是因为在set_pixel计算ind的时候,超出屏幕外的三角形边上的点对应的ind会大于或者等于width * weight,而frame_buf的大小为width * weight,所以会发生数组访问越界错误,加上这个判断即可

This post has received 2 votes up.