2

一旦我运行我的应用程序,应用程序的视口就会在两个渲染之间以闪烁的模式闪烁。我假设前后缓冲区正在交换。

我怎样才能防止这种情况?我希望所有的三角形一直都在屏幕上。

我正在研究一个谢尔宾斯基垫片(我很接近但还没有完成。不要告诉我我想弄清楚!)。不过,我对一般代码清理很感兴趣。我愿意接受任何建议!

#include SFML/Graphics.hpp
#include GL/glew.h

using namespace sf;

struct point
{
    float x, y; // z is always 0 ... for now. MWUAHAHAA
};

point points[3] = {{0.f, 1.f},     // top
                 {-1.f, -1.f},     // left
                 { 1.f, -1.f}};     // right

point temp = {points[0].x, points[0].y};

void renderFirstTriangle()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);

    glColor3f(1.f, 1.f, 1.f);
    glVertex3f(points[0].x, points[0].y, 0.f);

    glColor3f(1.f, 1.f, 1.f);
    glVertex3f(points[1].x, points[1].y, 0.f);

    glColor3f(1.f, 1.f, 1.f);
    glVertex3f(points[2].x, points[2].y, 0.f);

    glEnd();
}

int main()
{
Window window(VideoMode(800, 600), "Fractal", Style::Default, ContextSettings(32));
window.setVerticalSyncEnabled(true);

renderFirstTriangle();

while (window.isOpen())
{
    Event event;
    while (window.pollEvent(event))
    {
        if (event.type == Event::Resized)
            glViewport(0, 0, event.size.width, event.size.height);

        else if (event.type == Event::Closed)
            window.close();
    }

    for (int i = 0; i < 2; i++)
    {
        // calculate new vertex positions

        temp.x = points[0].x;
        temp.y = points[0].y;

        glBegin(GL_TRIANGLES);

        if( i % 2 )
            glColor3f(0.1f, 0.1f, 0.1f);

        glVertex3f((points[0].x + points[1].x) / 2, (points[0].y + points[1].y) / 2, 0.0f);
        glVertex3f((points[1].x + points[2].x) / 2, (points[1].y + points[2].y) / 2, 0.0f);
        glVertex3f((points[2].x + temp.x) / 2, (points[2].y + temp.y) / 2, 0.0f);

        glEnd();
    }
    window.display();
}
}
4

1 回答 1

0

每次通过主循环清屏:

// g++ main.cpp -lGL -lsfml-graphics -lsfml-window
// using SFML 1.6

#include <SFML/Graphics.hpp>

using namespace sf;

struct point
{
    float x, y; // z is always 0 ... for now. MWUAHAHAA
};

point points[3] = {{0.f, 1.f},     // top
                 {-1.f, -1.f},     // left
                 { 1.f, -1.f}};     // right

point temp = {points[0].x, points[0].y};

void renderFirstTriangle()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);

    glColor3f(1.f, 1.f, 1.f);
    glVertex3f(points[0].x, points[0].y, 0.f);

    glColor3f(1.f, 1.f, 1.f);
    glVertex3f(points[1].x, points[1].y, 0.f);

    glColor3f(1.f, 1.f, 1.f);
    glVertex3f(points[2].x, points[2].y, 0.f);

    glEnd();
}

int main()
{
    Window window(VideoMode(800, 600), "Fractal");
    window.UseVerticalSync(true);

    renderFirstTriangle();

    while (window.IsOpened())
    {
        Event event;
        while (window.GetEvent(event))
        {
            if (event.Type == Event::Resized)
                glViewport(0, 0, event.Size.Width, event.Size.Height);

            else if (event.Type == Event::Closed)
                window.Close();
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        for (int i = 0; i < 2; i++)
        {
            // calculate new vertex positions

            temp.x = points[0].x;
            temp.y = points[0].y;

            glBegin(GL_TRIANGLES);

            if( i % 2 )
                glColor3f(0.1f, 0.1f, 0.1f);

            glVertex3f((points[0].x + points[1].x) / 2, (points[0].y + points[1].y) / 2, 0.0f);
            glVertex3f((points[1].x + points[2].x) / 2, (points[1].y + points[2].y) / 2, 0.0f);
            glVertex3f((points[2].x + temp.x) / 2, (points[2].y + temp.y) / 2, 0.0f);

            glEnd();
        }
        window.Display();
    }
}
于 2012-09-29T06:42:14.127 回答