From d8be22ebdc35a549853cd5587b4aa0921a2605a9 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 17 Mar 2021 23:58:12 -0400 Subject: [PATCH] 3d rays --- demo/demo.cpp | 28 ++++++++++++++++++++++-- demo/demo.h | 2 ++ main.cpp | 4 ++-- stuff/globals/globals.cpp | 3 ++- stuff/globals/globals.h | 2 ++ stuff/objects/camera.h | 16 +++++++++++++- stuff/objects/cube.h | 2 +- stuff/objects/ray3D.h | 46 +++++++++++++++++++++++++++++++++++++++ stuff/objects/vector3.h | 9 ++++++++ threedee.vcxproj | 1 + threedee.vcxproj.filters | 3 +++ 11 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 stuff/objects/ray3D.h diff --git a/demo/demo.cpp b/demo/demo.cpp index a399c1f..f649a80 100644 --- a/demo/demo.cpp +++ b/demo/demo.cpp @@ -11,7 +11,31 @@ void game::run(SDL_Renderer* renderer) SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y); } global::entList.at(0)->x = global::mousePos.x; - global::entList.at(0)->y = global::mousePos.y;*/ - //global::entList.run(renderer); + global::entList.at(0)->y = global::mousePos.y; + global::entList.run(renderer);*/ + //global::cam.run(renderer); + static float pitch, yaw; + for (int y = 1; y < 250; y += 1) + { + for (int x = 1; x < 250; x += 1) + { + ray3D ray1 = ray3D::trace(vector3(x, y, 0), vector3(yaw, pitch, 1)); + if (ray1.dist < 500) + { + SDL_SetRenderDrawColor(renderer, 255 - ray1.dist, 255 - ray1.dist, 255 - ray1.dist, 255); + SDL_RenderDrawPoint(renderer, x * 2, y * 2); + SETRENDER_WHITE; + } + } + } + if (GetAsyncKeyState(VK_DOWN)) + pitch += 1; + if (GetAsyncKeyState(VK_UP)) + pitch -= 1; + if (GetAsyncKeyState(VK_LEFT)) + yaw -= 1; + if (GetAsyncKeyState(VK_RIGHT)) + yaw += 1; + std::cout << global::entList3D.at(0)->x << global::entList3D.at(0)->y << std::endl; global::entList3D.run(renderer); } \ No newline at end of file diff --git a/demo/demo.h b/demo/demo.h index ae6ebee..cb02fd1 100644 --- a/demo/demo.h +++ b/demo/demo.h @@ -7,6 +7,8 @@ #include "../stuff/objects/vector2.h" #include "../stuff/objects/vector3.h" #include "../stuff/objects/ray2D.h" +#include "../stuff/objects/ray3D.h" +#include "../stuff/objects/camera.h" #include "../stuff/globals/globals.h" #include "../stuff/callbacks/callbacks.h" diff --git a/main.cpp b/main.cpp index bfb5aaa..ec43b0f 100644 --- a/main.cpp +++ b/main.cpp @@ -12,14 +12,14 @@ int main(int argc, char** argv) SDL_Renderer* renderer = nullptr; SDL_Init(SDL_INIT_VIDEO); - window = SDL_CreateWindow("threedee", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN); + window = SDL_CreateWindow("threedee", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN); surface = SDL_GetWindowSurface(window); renderer = SDL_CreateRenderer(window, -1, NULL); SETRENDER_WHITE; SDL_Event eventHandler; //global::entList.push_back(&square(200, 200, 50)); - global::entList3D.push_back(&cube(200, 200, 200, 50)); + global::entList3D.push_back(&cube(1, 1, 50, 50)); while (global::running) { diff --git a/stuff/globals/globals.cpp b/stuff/globals/globals.cpp index b7023da..2fb5891 100644 --- a/stuff/globals/globals.cpp +++ b/stuff/globals/globals.cpp @@ -3,4 +3,5 @@ bool global::running = true; vector2 global::mousePos; entityList2D global::entList; -entityList3D global::entList3D; \ No newline at end of file +entityList3D global::entList3D; +camera global::cam(vector3(0, 0, 0), vector3(0, 0, 0)); \ No newline at end of file diff --git a/stuff/globals/globals.h b/stuff/globals/globals.h index ea68238..03c053f 100644 --- a/stuff/globals/globals.h +++ b/stuff/globals/globals.h @@ -1,5 +1,6 @@ #pragma once #include "../objects/vector3.h" +#include "../objects/camera.h" #include "../objects/entityList2D.h" #include "../objects/entityList3D.h" namespace global @@ -8,4 +9,5 @@ namespace global extern vector2 mousePos; extern entityList2D entList; extern entityList3D entList3D; + extern camera cam; } \ No newline at end of file diff --git a/stuff/objects/camera.h b/stuff/objects/camera.h index dd88965..10bf2df 100644 --- a/stuff/objects/camera.h +++ b/stuff/objects/camera.h @@ -1,5 +1,19 @@ #pragma once +#include "vector3.h" +#include "SDL.h" +#include struct camera { - + vector3 position; + vector3 rotation; + camera(vector3 position_, vector3 rotation_) + { + this->position = position_; + this->rotation = rotation_; + } + void run(SDL_Renderer* renderer) + { + //ray3D ray = ray3D::trace(vector3(0, 0, -10), vector3(0, 0, 1)); + //std::cout << ray.dist << std::endl; + } }; \ No newline at end of file diff --git a/stuff/objects/cube.h b/stuff/objects/cube.h index a225ef3..7238c15 100644 --- a/stuff/objects/cube.h +++ b/stuff/objects/cube.h @@ -16,7 +16,7 @@ struct cube : public square } void run(SDL_Renderer* renderer) { - //SDL_Rect rect{ this->x, this->y, this->radius, this->radius }; + SDL_Rect rect{ this->x, this->y, this->radius, this->radius}; //SDL_RenderFillRect(renderer, &rect); } }; \ No newline at end of file diff --git a/stuff/objects/ray3D.h b/stuff/objects/ray3D.h new file mode 100644 index 0000000..a31f74e --- /dev/null +++ b/stuff/objects/ray3D.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include "../globals/globals.h" + +#include "vector3.h" +#include "cube.h" + +struct ray3D +{ + vector3 start; + vector3 end; + float dist; + cube* hitEnt; + static ray3D trace(vector3 start, vector3 direction) + { + ray3D ray{ start, start, -1 }; + vector3 point = start; + bool hit = false; + while (!hit) + { + for (int i = 0; i < global::entList3D.size(); i++) + { + cube* object = global::entList3D.at(i); + if (inCube(point, vector3(object->x, object->y, object->z), vector3(object->x + object->radius, object->y + object->radius, object->z + object->radius)) || ray.dist > 500) + { + ray.start = start; + ray.end = point; + ray.dist = start.distance(point); + ray.hitEnt = object; + hit = true; + break; + } + } + ray.dist = start.distance(point); + point.x += direction.x; + point.y += direction.y; + point.z += direction.z; + } + return ray; + } + //temp + static bool inCube(vector3 point, vector3 a, vector3 b) + { + return (point.x > a.x && point.y > a.y && point.x < b.x && point.y < b.y && point.z > a.z && point.z < b.z); + } +}; \ No newline at end of file diff --git a/stuff/objects/vector3.h b/stuff/objects/vector3.h index 6a53c60..0189e88 100644 --- a/stuff/objects/vector3.h +++ b/stuff/objects/vector3.h @@ -18,4 +18,13 @@ struct vector3 : public vector2 this->y = y_; this->z = z_; } + float length() + { + return sqrtf((x * x) + (y * y) + (z * z)); + } + float distance(vector3& other) + { + vector3 dist = vector3(this->x - other.x, this->y - other.y, this->z - other.z); + return dist.length(); + } }; \ No newline at end of file diff --git a/threedee.vcxproj b/threedee.vcxproj index 4738bef..d056ef2 100644 --- a/threedee.vcxproj +++ b/threedee.vcxproj @@ -249,6 +249,7 @@ + diff --git a/threedee.vcxproj.filters b/threedee.vcxproj.filters index 577961c..d8ee5a6 100644 --- a/threedee.vcxproj.filters +++ b/threedee.vcxproj.filters @@ -309,6 +309,9 @@ Header Files + + Header Files +