// Matrix code is based on cube example
DCPIPE 3 // -1 to 1 coordinate range, required by DDTERRAIN
DVXPIPE 5 // XYZ projection + matrix

DENABLE 0 // Vertex buffer
DENABLE 1 // Z sorting
DENABLE 2 // Lighting
DENABLE 3 // Backface culling

DSETLIGHT 0, Light

MPERSPECTIVE ProjectionMatrix, Perspective

// Rotate the terrain
TIMER EAX
DIV EAX, 4
MOV #RotateVector.w, EAX
MROTATE RotateMatrix, RotateVector

// Point camera at terrain
MLOOKAT ViewMatrix, LookAtArgs
MMUL ViewMatrix, RotateMatrix

MLOAD ViewMatrix
MLOADPROJ ProjectionMatrix

DCLRSCR Background

DCOLOR Foreground
//DXTEXTURE Texture // Doesn't work
DDTERRAIN Terrain

DVXFLUSH

DEXIT

COLOR Foreground, 253, 186, 49, 255
COLOR Background, 1, 46, 87, 255
COLOR White, 255, 255, 255, 255

//STRING Texture, "brick/brick_model";

// The terrain struct
Terrain:
    DB 8, 8 // Terrain size
    DB 16   // Draw distance, between 0 and 16
    DB 0, 0 // Terrain offset

    // 11 bytes unused
    DB 0,0,0,0,0,0
    DB 0,0,0,0,0

    // 8 x 8 heightmap
    DB  0.0,  0.3,  0.3,  0.3,  0.3,  0.0,  0.0,  0.0
    DB  0.3,  0.3,  0.3,  0.5,  0.0, -0.5,  0.0,  0.0
    DB  0.0,  1.5,  1.5,  1.0,  0.3, -0.5, -0.3,  0.0
    DB  0.0,  1.0,  2.3,  1.8,  0.8,  0.3,  0.0,  0.0
    DB  0.3,  0.8,  1.3,  2.3,  1.6,  0.8,  0.5,  0.0
    DB  0.3,  0.5,  1.0,  1.3,  0.5,  0.3,  0.3,  0.0
    DB  0.0,  0.3,  0.3,  0.3,  0.0, -0.3, -0.5,  0.0
    DB  0.0,  0.0,  0.0,  0.0, -0.8, -0.8, -1.0, -0.5

Light:
    DB 0,   50, -50,  0   // Position
    DB 255, 249, 225, 0.9 // RGB + Intensity

MATRIX ProjectionMatrix
MATRIX RotateMatrix
MATRIX ViewMatrix

VEC4F RotateVector, 0,  0,  1,  0  // Rotate around Z axis
VEC4F Perspective,  50, 1,  1,  20 // 2nd value is aspect ratio

LookAtArgs:
    DB 0,  5,  4 // Camera
    DB 0,  0,  0 // Look at
    DB 0,  0, -1 // Up (terrain is upside-down for some reason)
