• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
irreversible

Calculating bounding box values from contained triangle vertices

7 posts in this topic

[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.

 

0

Share this post


Link to post
Share on other sites

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!
0

Share this post


Link to post
Share on other sites

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 );
}
Edited by Aressera
2

Share this post


Link to post
Share on other sites

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 :)

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

Edited by irreversible
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0