Calculating bounding box values from contained triangle vertices

Started by
6 comments, last by irreversible 11 years, 3 months ago

[attachment=13502:uv.jpg]

What I have is an arbitrary triangle and its bounding box in 2D. Each of the triangle's vertices may carry the usual attributes like UV coordinates, color, etc. Problem is, I need to find the interpolated values of any present attributes within the triangle at arbitrary points (eg from fragment screen space coordinates). Another problem is I'm not keeping any data associated with the triangle other than the actual vertices and its bounding box.

Ergo I figured the fastest solution would probably be to calculate the interpolated values at the corners of the bounding box, from where it's a simple matter of bilerping the two sides to get the value at any location. For UV-s this is easy, but color and normals are turning out to be a bit of a headache.

Or it could be that it's 5 AM and I'm just braindead.

The attached image contains two out of three possible generic cases (the last one is that all of the triangle's vertices match the bounding box's corners).

To recap the problem: I know color (or any other arbitrary) values at v0, v1 and v2 and I need to find them at p0, p1, p2 and p3. I can only assume to have access to normalized and absolute bounding box coordinates and triangle vertices.

Advertisement

You could calculate the barycentric coordinates of the point you are interested in and then use the barycentric coordinates to weight the vertex values.

The barycentric coordinates of a point P for a triangle (v0, v1, v2) have the following relationship:

P = a0*v0 + a1*v1 + a2*v2

where (a0,a1,a2) are scalar weights for the vertices. For colors, etc just replace (v0,v1,v2) with (c0,c1,c2) to get the interpolated value at that point.

This even works for points outside of the triangle (which will produce negative weights), so it will handle your bounding box corners.

EDIT: Ack, wait! Replied too soon smile.png

Har. Got it working. Here's a brute force version of the code for the needy:

            //vcorner[i] is the point whose weights need to be calculated; 'points' are the triangle vertices
 

            IVector3D e0 = points[0].vertex - vcorner[i];
            IVector3D e1 = points[1].vertex - vcorner[i];
            IVector3D e2 = points[2].vertex - vcorner[i];
 
            //calculate triangle edges
            IVector3D etri0 = points[0].vertex - points[1].vertex;
            IVector3D etri1 = points[0].vertex - points[2].vertex;
 
            IVector3D cpt = VecCrossProduct(etri0, etri1);
            IVector3D cp0 = VecCrossProduct(e1, e2);
            IVector3D cp1 = VecCrossProduct(e2, e0);
            IVector3D cp2 = VecCrossProduct(e0, e1);
 
            #define RAST_Sign(a) ((a < -EPSILON) ? -1 : (a > EPSILON ? 1 : 0))
 
            float invarea = 1.0f / VecMagnitude(cpt);
            float w0 = VecMagnitude(cp0) * invarea * RAST_Sign(VecDotProduct(cpt, cp0));
            float w1 = VecMagnitude(cp1) * invarea * RAST_Sign(VecDotProduct(cpt, cp1));
            float w2 = VecMagnitude(cp2) * invarea * RAST_Sign(VecDotProduct(cpt, cp2));
 
            //do interpolating stuff
            float valinterp = w0 * points[0].val + w1 * points[1].val + w2 * points[2].val;



Thank you!

Here's a faster version for computing barycentric coordinates, adapted from Ericson's Real Time Collision Detection book:


Vector3 computeBarycentricCoordinates( const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& point )
{
	Vector3 e0 = v2 - v1;
	Vector3 e1 = v3 - v1;
	Vector3 e2 = point - v1;
	
	Real d00 = math::dot( e0, e0 );
	Real d01 = math::dot( e0, e1 );
	Real d11 = math::dot( e1, e1 );
	Real d20 = math::dot( e2, e0 );
	Real d21 = math::dot( e2, e1 );
	Real inverseDenom = Real(1) / (d00*d11 - d01*d01);
	
	Real v = (d11*d20 - d01*d21) * inverseDenom;
	Real w = (d00*d21 - d01*d20) * inverseDenom;
	Real u = Real(1) - v - w;
	
	return Vector3(	u, v, w );
}

Hm this is interesting and also slightly annoying seeing as I already had bilerping working and it took me quite some time :). Here's some analysis:

Bilerps amount to 8 muls and 7 adds/subs per weight per fragment, which amounts to: 24 muls + 21 adds/subs

Precomuputing the the static edges, two of the dot products and the inverse denominator and calculating weights per fragment using your code amounts to: 10 muls + 8 adds/subs

Blah :)

There's an even faster way if you're doing triangle rasterization. You can split the triangle into two edge pairs (based on the vertex with the middle Y coordinate). For instance, in your original image, you would split the triangle (v0,v1,v2) horizontally through v0 to produce two triangles that can be rasterized in one pass each. There are two stages to the interpolation: edge interpolation which proceeds from bottom to top and horizontal interpolation from left to right.

First you calculate the derivative for each interpolated quantity with respect to each vertical pixel. This allows you to incrementally compute the interpolated values at each edge point, starting from the bottom of each edge. Interpolating both vertical edges of a triangle at once, you then perform a similar incremental interpolation across each scan line. This means that most pixels on big triangles only require an add operation for each pixel/interpolated value to compute the next one. You can't get any faster than that.

There's an even faster way if you're doing triangle rasterization. You can split the triangle into two edge pairs (based on the vertex with the middle Y coordinate). For instance, in your original image, you would split the triangle (v0,v1,v2) horizontally through v0 to produce two triangles that can be rasterized in one pass each. There are two stages to the interpolation: edge interpolation which proceeds from bottom to top and horizontal interpolation from left to right.

First you calculate the derivative for each interpolated quantity with respect to each vertical pixel. This allows you to incrementally compute the interpolated values at each edge point, starting from the bottom of each edge. Interpolating both vertical edges of a triangle at once, you then perform a similar incremental interpolation across each scan line. This means that most pixels on big triangles only require an add operation for each pixel/interpolated value to compute the next one. You can't get any faster than that.

I got halfspace rejection with block rastererization (see here) running about 3-4 times faster than my previous scanline-based code (~1.6-2M cycles down to about 0.5M per triangle). I never considered multithreading my scanline code, but halfspaces completely decouple interpolation across the triangle (eg rounding errors from deltas) from position information, which is perfect for multithreading. The only trick is getting interpolated vertex attributes, which this thread is about. Since edge derivatives aren't available to me, I can't exploit them the way you're describing (my scanline code did, though).

EDIT: note that of the 0.5M cycles only about 0.06-0.1 are spent interpolating across the triangle and attribute interpolation. The rest of the overhead is from type conversions and shading the fragment itself.

Also - my test triangles are relatively small, but I'm keeping them constant size for comparison.

This topic is closed to new replies.

Advertisement