How does the tinyrenderer use cross product to get the barycentric coordinates of a point?
- User
How does the tinyrenderer use cross product to get the barycentric coordinates of a point?
- Assistant
TinyRenderer is a simple 3D rendering engine that uses rasterization to render 3D objects on a 2D screen. During rasterization, the engine projects 3D points onto a 2D image plane, and then calculates the color of pixels within the projected area.
One of the first steps in this process is to find the barycentric coordinates of each projected point. Barycentric coordinates are a way to represent a point's position within a triangle using a weighted average of its vertices.
To calculate the barycentric coordinates of a point inside a triangle, TinyRenderer uses the cross product, a mathematical operation that takes two 3D vectors as input and returns a new vector perpendicular to them.
Here is the process:
- Create 2D vectors for each side of the triangle, using the screen-space coordinates of its vertices (A, B, and C).
- Determine the 2D vector representing the position of the point (P) relative to vertex A.
- Calculate the cross products of the following vector pairs: (B - A) x (C - A), (B - A) x (P - A), and (C - A) x (P - A).
- From these cross products, obtain the z-coordinates which will be used now.
- Normalize the z-coordinates of the cross products involving the point P (from steps 3 and 4) by dividing them by the z-coordinate of the cross product involving the triangle sides (from step 3).
- The resulting ratios represent the barycentric coordinates of the point P with respect to the triangle vertices (u, v, w). The sum of these coordinates will be equal to 1, and each coordinate is a weight for one of the triangle vertices.
With these barycentric coordinates, TinyRenderer can perform various operations, such as determining whether the point is inside the triangle (u, v, and w are all non-negative), interpolating vertex attributes (colors, normals, etc.), and computing the depth of the point in camera space.
- User
Explain it more simply. We have a triangle with the vertices A, B, and C. We want to check if the point P is within that triangle. Why do we create vectors with Z values when we are working in 2D? Why do we need to take the cross product?
- Assistant
In simpler terms, TinyRenderer uses the cross product to find the barycentric coordinates of a point to determine whether the point P lies within the triangle ABC in 2D space.
The reason we use Z values and cross product even in 2D is that the cross product can help us determine whether P is on the left side or the right side of a particular edge of the triangle by examining the signs of the resulting Z-values. For instance, with the cross products calculated for (B - A) x (P - A) and (C - A) x (P - A), we can check whether P lies on the same side of the line passing through B and C. Using each pair of triangle vertices and the point P, we check if P lies within the triangle as follows:
- User
But I thought cross product is not defined in 2D space?
- Assistant
You are correct. The cross product is not defined in 2D space; it is an operation specific to 3D vectors. However, one can emulate the behavior of the cross product for 2D vectors using a trick called the "pseudo cross product."
The idea is that you convert your 2D vectors into 3D vectors by setting the Z component to be 0, then compute the cross product. So, given two 2D vectors u(x1, y1) and v(x2, y2), you can consider them as 3D vectors u(x1, y1, 0) and v(x2, y2, 0). The cross product result will also be a 3D vector with X and Y components equal to 0, and the Z component will be the determinant of the 2D vectors. For instance:
Pseudo cross product, P = u x v, where P.x = 0 P.y = 0 P.z = x1 * y2 - x2 * y1
In this case, the Z-value, P.z, represents the signed area of the parallelogram formed by the vectors u and v. By examining the sign of P.z, you can deduce the relative orientation of these vectors.
While you are using 3D vector cross products in the algorithm, the actual operations and the information being used is, in fact, a part of the 2D space; only the Z component of the result is relevant in this context.
- User
With this knowledge, describe how the following code calculates the barycentric coordinates for the point P and why the return value is a 3D vector:
codeinline Vec3<t> operator^(const Vec3<t>& v) const { return Vec3<t>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); }Vec3f barycentric(Vec2i pts, Vec2i P) { Vec3f u = Vec3f(pts[2][0]-pts[0][0], pts[1][0]-pts[0][0], pts[0][0]-P[0])^Vec3f(pts[2][1]-pts[0][1], pts[1][1]-pts[0][1], pts[0][1]-P[1]); /
ptsandPhas integer value as coordinates soabs(u[2])< 1 meansu[2]is 0, that means triangle is degenerate, in this case return something with negative coordinates */ if (std::abs(u.z)<1) return Vec3f(-1,1,1); return Vec3f(1.f-(u.x+u.y)/u.z, u.y/u.z, u.x/u.z); } - Assistant
This code calculates the barycentric coordinates of a 2D point P with respect to a triangle defined by three points (pts array, containing pts[0], pts[1], and pts[2]).
Operator^ is an overloaded version of the cross product between 3D vectors defined as a member function of the Vec3 class. It takes another Vec3 as input and returns the cross product of the current Vec3 object and the input Vec3 object, represented as a new Vec3 object.
The function barycentric performs the following steps:
-
Calculate the 3D vector u by taking the cross product (^ operator) of two specific 3D vectors, where the Z components of these vectors are determined by the differences in X and Y coordinates of the 2D triangle vertices (pts array) and the point P.
-