SMFL的2D射线投射问题

时间:2018-12-02 00:17:41

标签: c++ raycasting

我是SMFL的新手,我正在使用多种光源在多个三角形下进行射线投射。我做了一个。 但是我认为有一些错误。有时我的光源消失了,除了一个点。如果光源是控制台的上部,它将仅出现在控制台中,有时甚至不会出现在控制台中。 帮我解决这个问题。我将非常感谢。

我在存储过程中包括了屏幕截图。

#include "pch.h"
#include <cmath>
#include <SFML/Graphics.hpp>
#include <vector>

using namespace sf;

float v2fNorm(Vector2f v) {
    return sqrt(v.x * v.x + v.y * v.y);
}


//ray cast from lightsource 1
Vector2f raycast(Vector2f lightsource1, Vector2f mouse, Image img) {
    // Grab the normalized vector
    Vector2f u = mouse - lightsource1;
    u /= v2fNorm(u);

    Rect<float> r(Vector2f(0, 0), (Vector2f)img.getSize());

    // Do it until you go outside of the bounds
    while (r.contains(lightsource1.x, lightsource1.y)) {
        if (img.getPixel(lightsource1.x, lightsource1.y) != Color::Black &&
            img.getPixel(lightsource1.x, lightsource1.y) != Color::Red) {
            // We've hit it!
            return lightsource1;
        }

        lightsource1 += u;
    }

    return lightsource1;
}


//ray cast from lightsource 2
Vector2f raycast2(Vector2f lightsource2, Vector2f mouse, Image img) {
    // Grab the normalized vector
    Vector2f u = mouse - lightsource2;
    u /= v2fNorm(u);

    Rect<float> r(Vector2f(0, 0), (Vector2f)img.getSize());

    // Do it until you go outside of the bounds
    while (r.contains(lightsource2.x, lightsource2.y)) {
        if (img.getPixel(lightsource2.x, lightsource2.y) != Color::Black &&
            img.getPixel(lightsource2.x, lightsource2.y) != Color::Red) {
            // We've hit it!
            return lightsource2;
        }

        lightsource2 += u;
    }

    return lightsource2;
}


//ray cast from lightsource 3
Vector2f raycast3(Vector2f lightsource3, Vector2f mouse, Image img) {
    // Grab the normalized vector
    Vector2f u = mouse - lightsource3;
    u /= v2fNorm(u);

    Rect<float> r(Vector2f(0, 0), (Vector2f)img.getSize());

    // Do it until you go outside of the bounds
    while (r.contains(lightsource3.x, lightsource3.y)) {
        if (img.getPixel(lightsource3.x, lightsource3.y) != Color::Black &&
            img.getPixel(lightsource3.x, lightsource3.y) != Color::Red) {
            // We've hit it!
            return lightsource3;
        }

        lightsource3 += u;
    }

    return lightsource3;
}


int main() {
    RenderWindow w(VideoMode(1000, 600), "Raycast Thingy", Style::Close);
    Event evt;
    Texture wt;
    Image img;
    Vector2f lightsource1(50, 150);
    Vector2f lightsource2(50, 500);
    Vector2f lightsource3(950, 150);
    Vector2f lightsource4(950, 500);
    CircleShape triangles(40, 3);
    std::vector<decltype(Mouse::getPosition())> cpos;
    bool hasDrawn = false;

    wt.create(w.getSize().x, w.getSize().y);
    wt.update(w);
    img = wt.copyToImage();

    triangles.setFillColor(Color::White);


    while (w.isOpen()) {
        while (w.pollEvent(evt)) {
            switch (evt.type) {
            case Event::Closed:
                w.close();
                break;
            case Event::MouseButtonReleased:
                cpos.push_back(Mouse::getPosition() - w.getPosition());
                hasDrawn = true;
                break;
            default:
                break;
            }
        }

        w.clear();

        // Draw line from lightsource1
        VertexArray ray1(Lines, 2);
        ray1[0].position = lightsource1;
        ray1[1].position = raycast(lightsource1, (Vector2f)(Mouse::getPosition() - w.getPosition()), img);
        ray1[0].color = Color::Red;
        ray1[1].color = Color::Red;
        w.draw(ray1);

        // Draw line from lightsource2
        VertexArray ray2(Lines, 2);
        ray2[0].position = lightsource2;
        ray2[1].position = raycast2(lightsource2, (Vector2f)(Mouse::getPosition() - w.getPosition()), img);
        ray2[0].color = Color::Yellow;
        ray2[1].color = Color::Yellow;
        w.draw(ray2);

        // Draw line from lightsource3
        VertexArray ray3(Lines, 2);
        ray3[0].position = lightsource3;
        ray3[1].position = raycast3(lightsource3, (Vector2f)(Mouse::getPosition() - w.getPosition()), img);
        ray3[0].color = Color::Blue;
        ray3[1].color = Color::Blue;
        w.draw(ray3);

        for (auto v : cpos) {
            triangles.setPosition(v.x, v.y);
            w.draw(triangles);
        }

        if (hasDrawn) {
            wt.update(w);
            img = wt.copyToImage();
            hasDrawn = false;
        }

        w.display();
    }
}

0 个答案:

没有答案