{"id":2889,"date":"2013-09-23T21:53:44","date_gmt":"2013-09-23T11:53:44","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=2889"},"modified":"2013-09-23T21:53:44","modified_gmt":"2013-09-23T11:53:44","slug":"spots","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2013\/09\/23\/spots\/","title":{"rendered":"Spots"},"content":{"rendered":"

\"Screen<\/a>I need to stop posting nothing but bug shots.<\/p>\n

But here’s one from today:\u00a0 These odd little surface blemishes.\u00a0 In this shot, they’re all black.\u00a0 In others, there are some white ones, too.<\/p>\n

As before, this turned out to be another little glitch in my multithreaded terrain generation.<\/p>\n

In MMORPG2, generating the world terrain data happens in the background — when the game starts up for the first time, it starts generating terrain data.\u00a0 It uses several threads to do this;\u00a0 all working at the same time.\u00a0 Each thread works on building the data for a little rectangle of terrain.<\/p>\n

Some bits of the terrain data being generated rely on other, nearby bits of terrain data.\u00a0 For example, on a particularly steep slope, fields expose dirt, rather than showing grass.\u00a0 Sometimes that terrain data which I need to fetch (particularly when determining the surface normal, which itself affects the surface color), a thread needs to query the data being worked on by another thread.<\/p>\n

To handle this case, I keep a flag for each bit of terrain data, marking whether or not that bit of terrain data has yet been generated.\u00a0 If it hasn’t, the terrain generation thread will go ahead and generate that data itself, even if it’s outside the region it was supposed to be generating.\u00a0 If it has, then it just uses the data which had already been generated.\u00a0\u00a0 The theory is that when the thread which had been supposed to generate that data eventually gets around it doing it, it’ll say “hey, somebody’s already generated this for me.\u00a0 So I don’t need to generate it myself, I’ll just use whatever they calculated.”<\/p>\n

Works great, except for a minor problem;\u00a0 I was setting the “I’ve generated this data” flag slightly before I actually set the data.\u00a0 So if thread 1 needed external data and started generating it, then if thread 2 needed that data, it was possible that thread 2 would ask for the data in the brief moments between when thread 1 set the flag which set “this data has been generated!” and when it had actually stored the data.\u00a0 Which resulted in thread 2 grabbing data which it thought was set, but actually wasn’t.\u00a0 Which led to artifacts like those shown above;\u00a0 little single-vertex-wide glitches, mostly along the edges of the blocks of terrain during initial terrain generation.<\/p>\n

By switching the terrain generation to set the generated terrain values *first*, and the flag stating that the terrain values had been set *after*, this problem has now gone away.<\/p>\n","protected":false},"excerpt":{"rendered":"

I need to stop posting nothing but bug shots. But here’s one from today:\u00a0 These odd little surface blemishes.\u00a0 In this shot, they’re all black.\u00a0 In others, there are some white ones, too. As before, this turned out to be another little glitch in my multithreaded terrain generation. In MMORPG2, generating the world terrain data…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[24,25],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/so9WK-spots","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/2889"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=2889"}],"version-history":[{"count":1,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/2889\/revisions"}],"predecessor-version":[{"id":2891,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/2889\/revisions\/2891"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=2889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=2889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=2889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}