【问题标题】:VertexArray of circles圆的顶点数组
【发布时间】:2017-12-18 15:07:17
【问题描述】:

我想知道是否可以在 SFML 中创建圆的 VertexArray。我一直在寻找答案,但我没有找到任何可以帮助的东西。此外,我不理解 SFML 文档中关于我可以创建自己的实体的部分,我认为这可能是我实际上想要做的。

编辑:我想这样做,因为我必须画很多圈。

谢谢你帮助我

【问题讨论】:

  • 为什么不使用 sf::CircleShape 的数组?

标签: c++ sfml vertex-array


【解决方案1】:

虽然@nvoigt 的回答是正确的,但我发现在我的实现中使用向量很有用(有关更多详细信息,请参阅http://en.cppreference.com/w/cpp/container/vector,查找“c++ 容器”,有几种类型的容器可以优化读/写时间) .

对于上述用例,您可能不需要它,但在未来的实现中可能需要它,并考虑将其作为良好的编码实践。

#include <SFML/Graphics.hpp>
#include <vector>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");


    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // initialize myvector
        std::vector<sf::CircleShape> myvector;

        // add 10 circles
        for (int i = 0; i < 10; i++)
        {
          sf::CircleShape shape(50);
          // draw a circle every 100 pixels
          shape.setPosition(i * 100, 25);
          shape.setFillColor(sf::Color(100, 250, 50));

          // copy shape to vector
          myvector.push_back(shape);
        }

        // iterate through vector
        for (std::vector<sf::CircleShape>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
        {
          // draw all circles
          window.draw(*it);
        }
        window.display();
    }

    return 0;
}

【讨论】:

  • 谢谢,事实上我已经使用了矢量,但正如我在另一条评论中提到的那样,我最不想调用 draw 方法。
【解决方案2】:

sf::CircleShape已经使用顶点数组(感谢继承自 sf::Shape)。您无需做任何额外的事情。

如果您有很多圈子,请先尝试使用sf::CircleShape,并且仅在您有可以衡量您的解决方案的真实用例时进行优化。

【讨论】:

  • 谢谢,但我真的没有办法只调用一次draw方法来绘制我所有的圈子吗?
  • 您当然可以在一次调用中绘制所有圆圈,但该调用背后的方法需要逐圈绘制。同样,为什么不这样做,即使只是作为衡量您的解决方案的练习?计算机处理复杂的 3D 世界,您的显卡需要很多个圆圈才能启动它的风扇。
  • 不,我的意思是,在 SFML 中调用绘图函数会花费很多,因为它必须设置很多东西,因此我只想在 VertexArray 的圆圈上调用它一次,这样它就可以设置所有内容只有一次。我已经用正方形做到了,它确实快了很多。
  • 好吧,如果您已经使用正方形,为什么不尝试使用圆形并在遇到问题时使用minimal reproducible example 提出具体问题?
  • 我无法尝试,因为我不知道如何创建圆的 VertexArray,我什至不知道是否可以。
【解决方案3】:

除了之前的两个答案,我将尝试解释为什么没有默认的 VertexArray of circles。

根据计算机图形学(在我们的例子中是 SFML)的思想,顶点是最小的绘图图元,具有最少的必要功能。顶点的经典示例是点、线、三角形、瓜德和多边形。前四个对于您的视频卡来说非常容易存储和绘制。多边形可以是任何几何图形,但处理起来会更重,这就是为什么在 3D 图形中多边形是三角形的原因。

圆有点复杂。例如,视频卡不知道她需要多少点才能将您的圆圈画得足够光滑。因此,正如@nvoigt 回答的那样,存在一个 sf::CircleShape 是从更原始的顶点构建的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多