From c42b641213098288477e132a11ea9797493e4d12 Mon Sep 17 00:00:00 2001 From: mRnea Date: Tue, 24 Dec 2024 15:08:39 +0300 Subject: testing raylib 3d plane --- 3d-test/Makefile | 6 ++++ 3d-test/plane.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 3d-test/Makefile create mode 100644 3d-test/plane.c diff --git a/3d-test/Makefile b/3d-test/Makefile new file mode 100644 index 0000000..9d156e9 --- /dev/null +++ b/3d-test/Makefile @@ -0,0 +1,6 @@ +SRC := ../util.c plane.c +OUT := plane +LIBS := -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 + +make all: + gcc $(SRC) -o $(OUT) $(LIBS) diff --git a/3d-test/plane.c b/3d-test/plane.c new file mode 100644 index 0000000..b2197a7 --- /dev/null +++ b/3d-test/plane.c @@ -0,0 +1,108 @@ +#include "raylib.h" + +#include "raymath.h" // Required for: MatrixRotateXYZ() + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + //SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_WINDOW_HIGHDPI); + InitWindow(screenWidth, screenHeight, "raylib [models] example - plane rotations (yaw, pitch, roll)"); + + Camera camera = { 0 }; + camera.position = (Vector3){ 0.0f, 50.0f, -120.0f };// Camera position perspective + 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 = 30.0f; // Camera field-of-view Y + camera.projection = CAMERA_PERSPECTIVE; // Camera type + + Model model = LoadModel("res/plane.obj"); // Load model + Texture2D texture = LoadTexture("res/plane_diffuse.png"); // Load model texture + model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture + + float pitch = 0.0f; + float roll = 0.0f; + float yaw = 0.0f; + + 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 + //---------------------------------------------------------------------------------- + // Plane pitch (x-axis) controls + if (IsKeyDown(KEY_DOWN)) pitch += 0.6f; + else if (IsKeyDown(KEY_UP)) pitch -= 0.6f; + else + { + if (pitch > 0.3f) pitch -= 0.3f; + else if (pitch < -0.3f) pitch += 0.3f; + } + + // Plane yaw (y-axis) controls + if (IsKeyDown(KEY_S)) yaw -= 1.0f; + else if (IsKeyDown(KEY_A)) yaw += 1.0f; + else + { + if (yaw > 0.0f) yaw -= 0.5f; + else if (yaw < 0.0f) yaw += 0.5f; + } + + // Plane roll (z-axis) controls + if (IsKeyDown(KEY_LEFT)) roll -= 1.0f; + else if (IsKeyDown(KEY_RIGHT)) roll += 1.0f; + else + { + if (roll > 0.0f) roll -= 0.5f; + else if (roll < 0.0f) roll += 0.5f; + } + + // Tranformation matrix for rotations + model.transform = MatrixRotateXYZ((Vector3){ DEG2RAD*pitch, DEG2RAD*yaw, DEG2RAD*roll }); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Draw 3D model (recomended to draw 3D always before 2D) + BeginMode3D(camera); + + DrawModel(model, (Vector3){ 0.0f, -8.0f, 0.0f }, 1.0f, WHITE); // Draw 3d model with texture + DrawGrid(10, 10.0f); + + EndMode3D(); + + // Draw controls info + DrawRectangle(30, 370, 260, 70, Fade(GREEN, 0.5f)); + DrawRectangleLines(30, 370, 260, 70, Fade(DARKGREEN, 0.5f)); + DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, DARKGRAY); + DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, DARKGRAY); + DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, DARKGRAY); + + DrawText("(c) WWI Plane Model created by GiaHanLam", screenWidth - 240, screenHeight - 20, 10, DARKGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadModel(model); // Unload model data + UnloadTexture(texture); // Unload texture data + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} -- cgit v1.2.3