summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-03 13:59:12 +0200
committerAki <please@ignore.pl>2022-05-03 14:01:45 +0200
commit8e85b084e6ec73cd99899ad276891eb40944b909 (patch)
tree027db6a699df31a27819a9de68cdbab82acf6258
downloadderelict-8e85b084e6ec73cd99899ad276891eb40944b909.zip
derelict-8e85b084e6ec73cd99899ad276891eb40944b909.tar.gz
derelict-8e85b084e6ec73cd99899ad276891eb40944b909.tar.bz2
Created stub 3d space with 2d points
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt11
-rw-r--r--main.cpp81
3 files changed, 93 insertions, 0 deletions
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 <algorithm>
+#include <cmath>
+#include <vector>
+
+#include <raylib.h>
+
+
+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<Vector3> 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<Entry> 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();
+}