298 lines
6.4 KiB
C++
298 lines
6.4 KiB
C++
#include <cmath>
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
|
|
#include <SFML/Graphics.hpp>
|
|
#include <imgui-SFML.h>
|
|
#include <imgui.h>
|
|
|
|
#include "Utility.hpp"
|
|
#include "Ray.h"
|
|
#include "Light.h"
|
|
|
|
|
|
auto window = sf::RenderWindow(sf::VideoMode({ 1920u, 1080u }), "Ray Casting Test");
|
|
|
|
Light light;
|
|
std::vector<sf::VertexArray> polygons;
|
|
|
|
sf::Vector2f mousePos{};
|
|
sf::Vector2f center{ 1920.f / 2.f, 1080.f / 2.f };
|
|
|
|
//imGui Variables
|
|
float polySize[2] = { 100.f, 100.f };
|
|
int numRays{ 500 };
|
|
int vertexBounds[2] = { 3u, 10u };
|
|
int averageSize{ 100u };
|
|
float rayLength{ 500.f };
|
|
imguiColor rayColor = constructImguiColor(sf::Color::Red);
|
|
imguiColor lightColor = constructImguiColor(sf::Color::Yellow);
|
|
imguiColor endPointColor = constructImguiColor(sf::Color::Magenta);
|
|
bool drawRays{ false };
|
|
bool drawLight{ true };
|
|
bool drawEndPoints{ false };
|
|
bool boundedLight{ false };
|
|
bool infiniteRayLength{ false };
|
|
int lightCenterAngle{ 0 };
|
|
int lightOffsetAngle{ 45 };
|
|
|
|
void GUI();
|
|
void userInput();
|
|
void render();
|
|
|
|
int main()
|
|
{
|
|
|
|
//sfml setup
|
|
sf::Clock clock;
|
|
window.setFramerateLimit(144);
|
|
if (!ImGui::SFML::Init(window))
|
|
return -1;
|
|
|
|
mousePos = center;
|
|
|
|
polygons.emplace_back(constructRectangle(center, center));
|
|
|
|
//main game loop
|
|
while (window.isOpen())
|
|
{
|
|
ImGui::SFML::Update(window, clock.restart());
|
|
|
|
light.clearRays();
|
|
|
|
userInput();
|
|
|
|
GUI();
|
|
|
|
light.setOrigin(mousePos);
|
|
light.processRays(polygons);
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
ImGui::SFML::Shutdown();
|
|
}
|
|
|
|
void GUI()
|
|
{
|
|
ImGui::Begin("Config");
|
|
ImGui::Text("Controls:");
|
|
ImGui::Indent();
|
|
ImGui::Text("Right Click: place rectangle at mouse position");
|
|
ImGui::Text("R: place a randomly sized polygon at mouse position");
|
|
ImGui::Text("C: clear all polygons");
|
|
ImGui::Text("Scroll wheel: increase/decrease light direction (if light is bounded)");
|
|
ImGui::Unindent();
|
|
if (ImGui::Checkbox("Render rays", &drawRays))
|
|
{
|
|
if (drawRays)
|
|
{
|
|
light.setFlags(Light::Flags::drawRays);
|
|
}
|
|
else
|
|
{
|
|
light.unsetFlags(Light::Flags::drawRays);
|
|
}
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
if (ImGui::Checkbox("Render end points", &drawEndPoints))
|
|
{
|
|
if (drawEndPoints)
|
|
{
|
|
light.setFlags(Light::Flags::drawEndPoints);
|
|
}
|
|
else
|
|
{
|
|
light.unsetFlags(Light::Flags::drawEndPoints);
|
|
}
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
if (ImGui::Checkbox("Render light", &drawLight))
|
|
{
|
|
if (drawLight)
|
|
{
|
|
light.setFlags(Light::Flags::visibility);
|
|
}
|
|
else
|
|
{
|
|
light.unsetFlags(Light::Flags::visibility);
|
|
}
|
|
}
|
|
|
|
if (ImGui::Checkbox("Infinite ray length", &infiniteRayLength))
|
|
{
|
|
if (infiniteRayLength)
|
|
{
|
|
light.setFlags(Light::Flags::infiniteLength);
|
|
}
|
|
else
|
|
{
|
|
light.unsetFlags(Light::Flags::infiniteLength);
|
|
}
|
|
}
|
|
|
|
if (not infiniteRayLength)
|
|
{
|
|
ImGui::SliderInt("number of rays", &numRays, 5, 500);
|
|
light.setRayCount(numRays);
|
|
|
|
if (ImGui::InputFloat("Ray Length", &rayLength))
|
|
{
|
|
rayLength = std::clamp(rayLength, 5.f, 10000.f);
|
|
}
|
|
light.setRayLength(rayLength);
|
|
}
|
|
|
|
if (drawRays)
|
|
{
|
|
ImGui::ColorEdit4("Ray color", &rayColor.r);
|
|
}
|
|
|
|
if (drawLight)
|
|
{
|
|
ImGui::ColorEdit4("Light color", &lightColor.r);
|
|
}
|
|
|
|
if (drawEndPoints)
|
|
{
|
|
ImGui::ColorEdit4("End point color", &endPointColor.r);
|
|
}
|
|
light.changeLightColor(lightColor.asSfColor());
|
|
light.changeEndPointColor(endPointColor.asSfColor());
|
|
light.changeRayColor(rayColor.asSfColor());
|
|
|
|
if(ImGui::Checkbox("Light is bounded", &boundedLight))
|
|
{
|
|
if (boundedLight)
|
|
{
|
|
light.addAngleBounds(sf::degrees((float)lightCenterAngle), sf::degrees((float)lightOffsetAngle));
|
|
}
|
|
else
|
|
{
|
|
light.removeAngleBounds();
|
|
}
|
|
}
|
|
if (boundedLight)
|
|
{
|
|
ImGui::SliderInt("Light direction", &lightCenterAngle, 0, 359);
|
|
light.setCenterAngle(sf::degrees((float)lightCenterAngle));
|
|
|
|
if (ImGui::InputInt("Offset size", &lightOffsetAngle))
|
|
{
|
|
if (lightOffsetAngle > 175)
|
|
{
|
|
lightOffsetAngle = 175;
|
|
}
|
|
if (lightOffsetAngle < 5)
|
|
{
|
|
lightOffsetAngle = 5;
|
|
}
|
|
}
|
|
light.setOffsetAngle(sf::degrees((float)lightOffsetAngle));
|
|
}
|
|
ImGui::InputFloat2("rectangle Size (X,Y)", polySize, "%.1f");
|
|
ImGui::Text("Random polygon config: ");
|
|
if (ImGui::SliderInt2("vertex count lower/upper bounds", vertexBounds, 3, 20))
|
|
{
|
|
if (vertexBounds[0] > vertexBounds[1])
|
|
vertexBounds[0] = vertexBounds[1];
|
|
|
|
if (vertexBounds[1] < vertexBounds[0])
|
|
vertexBounds[0] = vertexBounds[1];
|
|
}
|
|
|
|
if (ImGui::InputInt("Average size", &averageSize))
|
|
{
|
|
if (averageSize < 20)
|
|
{
|
|
averageSize = 20;
|
|
}
|
|
}
|
|
|
|
ImGui::End();
|
|
}
|
|
|
|
void userInput()
|
|
{
|
|
while (const std::optional event = window.pollEvent())
|
|
{
|
|
ImGui::SFML::ProcessEvent(window, *event);
|
|
|
|
if (event->is<sf::Event::Closed>())
|
|
{
|
|
window.close();
|
|
}
|
|
|
|
if (const auto* buttonPressed = event->getIf<sf::Event::KeyPressed>())
|
|
{
|
|
switch (buttonPressed->scancode)
|
|
{
|
|
case sf::Keyboard::Scancode::C:
|
|
polygons.clear();
|
|
polygons.emplace_back(constructRectangle(center, center));
|
|
break;
|
|
|
|
case sf::Keyboard::Scancode::R:
|
|
polygons.emplace_back(constructRandomPolygon(mousePos, vertexBounds[0] + 1, vertexBounds[1] + 1, averageSize));
|
|
break;
|
|
|
|
case sf::Keyboard::Scancode::Escape:
|
|
window.close();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (const auto* mousePressed = event->getIf<sf::Event::MouseButtonPressed>())
|
|
{
|
|
switch (mousePressed->button)
|
|
{
|
|
case sf::Mouse::Button::Right:
|
|
polygons.emplace_back(constructRectangle((sf::Vector2f)mousePressed->position, { polySize[0], polySize[1] }, sf::Color::Black));
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (const auto* mouseWheelScrolled = event->getIf<sf::Event::MouseWheelScrolled>())
|
|
{
|
|
switch (mouseWheelScrolled->wheel)
|
|
{
|
|
case sf::Mouse::Wheel::Vertical:
|
|
if (boundedLight)
|
|
lightCenterAngle = std::clamp(lightCenterAngle += (int)mouseWheelScrolled->delta, 0, 360);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (const auto* mouseMoved = event->getIf<sf::Event::MouseMoved>())
|
|
{
|
|
mousePos = (sf::Vector2f)mouseMoved->position;
|
|
}
|
|
}
|
|
}
|
|
|
|
void render()
|
|
{
|
|
window.clear(sf::Color::White);
|
|
|
|
light.draw(window);
|
|
|
|
for (auto& poly : polygons) window.draw(poly);
|
|
|
|
ImGui::SFML::Render(window);
|
|
|
|
window.display();
|
|
} |