From d50487f4846e7ecb2a7455ad9e49f53ff3db5abc Mon Sep 17 00:00:00 2001 From: Joseph Aquino Date: Tue, 22 Apr 2025 14:25:35 -0400 Subject: [PATCH] minor code update -Ray.cpp and Ray.h have been removed from the project -added constructVertexArray() which can take any object that inherits from sf::Shape and create a vertex array with the same shape with approriate transforms taken into account --- Light.cpp | 19 ++-- Light.h | 2 +- Ray.cpp | 189 -------------------------------------- Ray.h | 54 ----------- Utility.hpp | 15 ++- imgui.ini | 2 +- main.cpp | 10 +- ray-casting-demos.vcxproj | 8 +- 8 files changed, 38 insertions(+), 261 deletions(-) delete mode 100644 Ray.cpp delete mode 100644 Ray.h diff --git a/Light.cpp b/Light.cpp index ab3fba5..becbd66 100644 --- a/Light.cpp +++ b/Light.cpp @@ -93,6 +93,7 @@ void Light::createAndSortRays(const std::vector& polygons) } sf::Angle angle; + const float infiniteLength = 5000.f * hasFlags(Flags::infiniteLength); for (auto& poly : polygons) { @@ -103,8 +104,8 @@ void Light::createAndSortRays(const std::vector& polygons) angle = sf::radians(std::atan2f(poly[i].position.y - m_origin.y, poly[i].position.x - m_origin.x)); m_rays.emplace_back(sf::Vertex{ poly[i].position, m_lightColor }); - m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle + sf::radians(.00001f), m_rayLength + (5000.f * hasFlags(Flags::infiniteLength)) ), m_lightColor }); - m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle - sf::radians(.00001f), m_rayLength + (5000.f * hasFlags(Flags::infiniteLength)) ), m_lightColor }); + m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle + sf::radians(.00001f), m_rayLength + infiniteLength), m_lightColor }); + m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle - sf::radians(.00001f), m_rayLength + infiniteLength), m_lightColor }); } } @@ -114,9 +115,9 @@ void Light::createAndSortRays(const std::vector& polygons) // overload for bounded light void Light::createAndSortRays(const std::vector& polygons, sf::Angle lowerAngleBounds, sf::Angle upperAngleBounds) { - - sf::Vertex lowerBound{ constructVector( m_origin, lowerAngleBounds, m_rayLength + (5000.f * hasFlags(Flags::infiniteLength)) ), m_lightColor }; - sf::Vertex upperBound{ constructVector( m_origin, upperAngleBounds, m_rayLength + (5000.f * hasFlags(Flags::infiniteLength)) ), m_lightColor }; + const float infiniteLength = 5000.f * hasFlags(Flags::infiniteLength); + sf::Vertex lowerBound{ constructVector( m_origin, lowerAngleBounds, m_rayLength + infiniteLength), m_lightColor }; + sf::Vertex upperBound{ constructVector( m_origin, upperAngleBounds, m_rayLength + infiniteLength), m_lightColor }; auto lowerBoundVector = lowerBound.position - m_origin; auto upperBoundVector = upperBound.position - m_origin; @@ -132,7 +133,6 @@ void Light::createAndSortRays(const std::vector& polygons, sf:: } } - constexpr float infiniteLength = 5000.f; sf::Angle angle; for (auto& poly : polygons) @@ -148,8 +148,8 @@ void Light::createAndSortRays(const std::vector& polygons, sf:: angle = sf::radians(std::atan2f(poly[i].position.y - m_origin.y, poly[i].position.x - m_origin.x)); m_rays.emplace_back(sf::Vertex{ poly[i].position, m_lightColor }); - m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle + sf::radians(.00001f), m_rayLength + (infiniteLength * hasFlags(Flags::infiniteLength)) ), m_lightColor }); - m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle - sf::radians(.00001f), m_rayLength + (infiniteLength * hasFlags(Flags::infiniteLength)) ), m_lightColor }); + m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle + sf::radians(.00001f), m_rayLength + infiniteLength), m_lightColor }); + m_rays.emplace_back(sf::Vertex{ constructVector( m_origin, angle - sf::radians(.00001f), m_rayLength + infiniteLength), m_lightColor }); } } @@ -189,5 +189,4 @@ void Light::calculateIntersects(const std::vector& polygons) bool Light::withinBounds(sf::Vector2f point, sf::Vector2f lower, sf::Vector2f upper) { return point.angleTo(upper).wrapUnsigned().asRadians() < (lower).angleTo(upper).wrapUnsigned().asRadians(); -} - +} \ No newline at end of file diff --git a/Light.h b/Light.h index 1a0f30a..e31a4ce 100644 --- a/Light.h +++ b/Light.h @@ -32,6 +32,7 @@ public: visible ? setFlags(Flags::visibility) : unsetFlags(Flags::visibility); } +public: enum class Flags { visibility = 1 << 0, @@ -96,7 +97,6 @@ private: int m_rayCount{ 500 }; int m_flags{}; - enum { start, diff --git a/Ray.cpp b/Ray.cpp deleted file mode 100644 index d74f0eb..0000000 --- a/Ray.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#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; -} - - void Ray::calculateIntersect(const std::vector& polygons) - { - sf::Vector2f closestIntersection = m_points[end].position; - std::optional currentIntersect{}; - for (auto& poly : polygons) - { - for (int i = 1; i <= poly.getVertexCount() - 1; i++) { - currentIntersect = lineIntersect({ m_points[start].position, m_points[end].position }, { poly[i - 1].position , poly[i].position }); - if (currentIntersect) - { - if ((closestIntersection - m_points[start].position).length() > (currentIntersect.value() - m_points[start].position).length()) - { - closestIntersection = currentIntersect.value(); - } - } - } - } - changeEnd(closestIntersection); - } diff --git a/Ray.h b/Ray.h deleted file mode 100644 index a5c16a8..0000000 --- a/Ray.h +++ /dev/null @@ -1,54 +0,0 @@ -#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); - - void calculateIntersect(const std::vector& polygons); - -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 index 7f6f782..6e3adca 100644 --- a/Utility.hpp +++ b/Utility.hpp @@ -102,7 +102,6 @@ inline sf::VertexArray constructRectangle(sf::Vector2f center, sf::Vector2f size rectangle[4].position = rectangle[0].position; return rectangle; - } inline sf::Vector2f constructVector(sf::Vector2f startPoint, sf::Angle angle, float length) @@ -133,3 +132,17 @@ inline float distanceBetween(sf::Vector2f startPoint, sf::Vector2f endPoint) { return sqrtf(powf(endPoint.x - startPoint.x, 2) + powf(endPoint.y - startPoint.y, 2)); } + +inline sf::VertexArray constructVertexArray(sf::Shape& shape) +{ + sf::VertexArray result(sf::PrimitiveType::LineStrip); + auto& transform = shape.getTransform(); + + for (int i = 0; i < shape.getPointCount(); i++) + { + result.append(sf::Vertex{ transform.transformPoint(shape.getPoint(i)), sf::Color::Black }); + } + + result.append(result[0]); + return result; +} \ No newline at end of file diff --git a/imgui.ini b/imgui.ini index b7410a7..fcc81b4 100644 --- a/imgui.ini +++ b/imgui.ini @@ -3,6 +3,6 @@ Pos=60,60 Size=400,400 [Window][Config] -Pos=6,12 +Pos=7,12 Size=670,442 diff --git a/main.cpp b/main.cpp index 5e90175..3456956 100644 --- a/main.cpp +++ b/main.cpp @@ -9,7 +9,6 @@ #include #include "Utility.hpp" -#include "Ray.h" #include "Light.h" @@ -17,6 +16,7 @@ auto window = sf::RenderWindow(sf::VideoMode({ 1920u, 1080u }), "Ray Casting Tes Light light; std::vector polygons; +sf::CircleShape circle{}; sf::Vector2f mousePos{}; sf::Vector2f center{ 1920.f / 2.f, 1080.f / 2.f }; @@ -53,6 +53,7 @@ int main() //configure light with default imgui vatiables drawRays ? light.setFlags(Light::Flags::drawRays) : light.unsetFlags(Light::Flags::drawRays); drawLight ? light.setFlags(Light::Flags::visibility) : light.unsetFlags(Light::Flags::visibility); + boundedLight ? light.addAngleBounds(sf::degrees(lightCenterAngle), sf::degrees(lightOffsetAngle)) : light.removeAngleBounds(); drawEndPoints ? light.setFlags(Light::Flags::drawEndPoints) : light.unsetFlags(Light::Flags::drawEndPoints); infiniteRayLength ? light.setFlags(Light::Flags::infiniteLength) : light.unsetFlags(Light::Flags::infiniteLength); @@ -66,6 +67,12 @@ int main() polygons.emplace_back(constructRectangle(center, center)); + circle.setOrigin({0,0}); + circle.setPosition(center); + circle.setRadius(200.f); + circle.setPointCount(30u); + polygons.emplace_back(constructVertexArray(circle)); + //main game loop while (window.isOpen()) { @@ -145,6 +152,7 @@ void GUI() { ImGui::ColorEdit4("End point color", &endPointColor.r); } + light.changeLightColor(lightColor.asSfColor()); light.changeEndPointColor(endPointColor.asSfColor()); light.changeRayColor(rayColor.asSfColor()); diff --git a/ray-casting-demos.vcxproj b/ray-casting-demos.vcxproj index 59d295b..8782df7 100644 --- a/ray-casting-demos.vcxproj +++ b/ray-casting-demos.vcxproj @@ -104,14 +104,14 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true - $(SFML_DIR)\include;$(IMGUI_SFML_DIR);C:\dev\libraries\opengl\include;%(AdditionalIncludeDirectories) + $(SFML_DIR)\include;$(IMGUI_SFML_DIR);%(AdditionalIncludeDirectories) stdcpp20 AssemblyCode Console true - $(SFML_DIR)\lib;C:\dev\libraries\opengl\lib-vc2022;%(AdditionalLibraryDirectories) + $(SFML_DIR)\lib;%(AdditionalLibraryDirectories) sfml-graphics-d.lib;sfml-system-d.lib;sfml-window-d.lib;sfml-audio-d.lib;sfml-network-d.lib;opengl32.lib;%(AdditionalDependencies) @@ -123,7 +123,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - C:\dev\libraries\SFML-3.0.0\include;C:\dev\libraries\imgui;C:\dev\libraries\opengl\include;%(AdditionalIncludeDirectories) + $(IMGUI_SFML_DIR);$(SFML_DIR)\include;%(AdditionalIncludeDirectories) stdcpp20 @@ -131,7 +131,7 @@ true true true - C:\dev\libraries\SFML-3.0.0\lib;C:\dev\libraries\opengl\lib-vc2022;%(AdditionalLibraryDirectories) + $(SFML_DIR)\lib;%(AdditionalLibraryDirectories) sfml-graphics.lib;sfml-system.lib;sfml-window.lib;sfml-audio.lib;sfml-network.lib;opengl32.lib;%(AdditionalDependencies) mainCRTStartup