Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shuaitq opened this issue Jun 6, 2017 · 1 comment

Comments

@shuaitq
Copy link

shuaitq commented Jun 6, 2017

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

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) };有什么作用。
希望作者能够指导一下。
十分感谢!

@salarwalker
Copy link

salarwalker commented Jun 6, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants