c++ - 来自 VertexArray 的 SFML 绘图基元

标签 c++ sfml vertex-array

如何从构造的 VertexArray 中绘制我选择的图元?在下面的示例中,我向“vertices”数组添加两个顶点,并尝试使用“window.draw(vertices, 2, sf::Lines)”绘制它,但它给了我一个错误。我知道我可以使用 'sf::Vertex foo[] = {..}' 创建线对象,但我希望能够继续将顶点附加到数组,而不是一次初始化所有顶点。

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML");
    sf::Clock clock;

    sf::VertexArray vertices;
    sf::Vertex vertex;

    vertex.position = sf::Vector2f(0, 0);
    vertex.color = sf::Color(100, 0, 200);
    vertices.append(vertex);
    vertex.position = sf::Vector2f(100, 100);
    vertex.color = sf::Color(100, 0, 200);
    vertices.append(vertex);

    // Start the game loop
    bool running = true;
    while (running)
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                running = false;
        }

        window.clear(sf::Color::Black);
        window.draw(vertices,2 ,sf::Lines);

        window.display();
    }

    return 0;
}

最佳答案

您可以在声明 sf::VertexArray vertices; 后调用 vertices.setPrimitiveType(sf::Lines);,然后绘制它: window.draw(顶点);

或者您可以在构造函数中设置其原始类型以及点数,然后您可以使用 operator[] 访问这些点:

sf::VertexArray line(sf::Lines, 2); //or sf::LineStrip
line[0].position = sf::Vector2f(0, 0);
line[0].color = sf::Color(100, 0, 200);
line[1].position = sf::Vector2f(100, 100);
line[1].color = sf::Color(100, 0, 200);
...
window.draw(line);

引用:enum sf::PrimitiveType , sf::VertexArray , VertexArray tutorial

关于c++ - 来自 VertexArray 的 SFML 绘图基元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50124783/

相关文章:

c++ - 如何将字体文件集成(硬编码)到我的程序中?

android-ndk,找不到 glGenVertexArraysOES

opengl - 为什么 OpenGL 的 glDrawArrays() 在核心配置文件 3.2 下会因 GL_INVALID_OPERATION 而失败,但在 3.3 或 4.2 下却不会?

c++ - CMake 3.8+ : Setting different compiler flags for projects that include both . cpp (C++) 和 .cu (CUDA) 文件

c++ - std::launch::async 像同步进程一样阻塞

c++ - SFML 2.1 - 检测单击鼠标并更改纹理

opengl - 用 glDrawElements 很难理解索引

c++ - 创建频率数组

c++ - 以编程方式模拟 Alt + Enter 按键不起作用

c++ - SFML + 固定时间步 = 滞后?