{"id":23667,"date":"2014-07-13T23:28:10","date_gmt":"2014-07-14T04:28:10","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=23667"},"modified":"2014-07-14T09:53:05","modified_gmt":"2014-07-14T14:53:05","slug":"project-unearth-part-6-kissing-cubes","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=23667","title":{"rendered":"Project Unearth Part 6: Kissing Cubes"},"content":{"rendered":"<p>Shadow volumes are interesting things. For everything that casts a shadow, you need to have a fully enclosed solid. Because of the way our shader works, for every triangle we need to know what its 3 adjacent neighbors are. And when I say &#8220;need&#8221; I don&#8217;t mean &#8220;ought to&#8221; I mean it&#8217;s impossible to do otherwise. You can&#8217;t supply a triangle to the shader without neighbors for the same reason you can&#8217;t draw a triangle with less than three vertices. It wouldn&#8217;t make any sense. (And if there aren&#8217;t 3 neighbors? Then this isn&#8217;t an enclosed solid.) <\/p>\n<p>Let me bring up this diagram again:<\/p>\n<pre lang=\"glsl\" line=\"1\">\r\n\/*-----------------------------------------------------------------------------\r\nThis shader takes a triangle of type GL_TRIANGLES_ADJACENCY. It takes the \r\nfollowing form:\r\n \r\n                 1-----2-----3\r\n                  \\   \/ \\   \/\r\n                   \\ \/   \\ \/\r\n                    0-----4\r\n                     \\   \/\r\n                      \\ \/\r\n                       5\r\nPoints 0, 2, and 4 are the points of the triangle actually being drawn. \r\nPoints 1, 3, and 5 are corners of adjacent triangles, provided by OpenGL \r\nfor the purposes of being able to analyze the topology here in a geometry\r\nshader.\r\n-----------------------------------------------------------------------------*\/\r\n<\/pre>\n<p>This is no big deal when you&#8217;re using models made by artists. Maybe the artist has some kind of tool or conversion utility for identifying all the triangle relationships. But this becomes tricky when we&#8217;re building objects on the fly. When I&#8217;m building a particular cube, I can&#8217;t hook its triangles up to its neighbors, because at least half of them haven&#8217;t been built yet. I could peek at the next-door cube to see what I will eventually build there, but I won&#8217;t know what vertices it will use until I get there. So what you have to do is just build a bunch of triangles and then stitch them together when you&#8217;re done. <\/p>\n<p>This is actually really time consuming. Like, 90% of the time spent waiting for chunks to appear is waiting for it to thrash through these huge lists of triangles and figure out which ones are neighbors<span class='snote' title='1'>This is the major reason I&#8217;m not using larger chunk sizes. Larger chunks make this triangle-matching less efficient. There are ways to improve this, but I haven&#8217;t gotten around to it yet.<\/span>. Eventually I&#8217;m going to have to fix that. Chunks take about a quarter second to form. A given scene with even a modest view distance will have hundreds and hundreds of chunks. Which means filling in a scene can take a few minutes. While this doesn&#8217;t matter from a gameplay standpoint (this isn&#8217;t a game, and nobody is ever going to play it, so who cares?) it does matter from a testing perspective because I do a lot of tests and I&#8217;m really impatient. But this is a challenge for a future post. In the meantime&#8230;<\/p>\n<p>It turns out that we have a problem:<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing1.jpg' class='insetimage'   alt='unearth_kissing1.jpg' title='unearth_kissing1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Those dark streaks in the center are shadows, reaching off to infinity. We don&#8217;t want that. <\/p>\n<p>Let&#8217;s flatten the problem out to 2 dimensions. So instead of building triangles around cubes we&#8217;re building lines around squares. Imagine the dark squares are solid and the white ones are empty space:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing2.png' class='insetimage'   alt='unearth_kissing2.png' title='unearth_kissing2.png'\/><\/td><\/tr><\/table><\/p>\n<p>Blue line A has 2 neighbors, lines B and C. You can look at this diagram and see that no matter what sorts of squares I make or where I put them, every group of squares would be surrounded by line segments and every line segment would have exactly 2 neighbors. <\/p>\n<p>Well, ALMOST. It turns out there is an exception to this rule:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing3.png' class='insetimage'   alt='unearth_kissing3.png' title='unearth_kissing3.png'\/><\/td><\/tr><\/table><\/p>\n<p>These two squares share a corner, so they&#8217;re &#8220;kissing&#8221;. This means that the purple line now has 4 neighbors. Sort of. Four lines all converge on the same point, and it&#8217;s not at all clear who should be neighbors. If we add a cube at point X, then purple&#8217;s neighbors would be red and cyan. If X is empty, then purple should be neighbors with red and blue. <\/p>\n<p>The crazy shadows I showed you earlier happen when a couple of kissing cubes confuse the triangle-stitching code. It puts the wrong neighbors together, which results in holes in our shadow volume, which results in the broken shadows you see above. <\/p>\n<p>Visually, it&#8217;s pretty clear that red and blue are the &#8220;real&#8221; adjacent lines in the above diagram, and the other two belong to a different cube. But it&#8217;s only obvious because you&#8217;re a human being with a fabulous human brain and I&#8217;ve drawn you this handy diagram. To the computer:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing4.png' class='insetimage'   alt='unearth_kissing4.png' title='unearth_kissing4.png'\/><\/td><\/tr><\/table><\/p>\n<p>It doesn&#8217;t know what you&#8217;re trying to build. It can&#8217;t &#8220;see&#8221; the squares and intuit what it&#8217;s supposed to do. It just sees this list of points and these arbitrary relationships between them. There&#8217;s nothing here to suggest that blue is a more correct neighbor for purple than cyan or green. We need some way to explain it to the computer. <\/p>\n<p>Unfortunately, this is harder than it sounds. When we&#8217;re building the lines the information isn&#8217;t ready yet, and when we&#8217;re hooking up neighbors later the information about the ownership of these polygons is gone. We could probably sort it out by doing some kind of complex spatial analysis. But that would be&#8230;<\/p>\n<ol>\n<li>A major pain in the ass to write.\n<li>Super slow, so that it takes EVEN LONGER for this dang scenery to fill in when I run the program.\n<\/ol>\n<p>So I am not crazy about this solution. In fact, I&#8217;d be happy if I could just hack around this problem. I&#8217;ve kind of had my fill of cube logic for now and I want to be working on other stuff. But it&#8217;s really annoying to have these massive bogus shadows cutting across my view while I&#8217;m trying to test. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing8.jpg' class='insetimage'   alt='unearth_kissing8.jpg' title='unearth_kissing8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;ve been trying to ignore this for a while now. Usually when a bad dice roll results in a couple of kissing cubes I&#8217;ve been just hunting them down manually and deleting one of them. As far as solutions go, that&#8217;s kind of pathetic. Also time consuming. These shadows can originate from anywhere in the scene, so often I have to fly a long way to reach the culprit. <\/p>\n<p>So the quick-and-easy solution seems to be having it detect and automatically delete one of the offending cubes. Actually, I don&#8217;t even need it to delete the cube itself, it just needs to leave the cube out of the shadow volume. What this means is that when we have a pair of kissing cubes, one of them won&#8217;t cast a shadow. In the words of the great philosopher Strong Bad, &#8220;Who cares?&#8221;<\/p>\n<p>Fine. Easy. When it&#8217;s making the model, it looks for kissing cubes and then leaves one of them out of the shadow volume. <\/p>\n<p>I get back to what I was doing. I&#8217;m messing around with generating caves and hills when&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing5.jpg' class='insetimage'   alt='unearth_kissing5.jpg' title='unearth_kissing5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Wait what? Oh, right. Duh:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing6.png' class='insetimage'   alt='unearth_kissing6.png' title='unearth_kissing6.png'\/><\/td><\/tr><\/table><\/p>\n<p>A and B are kissing. But if I remove A then C and D become kissing cubes. This is probably what we see in the previous screenshot: It tried to fix one set of kissing cubes and ended up creating another.  I could ADD a cube at point E (or just force a shadow there) but that would create a new connection between E and F. <\/p>\n<p>And remember that while this looks simple and clean in these diagrams, I&#8217;m actually dealing with the problem in 3 dimensions. A 3D cube has 12 edges, which means there are 12 possible relationships to worry about and 12 places where we can inadvertently create kissing pairs by adding or removing cubes. <\/p>\n<p>This is a silly little problem.<\/p>\n<p>It turns out the most expedient answer is to just cut the entire chunk in half:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unearth_kissing7.png' class='insetimage'   alt='unearth_kissing7.png' title='unearth_kissing7.png'\/><\/td><\/tr><\/table><\/p>\n<p>If we find a kissing pair, we whip out our lightsaber and slice the whole chunk right down through the problem edge. This will result in slightly more total faces. Before the slice, D and G were part of the same volume. After the cut they&#8217;re isolated from each other, and they have (wasted) polygons facing one another. <\/p>\n<p>I try to feel bad about this, but I have other stuff I&#8217;d rather be working on. <\/p>\n<p>We&#8217;ll try and do some Cool Stuff next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shadow volumes are interesting things. For everything that casts a shadow, you need to have a fully enclosed solid. Because of the way our shader works, for every triangle we need to know what its 3 adjacent neighbors are. And when I say &#8220;need&#8221; I don&#8217;t mean &#8220;ought to&#8221; I mean it&#8217;s impossible to do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-23667","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/23667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23667"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/23667\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}