ported example 3d camera free (#162)

This commit is contained in:
Nicholas S. Zivkovic 2024-10-18 13:19:22 +02:00 committed by GitHub
parent a2397363aa
commit a6f94ce655
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 0 deletions

View File

@ -231,6 +231,11 @@ pub fn build(b: *std.Build) !void {
.path = "examples/core/3d_camera_first_person.zig", .path = "examples/core/3d_camera_first_person.zig",
.desc = "Simple first person demo", .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", .name = "2d_camera_mouse_zoom",
.path = "examples/core/2d_camera_mouse_zoom.zig", .path = "examples/core/2d_camera_mouse_zoom.zig",

View File

@ -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);
//-----------------------------------------------------------------------------
}
}