{"id":12194,"date":"2011-07-01T08:56:20","date_gmt":"2011-07-01T13:56:20","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=12194"},"modified":"2011-07-01T09:00:39","modified_gmt":"2011-07-01T14:00:39","slug":"project-frontier-11-bug-hunt","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=12194","title":{"rendered":"Project Frontier #11: Bug Hunt"},"content":{"rendered":"<p>I don&#8217;t have a proper post to cap off this week.  My attention is elsewhere right now.  To placate you, I thought I would show you my mistakes and let you point and laugh at my folly.  This is a collection of screenshots I&#8217;ve collected over the past month.  These are moments when something has gone horribly wrong and I&#8217;ve smacked the screenshot key instead of (or just before) cursing. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_1.jpg' class='insetimage'   alt='frontier11_1.jpg' title='frontier11_1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This happened when I was working on the river system.  I was working on making the rivers bend, instead of doing a hard turn.  This is done by hooking the river around one of the four corners of the region.  Obviously, I chose the wrong corner.<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_2.jpg' class='insetimage'   alt='frontier11_2.jpg' title='frontier11_2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This was my very first forest.  Well, tree. Okay, tree <em>trunk<\/em>.  The point is, this was the very first of whatever it was going to eventually be.  If you look closely, you may notice that some dullard constructed the trunk upside-down.<\/p>\n<p>I think it looks like a bolt of lightning. Wooden lightning, I guess. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_3.jpg' class='insetimage'   alt='frontier11_3.jpg' title='frontier11_3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This was REALLY early in the development of rivers. (These screenshots aren&#8217;t in any sort of order.) I was playing around with depth values, trying to discover what values looked good and what looked horrible. This was one of the latter.<\/p>\n<p>These next two shots go together:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_4.jpg' class='insetimage'   alt='frontier11_4.jpg' title='frontier11_4.jpg'\/><\/td><\/tr><\/table><br \/>\n<table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_8.jpg' class='insetimage'   alt='frontier11_8.jpg' title='frontier11_8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Ugh. This bug.  This happened when I had finished the system to make a &#8220;species&#8221; of tree and was working on building them in groups. Sigh.<\/p>\n<p>For you coders, I was doing something like this:<\/p>\n<pre lang=\"c\">\r\nfor (i = 0; i < number_of_trees_around_here; i++) {\r\n  Class TreeGroup;\r\n\r\n  TreeGroup.BuildAndAllocateAndDoOtherStuff ();\r\n  \/\/Forest here is a vector of trees groups.\r\n  Forest.push_back (TreeGroup);\r\n}\r\n<\/pre>\n<p>Pseudocode, obviously.  The point is, I was making an individual group of polygons and jamming them into a vertex buffer.  Then I was handing off the info for the vertex buffer to the forest. I do this all the time with structs.  I rarely do it with Classes, which might help explain how I ended up making such a bone-headed blunder. <\/p>\n<p>See, when Tree went out of scope (you know, every single time the for () loop did its thing, once for every kind of tree in this region) it would of course call its deconstructor. This would notice that, during it's fleeting lifespan, TreeGroup had allocated a couple of handles for vertex buffers.  It thoughtfully released the handles before sailing off into oblivion.  Of course, those handles (NOT pointers!) had been passed along to Forest, and Forest was planning on using them.  <\/p>\n<p>Allow me to re-enact this drama:<br \/>\n<center><br \/>\nINTERIOR - MY COMPUTER: DAY<\/p>\n<p>MY PROGRAM:<br \/>\nHey OpenGL! I need a couple of handles.<\/p>\n<p>OPENGL:<br \/>\nYou do?<\/p>\n<p>MY PROGRAM:<br \/>\nYeah.  I guess I need two.  One for a vertex buffer, and one for an index buffer.<\/p>\n<p>OPENGL:<br \/>\nOkay kid. Index #15 and #16 are free. You can use those.<\/p>\n<p>MY PROGRAM:<br \/>\nThanks mister! NOW I'M DESTROYING THEM! I DON'T WANT THEM ANYMORE! HAHAHAHA!<\/p>\n<p>OPENGL:<br \/>\nWhatever. <\/p>\n<p>MY PROGRAM:<br \/>\nHey OpenGL! I need a couple of handles.<\/p>\n<p>OPENGL:<br \/>\nI'll bet. Here, #15 and #16 were taken about a nanosecond ago, but they have recently become available. <\/p>\n<p>MY PROGRAM:<br \/>\nThanks mister! NOW I'M DESTROYING THEM! I DON'T WANT THEM ANYMORE! HAHAHAHA!<\/p>\n<p>OPENGL:<br \/>\nIf you say so.<\/p>\n<p>MY PROGRAM:<br \/>\nHey OpenGL! I need a couple of handles.<\/p>\n<p>OPENGL:<br \/>\n#15 and #16 are very popular today.<\/p>\n<p>MY PROGRAM:<br \/>\nYippie! Now I have six handles! I'm going to draw lots of trees!<\/p>\n<p>OPENGL:<br \/>\nKids these days.<br \/>\n<\/center><\/p>\n<p>At this point I had (say) three groups, all of which were using the exact same handles.  What SHOULD have happened was that all three of them would call the same handles.  In effect, all three groups would render the exact same polygons.  From my viewpoint, it would have looked like two-thirds of the trees were missing.  The last third was getting drawn three times.  (Which I wouldn't have been able to tell.)<\/p>\n<p>But instead it was doing crazy stuff.  It would render groups one and two using their proper index lists, but using the vertices of group three.  See, drawing polygons is a bit like playing connect-the-dots. Let's say I give you a collection of dot positions:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier_ctd1.jpg' class='insetimage'   alt='frontier_ctd1.jpg' title='frontier_ctd1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And then I give you a list of indexes: A C E B D A<\/p>\n<p>If you connect the dots in that order, you'll make a star.  If I give you another bunch of dots:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier_ctd2.jpg' class='insetimage'   alt='frontier_ctd2.jpg' title='frontier_ctd2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And the following indexes: A B C D E F G H A<\/p>\n<p>You'll draw a heart.  (Sort of.)  But if something goes wrong and I give you the first set of dots and the second list of indexes, you'll end up drawing gibberish.  Which is exactly what was going on in the screenshots.  <\/p>\n<p>The problem was, this strange behavior led me to believe the problem was elsewhere in my code. The code to create the trees and turn them into polygons is huge, and I naturally assumed that's where the problem was.  Since a lot of the trees are of similar topology (even if they are vastly different size and shape, their polygon makeup can often be very similar) this bug would appear to \"go away\".  So sometimes trees were missing. Sometimes they were scrambled.  And sometimes things looked fine.  <\/p>\n<p>Five hours of slamming my head off the desk to find a problem caused by thirty seconds of sloppy coding. Sigh.  That was time I would have preferred to spend on something else. Moving on...<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_5.jpg' class='insetimage'   alt='frontier11_5.jpg' title='frontier11_5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I don't remember what I did to create this.  I was messing around with the various climates and some numbers got away from me.  It's still oddly appealing. <\/p>\n<p>And finally, here are a couple of shots of the trees without their textures:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_6.jpg' class='insetimage'   alt='frontier11_6.jpg' title='frontier11_6.jpg'\/><\/td><\/tr><\/table><br \/>\n<table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/frontier11_7.jpg' class='insetimage'   alt='frontier11_7.jpg' title='frontier11_7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So that was fun. Next time I hope to show off some sort of progress.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t have a proper post to cap off this week. My attention is elsewhere right now. To placate you, I thought I would show you my mistakes and let you point and laugh at my folly. This is a collection of screenshots I&#8217;ve collected over the past month. These are moments when something has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-12194","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\/12194","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=12194"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/12194\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}