cobra icon indicating copy to clipboard operation
cobra copied to clipboard

不明白代码中TriangleCheck EdgeFunc Interpolate这三个函数是什么原理,为什么这么写?

Open shuaitq opened this issue 7 years ago • 1 comments

我正在写一个光栅化的渲染引擎,从知乎找到这里。 您的代码给了我很大指导作用。 我在看您的代码的时候不知道这三个函数的原理是什么

static bool TriangleCheck (const Vertex &v0, const Vertex &v1, const Vertex &v2, Vertex &v, Vector4 &w) {
	w.x = EdgeFunc (v1.pos, v2.pos, v.pos) * v0.pos.w / w.w; // pos.w == 1 / pos.z . we did that in Ndc2Screen()
	w.y = EdgeFunc (v2.pos, v0.pos, v.pos) * v1.pos.w / w.w;
	w.z = EdgeFunc (v0.pos, v1.pos, v.pos) * v2.pos.w / w.w;
	return (w.x < 0 || w.y < 0 || w.z < 0);
} // return true if v is outside the triangle
static inline float EdgeFunc (const Vector4 &p0, const Vector4 &p1, const Vector4 &p2) {
	return ((p2.x - p0.x) * (p1.y - p0.y) - (p2.y - p0.y) * (p1.x - p0.x));
} // note that the result of edge function could be represent as area as well.
static inline void Interpolate (const Vertex &v0, const Vertex &v1, const Vertex &v2, Vertex &v, const Vector4 &w) {
	v.pos.z = 1.0f / (w.x + w.y + w.z); // keep in maind that in TriangleCheck() we already done the (w = w * 1/z) part
	v.viewPos = (v0.viewPos * w.x + v1.viewPos * w.y + v2.viewPos * w.z) * v.pos.z;
	v.normal = (v0.normal * w.x + v1.normal * w.y + v2.normal * w.z) * v.pos.z;
	v.color = (v0.color * w.x + v1.color * w.y + v2.color * w.z) * v.pos.z;
	v.uv = (v0.uv * w.x + v1.uv * w.y + v2.uv * w.z) * v.pos.z;
} // yes we interpolate all variables, no matter they are going to be used or not.

还有就是Vector4 weight = { 0, 0, 0, EdgeFunc (v0.pos, v1.pos, v2.pos) };有什么作用。 希望作者能够指导一下。 十分感谢!

shuaitq avatar Jun 06 '17 06:06 shuaitq

EdgeFunc意思就是判断点是否在三角形内。 依据是是否同时连线该点到任一角,连线与某时针序邻边的夹角为锐角。

发件人: Tom smithmailto:[email protected] 发送时间: 2017年6月6日 14:58 收件人: jintiao/cobramailto:[email protected] 抄送: Subscribedmailto:[email protected] 主题: [jintiao/cobra] 不明白代码中TriangleCheck EdgeFunc Interpolate这三个函数是什么原理,为什么这么写? (#1)

我正在写一个光栅化的渲染引擎,从知乎找到这里。 您的代码给了我很大指导作用。 我在看您的代码的时候不知道这三个函数的原理是什么 static bool TriangleCheck (const Vertex &v0, const Vertex &v1, const Vertex &v2, Vertex &v, Vector4 &w) { w.x = EdgeFunc (v1.pos, v2.pos, v.pos) * v0.pos.w / w.w; // pos.w == 1 / pos.z . we did that in Ndc2Screen() w.y = EdgeFunc (v2.pos, v0.pos, v.pos) * v1.pos.w / w.w; w.z = EdgeFunc (v0.pos, v1.pos, v.pos) * v2.pos.w / w.w; return (w.x < 0 || w.y < 0 || w.z < 0); } // return true if v is outside the triangle

static inline float EdgeFunc (const Vector4 &p0, const Vector4 &p1, const Vector4 &p2) {

    return ((p2.x - p0.x) * (p1.y - p0.y) - (p2.y - p0.y) * (p1.x - p0.x));

} // note that the result of edge function could be represent as area as well.

static inline void Interpolate (const Vertex &v0, const Vertex &v1, const Vertex &v2, Vertex &v, const Vector4 &w) {

    v.pos.z = 1.0f / (w.x + w.y + w.z); // keep in maind that in TriangleCheck() we already done the (w = w * 1/z) part

    v.viewPos = (v0.viewPos * w.x + v1.viewPos * w.y + v2.viewPos * w.z) * v.pos.z;

    v.normal = (v0.normal * w.x + v1.normal * w.y + v2.normal * w.z) * v.pos.z;

    v.color = (v0.color * w.x + v1.color * w.y + v2.color * w.z) * v.pos.z;

    v.uv = (v0.uv * w.x + v1.uv * w.y + v2.uv * w.z) * v.pos.z;

} // yes we interpolate all variables, no matter they are going to be used or not.

还有就是Vector4 weight = { 0, 0, 0, EdgeFunc (v0.pos, v1.pos, v2.pos) };有什么作用。 希望作者能够指导一下。 十分感谢!

― You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/jintiao/cobra/issues/1, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGaYWPtn98TZe9wLrSYUwjk2lImHjPS3ks5sBPiogaJpZM4Nw7qi.

salarwalker avatar Jun 06 '17 14:06 salarwalker