{"id":15751,"date":"2012-05-02T04:15:01","date_gmt":"2012-05-02T09:15:01","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15751"},"modified":"2012-05-02T04:16:46","modified_gmt":"2012-05-02T09:16:46","slug":"project-octant-part-2-octree","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15751","title":{"rendered":"Project Octant Part 2: Octree"},"content":{"rendered":"<p>Step one of this project is to bootstrap myself up to the point where I can start doing some 3D programming.  If I was working in my familiar environment this would be about ten minutes, but now that I&#8217;m using <a href=\"http:\/\/qt.nokia.com\/products\/\">Qt<\/a> it takes me a couple of hours to get a sense of what I need and how I get it working.  (Incidentally, Qt is pronounced &#8220;cute&#8221;, if you&#8217;re one of those people who likes to read my blog aloud.) There are example programs, but they&#8217;re usually demos of &#8220;OpenGL and some other concepts&#8221; and it&#8217;s not clear what parts of the code are the demo, what parts are infrastructure, and what parts are cruft.  <\/p>\n<p>Eventually I get a bare-bones application working that creates a simple scene with a checkerboard ground.  Something like this is always step 1 for me. I learned a long time ago that you should have a simple, reliable, non-textured, orient-able object in the scene at all times. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_1.jpg' class='insetimage'   alt='octant1_1.jpg' title='octant1_1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The reason for this is that early in a project, you can often find yourself in a situation where you&#8217;re looking at a blank scene and you don&#8217;t know why. I start up the program and I&#8217;m looking at a solid color. <em>Did I accidentally type a wrong number and move all the scenery 1,000Km to the right, instead of one meter? Or maybe the scenery is where it&#8217;s supposed to be, but the camera has been placed far off? Did I mistakenly apply a transparent texture to the scene, thus making everything invisible? Is it even drawing the world at all? Or is everything fine, I&#8217;m just looking straight up at the sky? Actually, is the camera moving? Maybe the scenery is just off to one side but I&#8217;ve broken the controls so that I can&#8217;t move?<\/em> After a few minutes of waving the mouse around and staring at a blank screen, the wise programmer will see the value of having a handy marker nearby.  If something goes wrong, the checkerboard can help me know where to look for the problem.<\/p>\n<p><!--more-->(If I wasn&#8217;t lazy, I would have drawn this checkerboard so that the four corners were all different colors, or done something else to ruin the symmetry. Several times while working I&#8217;d get disoriented because I couldn&#8217;t tell which way was north, south, east, or west. Instead of paying the time up-front to do it right, I spent it in many small doses as I fumbled around, looking at the wrong bit of scenery because I&#8217;d lost my bearings. Live and learn. Or don&#8217;t, in my case.)<\/p>\n<p>So my basic Qt application is working. I won&#8217;t do my full write-up on Qt now. I will say that after a few hours with the platform, I am not a huge fan. It has distinct advantages, but some non-trivial costs. (Not talking about money, mind you.) I&#8217;ll get into this more later, but for now let&#8217;s just move on and work on this octree stuff.<\/p>\n<p>Octree is named for &#8220;Oct&#8221; + &#8220;tree&#8221;, meaning a tree with eight branches. Yes, most sensible people can see that it&#8217;s shaped like a cube and not a tree. It&#8217;s called a &#8220;tree&#8221; because programmers tend to name things after how they behave in memory, not how they look in 3D space.  In the code, an Octree is conceptually tree-like in that each cube can be divided into smaller cubes, each of which can be divided into eight smaller cubes, each of which&#8230; you get the idea.  <\/p>\n<p>Let&#8217;s start with a cube:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_2.jpg' class='insetimage'   alt='octant1_2.jpg' title='octant1_2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Pretend this is a section of a cubist quasi-Minecraft world. This area is sixteen meters on a side.  Now, the brute-force solution is to represent this as a 16x16x16 grid. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_3.jpg' class='insetimage'   alt='octant1_3.jpg' title='octant1_3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That is a LOT of cubes. 4,096, actually.  Perhaps a frame of reference would help.  In Minecraft, at maximum settings you can see 256m in every direction. When you look to the horizon, the most distant visible block is only 256 meters away. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/minecraft_view_distance.jpg' class='insetimage'   alt='minecraft_view_distance.jpg' title='minecraft_view_distance.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That means there are also 256 blocks behind you that you&#8217;re not seeing right now. Which means the area around you is 512&#215;512 meters.  It&#8217;s also 256 meters from the top of the world to the bottom. Which means that at any given time, the game has 512x512x256 cubes in memory. That&#8217;s 67,108,864 cubes.  This is do-able on modern hardware, but horribly inefficient. That&#8217;s a great big rolling sea of data. Just passing over it to do calculations would be murder. Moreover, if we&#8217;re talking about something Minecraft-y then we&#8217;re dealing with a situation where a vast majority of the data is either solid stone or empty air. <\/p>\n<p>In this case, an octree can help.<\/p>\n<p>So back to our 16x16x16 cube.  If this is solid stone, then we can just have this one cube that represents 16x16x16 meters of solid stone. If the game says, &#8220;what is the block in-such-and-such a place?&#8221; we don&#8217;t need to go fishing in a big soup of data.  We see the coords fall inside of our cube here, so we just say, &#8220;stone&#8221;. It&#8217;s all stone. <\/p>\n<p>But what if it&#8217;s NOT all stone?  What if one little block at the very bottom is different from all the others? Well, that means we subdivide our cube into eight smaller cubes by cutting once along each axis:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_4.jpg' class='insetimage'   alt='octant1_4.jpg' title='octant1_4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>It&#8217;s a bit like the Rubik&#8217;s Cube from yesterday. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/2x2cube.jpg' class='insetimage'   alt='2x2cube.jpg' title='2x2cube.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Our cube is now broken up into eight sub-cubes. Each of these sub cubes is 8x8x8 meters. Most of them are still solid stone, but the one at the bottom front needs to be divided again:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_5.jpg' class='insetimage'   alt='octant1_5.jpg' title='octant1_5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Now these new cubes are 4x4x4 meters each.  We keep dividing until we get down to the level where we have just 1x1x1 cubes, and we can at last set our one lone cube at the bottom to be different from the others.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_6.jpg' class='insetimage'   alt='octant1_6.jpg' title='octant1_6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Note that originally this entire block of stuff would have taken up 4,096 worth of cubes in memory.  Using an octree, we now have:<\/p>\n<p>1 cube of 16x16x16, which contains:<br \/>\n8 cubes of 8x8x8, one of which is divided into:<br \/>\n8 cubes of 4x4x4, one of which is divided into:<br \/>\n8 cubes of 2x2x2, one of which is divided into:<br \/>\n8 one-meter cubes.<\/p>\n<p>So we have 33 cubes instead of 4,096. Even better, if we need to do a lot of processing looking for particular blocks (perhaps we&#8217;re looking for blocks of air so we can make polygons and do lighting calculations) then we have a super-fast way of eliminating huge areas in our search. In a brute-force system, if I was looking for air blocks I&#8217;d have to pass over each and every one of these 4.096 cubes and ask, &#8220;Is this air? No? Okay, moving on.&#8221;  But here I won&#8217;t need to make more than 25 moves.<\/p>\n<p>There is a slight cost to doing things this way.  In a brute system, if I want to look at a <em>specific<\/em> block I can do so directly.  If the player destroys the block in row 4, column 2, layer 7, then I can go to 4,2,7 and change it to air.  In an Octree, I have to start at the top level and step down. If I want to find Jimmy, I have to ask his great-grandfather, who will tell me where I can find Jimmy&#8217;s grandpa, who will tell me where Jimmy&#8217;s dad is, who can point me to Jimmy. <\/p>\n<p>The upshot is that when I need a <em>particular<\/em> block it can take me a few hops to get there, but when I need to see <em>every<\/em> block it can save me orders of magnitude of time. <\/p>\n<p>If I throw in some basic &#8220;hills&#8221;, here is what it looks like:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_7.jpg' class='insetimage'   alt='octant1_7.jpg' title='octant1_7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And here is how that same scene looks on the octree:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant1_8.jpg' class='insetimage'   alt='octant1_8.jpg' title='octant1_8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The huge white cubes are blocks of air. You can see how the top and bottom of the image have lots of big, consolidated cubes, while the center has lots of tiny little cubes where the air and ground meet.  This is how it works: Lots of detail around areas with diversity, and lots of simplification in homogeneous areas. <\/p>\n<p>So that was relatively painless. Next time we&#8217;ll be talking about things that are less not painful.<\/p>\n<p>Also: I&#8217;ll be releasing the source to Project Frontier at some point this week. What is the go-to place for doing that sort of thing these days? Github? Google Code? Facebook? Geocities?  Anything else to say about it before I make the source public?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step one of this project is to bootstrap myself up to the point where I can start doing some 3D programming. If I was working in my familiar environment this would be about ten minutes, but now that I&#8217;m using Qt it takes me a couple of hours to get a sense of what I [&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":[233],"class_list":["post-15751","post","type-post","status-publish","format-standard","hentry","category-programming","tag-octant"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15751","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=15751"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15751\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}