From 637dc7e8217c1cf3876d1c68d3de94309d356c86 Mon Sep 17 00:00:00 2001 From: Joseph Aquino Date: Thu, 10 Apr 2025 12:53:51 -0400 Subject: [PATCH] major code rewrite. -rays are now handled inside class Ray -constructRay function removed as the functionality is replaced by Ray's constructor - added an unnamed enum to make indexing Ray::m_points more clear - Ray::angle is used to keep track of the angle to +x, is updated when any point in Ray::m_points is updated - Helper.hpp has been renamed to Utility.hpp -all functions that were above main() are now in Utility.hpp -rays now behave like a light source -rays get sorted by angle and get placed into a TriangleFan which is drawn as the light source -added additional options to the imgui menu --- Helper.hpp | 43 ------ Ray.cpp | 169 ++++++++++++++++++++++ Ray.h | 67 +++++++++ Utility.hpp | 133 ++++++++++++++++++ imgui.ini | 4 +- main.cpp | 226 +++++++++++------------------- ray-casting-demos.vcxproj | 4 +- ray-casting-demos.vcxproj.filters | 14 +- 8 files changed, 471 insertions(+), 189 deletions(-) delete mode 100644 Helper.hpp create mode 100644 Ray.cpp create mode 100644 Ray.h create mode 100644 Utility.hpp diff --git a/Helper.hpp b/Helper.hpp deleted file mode 100644 index 5a795d2..0000000 --- a/Helper.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -// a collection of useful funtions that i use across projects - -#include -#include -#include -#include -#include - -#include "Random.h" - -template -T getRandomNumber(U min, V max) -{ - return static_cast(Random::get(min, max)); -} - -template -inline sf::Color createColor(T r, U g, V b, W a) -{ - return sf::Color(static_cast(r), static_cast(g), static_cast(b), static_cast(a)); -} - -template -inline sf::Color createColor(T r, U g, V b, std::uint8_t a = 255u) -{ - return sf::Color(static_cast(r), static_cast(g), static_cast(b), a); -} - -sf::Color getRandomColor() -{ - return sf::Color(getRandomNumber(0u, 255u), getRandomNumber(0u, 255u), getRandomNumber(0u, 255u)); -} - -template -T signage(T in) -{ - T zero{}; - if (in > zero) return ++zero; - if (in < zero) return --zero; - return zero; -} \ No newline at end of file diff --git a/Ray.cpp b/Ray.cpp new file mode 100644 index 0000000..a59395f --- /dev/null +++ b/Ray.cpp @@ -0,0 +1,169 @@ +#include "Ray.h" + + Ray::Ray(sf::Vector2f startPoint, sf::Vector2f endPoint, sf::Color color) +{ + m_points[start].position = startPoint; + m_points[start].color = color; + m_points[end].position = endPoint; + m_points[end].color = color; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + Ray::Ray(sf::Vector2f startPoint, float length, sf::Angle angle, sf::Color color) +{ + m_points[start].position = startPoint; + m_points[start].color = color; + m_points[end].position = constructVector(startPoint, angle, length); + m_points[end].color = color; + m_angle = angle; +} + + Ray::Ray(const Ray& ray) +{ + auto& newPoints = ray.points(); + m_points[start].position = newPoints[start].position; + m_points[start].color = newPoints[start].color; + m_points[end].position = newPoints[end].position; + m_points[end].color = newPoints[end].color; + m_angle = ray.angle(); +} + + void Ray::change(const Ray& ray, bool copyColor) +{ + auto& newPoints = ray.points(); + m_points[start].position = newPoints[start].position; + m_points[end].position = newPoints[end].position; + if (copyColor) + { + m_points[start].color = newPoints[start].color; + m_points[end].color = newPoints[0].color; + } + m_angle = ray.angle(); +} + + void Ray::change(sf::Vector2f startPoint, sf::Vector2f endPoint) +{ + m_points[start].position = startPoint; + m_points[end].position = endPoint; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::change(sf::Vector2f startPoint, sf::Vector2f endPoint, sf::Color color) +{ + m_points[start].position = startPoint; + m_points[start].color = color; + m_points[end].position = endPoint; + m_points[end].color = color; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeStart(sf::Vector2f startPoint, sf::Color color) +{ + m_points[start].position = startPoint; + m_points[start].color = color; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeStart(sf::Vector2f startPoint) +{ + m_points[start].position = startPoint; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeStart(const Ray& ray, bool copyColor) +{ + auto& newStart = ray.points()[0]; + m_points[start].position = newStart.position; + if (copyColor) m_points[start].color = newStart.color; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeEnd(sf::Vector2f endPoint, sf::Color color) +{ + m_points[end].position = endPoint; + m_points[end].color = color; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeEnd(sf::Vector2f endPoint) +{ + m_points[end].position = endPoint; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeEnd(const Ray& ray, bool copyColor) +{ + auto& newEnd = ray.points()[end]; + m_points[end].position = newEnd.position; + if (copyColor) m_points[end].color = newEnd.color; + if (m_points[start].position - m_points[end].position == sf::Vector2f{ 0.f, 0.f }) + { + m_angle = sf::Angle::Zero; + } + else + { + m_angle = sf::radians(std::atan2f(m_points[end].position.y - m_points[start].position.y, m_points[end].position.x - m_points[start].position.x)); + } +} + + void Ray::changeColor(sf::Color color) +{ + m_points[start].color = color; + m_points[end].color = color; +} diff --git a/Ray.h b/Ray.h new file mode 100644 index 0000000..1bea465 --- /dev/null +++ b/Ray.h @@ -0,0 +1,67 @@ +#pragma once +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "Utility.hpp" + +//used for sf::VertexArray index +enum +{ + start, + end +}; + +class Ray +{ +public: + Ray(sf::Vector2f startPoint, sf::Vector2f endPoint, sf::Color color = sf::Color::Red); + Ray(sf::Vector2f startPoint, float length, sf::Angle angle, sf::Color color = sf::Color::Red); + Ray(const Ray& ray); + + ~Ray() = default; + + const sf::Angle& angle() const { return m_angle; } + const sf::VertexArray& points() const { return m_points; } + + void change(const Ray& ray, bool copyColor = false); + void change(sf::Vector2f startPoint, sf::Vector2f endPoint); + void change(sf::Vector2f startPoint, sf::Vector2f endPoint, sf::Color color); + + void changeStart(sf::Vector2f startPoint, sf::Color color); + void changeStart(sf::Vector2f startPoint); + void changeStart(const Ray& ray, bool copyColor = false); + + void changeEnd(sf::Vector2f endPoint, sf::Color color); + void changeEnd(sf::Vector2f endPoint); + void changeEnd(const Ray& ray, bool copyColor = false); + + void changeColor(sf::Color color); + + bool operator== (const Ray& rhs) + { + return m_points[start].position == rhs.points()[start].position and m_points[end].position == rhs.points()[end].position; + } + + Ray operator+ (const Ray& rhs) + { + return Ray(this->m_points[start].position + rhs.m_points[start].position, this->m_points[end].position + rhs.m_points[end].position); + } + + Ray operator- (const Ray& rhs) + { + return Ray(this->m_points[start].position - rhs.m_points[start].position, this->m_points[end].position - rhs.m_points[end].position); + } + +private: + sf::VertexArray m_points{ sf::PrimitiveType::Lines, 2 };//points[0] = origin of ray, points[1] = end of ray + sf::Angle m_angle{}; +}; + +using RayVector = std::vector; \ No newline at end of file diff --git a/Utility.hpp b/Utility.hpp new file mode 100644 index 0000000..f40883d --- /dev/null +++ b/Utility.hpp @@ -0,0 +1,133 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "Random.h" +#include "Ray.h" + +template +inline T getRandomNumber(U min, V max) +{ + return static_cast(Random::get(min, max)); +} + +template +inline sf::Color createColor(T r, U g, V b, W a) +{ + return sf::Color(static_cast(r), static_cast(g), static_cast(b), static_cast(a)); +} + +template +inline sf::Color createColor(T r, U g, V b, std::uint8_t a = 255u) +{ + return sf::Color(static_cast(r), static_cast(g), static_cast(b), a); +} + +inline sf::Color getRandomColor() +{ + return sf::Color(getRandomNumber(0u, 255u), getRandomNumber(0u, 255u), getRandomNumber(0u, 255u)); +} + +template +inline T signage(T in) +{ + T zero{}; + if (in > zero) return ++zero; + if (in < zero) return --zero; + return zero; +} + +struct LineSegment +{ + sf::Vector2f startPoint{}; + + sf::Vector2f endPoint{}; +}; + +struct imguiColor +{ + float r{}; + float g{}; + float b{}; + float a{ 1.f }; + + sf::Color asSfColor() + { + return sf::Color(std::uint8_t(r * 255.f), std::uint8_t(g * 255.f), std::uint8_t(b * 255.f), std::uint8_t(a * 255.f)); + } +}; + +inline imguiColor constructImguiColor(sf::Color color) +{ + return imguiColor((float)color.r / 255.f, (float)color.g / 255.f, (float)color.b / 255.f, (float)color.a / 255.f); +} + +inline std::optional lineIntersect(LineSegment first, LineSegment second) +{ + sf::Vector2f firstVector = (first.endPoint - first.startPoint); + sf::Vector2f secondVector = (second.endPoint - second.startPoint); + + float lengthCrossProduct = firstVector.cross(secondVector); + + sf::Vector2f fms = second.startPoint - first.startPoint; //first line start point minus second + + float firstScalar = fms.cross(secondVector) / lengthCrossProduct; + float secondScalar = fms.cross(firstVector) / lengthCrossProduct; + + if ((firstScalar >= 0 and firstScalar <= 1) and (secondScalar >= 0 and secondScalar <= 1)) + return sf::Vector2f(first.startPoint.x + (firstScalar * firstVector.x), first.startPoint.y + (firstScalar * firstVector.y)); + else + return std::nullopt; +} + +inline sf::VertexArray constructRectangle(sf::Vector2f center, sf::Vector2f size, sf::Color color = sf::Color::Black) +{ + sf::VertexArray rectangle(sf::PrimitiveType::LineStrip, 5); + rectangle[0].color = color; + rectangle[1].color = color; + rectangle[2].color = color; + rectangle[3].color = color; + rectangle[4].color = color; + + rectangle[0].position = { center.x - size.x, center.y - size.y }; + rectangle[1].position = { center.x + size.x, center.y - size.y }; + rectangle[2].position = { center.x + size.x, center.y + size.y }; + rectangle[3].position = { center.x - size.x, center.y + size.y }; + rectangle[4].position = rectangle[0].position; + + return rectangle; + +} + +inline sf::Vector2f constructVector(sf::Vector2f startPoint, sf::Angle angle, float length) +{ + return { startPoint.x + (length * std::cos(angle.asRadians())), startPoint.y + (length * std::sin(angle.asRadians())) }; +} + +inline sf::VertexArray constructRandomPolygon(sf::Vector2f center, int vertexLowerBound, int vertexUpperBound, int averageSize, sf::Color color = sf::Color::Black) +{ + std::size_t vertexCount = getRandomNumber(vertexLowerBound, vertexUpperBound); + sf::VertexArray polygon(sf::PrimitiveType::LineStrip, vertexCount); + + float angleIncrement = 360.f / (float)(vertexCount - 1); + float angleStart = getRandomNumber(0, 360); + + for (int i = 0; i < vertexCount - 1; i++) + { + float angleOffset = getRandomNumber(-(90 / (int)vertexCount), (90 / (int)vertexCount)); + polygon[i].color = color; + polygon[i].position = constructVector(center, sf::degrees(angleStart + (angleIncrement * i) + angleOffset), getRandomNumber((averageSize - 50), (averageSize + 50))); + } + polygon[vertexCount - 1].position = polygon[0].position; + polygon[vertexCount - 1].color = color; + return polygon; +} + +inline float distanceBetween(sf::Vector2f startPoint, sf::Vector2f endPoint) +{ + return sqrtf(powf(endPoint.x - startPoint.x, 2) + powf(endPoint.y - startPoint.y, 2)); +} diff --git a/imgui.ini b/imgui.ini index 00ed0b9..14b600c 100644 --- a/imgui.ini +++ b/imgui.ini @@ -3,6 +3,6 @@ Pos=60,60 Size=400,400 [Window][Config] -Pos=7,8 -Size=705,277 +Pos=7,7 +Size=710,322 diff --git a/main.cpp b/main.cpp index 8e8f67b..fac0d63 100644 --- a/main.cpp +++ b/main.cpp @@ -1,116 +1,20 @@ -#include -#include -#include #include - #include #include #include +#include -#include "Helper.hpp" +#include +#include +#include -struct LineSegment -{ - sf::Vector2f startPoint{}; - - sf::Vector2f endPoint{}; -}; - -struct imguiColor -{ - float r{}; - float g{}; - float b{}; - float a{1.f}; - - sf::Color asSfColor() - { - return sf::Color(std::uint8_t(r * 255.f), std::uint8_t(g * 255.f), std::uint8_t(b * 255.f), std::uint8_t(a * 255.f)); - } -}; - -imguiColor constructImguiColor(sf::Color color) -{ - return imguiColor((float)color.r / 255.f, (float)color.g / 255.f, (float)color.b / 255.f, (float)color.a / 255.f); -} - -std::optional lineIntersect(LineSegment first, LineSegment second) -{ - sf::Vector2f firstVector = (first.endPoint - first.startPoint); - sf::Vector2f secondVector = (second.endPoint - second.startPoint); - - float lengthCrossProduct = firstVector.cross(secondVector); - - sf::Vector2f fms = second.startPoint - first.startPoint; //first line start point minus second - - float firstScalar = fms.cross(secondVector) / lengthCrossProduct; - float secondScalar = fms.cross(firstVector) / lengthCrossProduct; - - if ((firstScalar >= 0 and firstScalar <= 1) and (secondScalar >= 0 and secondScalar <= 1)) - return sf::Vector2f(first.startPoint.x + (firstScalar * firstVector.x), first.startPoint.y + (firstScalar * firstVector.y)); - else - return std::nullopt; -} - -sf::VertexArray constructRectangle(sf::Vector2f center, sf::Vector2f size, sf::Color color = sf::Color::Black) -{ - sf::VertexArray rectangle(sf::PrimitiveType::LineStrip, 5); - rectangle[0].color = color; - rectangle[1].color = color; - rectangle[2].color = color; - rectangle[3].color = color; - rectangle[4].color = color; - - rectangle[0].position = { center.x - size.x, center.y - size.y }; - rectangle[1].position = { center.x + size.x, center.y - size.y }; - rectangle[2].position = { center.x + size.x, center.y + size.y }; - rectangle[3].position = { center.x - size.x, center.y + size.y }; - rectangle[4].position = { center.x - size.x, center.y - size.y }; - - return rectangle; - -} - -sf::Vector2f constructVector(sf::Vector2f startPoint, sf::Angle angle, float length) -{ - return { startPoint.x + (length * std::cos(angle.asRadians())), startPoint.y + (length * std::sin(angle.asRadians())) }; -} - -sf::VertexArray constructRandomPolygon(sf::Vector2f center, int vertexLowerBound, int vertexUpperBound, int averageSize, sf::Color color = sf::Color::Black) -{ - size_t vertexCount = getRandomNumber(vertexLowerBound, vertexUpperBound); - sf::VertexArray polygon(sf::PrimitiveType::LineStrip, vertexCount); - - float angleIncrement = 360.f / (float)(vertexCount - 1); - float angleStart = getRandomNumber(0, 360); - - for (int i = 0; i < vertexCount - 1; i++) - { - float angleOffset = getRandomNumber(-(90 / (int)vertexCount), (90 / (int)vertexCount)); - polygon[i].color = color; - polygon[i].position = constructVector(center, sf::degrees(angleStart + (angleIncrement * i) + angleOffset), getRandomNumber((averageSize - 50), (averageSize + 50))); - } - polygon[vertexCount - 1].position = polygon[0].position; - polygon[vertexCount - 1].color = color; - return polygon; -} - -sf::VertexArray constructRay(sf::Vector2f start, float length, sf::Angle angle, sf::Color color = sf::Color::Red) -{ - sf::VertexArray ray(sf::PrimitiveType::Lines, 2); - - ray[0].position = start; - ray[0].color = color; - ray[1].position = constructVector(start, angle, length); - ray[1].color = color; - - return ray; -} +#include "Utility.hpp" +#include "Ray.h" int main() { sf::Vector2f center{ 1920.f / 2.f, 1080.f / 2.f }; - + //sfml setup auto window = sf::RenderWindow(sf::VideoMode({ 1920u, 1080u }), "Ray Casting Test"); @@ -123,28 +27,26 @@ int main() //imGui Variables float polySize[2] = { 100.f, 100.f }; - int numRays{ 10 }; + int numRays{ 500 }; int vertexBounds[2] = { 3u, 10u }; int averageSize{ 100u }; - float rayLength{ 5000.f }; + float rayLength{ 500.f }; imguiColor rayColor = constructImguiColor(sf::Color::Red); + imguiColor lightColor = constructImguiColor(sf::Color::Red); + bool drawRays{ false }; + bool drawLight{ true }; //initalize vectors - std::vector rays; - - { - float i = 0.f, angleIncrement = 360.f / (float)numRays; - for (int i = 0; i < numRays; i++) rays.push_back(constructRay(mousePos, rayLength, sf::degrees(angleIncrement * i), rayColor.asSfColor())); - } + RayVector rays; std::vector polygons; - polygons.push_back(constructRectangle(center, center));//outer bounds of the window - + polygons.push_back(constructRectangle(center, center)); //main game loop while (window.isOpen()) { + rays.clear(); //handle input while (const std::optional event = window.pollEvent()) { @@ -161,13 +63,16 @@ int main() { case sf::Keyboard::Scancode::C: polygons.clear(); - polygons.push_back(constructRectangle(center, center));//outer bounds of the window break; case sf::Keyboard::Scancode::R: polygons.push_back(constructRandomPolygon(mousePos, vertexBounds[0] + 1, vertexBounds[1] + 1, averageSize)); break; + case sf::Keyboard::Scancode::Escape: + window.close(); + break; + default: break; } @@ -186,44 +91,72 @@ int main() } } - if (rays.empty()) continue; if (const auto* mouseMoved = event->getIf()) { mousePos = (sf::Vector2f)mouseMoved->position; - for (auto& ray : rays) ray[0].position = mousePos; + for (auto& ray : rays) ray.changeStart(mousePos); } }// end user input loop + if (numRays > 0) { - float i = 0, angleIncrement = 360.f / (float)rays.size(); - for (auto& ray : rays) + float i = 0.f, angleIncrement = 360.f / (float)numRays; + for (int i = 0; i < numRays; i++) rays.push_back(Ray(mousePos, rayLength, sf::degrees(angleIncrement * i), rayColor.asSfColor())); + } + + for (auto& poly : polygons) + { + for (std::size_t i = 0; i < poly.getVertexCount() - 1; i++) { - ray[1].position = constructVector(ray[0].position, sf::degrees(i * angleIncrement), rayLength); - i++; + if (distanceBetween(mousePos, poly[i].position) <= rayLength) + { + sf::Angle angle = sf::radians(std::atan2f(poly[i].position.y - mousePos.y, poly[i].position.x - mousePos.x)); + rays.push_back(Ray(mousePos, poly[i].position, rayColor.asSfColor())); + rays.push_back(Ray(mousePos, rayLength, angle + sf::radians(.00001f), rayColor.asSfColor())); + rays.push_back(Ray(mousePos, rayLength, angle - sf::radians(.00001f), rayColor.asSfColor())); + } } } for (auto& ray : rays) { - sf::Vector2f closestIntersection = ray[1].position; + auto& rayPoints = ray.points(); + sf::Vector2f closestIntersection = rayPoints[end].position; std::optional currentIntersect{}; for (auto& poly : polygons) { for (int i = 1; i <= poly.getVertexCount() - 1; i++) { - currentIntersect = lineIntersect({ ray[0].position, ray[1].position }, { poly[i - 1].position , poly[i].position }); + currentIntersect = lineIntersect({ rayPoints[start].position, rayPoints[end].position }, { poly[i - 1].position , poly[i].position }); if (currentIntersect) { - if ((closestIntersection - ray[0].position).length() > (currentIntersect.value() - ray[0].position).length()) + if ((closestIntersection - rayPoints[start].position).length() > (currentIntersect.value() - rayPoints[start].position).length()) { closestIntersection = currentIntersect.value(); } } } } - - ray[1].position = closestIntersection; + ray.changeEnd(closestIntersection); } + std::sort(rays.begin(), rays.end(), [](const auto& lhs, const auto& rhs) { return lhs.angle().wrapUnsigned().asRadians() < rhs.angle().wrapUnsigned().asRadians(); }); + + sf::VertexArray light(sf::PrimitiveType::TriangleFan, 1); + if (not rays.empty()) + { + light[0].position = mousePos; + light[0].color = lightColor.asSfColor(); + for (auto& ray : rays) + { + light.append(ray.points()[end]); + } + light.append(rays.front().points()[end]); + } + + for (int i = 0; i < light.getVertexCount(); i++) + light[i].color = lightColor.asSfColor(); + + ImGui::SFML::Update(window, clock.restart()); ImGui::Begin("Config"); @@ -233,24 +166,28 @@ int main() ImGui::Text("R: place a randomly sized polygon at mouse position"); ImGui::Text("C: clear all polygons"); ImGui::Unindent(); - if (ImGui::SliderInt("number of rays", &numRays, 0, 50)) + ImGui::Checkbox("Render rays", &drawRays); + ImGui::SameLine(); + ImGui::Checkbox("Render light", &drawLight); + if (ImGui::SliderInt("number of rays", &numRays, 5, 500)) { rays.clear(); { float i = 0.f, angleIncrement = 360.f / (float)numRays; - for (int i = 0; i < numRays; i++) rays.push_back(constructRay(mousePos, rayLength, sf::degrees(angleIncrement * i), rayColor.asSfColor())); + for (int i = 0; i < numRays; i++) rays.push_back(Ray(mousePos, rayLength, sf::degrees(angleIncrement * i), rayColor.asSfColor())); } } ImGui::InputFloat("Ray Length", &rayLength); if (ImGui::ColorEdit3("Ray color", &rayColor.r)) { - for (auto& ray : rays) - { - ray[0].color = rayColor.asSfColor(); - ray[1].color = rayColor.asSfColor(); - } + for (auto& ray : rays) ray.changeColor(rayColor.asSfColor()); + } + if (ImGui::ColorEdit3("Light color", &lightColor.r)) + { + for (int i = 0; i < light.getVertexCount(); i++) + light[i].color = lightColor.asSfColor(); } ImGui::InputFloat2("rectangle Size (X,Y)", polySize, "%.1f"); ImGui::Text("Random polygon config: "); @@ -263,7 +200,7 @@ int main() vertexBounds[0] = vertexBounds[1]; } - if(ImGui::InputInt("Average size", &averageSize)) + if (ImGui::InputInt("Average size", &averageSize)) { if (averageSize < 20) { @@ -276,19 +213,24 @@ int main() window.clear(sf::Color::White); //render entities - for (auto& poly : polygons) - window.draw(poly); + for (auto& poly : polygons) window.draw(poly); - for (auto& ray : rays) + if (drawRays) { - sf::CircleShape endPoint(5.f, 20u); - endPoint.setOrigin({ 5, 5 }); - endPoint.setFillColor(rayColor.asSfColor()); - endPoint.setPosition(ray[1].position); - window.draw(ray); - window.draw(endPoint); + for (auto& ray : rays) + { + auto& rayPoints = ray.points(); + sf::CircleShape endPoint(5.f, 20u); + endPoint.setOrigin({ 5, 5 }); + endPoint.setFillColor(rayColor.asSfColor()); + endPoint.setPosition(rayPoints[end].position); + window.draw(rayPoints); + window.draw(endPoint); + } } + if (drawLight) window.draw(light); + ImGui::SFML::Render(window); window.display(); diff --git a/ray-casting-demos.vcxproj b/ray-casting-demos.vcxproj index f168587..81f4e2e 100644 --- a/ray-casting-demos.vcxproj +++ b/ray-casting-demos.vcxproj @@ -143,10 +143,12 @@ + - + + diff --git a/ray-casting-demos.vcxproj.filters b/ray-casting-demos.vcxproj.filters index b98d173..715ff2f 100644 --- a/ray-casting-demos.vcxproj.filters +++ b/ray-casting-demos.vcxproj.filters @@ -16,6 +16,12 @@ {20b9a3b4-eb7c-4baa-be0b-c6e0d295c347} + + {fe3c7015-3de6-4e35-adea-c623dcb78c0a} + + + {a5f9e058-615a-4f77-a4a5-50a0c3ab5cd7} + @@ -36,13 +42,19 @@ Source Files\Imgui-sfml Files + + Source Files\Classes + - + Header Files Header Files + + Header Files\Classes + \ No newline at end of file