Home Forums GAMES在线课程(现代计算机图形学入门)讨论区 作业3:小牛正面或背面正对镜头时出现缝隙 Reply To: 作业3:小牛正面或背面正对镜头时出现缝隙

#11032 Score: 0
happyfire
Participant
4 pts

找到原因了,作业3框架Texture类getColor方法有bug:

Eigen::Vector3f getColor(float u, float v)
{
auto u_img = u * width;
auto v_img = (1 – v) * height;
auto color = image_data.at<cv::Vec3b>(v_img, u_img);
return Eigen::Vector3f(color[0], color[1], color[2]);
}

uv坐标转像素坐标的时候,不能直接乘width和height。这是因为uv坐标的范围是[0,1],而像素坐标的范围是[0,width-1]和[0,height-1]。如果直接乘width,height。那么uv为1的时候,坐标就会越界。但是作业框架使用的库对于越界的像素坐标并不会crash或报错,而是返回一个透明值,因此小牛uv为1的地方就没有颜色了。
正确的方法是:

auto u_img = u * (width-1);
auto v_img = (1 – v) * (height-1);

另外打个广告,以GAMES101的理论为基础,在Unity平台上实现了一个光栅化渲染器,同时支持多核和GPGPU加速
https://github.com/happyfire/URasterizer

Attachments:
You must be logged in to view attached files.