Rendering Terrain Part 31 – Conclusion

After some four and a half months working on this project, I believe that I have finally come to the end. I think I’ve pretty much met my goals. But let’s recap and see.

1. I want to render the height map in a black and white 2D texture view. I want to work with height maps of more or less arbitrary size, determined at run time.

Done and done. The program opens up with a 2D texture view of the height map. I have a number of height maps that I’ve tested it with, ranging in size from 205×205 to 4096×4096. They all work without issue.

2. I want a sort of bird’s eye view, looking down at the terrain from far away and at an angle so you can see the entire terrain at once. I’ll be treating the height map as being 1 pixel per 1m2.

3. I want a first person view so I can run around on the terrain. I’ll implement simple mouse look and lock the camera to be a certain distance from the surface.

I originally thought of these as two separate views, but I later decided to treat them as one. So in our 3D view, you can fly around freely or lock the camera to the terrain. The locking the camera to the terrain feature is new. I had actually forgotten about wanting to do that.
It was relatively simple to implement. As far as the camera was concerned, I just needed to get the position of the eye, use it to find the height of the terrain at that point, and then reset the eye to a specific height above the terrain.

Calculating the height of the terrain at the given point was a little bit trickier since the final height of a point is actually calculated by the GPU when rendering. I had to reproduce the same calculation on the CPU. Of course, we’re not sampling a texture. We’re indexing into an array. In fact, two arrays as we need to add the height value from the displacement map to the height map value. But we also need to calculate the normal so we can offset in the correct direction.

This isn’t perfect. Because we displace the terrain’s vertices along the normals, we cause the terrain to bump out in ways that this doesn’t take into account. When we climb a steep hill, our camera may wind up inside the terrain because there are vertices that have been projected out above the point we are at. Since their original position is a different (x, y) value, we can’t easily find them.
We’d need some sort of collision detection system to handle this. We’d have to find our expected position and then collide that with the terrain to see if we can actually be there. If not, adjust for it.

4. I’m going to be using Direct3D 12.0 for this.

Yup. I used DirectX 12. It was totally unnecessary and I probably would have had a generally easier time working in DirectX 11, but it was good to get some experience with the basics of DX12. That being said, I still have a lot to learn. I only used Committed Resources, but what about Reserved and Placed Resources? What about multi-threading, which is pretty much the whole point of using DX12? I’ll have to tackle these and other points in future projects.

5. I want to implement tessellation. I’ll only implement it in the first person view mode as the bird’s eye view mode camera is too far away from the terrain to be visible anyway. I haven’t decided at this point whether or not I want to implement any sort of Level of Detail system. I think it depends on the FPS I’m getting. Long term, the tessellation would be part of the LOD system and would automatically be taken into account so there would be no difference between the 2 3D view modes I want. I don’t know if I’ll implement that right away or not.

I wound up implementing an LOD system using dynamic tessellation. It actually turned out to be one of the easier parts of this project.

6. In terms of texturing the terrain, I’d prefer not to use textures, but rather just use a decent colour palette.

I did implement a simple colour palette. Nothing too fancy, but it works. I actually wound up adding texturing and bump mapping as well. I think that looks a lot better.

7. I’d like to implement self shadowing.

This was by far the most frustrating part of the project. Implementing basic Shadow Mapping wasn’t terrible, but Cascaded Shadow Maps was painful. I did eventually get them figured out and working correctly and now I think they look pretty good.

There will be only one source of light: the Sun. It would be nice, however, to be able to support arbitrary directions for the Sun, so that I can have a day-night cycle and treat the height map as though it is at an arbitrary position on the globe.

I didn’t really do all this. I do have a Sun. Hell, I almost implemented a second light source for the moon. It wouldn’t have been that difficult to add, but it would have killed the frame rate as I’d have needed an additional four shadow passes for it.
My Shadow Mapping implementation will also handle an arbitrary position for the light source. But my actual day/night cycle is hard-coded to a specific, easy to implement, axis. I’m not going to deal with changing that now. It would just be a matter of implementing something different in the DayNightCycle class.

8. While I’m not terribly worried about efficiency with this project, I’d still like to maintain a frame rate at or above 60fps. Best case, I’d like it to stay above 120fps.

I actually didn’t do too terrible with this. With diffuse texturing disabled, the frame rate pretty much always stays above 60fps. I didn’t quite manage to keep it above 120fps. On average, it runs closer to 100fps.
With diffuse texturing enabled, the frame rate drops significantly, but we still stay above 30fps. That’s not terrible.
To improve on performance, I’d have to find a way to further reduce texture sampling. That seems to be the real killer right now. I was geometry limited until I implemented triplanar mapping. If I could find a way to generate (u, v) texture coordinates for the terrain that took the shape of the terrain into account and avoided texture stretching, I could drastically reduce the number of samples needed.
I never did revisit trying to reduce the number of passes required for Shadow Mapping. Using CSM currently means I need four passes, and we need to tessellate and render the terrain for each of those passes. I’ll have to come back to this at some point.
If we could get back to being geometry limited, I think there’s a big savings that could be made with the terrain’s mesh. Right now, we’re generating a static, homogeneous mesh centered on the origin. If we instead centered the mesh on the eye and stepped down the resolution of the patches as we got further away, we could reduce the number of triangles by a fair bit, while maintaining or even improving the visible resolution. I definitely want to implement this at some point, but I don’t really think it will have an impact on this project with the current texture sampling scheme.

My desktop will be my target platform. I’m running Windows 10 Pro, 32GB of RAM, a GTX680 with 4GB of video RAM, and a 4K monitor. So any target fps I aim for will be on that computer. As I’ve already made mention to, I’m working in Visual Studio 2015 and using Direct3D 12.0, although my card actually only supports feature level 11.0 so I won’t be using any more advanced features. I really have no reason to in this project, anyway.
EDIT – I have a 4K monitor, but for most of the performance info I mention, I’m actually only rendering a 1920×1080 window.

Nothing changed here. I tried running the program at 4K, but that drops the frame rate a bit further. I didn’t test extensively, but we drop down to at least 25fps, probably lower at worst case.

Overall, I’m pretty happy with the results of this project. I do think I wound up tackling too much at once. The shear number of posts attached to this project can tell you that.
What was new to me in this project? DirectX 12, tessellation, bump mapping, texture splatting, shadow maps, and CSM. So, pretty much everything of note.
In future projects I’d like to try and minimize the number of new things I introduce. That will lead to more, smaller projects. At least in theory. To some extent, projects may still wind up being of a decent size, as I’ll often be building on work done in earlier projects. The goal is just to not overwhelm myself with new topics like I’d say I did in this project.

3 Comments

I programmed with DX9 some years ago using Frank Luna’s book as a guide. Now I’m trying to get up to speed with DX12, but was really disappointed that all the terrain stuff was missing from his latest offering. I trawled through loads of online examples, but concluded that there was very little that would serve as a springboard to the type of game I was planning.

So you can imagine how pleased I was to find this site and read through your work. I like the fact that you present it warts and all with details of your thinking in relation to problems and bugs. Great stuff!

Although I work as a software developer, my DX12 project game is a hobby. I’d be happy to provide more details if you care to email me privately.

Thanks very much for your kind words. I hope my work is helpful.
Depending on what you’re looking to do with your game, you may ultimately be better off sticking with DX11. It would probably be easier to pick up as it would be very similar to the DX9 code you’re already used to. With DX12, you wind up having to handle a lot more resource management, and at a finer level of detail. Unless your project is going to be bottle-necked by the CPU processing frames and you want to try and split that work up across multiple threads, you’re doing more work for no reward.
Unless, of course, the challenge is, itself, enough reward…

DX9 was a long time ago and I haven’t done any graphics programming since then. DirectX seems sufficiently different now that I might as well jump back in at the deep end with DX12. I did consider DX11, but my project is quite ambitious in terms of both geometry and AI. There’s a 32x32km terrain, with tens of thousands of moving objects, needing effective LOD handling, routing, collision detection, etc. Since most people have multi-core processors these days I figured the ability to fully utilise them would be well worth having. And yes, for me the challenge is what programming is all about.