闪烁(后缓冲?)

时间:2012-09-29 04:53:46

标签: c++ opengl

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

我该怎样防止这种情况?我希望所有三角形始终保持在屏幕上。

我正在研究一个Sierpinski垫片(我很接近但尚未完成。不要告诉我,我想弄明白!)。我对通用代码清理感兴趣。我愿意接受任何建议!

#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();
}
}

1 个答案:

答案 0 :(得分: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();
    }
}