From 84a1aa0a7dd0116ca15b8c6e62966849f0432bb0 Mon Sep 17 00:00:00 2001 From: Crydsch Date: Tue, 30 Aug 2022 18:17:40 +0200 Subject: [PATCH] remove camera.mode from struct --- examples/core/core_3d_camera_first_person.c | 64 ++++++++++++-- src/raylib.h | 4 +- src/rcamera.h | 96 +++------------------ 3 files changed, 68 insertions(+), 96 deletions(-) diff --git a/examples/core/core_3d_camera_first_person.c b/examples/core/core_3d_camera_first_person.c index fec20f633..583ac0e1d 100644 --- a/examples/core/core_3d_camera_first_person.c +++ b/examples/core/core_3d_camera_first_person.c @@ -12,6 +12,7 @@ ********************************************************************************************/ #include "raylib.h" +#include "rcamera.h" #define MAX_COLUMNS 20 @@ -34,9 +35,9 @@ int main(void) camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; camera.fovy = 60.0f; camera.projection = CAMERA_PERSPECTIVE; + camera.swingCounter = 1; // Enable view bobbing - // Set a first person camera mode - SetCameraMode(&camera, CAMERA_FIRST_PERSON); + int cameraMode = CAMERA_FIRST_PERSON; // Generates some random columns float heights[MAX_COLUMNS] = { 0 }; @@ -50,6 +51,9 @@ int main(void) colors[i] = (Color){ GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255 }; } + // Catch cursor + DisableCursor(); + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -58,7 +62,51 @@ int main(void) { // Update //---------------------------------------------------------------------------------- - UpdateCamera(&camera); + // Switch camera mode + if (IsKeyPressed(KEY_ONE)) { + cameraMode = CAMERA_FREE; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll + } + if (IsKeyPressed(KEY_TWO)) { + cameraMode = CAMERA_FIRST_PERSON; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll + } + if (IsKeyPressed(KEY_THREE)) { + cameraMode = CAMERA_THIRD_PERSON; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll + } + if (IsKeyPressed(KEY_FOUR)) { + cameraMode = CAMERA_ORBITAL; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Reset roll + } + + // Switch camera projection + if (IsKeyPressed(KEY_P)) { + if (camera.projection == CAMERA_PERSPECTIVE) { + // Create isometric view + cameraMode = CAMERA_THIRD_PERSON; + // Note: The target distance is related to the render distance in the orthographic projection + camera.position = (Vector3){ 0.0f, 2.0f, -100.0f }; + camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; + camera.projection = CAMERA_ORTHOGRAPHIC; + camera.fovy = 20.0f; // near plane width in CAMERA_ORTHOGRAPHIC + CameraYaw(&camera, -135 * DEG2RAD, true); + CameraPitch(&camera, -45 * DEG2RAD, true, true, false); + } + else if (camera.projection == CAMERA_ORTHOGRAPHIC) + { + // Reset to default view + cameraMode = CAMERA_THIRD_PERSON; + camera.position = (Vector3){ 0.0f, 2.0f, 10.0f }; + camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; + camera.projection = CAMERA_PERSPECTIVE; + camera.fovy = 60.0f; + } + } + + UpdateCamera(&camera, cameraMode); // Update camera //---------------------------------------------------------------------------------- // Draw @@ -82,7 +130,7 @@ int main(void) } // Draw player cube - if (camera.mode == CAMERA_THIRD_PERSON) + if (cameraMode == CAMERA_THIRD_PERSON) { DrawCube(camera.target, 0.5f, 0.5f, 0.5f, PURPLE); DrawCubeWires(camera.target, 0.5f, 0.5f, 0.5f, DARKPURPLE); @@ -105,10 +153,10 @@ int main(void) DrawRectangleLines(600, 5, 195, 100, BLUE); DrawText("Camera status:", 610, 15, 10, BLACK); - DrawText(TextFormat("- Mode: %s", (camera.mode == CAMERA_FREE) ? "FREE" : - (camera.mode == CAMERA_FIRST_PERSON) ? "FIRST_PERSON" : - (camera.mode == CAMERA_THIRD_PERSON) ? "THIRD_PERSON" : - (camera.mode == CAMERA_ORBITAL) ? "ORBITAL" : "CUSTOM"), 610, 30, 10, BLACK); + DrawText(TextFormat("- Mode: %s", (cameraMode == CAMERA_FREE) ? "FREE" : + (cameraMode == CAMERA_FIRST_PERSON) ? "FIRST_PERSON" : + (cameraMode == CAMERA_THIRD_PERSON) ? "THIRD_PERSON" : + (cameraMode == CAMERA_ORBITAL) ? "ORBITAL" : "CUSTOM"), 610, 30, 10, BLACK); DrawText(TextFormat("- Projection: %s", (camera.projection == CAMERA_PERSPECTIVE) ? "PERSPECTIVE" : (camera.projection == CAMERA_ORTHOGRAPHIC) ? "ORTHOGRAPHIC" : "CUSTOM"), 610, 45, 10, BLACK); DrawText(TextFormat("- Position: (%06.3f, %06.3f, %06.3f)", camera.position.x, camera.position.y, camera.position.z), 610, 60, 10, BLACK); diff --git a/src/raylib.h b/src/raylib.h index fa32d34e1..fb25532e6 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -307,7 +307,6 @@ typedef struct Camera3D { Vector3 up; // Camera up vector (rotation around the view axis) float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic int projection; // Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC - int mode; // Camera mode: CAMERA_FREE, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON, CAMERA_ORBITAL or CUSTOM int swingCounter; // Camera view-bobbing. Set to 0 to deactivate. } Camera3D; @@ -1153,8 +1152,7 @@ RLAPI float GetGesturePinchAngle(void); // Get gesture pinch ang //------------------------------------------------------------------------------------ // Camera System Functions (Module: rcamera) //------------------------------------------------------------------------------------ -RLAPI void SetCameraMode(Camera3D* camera, int mode); // Set camera mode (multiple camera modes available) -RLAPI void UpdateCamera(Camera3D *camera); // Update camera position for selected mode +RLAPI void UpdateCamera(Camera3D *camera, int mode); // Update camera position for selected mode //------------------------------------------------------------------------------------ // Basic Shapes Drawing Functions (Module: shapes) diff --git a/src/rcamera.h b/src/rcamera.h index 70baacf9c..ec3bc9af5 100644 --- a/src/rcamera.h +++ b/src/rcamera.h @@ -115,22 +115,19 @@ extern "C" { // Prevents name mangling of functions #endif -#if defined(CAMERA_STANDALONE) -void SetCameraMode(Camera3D* camera, int mode); Vector3 GetCameraForward(Camera3D* camera); Vector3 GetCameraUp(Camera3D* camera); Vector3 GetCameraRight(Camera3D* camera); -void CameraMoveForward(Camera3D* camera, float distance); +void CameraMoveForward(Camera3D* camera, float distance, bool moveInWorldPlane); void CameraMoveUp(Camera3D* camera, float distance); -void CameraMoveRight(Camera3D* camera, float distance); +void CameraMoveRight(Camera3D* camera, float distance, bool moveInWorldPlane); void CameraZoom(Camera3D* camera, float delta); -void CameraYaw(Camera3D* camera, float angle); -void CameraPitch(Camera3D* camera, float angle); +void CameraYaw(Camera3D* camera, float angle, bool rotateAroundTarget); +void CameraPitch(Camera3D* camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp); void CameraRoll(Camera3D* camera, float angle); void CameraViewBobbing(Camera3D* camera); Matrix GetCameraViewMatrix(Camera3D* camera); Matrix GetCameraProjectionMatrix(Camera3D* camera, float aspect); -#endif #if defined(__cplusplus) } @@ -173,22 +170,6 @@ Matrix GetCameraProjectionMatrix(Camera3D* camera, float aspect); //---------------------------------------------------------------------------------- // Types and Structures Definition //---------------------------------------------------------------------------------- -// TODO review -// Camera actions -//typedef enum { -// CAMERA_MOVE_FORWARD = 0, -// CAMERA_MOVE_BACK, -// CAMERA_MOVE_RIGHT, -// CAMERA_MOVE_LEFT, -// CAMERA_MOVE_UP, -// CAMERA_MOVE_DOWN, -// CAMERA_YAW_RIGHT, -// CAMERA_YAW_LEFT, -// CAMERA_PITCH_UP, -// CAMERA_PITCH_DOWN, -// CAMERA_ROLL_RIGHT, -// CAMERA_ROLL_LEFT, -//} CameraActions; //---------------------------------------------------------------------------------- @@ -202,6 +183,7 @@ Matrix GetCameraProjectionMatrix(Camera3D* camera, float aspect); // Rotates the given vector around the given axis // Note: angle must be provided in radians +// TODO remove after rebase (available in raymath now) Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle) { // Using Euler-Rodrigues Formula @@ -257,31 +239,6 @@ Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle) // Module Functions Definition //---------------------------------------------------------------------------------- -// Select camera mode (multiple camera modes available) -void SetCameraMode(Camera3D *camera, int mode) -{ - camera->mode = mode; - - // Reset roll - camera->up = (Vector3){ 0.0f, 1.0f, 0.0f }; - - // Enable view bobbing in CAMERA_FIRST_PERSON - if (camera->mode == CAMERA_FIRST_PERSON) - { - camera->swingCounter = 1; // Enable - } - else - { - camera->swingCounter = 0; // Disable - } - - // Catch cursor -#ifndef CAMERA_STANDALONE - // Note: This is engine specific functionality - DisableCursor(); -#endif -} - // Returns the cameras forward vector (normalized) Vector3 GetCameraForward(Camera3D *camera) { @@ -511,7 +468,8 @@ Matrix GetCameraProjectionMatrix(Camera3D *camera, float aspect) static int init_frames = 3; // TODO review and remove // Update camera position for selected mode -void UpdateCamera(Camera3D *camera) +// Camera mode: CAMERA_FREE, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON, CAMERA_ORBITAL or CUSTOM +void UpdateCamera(Camera3D *camera, int mode) { // Avoid inital mouse "jump" if (init_frames > 0) { @@ -521,10 +479,10 @@ void UpdateCamera(Camera3D *camera) } Vector2 mousePositionDelta = GetMouseDelta(); - bool moveInWorldPlane = camera->mode == CAMERA_FIRST_PERSON || camera->mode == CAMERA_THIRD_PERSON; - bool rotateAroundTarget = camera->mode == CAMERA_THIRD_PERSON || camera->mode == CAMERA_ORBITAL; - bool lockView = camera->mode == CAMERA_FIRST_PERSON || camera->mode == CAMERA_THIRD_PERSON || camera->mode == CAMERA_ORBITAL; - bool rotateUp = camera->mode == CAMERA_FREE; + bool moveInWorldPlane = mode == CAMERA_FIRST_PERSON || mode == CAMERA_THIRD_PERSON; + bool rotateAroundTarget = mode == CAMERA_THIRD_PERSON || mode == CAMERA_ORBITAL; + bool lockView = mode == CAMERA_FIRST_PERSON || mode == CAMERA_THIRD_PERSON || mode == CAMERA_ORBITAL; + bool rotateUp = mode == CAMERA_FREE; // Camera movement if (IsKeyDown(KEY_W)) CameraMoveForward(camera, CAMERA_MOVE_SPEED, moveInWorldPlane); @@ -550,38 +508,6 @@ void UpdateCamera(Camera3D *camera) if (IsKeyPressed(KEY_KP_SUBTRACT)) CameraZoom(camera, 2.0f); if (IsKeyPressed(KEY_KP_ADD)) CameraZoom(camera, -2.0f); - // Switch camera mode - if (IsKeyPressed(KEY_ONE)) SetCameraMode(camera, CAMERA_FREE); - if (IsKeyPressed(KEY_TWO)) SetCameraMode(camera, CAMERA_FIRST_PERSON); - if (IsKeyPressed(KEY_THREE)) SetCameraMode(camera, CAMERA_THIRD_PERSON); - if (IsKeyPressed(KEY_FOUR)) SetCameraMode(camera, CAMERA_ORBITAL); - - // Switch camera projection - if (IsKeyPressed(KEY_P)) { - if (camera->projection == CAMERA_PERSPECTIVE) { - // Create isometric view - camera->mode = CAMERA_THIRD_PERSON; - // Note: The target distance is related to the render distance in the orthographic projection - camera->position = (Vector3){ 0.0f, 2.0f, -100.0f }; - camera->target = (Vector3){ 0.0f, 2.0f, 0.0f }; - camera->up = (Vector3){ 0.0f, 1.0f, 0.0f }; - camera->projection = CAMERA_ORTHOGRAPHIC; - camera->fovy = 20.0f; // near plane width in CAMERA_ORTHOGRAPHIC - CameraYaw(camera, -135 * DEG2RAD, rotateAroundTarget); - CameraPitch(camera, -45 * DEG2RAD, lockView, rotateAroundTarget, rotateUp); - } - else if (camera->projection == CAMERA_ORTHOGRAPHIC) - { - // Reset to default view - camera->mode = CAMERA_THIRD_PERSON; - // Note: The target distance is related to the render distance in the orthographic projection - camera->position = (Vector3){ 0.0f, 2.0f, 10.0f }; - camera->target = (Vector3){ 0.0f, 2.0f, 0.0f }; - camera->up = (Vector3){ 0.0f, 1.0f, 0.0f }; - camera->projection = CAMERA_PERSPECTIVE; - camera->fovy = 60.0f; - } - } // Apply view bobbing when moving around (per default only active in CAMERA_FIRST_PERSON) if (IsKeyDown(KEY_W) || IsKeyDown(KEY_A) || IsKeyDown(KEY_S) || IsKeyDown(KEY_D)) CameraViewBobbing(camera);