From 8e85b084e6ec73cd99899ad276891eb40944b909 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 3 May 2022 13:59:12 +0200 Subject: Created stub 3d space with 2d points --- .gitignore | 1 + CMakeLists.txt | 11 ++++++++ main.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a5309e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build*/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b99e075 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.16) +project(derelict) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_EXTENSIONS No) +find_package(raylib 3 REQUIRED) +add_executable( + ${PROJECT_NAME} + main.cpp +) +target_link_libraries(${PROJECT_NAME} raylib) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..dfe0990 --- /dev/null +++ b/main.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +#include + + +constexpr int AMOUNT {2000}; + + +struct Entry +{ + Vector2 pos; + Vector2 base; + float depth; +}; + + +int +main(int argc, char* argv[]) +{ + InitWindow(800, 600, "Derelict"); + SetWindowState(FLAG_WINDOW_RESIZABLE); + SetTargetFPS(60); + Camera camera {}; + camera.position = Vector3{10.0, 10.0, 10.0}; + camera.target = Vector3{0.0, 0.0, 0.0}; + camera.up = Vector3{0.0, 1.0, 0.0}; + camera.fovy = 45; + camera.projection = CAMERA_PERSPECTIVE; + SetCameraMode(camera, CAMERA_ORBITAL); + std::vector points; + points.reserve(AMOUNT); + for (int i = 0; i < AMOUNT; ++i) + points.push_back({ + GetRandomValue(-100, 100) * 0.05f, + GetRandomValue(-100, 100) * 0.05f, + GetRandomValue(-100, 100) * 0.05f}); + std::vector projected; + while (!WindowShouldClose()) + { + UpdateCamera(&camera); + projected.clear(); + projected.reserve(points.size()); + const int height = GetScreenHeight(); + const int width = GetScreenWidth(); + for (const auto& point : points) { + const auto vec2 = GetWorldToScreen(point, camera); + const float d = + std::sqrt( + std::pow(camera.position.x - point.x, 2) + + std::pow(camera.position.y - point.y, 2) + + std::pow(camera.position.z - point.z, 2)); + if (0 > vec2.x || width < vec2.x || 0 > vec2.y || height < vec2.y) + continue; + projected.push_back(Entry{ + vec2, GetWorldToScreen({point.x, 0, point.z}, camera), d + }); + } + std::sort(projected.begin(), projected.end(), [](Entry& a, Entry& b){ return a.depth > b.depth; }); + BeginDrawing(); + ClearBackground(RAYWHITE); + BeginMode3D(camera); + DrawGrid(12, 1); + EndMode3D(); + DrawFPS(5, 5); + for (const auto& entry : projected) { + DrawLine(entry.base.x, entry.base.y, entry.pos.x, entry.pos.y, LIGHTGRAY); + DrawCircle(entry.pos.x, entry.pos.y, 3, RED); + } + int y = 25; + for (const auto& entry : projected) { + DrawText(TextFormat("%5.1f x %5.1f x %5.1f", entry.pos.x, entry.pos.y, entry.depth), 5, y, 10, DARKGRAY); + y += 10; + if (y > height) + break; + } + EndDrawing(); + } + CloseWindow(); +} -- cgit v1.1