Open
Description
我正在写一个光栅化的渲染引擎,从知乎找到这里。
您的代码给了我很大指导作用。
我在看您的代码的时候不知道这三个函数的原理是什么
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) };有什么作用。
希望作者能够指导一下。
十分感谢!
Metadata
Metadata
Assignees
Labels
No labels