gpt4 book ai didi

c++ - 闪烁(后台缓冲区?)

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:03 25 4
gpt4 key购买 nike

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

我该如何防止这种情况发生?我希望所有三角形始终显示在屏幕上。

我正在研究 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();
}
}

最佳答案

每次通过主循环清除屏幕:

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

关于c++ - 闪烁(后台缓冲区?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com