Home › Forums › GAMES在线课程(现代计算机图形学入门)讨论区 › 作业2:三角形底边上出现了圆角
- This topic has 8 replies, 8 voices, and was last updated 3 years, 6 months ago by caspar.
-
AuthorPosts
-
-
APASSBYDREGParticipant
实现如下:
`
// insideTriangle
Eigen::Vector3f px = Eigen::Vector3f({x + 0.5f, y + 0.5f, 0});
auto times_0 = (px – _v[1]).cross(_v[0] – _v[1]);
auto times_1 = (px – _v[2]).cross(_v[1] – _v[2]);
auto times_2 = (px – _v[0]).cross(_v[2] – _v[0]);bool rval = true;
if (times_0.dot(times_1) < 0 || times_0.dot(times_2) < 0 || times_1.dot(times_2) < 0)
{
rval = false;
}
`
`
// resterize
auto v = t.toVector4();// TODO : Find out the bounding box of current triangle.
Vector3f v_3d[3];
v_3d[0] = Vector3f({v[0](0), v[0](1), v[0](2)});
v_3d[1] = Vector3f({v[1](0), v[1](1), v[1](2)});
v_3d[2] = Vector3f({v[2](0), v[2](1), v[2](2)});for (size_t w = 0; w < width; w++)
{
for (size_t h = 0; h < height; h++)
{
if (insideTriangle(w, h, v_3d))
{
int idx = get_index(w, h);auto barycentric_2d = computeBarycentric2D(w + 0.5f, h + 0.5f, t.v);
float alpha = std::get<0>(barycentric_2d), beta = std::get<1>(barycentric_2d), gamma = std::get<2>(barycentric_2d);
float w_reciprocal = 1.0/(alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
float z_interpolated = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
z_interpolated *= w_reciprocal;if (z_interpolated < depth_buf[idx])
{
depth_buf[idx] = z_interpolated;
Vector3f point = Vector3f::Zero();
point.x() = w;
point.y() = h;
set_pixel(point, t.getColor());
}
}
`
(这里还有三角形被中心对称了的问题……)
-
哈哈哈真可爱,你矩阵贴出来看看
This post has received 1 vote up. -
KarlmeisterParticipant
应该是insideTriangle函数写错了,判断三个叉积是不是同方向不能用点乘,而是随便找某一个维上看正负是否相同
-
KarlmeisterParticipant
不对。。好像点乘可以,但确实应该是判断三角形出了问题
-
-
叮叮咣咣Participant
太可爱了,应该是insideTriangle问题,
auto times_0 = (px – _v[0]).cross(_v[1] – _v[0]);
auto times_1 = (px – _v[1]).cross(_v[2] – _v[1]);
auto times_2 = (px – _v[2]).cross(_v[0] – _v[2]);试试这个
-
解决了吗
-
jason411Participant
auto times_0 = (px – _v[0]).cross(_v[1] – _v[0]);
px 和 _v[0] 的深度都不同,减出来没有意义吧?应该把px 和_v 的z分量都统一 -
是不是bounding box没有完全包住。比如你使用左下角与右上角两个点定义bounding box,并且bounding box坐标需要是整数。左下角横纵坐标要是三角形顶点横纵坐标的最小值,求最小值时要使用向下取整。同理,右上角是最大值,求最大值时要向上取整。
不知道是不是这个问题
-
问题出在insideTriangle使用dot判断错误,假如auto times_0 = (px – _v[1]).cross(_v[0] – _v[1])做叉乘那么对应的dot应该是和(_v[2] – _v[1]).cross(_v[0] – _v[1])进行点乘,并且结果大于等于0;表示这个点在这个夹角内,同理再计算其余两条边全部大于等于0则点在三角形内。不过也可以不用dot来判断,直接取time_012三个值同时小于0或者同时大于0则点在三角形内。
-
-
AuthorPosts
- You must be logged in to reply to this topic.