Hey guys.
I'm re-sharpening my OpenGL stone, and I've hit a problem that has stumped both myself and my internet researching skills. It's probably very simple.
The problem is once I run my application, the application's viewport flashes between two renders in a flickering pattern. I'm supposing the front and back buffers are swapping.
How can I prevent this? I want all the triangles to stay on screen at all times. I'm certain it has to do with my if/else statment followed by window.display(), which is from SFML 2.0rc.
I'm working on a Sierpinski Gasket (I'm close but not done yet. Don't tell me I want to figure it out!). I am interested in general code clean up though. I'm open for any suggestions!
Thanks!
#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 < 3; 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();
}
}