KPTerrain Generator System
Terrain Generation
The KPTerrain Generator System is a terrain generator that allows one to quickly generate terrain geometry with various Perlin noise parameters. One can then flexibly extract that data for geometry and vertex color data.
The Cool Things You Can Do
Parameters:
Terrain Width and Height in X and Z direction
Terrain Geometry Density with Width and Height Subdivision Counts
Max Altitude: Controls height in the Y direction
Min Altitude Color: Color of terrain at 0 Y value
Max Altitude Color: Color of terrain at Max Altitude Y value
Perlin Noise Seed: Controls randomness
Perlin Octave Count: Controls iterations of Perlin Noise to perform
Perlin X Sample Multiplier: Controls rate of sampling in X direction. Should mostly be the same as Y Sampler.
Perlin Y Sampler Multiplier: Controls rate of sampling in Y direction. Should mostly be the same as the X Sampler.
Shown below you can see a lua implemented input file to control the parameters of the terrain builder. Config is currently empty for possible future implementations.
Parameters
System Outline:
The System is composed of two primary parts:
KP Terrain Builder: Builds terrain binary file from lua file parameters
KP Terrain Library: Currently contains a reader for reading the binary file for geometry data. (This is optional if you want to read the terrain data directly into your own geometry, versus having this intermediate reader)
x86 building will automatically build for OpenGL (right handed), while x64 building will automatically build for DirectX (left handed)
Usage Instructions:
Set up
Download Zip at the bottom of this page for the KPTerrainBuilder, KPTerrainLibrary, and an example input lua file.
Add Projects to your solution.
Add new terrain file type shown below in figure 1 to AssetBuildFunctions.lua in your AssetBuildLibrary project. Should go next to your other asset types.
Add KPTerrainBuilder Dependency to BuildMyGameAssets Project, for auto building.
Figure 1
NewAssetTypeInfo( "terrain", { ConvertSourceRelativePathToBuiltRelativePath = function( i_sourceRelativePath ) -- Change the source file extension to the binary version local relativeDirectory, file = i_sourceRelativePath:match( "(.-)([^/\\]+)$" ) local fileName, extensionWithPeriod = file:match( "([^%.]+)(.*)" ) -- The line below just puts the original pieces back together, -- but you could change this to customize the way that you build assets -- (you could, for example, use a different extension for binary shaders) -- return relativeDirectory .. fileName .. extensionWithPeriod (To Keep Path) return relativeDirectory .. fileName .. ".terrainbin" end, GetBuilderRelativePath = function() return "KPTerrainBuilder.exe" end } )
Building terrain file:
Inside Content folder for your game. Add a folder for terrain.
Using one of the given terrain lua files as a reference. copy it to the folder.
Add a reference to it to your AssetsToBuild.lua. Shown in figure 2 below.
You should be able to right click on your BuildMyGameAssets and build the project to auto generate a binary terrain file into your building directory.
Using the referenced parameters up above. You should be able to modify parameters to your hearts content.
Figure 2
using file reader to read terrain geometry:
Add a reference to the KPTerrainLibrary project in your MyGame project.
With where you want to load in data, include: #include "cKPTerrainFile.h"
The interface and examples below show how to load a file, release a file, and extract data
One would first load a file.
Extract a copy of the data into the sKPTerrainData struct.
Release the file.
Utilize the data extracted in your own game objects.
With some extra sweat work. You could directly read the terrain binary file into your own geometry. You can reference the “CreateTerrainGeometryFromBinary” function in the ““cKPTerrainFile.cpp“ for reference. Otherwise, you can just use my file reader to extract data, and the build something to convert that data for your own geometry.
Interface for File Reading
Stuct for Data Extracted From Interface
Stuct for Vertex of Terrain Geometry Data
Loading terrain file.
Extracting terrain geometry data into my own geometry.
Notes:
Depending on your vertex size limit. You may have geometry that won’t render. The builder will not set a limit of geometry. So if you are rendering a 1000 x 1000. subdivided terrain and not everything is showing up. That’s probably why.
The load file ->extraction -> moving to your own geometry. Has the over-head of having to move data into your own geometry. I figured for simplicity sake this isn’t a problem because terrain is only ever really created a couple of times. Up to you if you want to directly read the file into your own geometry.
You will have to implement Vertex Coloring if you want to use the color part.
You may have to adjust the far-plane or near-plane of your camera. Terrain gets pretty big.
Things Learned:
Learned Perlin Noise algorithm implementation details. I tried on my own initially, but then had to resort to using someone else’s perlin noise algorithm (referenced in license) due to running into problems. I will probably dive more into this later.
Separation of modules. I got a builder, and a reader. You use a reader to read files made by builder. Simple and clearly separated for maintainability.
Problems I Ran Into:
Implementing my own Perlin Noise Algorithm. You have to watch out for floating point rounding issues, and check your math. I’m also still trying to understand the parameters into the algorithm and what they do.
Implementing Vertex Color had me running into a couple of issues.
Correct winding order for the terrain geometry had me running into a couple of issues at one point as well. I was at one point accidentally flipping triangle every other triangle.