Thứ Sáu, 26 tháng 7, 2013

Friday And The Randoms

Terrain Save, Load and Random Generate

Another good (but hot) night in the coders dungeon. At night I have to switch off the blessed air-con as the open window lets all the moths in.  After 10PM the office gets hotter and hotter and hotter this time of year!

It's now 3AM and I'm practically in a sauna, sweating like a coder without a compiler and hoping this blog entry is nice and succinct so I can go to bed. Some good news in that the Monday meeting has been pushed to Thursday which gives me much more time to add some polish and even start the Physics prototype next week!  The downside of course is that without a demo for management to approve, there will be no pictures or videos now until next Friday. If you want this policy to change, you know Rick's email ;)

Terrain Saver and Loader

It was key to be able to save all edits performed and reload the terrain in the exact same state to resume editing and of course to load in when the level requires it.  A few hitches though. One bug one is that when I construct the height data manually in the terrain module, I drop from 200 fps down to 30 fps for some unfathomable reason. I have yet to dive into the source code of the module and might just be something silly, but it has me a little vexed.

I also found that converting the terrain height data to a height map image, and then feeding that back through the loader created import distortion of some kind which would slowly (but subtly) change the terrain heights each time you saved and loaded. Not a good idea as your terrain would have washed away after the 100th save :)  I am instead directly storing the heights in a Dark Basic Pro memblock file so I can keep a very accurate eye on this most valuable data.  I also save the veg shadow map too, this time as a simple image and map it back to the system when I restart the prototype. It works just great.

Holy Random Terrains Batman!

A tickbox on my schedule was the successful generation of a believable 'random' terrain as part of the Reloaded terrain editing suite. I was certain many users would not want to define every hill, dip, lake, bump, slope and mountain over a 50,000 x 50,000 area.  They would want a single button to make one up, and then allow some editing in the part of interest.

To that end I borrowed inspiration from our friendly Dark Basic Pro forum, and an entry into our 20 line challenge which is a bit of fun which tasks any programmer to write something useful in under 20 lines of code. It's great to see the amazing things coders can do in such a small space and in this case, someone produced a terrain generator in 20 lines!

The name of this DBP star is Lewis999 who adapted a Diamond-Square algorithm to populate a matrix with realistic height data. Using the same technique, I generated pure height data using the same idea and applied it to the terrain constructor.  I then painted textures based on distance from the water level, and used smoothing to create a nice transition. I also painted a band of grass at the point I felt grass would ideally grow (not too high in the mountains, not underwater or on the shore).  You can still paint all this manually, but the terrain generator starts off as an automated process.

Finally I played with the grass sizes to create a more dynamic and rich veg experience, and concluded that a few more grass and vegetation models and textures would really lift my terrain visual. Something to add to Mark's list when he's not looking!

Is Something Missing?

Running around my newly generated terrain for a while, as good as it was, there was something missing. Naturally there is no game, so no enemies, no buildings, no game objects, no nout of course. Still, you can have a convincing terrain without all the above, right?  As I am making a deliberate effort not to add trees (as that is for another pack and another day), I wondered what else I could add. Perhaps a few cactus plants models when/if they are created, sure, but I need something more substantial!

Is it a bird, is it a plane, no it's a Rock!

Sure enough, what my bleak terrain level needed was rocks, and lots of them!  Not the little pebbles that can be baked into the floor texture but large ugly rocks left after the last glacier strolled through the land.  From my extensive terrain editing (playing) I knew how to create a rock by making a sharp raised land, then squashing the top bit, then adding a rock texture and surrounding it with grass.

Given that knowledge, I proceeded to add a system which would strategically add a rock, and not just a single rock but what I term a rock clump. These clumps comprise of about 15 rocks in close proximity and they really do look natural when sprawled over the floor. The system then moves somewhere else in the land and makes another clump, and so on until there are enough.

The terrain generator is pretty good for a V1 now, but it does take a while (30 seconds for 150 rocks) and the final level would probably need a few thousand of them.  The good news is that once the rocks are in the land, you can save and load the whole terrain within seconds :)

Signing Off

So that's the week, and apart from IDE integration, all my terrain objectives have been met on schedule which I am pleased about. Still needs spit and polish of course, but we're well on the way now.  It also occurs to me that our AI system will need to know about height map terrain so enemies can work out a path through cliffs, valleys, slopes, work out line of sight, know what height advantage means, e.t.c.  I have already started a list :)

I plan to use the weekend to quickly add shadow generation to the prototype, experimenting with things like burning the normals as shadows in the texture to improve LOD transitions, calculating when a large hill or rock casts a shadow onto the terrain and the big one, rendering the entire 'outdoor' scene of a level such that I can burn the resulting shadows into the veg shadow map too.  This of course means we cannot have a moving sun for day and night cycles, but adding cascade shadow mapping to the terrain proved too costly and unless there is a performance friendly solution out there I have decided to aim for quality visuals rather than a day / night cycle in V1.  I should have plenty of time to work on my shaders, and it won't be long before a solution pops into my head which allows us to have our cake and eat it.

As is my custom, no blog entries this weekend so I can be free to do nothing, or burn the candle at both ends.  I was going to sneak out a screenshot of the latest progress, but I resisted (Rick will be proud).  See ya Monday!

Không có nhận xét nào:

Đăng nhận xét