tags:

views:

89

answers:

2

Hi, I'm using win32 and opengl and I have a window set up with the projection at glOrtho of the window's coordinates. I have double buffering enabled, tested it with glGet as well. My program always seems to tear any primitives that I try to draw on it if it's being constantly translated.

Here is my OpenGL initialization function:

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 0, 480, 0, 100);

glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glDrawBuffer(GL_BACK);

glLoadIdentity();

And this is my rendering function, gMouseX and gMouseY are the coordinates of the mouse:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glTranslatef(gMouseX, gMouseY, 0.0f);
glColor3f(0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLES);
    glVertex2f(0.0f, 128.0f);
    glVertex2f(128.0f, 0.0f);
    glVertex2f(0.0f, 0.0f);
glEnd();

SwapBuffers(hDC);

The same tearing problem occurs regardless of how often the rendering function runs. Is there something I'm doing wrong or missing here? Thanks for any help.

+1  A: 

Tearing is caused by not synchronizing your updates with the monitor's vertical blanking interval. It's a bit hardware dependent in OpenGL (I think, not an expert), start with looking for glXWaitVideoSyncSGI. Googling "opengl sync to vblank" pays off too.

Hans Passant
glX is for Unix, OP said he is on win32.
slacker
+4  A: 

If you're just rendering one or two trianges, you're going to have an insanely high framerate (in the thousands of FPS probably), which means it's SwapBuffering like hell, which means you're probably always swapping during the screen refresh (even with double buffering on). Once your scene gets going with more content and the framerate falls to more realistic values, it should stop tearing. Alternatively, look in to enabling V-Sync, such as with wglSwapIntervalEXT on Windows.

AshleysBrain
I forgot to say that the framerate is capped at about 60 FPS (every 16ms using WM_TIMER), and it still tears no matter what I set the timer to.
kaykun
Enable vsync as @AshleysBrain said. Manually capping the framerate using timers won't synchronize it with the screen refresh.
jalf
After a call to glGetString(GL_EXTENSIONS), I found out that VSync isn't enabled on my system because the string "WGL_EXT_swap_control" does not return. Is there really absolutely no other way to reduce tearing because almost any other game I can download do not have this problem.
kaykun
@kaykun: wgl_ext_swap_control is not a GL extension. It's a _wgl_ extension. See http://www.opengl.org/registry/specs/EXT/wgl_extensions_string.txt for how to handle wgl extensions in general. All modern GL implementations on windows support it, afaik.
Bahbar