Jonatan Ebenholm
← all projects

Terrain Libraryon hold

C++ terrain library: heightmaps, marching cubes and voxel worlds, all on the GPU.

Overview

This project is a WIP library for personal use, but open for anyone to use or modify. Its purpose is to have a plethora of easy single call functions that can generate terrain for future game projects that I might have, and for debugging and testing purposes has demos to show the intent and possible use of the library.


Current Features

  • Heightmap generation on CPU or GPU: The CPU and GPU implementation are very similar, but the GPU version is of course working on the terrain in parallel. The heightmap uses simple gradient noise by patriciogonzalezvivo on GitHub as a way of displacing vertices on a plane mesh to create terrain. There are two demos of the height maps, one which shows the effect of noise on the terrain, and one which shows the terrain being rendered and created in chunks.

  • Marching Cubes 3D terrain Generation on CPU or GPU: The CPU and GPU implementation are again very similar, but the GPU version works in parallel. The noise used for the marching cubes algorithm is a 3D gradient noise, simplex noise in fact, which was also fetched from patriciogonzalezvivo on GitHub. The noise in this algorithm functions not as a way to displace vertices, but instead as a way of defining which parts of a grid are "solid" or "air", and if there is a transition, the marching cubes algorithm will interpolate positions for vertices. There is one demo showing of the algorithm and it works by chunking just like one of the heightmap demos. The look-up tables needed for the algorithm were taken from this article by Paul Bourke in May 1994.

  • VoxelCube terrain Generation on GPU: This part of the project is by far the most impressive part, since a lot of time has been put into creating terrain that is game ready. The algorithm works similarly to the marching cubes one in that it is a voxel engine, but the actual terrain creation (vertex placement, normals creation etc...) is much simpler. What makes this part of the project stand out is the advanced techniques used in shaping the terrain. It layers multiple different noise maps with different splines, to combine 2D noise with 3D noise which comes together to create extreme mountains, groves, flat land and vast cave systems. This terrain can both be used to create visuals, but the data can also be used for physics interaction, which is where the project will be taken in the future to gamefy it. This project has one demo currently.


Technologies Used

  • C++: C++ is a high level, general-purpose programming language.
  • OpenGL Graphics pipeline: OpenGL (Open Graphics Library) is a cross language, cross-platform API for rendering 2D and 3D vector graphics. Typically used for graphics, the render pipeline works almost entirelly on the GPU, allowing for super fast rendering.
  • OpenGL ComputeShaders/Gpu Programming: Compute shaders have been core to OpenGL since version 4.3, and are a way to program using the GPU as opposed to the CPU, which allows for parallel programming which can speed up certain tasks by making the task go from a linear time complexity to a constant time complexity (kind of).
  • Premake: A build language that helps set up the paths for the c++ program.

A showcase of the heightmap terrain:

This is the standard settings for a chunk as I have defined it. It creates a plane as such.**

image

This following image showcases the plane in a lower resolution (fewer vertices).**

image

The next image shows the effect of having a higher frequency applied on the gradient noise which displaces the vertices.**

image

This final image of the heightmap terrain showcases what a chunking system of the terrain could look like.**

image


A showcase of the marching cubes terrain:

Terrain with the normals as output color

image

Same terrain but with the absolute value of the normals as output color

image

Decreased the frequency in the 3D gradient noise, making the room much larger

image

A video showcase of the user moving around in the terrain

A video showcase of the user moving around in the terrain AFTER I recently improved the performance for another project


A showcase of the Voxelcubes terrain:

Flat surface given by the noise algorithms

image

A riverbed made with custom splines with gradient noise applied to them

image

A grove made with the same custom spline

image

Caves forming underground branching all over the world

image

A satisfying result from using a dropoff value based on terrain height when generating terrain

image

A video showcasing the branching of the caves and the tunnelsystems that are created


TODO List for Library

  • Make the splines more customisable for VoxelCubes
  • Add biome system to VoxelCubes

TODO List for VoxelCube Game

  • Implement a debug mode for testing noise changing
  • Make physics work for player controller
  • Make a state machine for player character
  • Allow interaction with terrain by for example detroying terrain