WIP rcamera redesign, base functionality

This commit is contained in:
Crydsch 2022-06-13 11:56:39 +02:00
commit 71f0c3d071

View file

@ -10,6 +10,7 @@
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#define MAX_COLUMNS 20
@ -25,13 +26,14 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
// Define the camera to look into our 3d world (position, target, up vector)
Camera camera = { 0 };
camera.position = (Vector3){ 4.0f, 2.0f, 4.0f };
camera.target = (Vector3){ 0.0f, 1.8f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 60.0f;
camera.projection = CAMERA_PERSPECTIVE;
// Define the camera to look into our 3d world
Camera3D camera = {0};
InitializeCamera(&camera, (double)screenWidth / (double)screenHeight);
camera.target_position = (Vector3){ 0.0f, 2.0f, 0.0f }; // This is the eye position in FREE (and FIRST_PERSON) mode
//SetCameraMode(camera, CAMERA_THIRD_PERSON); // Set a different camera mode
// Catch curser
DisableCursor();
// Generates some random columns
float heights[MAX_COLUMNS] = { 0 };
@ -41,12 +43,10 @@ int main(void)
for (int i = 0; i < MAX_COLUMNS; i++)
{
heights[i] = (float)GetRandomValue(1, 12);
positions[i] = (Vector3){ (float)GetRandomValue(-15, 15), heights[i]/2.0f, (float)GetRandomValue(-15, 15) };
positions[i] = (Vector3){ (float)GetRandomValue(-15, 15), heights[i] / 2.0f, (float)GetRandomValue(-15, 15) };
colors[i] = (Color){ GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255 };
}
SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -62,30 +62,53 @@ int main(void)
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
ClearBackground(RAYWHITE);
BeginMode3D(camera);
BeginMode3D(camera);
DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall
// Draw player
if (camera.target_distance > 1.0f) {
DrawCube(camera.target_position, 0.5f, 0.5f, 0.5f, DARKGREEN);
DrawCubeWires(camera.target_position, 0.5f, 0.5f, 0.5f, LIME);
}
// Draw some cubes around
for (int i = 0; i < MAX_COLUMNS; i++)
{
DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, MAROON);
}
DrawPlane((Vector3) { 0.0f, 0.0f, 0.0f }, (Vector2) { 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
DrawCube((Vector3) { -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3) { 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
DrawCube((Vector3) { 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall
EndMode3D();
// Draw some columns
for (int i = 0; i < MAX_COLUMNS; i++)
{
DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, MAROON);
}
DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 220, 70, BLUE);
EndMode3D();
DrawText("First person camera default controls:", 20, 20, 10, BLACK);
DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY);
DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY);
// Draw info boxes
DrawRectangle(5, 5, 330, 100, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(5, 5, 330, 100, BLUE);
DrawText("Camera controls:", 15, 15, 10, BLACK);
DrawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 10, BLACK);
DrawText("- Look around: arrow keys or mouse", 15, 45, 10, BLACK);
DrawText("- Camera mode keys: 1, 2, 3, 4", 15, 60, 10, BLACK);
DrawText("- Target distance keys: num-plus, num-minus or mouse scroll", 15, 75, 10, BLACK);
DrawText("- Camera projection key: P", 15, 90, 10, BLACK);
DrawRectangle(565, 5, 230, 100, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(565, 5, 230, 100, BLUE);
DrawText("Camera status:", 575, 20, 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"), 575, 35, 10, BLACK);
DrawText(TextFormat("- Projection: %s", (camera.projection == CAMERA_PERSPECTIVE) ? "PERSPECTIVE" :
(camera.projection == CAMERA_ORTHOGRAPHIC) ? "ORTHOGRAPHIC" : "CUSTOM"), 575, 50, 10, BLACK);
DrawText(TextFormat("- Target position: (%06.3f, %06.3f, %06.3f)", camera.target_position.x, camera.target_position.y, camera.target_position.z), 575, 65, 10, BLACK);
DrawText(TextFormat("- Target distance: %06.3f", camera.target_distance), 575, 80, 10, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------