Terrain

Mach::GL has a built in terrain generator. This is based on Perlin Noise and can take in many of the same parameters as a standard Perlin Noise generator.

A terrain object can be created by using one of the following constructors:

Object::Terrain terrain1(size, vertexCount);
Object::Terrain terrain2(size, vertexCount, amplitude, octaves, roughness, seed);

Notice that the second constructor has a seed option, this allows for control over the randomness of the terrain or allows the terrain to generated the same if a fixed value is entered. If the first constructor is used (without seed), the seed is randomly generated.

To use the generated terrain, call the function terrain1.getModel() this return an Object::MACH_MODEL type that can be used directly in a Object::MACH_OBJECT.

The texture used on the Object can be scaled using the properties.textureScale property in the Object::ObjectProperties type linked with the object the terrain is being used in.

Please note that setting the ObjectType to TERRAIN will not allow terrain objects to be culled in the renderer.

Example code

#include "../MachGL/MachGL.h"

using namespace MachGL;

int main() {

    uint32_t width = 1920;
    uint32_t height = 1080;

    MACH_WINDOW window = Window::createWindow("Window Title", width, height);
    window->MSAA(4);
    window->init();

    float3 terrainPos(0, 0, 0);

    Graphics::MACH_RENDERER_3D renderer = Graphics::Renderer3D::createRenderer();
    Graphics::MACH_IMAGE terrainTexture = Graphics::Image::createImage("terrain.png", Graphics::ImageType::RGB);
    Object::Terrain terrain(800, 50, 2.5f, 1, 1.5f, 1);
    
    //Create the Object using the Terrain model and setting the ObjectType to TERRAIN 
    Object:MACH_OBJECT terrainObject = Object::Object::createObject(terrain.getModel(), terrainPos, terrainTexture, Object::ObjectType::TERRAIN);

    //Setting the properties for the terrain object
    //Scaling the texture by a factor of 10 (this makes it 10x smaller)
    Object::ObjectProperties terrainProperties;
    terrainProperties.textureScale = 10;

    terrainObject->create(terrainProperties);

    while (!window->closed()) {
        window->clear();

        renderer->submit(terrainObject);

        window->update();
    }

    window->close();
    
    return 0;
}