diff --git a/build.zig b/build.zig index 2dbbfad..dcc2a20 100644 --- a/build.zig +++ b/build.zig @@ -373,11 +373,11 @@ pub fn build(b: *std.Build) !void { .desc = "Image loading and texture creation", }, - // .{ - // .name = "models_loading", - // .path = "examples/models/models_loading.zig", - // .desc = "Loads a model and renders it", - // }, + .{ + .name = "models_heightmap", + .path = "examples/models/models_heightmap.zig", + .desc = "Heightmap loading and drawing", + }, // .{ // .name = "shaders_basic_lighting", // .path = "examples/shaders/shaders_basic_lighting.zig", diff --git a/examples/models/models_heightmap.zig b/examples/models/models_heightmap.zig new file mode 100644 index 0000000..425bf83 --- /dev/null +++ b/examples/models/models_heightmap.zig @@ -0,0 +1,49 @@ +const rl = @import("raylib"); + +pub fn main() anyerror!void { + const screenWidth: i32 = 800; + const screenHeight: i32 = 450; + + rl.initWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing"); + const cameraPosition: rl.Vector3 = .{ .x = 18, .y = 21, .z = 18 }; + const cameraTarget: rl.Vector3 = .{ .x = 0, .y = 0, .z = 0 }; + const cameraUp: rl.Vector3 = .{ .x = 0, .y = 1, .z = 0 }; + const cameraProjection = rl.CameraProjection.perspective; + var camera = rl.Camera{ .fovy = 45.0, .position = cameraPosition, .up = cameraUp, .projection = cameraProjection, .target = cameraTarget }; + + const image: rl.Image = try rl.loadImage("examples/models/resources/heightmap.png"); + + const texture: rl.Texture2D = try rl.loadTextureFromImage(image); + + const meshSize = rl.Vector3{ .x = 16, .y = 8, .z = 16 }; + const mesh = rl.genMeshHeightmap(image, meshSize); + + var model = try rl.loadModelFromMesh(mesh); + model.materials[0].maps[@intFromEnum(rl.MATERIAL_MAP_DIFFUSE)].texture = texture; + + const mapPosition = rl.Vector3{ .x = -8.0, .y = 0.0, .z = -8.0 }; + + rl.unloadImage(image); + + rl.setTargetFPS(60); + + while (!rl.windowShouldClose()) { + rl.updateCamera(&camera, rl.CameraMode.orbital); + rl.beginDrawing(); + + rl.clearBackground(rl.Color.ray_white); + rl.beginMode3D(camera); + rl.drawModel(model, mapPosition, 1, rl.Color.red); + rl.drawGrid(20, 1.0); + rl.endMode3D(); + rl.drawTexture(texture, screenWidth - texture.width - 20, 20, rl.Color.white); + rl.drawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, rl.Color.green); + rl.drawFPS(10, 10); + + rl.endDrawing(); + } + rl.unloadTexture(texture); + rl.unloadModel(model); + + rl.closeWindow(); +} diff --git a/examples/models/resources/heightmap.png b/examples/models/resources/heightmap.png new file mode 100644 index 0000000..474db87 Binary files /dev/null and b/examples/models/resources/heightmap.png differ