heightmap rendering
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [models] example - heightmap rendering
//!
//! Zig-API translation of examples/models/models_heightmap_rendering.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
const rl = @import("raylib");
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [models] example - heightmap rendering");
defer rl.closeWindow(); // Close window and OpenGL context
// Define our custom camera to look into our 3d world
var camera: rl.Camera = .{
.position = .{ .x = 18.0, .y = 21.0, .z = 18.0 }, // Camera position
.target = .{ .x = 0.0, .y = 0.0, .z = 0.0 }, // Camera looking at point
.up = .{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target)
.fovy = 45.0, // Camera field-of-view Y
.projection = .perspective, // Camera projection type
};
const image = try rl.loadImage("resources/heightmap.png"); // Load heightmap image (RAM)
const texture = try rl.loadTextureFromImage(image); // Convert image to texture (VRAM)
defer rl.unloadTexture(texture);
const mesh = rl.genMeshHeightmap(image, .{ .x = 16, .y = 8, .z = 16 }); // Generate heightmap mesh (RAM and VRAM)
const model = try rl.loadModelFromMesh(mesh); // Load model from generated mesh
defer rl.unloadModel(model);
model.materials.?[0].map(.diffuse).texture = texture; // Set map diffuse texture
const map_position: rl.Vector3 = .{ .x = -8.0, .y = 0.0, .z = -8.0 }; // Define model position
rl.unloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
rl.updateCamera(&camera, .orbital);
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
{
rl.beginMode3D(camera);
defer rl.endMode3D();
rl.drawModel(model, map_position, 1.0, .red);
rl.drawGrid(20, 1.0);
}
rl.drawTexture(texture, screen_width - texture.width - 20, 20, .white);
rl.drawRectangleLines(screen_width - texture.width - 20, 20, texture.width, texture.height, .green);
rl.drawFPS(10, 10);
}
}/*******************************************************************************************
*
* raylib [models] example - heightmap rendering
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 1.8, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap rendering");
// Define our custom camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 18.0f, 21.0f, 18.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
Mesh mesh = GenMeshHeightmap(image, (Vector3){ 16, 8, 16 }); // Generate heightmap mesh (RAM and VRAM)
Model model = LoadModelFromMesh(mesh); // Load model from generated mesh
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Define model position
UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, RED);
DrawGrid(20, 1.0f);
EndMode3D();
DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture
UnloadModel(model); // Unload model
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.