{"id":211,"date":"2016-11-14T07:19:13","date_gmt":"2016-11-14T06:19:13","guid":{"rendered":"http:\/\/robert-lindner.com\/blog\/?p=211"},"modified":"2016-11-14T07:19:13","modified_gmt":"2016-11-14T06:19:13","slug":"planet-renderer-week-5-6","status":"publish","type":"post","link":"https:\/\/leah-lindner.com\/blog\/2016\/11\/14\/planet-renderer-week-5-6\/","title":{"rendered":"Planet Renderer \u2013 Week 5 &#8211; 6: Height, Triangular CDLOD"},"content":{"rendered":"<p>I missed last weeks post because I was writing a research paper about the graduation work and didn&#8217;t get around to updating my blog since. You can read the paper <a href=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/researchPaper.pdf\" target=\"_blank\">here<\/a>, I go over a lot of the topics I discussed in previous posts, but also discuss some new things. Aside from that I made the terrain sample a height map in the vertex shader to offset the height of the triangles appropriately. I also made sure that the backface culling doesn&#8217;t cull terrain with potential peaks that loom over the horizon. This video shows the results quite nicely:<br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/66VysDSQ8Mw?rel=0\" width=\"640\" height=\"360\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><br \/>\nThe more interesting change was added last week:<br \/>\n<!--more-->I decided to work with a modified version of CDLOD for my terrain algorithm. Among the reasons for this decision are:<\/p>\n<ul>\n<li>It is a modern algorithm designed to leverage the GPUs potential<\/li>\n<li>The morphing means that no neighbours need to be determined for crack fixing<\/li>\n<li>Morphing allows for smooth transitions in terrain detail instead of popping<\/li>\n<\/ul>\n<p>The idea behind CDLOD is that there is a quadtree that is subdivided based on camera distance, and at every leaf a patch is drawn. Every patch is a grid of vertices of a predefined resolution. In the vertex shader vertices are transformed to fit into the leaf of the quadtree and with the height of the terrain. Every other vertex is also morphed to linearly merge with its neighbour vertex if it is close to the border of a lower subdivision level based on the camera distance. This way cracks can be avoided at the edges of subdivision levels.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium ls_lazyimg\" src=\"https:\/\/leah-lindner.com\/blog\/wp-content\/plugins\/images-lazyload-and-slideshow\/blank_1x1.gif\" file=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/Cracks.JPG\" alt=\"terrain lod cdlod cracks planet rendering\" width=\"767\" height=\"386\" \/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/Cracks.JPG\" alt=\"terrain lod cdlod cracks planet rendering\" width=\"767\" height=\"386\" \/><\/noscript><br \/>\nThe advantage of using these patches is that they only need to be sent to the GPU once, which alleviates the CPU from tons of geometry calculations.<\/p>\n<p>Using CDLOD for planet rendering of course has the typical problem of the algorithm being designed for quadratic terrain patches. Under normal circumstances this would mean that the base geometry of the planet needs to consist of squares, like a cube, and thus loose all the advantageous look up tables generated for equilateral triangles in an icosphere. Therefore, I looked into the possibility of achieving the same morphing effect with triangular patchs, and found one:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium ls_lazyimg\" src=\"https:\/\/leah-lindner.com\/blog\/wp-content\/plugins\/images-lazyload-and-slideshow\/blank_1x1.gif\" file=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/TringleMorphing.jpg\" alt=\"Triangle morphing for crack fixing in triangular cdlod for planet rendering\" width=\"1900\" height=\"1350\" \/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/TringleMorphing.jpg\" alt=\"Triangle morphing for crack fixing in triangular cdlod for planet rendering\" width=\"1900\" height=\"1350\" \/><\/noscript><\/p>\n<p>Here, smaller triangles get twisted into the larger triangles until the vertices are at the same position. A small complication is that neighbouring triangles need to twist into the opposite direction, in other words every second triangle twists counter clockwise.<\/p>\n<p>In my implementation every patch holds the vertex positions and morph vectors in a normalised two dimensional form (PatchVertex: vec2 pos; vec2 morph;). These two dimensional coordinates can be treated like barycentric coordinates. Every patch is a leaf node of the triangle tree, where a triangle holds the position A, the vectors R and S and an integer with its subdivision level for morphing. R and S can be constructed from a triangle ABC, where R = B-A and S = C-A.<\/p>\n<p>When transforming a vertex from the patch data using such a triangle its position can be determined as follows:<\/p>\n<blockquote><p>vec3 triPos = A + R*pos.x + S*pos.y;<\/p><\/blockquote>\n<p>Using the distance lookup table described in an earlier post, the camera position and the subdivision level of the patch, a morphing factor can be determined. Once that is the case the position can be modified again:<\/p>\n<blockquote><p>triPos += R*morph.x + S*morph.y;<\/p><\/blockquote>\n<p>In order to make sure the winding is inverted every other triangle the R and S vector can be swapped during the triangle tree calculation on the CPU.<\/p>\n<p>Patches are drawn instanced in order to avoid hundreds of drawcalls per terrain, so a vertex buffer object is created to hold the triangle tree information and updated every frame (PatchInstanc: int level; vec3 A; vec3 R; vec3 S;).<\/p>\n<p>The following video shows how the effect looks like quite nicely:<br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/qDT5B_HbO84?rel=0\" width=\"640\" height=\"360\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Using this implementation allowed me to achieve solid framerates at ridiculously high vertex counts, in some cases 120 fps at nearly 3 million vertices, which is not a resolution needed by a long shot to achieve convincing terrain:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium ls_lazyimg\" src=\"https:\/\/leah-lindner.com\/blog\/wp-content\/plugins\/images-lazyload-and-slideshow\/blank_1x1.gif\" file=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/21.jpg\" alt=\"Insane resolution CDLOD with triangles for planet rendering\" width=\"1920\" height=\"1080\" \/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/21.jpg\" alt=\"Insane resolution CDLOD with triangles for planet rendering\" width=\"1920\" height=\"1080\" \/><\/noscript><\/p>\n<p>I have not profiled the performance in depth yet, but I seem to be getting very decent results at less than 250k triangles:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium ls_lazyimg\" src=\"https:\/\/leah-lindner.com\/blog\/wp-content\/plugins\/images-lazyload-and-slideshow\/blank_1x1.gif\" file=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/20.jpg\" alt=\"adequate resolution cdlod opengl planetary terrain rendering triangle morphing\" width=\"1920\" height=\"1080\" \/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/leah-lindner.com\/img\/blog\/planet_renderer\/week5-6\/20.jpg\" alt=\"adequate resolution cdlod opengl planetary terrain rendering triangle morphing\" width=\"1920\" height=\"1080\" \/><\/noscript><\/p>\n<p>Note that the performance is usually better than the screenshots indicate because the terrain is drawn twice here, solid and wireframe on top.<\/p>\n<p>One problem is that the subdivision always assumes that triangles are at sea level, which can cause problems with distributions on taller mountains, which is a problem I am currently looking into.<\/p>\n<p>Apart from this, I started researching atmospheric scattering now in order to make sure I get all required topics for my graduation work together in a timely manner, and I can always come back to the terrain LOD to refine it later.<\/p>\n<p>As usual, you can find the full implementation on the <a href=\"https:\/\/github.com\/Illation\/PlanetRenderer\">GitHub repository.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I missed last weeks post because I was writing a research paper about the graduation work and didn&#8217;t get around to updating my blog since. You can read the paper here, I go over a lot of the topics I discussed in previous posts, but also discuss some new things. Aside from that I made &hellip; <a href=\"https:\/\/leah-lindner.com\/blog\/2016\/11\/14\/planet-renderer-week-5-6\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Planet Renderer \u2013 Week 5 &#8211; 6: Height, Triangular CDLOD&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,10,6],"tags":[16,26,27,32,37,40,41,42,45,49,50],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-graphics-programming","category-planet-renderer","category-project","tag-c","tag-graduation-work","tag-graphics-programming","tag-lod","tag-opengl","tag-planet-renderer","tag-render","tag-rendering","tag-terrain","tag-triangle-subdivision","tag-triangular-cdlod"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":0,"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leah-lindner.com\/blog\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}