Advertisement
Necto

raymarching v 1.4

Aug 12th, 2022 (edited)
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. #include <sfml/Graphics.hpp>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. #define PI 3.14159265
  6.  
  7. void draw_rect(std::vector<sf::RectangleShape>& walls) {
  8.     sf::RectangleShape rect1(sf::Vector2f(100, 50));
  9.     rect1.setPosition(sf::Vector2f(100, 75));
  10.     sf::RectangleShape rect2(sf::Vector2f(120, 120));
  11.     rect2.setPosition(sf::Vector2f(300, 180));
  12.     sf::RectangleShape rect3(sf::Vector2f(150, 130));
  13.     rect3.setPosition(sf::Vector2f(550, 100));
  14.  
  15.  
  16.     sf::RectangleShape rect6(sf::Vector2f(800, 20));
  17.     rect6.setPosition(sf::Vector2f(0, 0));
  18.     sf::RectangleShape rect7(sf::Vector2f(20, 600));
  19.     rect7.setPosition(sf::Vector2f(0, 0));
  20.     sf::RectangleShape rect8(sf::Vector2f(800, 20));
  21.     rect8.setPosition(sf::Vector2f(0, 580));
  22.     sf::RectangleShape rect9(sf::Vector2f(20, 600));
  23.     rect9.setPosition(sf::Vector2f(780, 0));
  24.     walls.push_back(rect1);
  25.     walls.push_back(rect2);
  26.     walls.push_back(rect3);
  27.     walls.push_back(rect6);
  28.     walls.push_back(rect7);
  29.     walls.push_back(rect8);
  30.     walls.push_back(rect9);
  31.     for (int i = 0; i < walls.size(); ++i)
  32.         walls[i].setFillColor(sf::Color::Black);
  33. }
  34.  
  35. void draw_circle(std::vector<sf::CircleShape>& walls) {
  36.     sf::CircleShape circle1(50);
  37.     circle1.setPosition(sf::Vector2f(150, 450));
  38.     sf::CircleShape circle2(75);
  39.     circle2.setPosition(sf::Vector2f(450, 360));
  40.     walls.push_back(circle1);
  41.     walls.push_back(circle2);
  42.     for (int i = 0; i < walls.size(); ++i)
  43.         walls[i].setFillColor(sf::Color::Black);
  44. }
  45.  
  46. float dist_points(float a, float b) {
  47.     return sqrt(a * a + b * b);
  48. }
  49.  
  50. float dist_circle(sf::Vector2f& start, std::vector<sf::CircleShape>& walls, sf::Color& color) {
  51.     float min = 10000;
  52.     for (int i = 0; i < walls.size(); ++i) {
  53.         float dist = dist_points(walls[i].getPosition().x + walls[i].getRadius() - start.x, walls[i].getPosition().y + walls[i].getRadius() - start.y) - walls[i].getRadius();
  54.         if (dist <= min) {
  55.             min = dist;
  56.             sf::Color color = walls[i].getFillColor();
  57.         }
  58.     }
  59.     return min;
  60. }
  61.  
  62. float dist_rect(sf::Vector2f& start, std::vector<sf::RectangleShape>& walls, sf::Color& color) {
  63.     float dist;
  64.     float min = 1000000;
  65.     sf::Vector2f temp = start;
  66.     for (int i = 0; i < walls.size(); ++i) {
  67.         temp.x = abs(abs(start.x) - (walls[i].getPosition().x + (walls[i].getSize().x / 2)));
  68.         temp.y = abs(abs(start.y) - (walls[i].getPosition().y + (walls[i].getSize().y / 2)));
  69.         dist = dist_points(std::max(temp.x - walls[i].getSize().x / 2, 0.f), std::max(temp.y - walls[i].getSize().y / 2, 0.f));
  70.         if (dist < min) {
  71.             min = dist;
  72.             sf::Color color = walls[i].getFillColor();
  73.         }
  74.  
  75.     }
  76.     return min;
  77. }
  78.  
  79. float dist_to_walls(sf::Vector2f start, std::vector<sf::CircleShape>& circle_walls, std::vector<sf::RectangleShape>& rect_walls, sf::Color& color) {
  80.     float c = 10000, r = 10000;
  81.     c = dist_circle(start, circle_walls, color);
  82.     r = dist_rect(start, rect_walls, color);
  83.     return std::min(c, r);
  84. }
  85.  
  86. sf::Texture draw_background(sf::RenderWindow& window) {
  87.     sf::Image background;
  88.     background.create(window.getSize().x, window.getSize().y, sf::Color(41, 49, 51));
  89.     sf::Texture text_bg;
  90.     text_bg.loadFromImage(background);
  91.     return text_bg;
  92. }
  93.  
  94. void calc_ray(std::vector<sf::CircleShape>& circle_walls, std::vector<sf::RectangleShape>& rect_walls, sf::RectangleShape& ray, sf::RenderWindow& window, sf::VertexArray& marked) {
  95.     sf::Vector2f start(50, 450);
  96.     sf::CircleShape circle;
  97.     circle.setOutlineColor(sf::Color(220, 220, 220));
  98.     circle.setOutlineThickness(2.f);
  99.     circle.setFillColor(sf::Color(47, 79, 79, 0));
  100.     float rad = 1000;
  101.     sf::Color color = sf::Color(220, 220, 220);
  102.     while (rad > 1.f) {
  103.         rad = dist_to_walls(start, circle_walls, rect_walls, color);
  104.         circle.setRadius(rad);
  105.         circle.setPosition(sf::Vector2f(start.x - rad, start.y - rad));
  106.         circle.setOutlineColor(color);
  107.         sf::Vector2f b2;
  108.         start.x = sin(PI * (ray.getRotation() - 180) / 180) * rad + start.x;
  109.         start.y = cos(PI * ray.getRotation() / 180) * rad + start.y;
  110.         //color.a = 0;
  111.         window.draw(circle);
  112.     }
  113.     sf::Vertex temp = sf::Vertex(sf::Vector2f(start.x, start.y), sf::Color::White);
  114.     marked.append(temp);
  115.     float new_size = dist_points(start.x - 50, start.y - 450);
  116.     ray.setSize(sf::Vector2f(1, new_size));
  117. }
  118.  
  119. int WinMain() {
  120.  
  121.     sf::ContextSettings settings;
  122.     settings.antialiasingLevel = 8;
  123.     sf::RenderWindow window(sf::VideoMode(800, 600), "", sf::Style::Default, settings);
  124.     window.setFramerateLimit(240);
  125.     sf::Event event;
  126.     window.setMouseCursorVisible(false);
  127.     window.setMouseCursorGrabbed(true);
  128.  
  129.     sf::Texture text_bg = draw_background(window);
  130.     sf::Sprite sprite_bg(text_bg);
  131.  
  132.     sf::RectangleShape ray(sf::Vector2f(1, 3000));
  133.     ray.setPosition(50, 450);
  134.  
  135.     std::vector<sf::RectangleShape> rect_walls;
  136.     draw_rect(rect_walls);
  137.  
  138.     std::vector<sf::CircleShape> circle_walls;
  139.     draw_circle(circle_walls);
  140.  
  141.     sf::VertexArray marked(sf::Points);
  142.  
  143.     while (window.isOpen()) {
  144.  
  145.         while (window.pollEvent(event)) {
  146.             if (event.type == sf::Event::Closed)
  147.                 window.close();
  148.         }
  149.  
  150.         for (int i = 0; i <= 720; i += 1) {
  151.             window.clear();
  152.             window.draw(sprite_bg);
  153.             for (int i = 0; i < rect_walls.size(); ++i)
  154.                 window.draw(rect_walls[i]);
  155.             for (int i = 0; i < circle_walls.size(); ++i)
  156.                 window.draw(circle_walls[i]);
  157.             ray.setRotation(-(i / 4));
  158.             calc_ray(circle_walls, rect_walls, ray, window, marked);
  159.             window.draw(ray);
  160.             window.draw(marked);
  161.             window.display();
  162.         }
  163.     }
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement