From a6f94ce65584a0936dd14364dc2843c478b046ff Mon Sep 17 00:00:00 2001 From: "Nicholas S. Zivkovic" Date: Fri, 18 Oct 2024 13:19:22 +0200 Subject: [PATCH] ported example 3d camera free (#162) --- build.zig | 5 +++ examples/core/3d_camera_free.zig | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 examples/core/3d_camera_free.zig diff --git a/build.zig b/build.zig index 969f151..b34211f 100755 --- a/build.zig +++ b/build.zig @@ -231,6 +231,11 @@ pub fn build(b: *std.Build) !void { .path = "examples/core/3d_camera_first_person.zig", .desc = "Simple first person demo", }, + .{ + .name = "3d_camera_free", + .path = "examples/core/3d_camera_free.zig", + .desc = "Shows basic 3d camera initialization", + }, .{ .name = "2d_camera_mouse_zoom", .path = "examples/core/2d_camera_mouse_zoom.zig", diff --git a/examples/core/3d_camera_free.zig b/examples/core/3d_camera_free.zig new file mode 100644 index 0000000..403db77 --- /dev/null +++ b/examples/core/3d_camera_free.zig @@ -0,0 +1,69 @@ +// A raylib port of https://github.com/raysan5/raylib/blob/master/examples/core/core_3d_camera_free.c + +const rl = @import("raylib"); + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +pub fn main() anyerror!void { + // Initialization + //-------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + rl.initWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free"); + defer rl.closeWindow(); // Close window and OpenGL context + + // Define the camera to look into our 3d world + var camera = rl.Camera{ + .position = rl.Vector3.init(10, 10, 10), + .target = rl.Vector3.init(0, 0, 0), + .up = rl.Vector3.init(0, 1, 0), + .fovy = 45, + .projection = rl.CameraProjection.camera_perspective, + }; + + const cubePosition = rl.Vector3.init(0, 0, 0); + + rl.disableCursor(); // Limit cursor to relative movement inside the window + 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 + //----------------------------------------------------------------------------- + camera.update(rl.CameraMode.camera_free); + + if (rl.isKeyPressed(rl.KeyboardKey.key_z)) { + camera.target = rl.Vector3.init(0, 0, 0); + } + //----------------------------------------------------------------------------- + + // Draw + //----------------------------------------------------------------------------- + rl.beginDrawing(); + defer rl.endDrawing(); + + rl.clearBackground(rl.Color.ray_white); + + { + camera.begin(); + defer camera.end(); + + rl.drawCube(cubePosition, 2, 2, 2, rl.Color.red); + rl.drawCubeWires(cubePosition, 2, 2, 2, rl.Color.maroon); + + rl.drawGrid(10, 1); + } + + rl.drawRectangle(10, 10, 320, 93, rl.Color.fade(rl.Color.sky_blue, 0.5)); + rl.drawRectangleLines(10, 10, 320, 93, rl.Color.blue); + + rl.drawText("Free camera default controls:", 20, 20, 10, rl.Color.black); + rl.drawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, rl.Color.dark_gray); + rl.drawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, rl.Color.dark_gray); + rl.drawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, rl.Color.dark_gray); + //----------------------------------------------------------------------------- + } +}