Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSOpenGLView and VBL Synchronization

NSOpenGLView and VBL Synchronization
Thread Tools
JBL
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Apr 6, 2001, 07:11 PM
 
Okay, I am in over my head again...

I want to do a very simple animation in a subclass of NSOpenGLView. The trick is that I do not want it to "tear." To prevent this I need to synchronize the drawing with the screen refresh. How do I do this?

There is this on Apple's website:

------------------------------------------------------------
VBL syncing

Synchronize the frame rate of my application to the monitor's refresh rate (VBL syncing) can be enabled with thefollowing:

GLint swapInt = 1;
AGLSetInteger(aglContext, AGL_SWAP_INTERVAL, &swapInt);

Note that this will lock your frame rate to a divisible integer of your monitor's current refresh rate.
...

Frame Tears

Frame tears are a synchronization issue unrelated to swap speeds, contrary to popular belief. A tear occurs when thebuffers are swapped while the electron beam is in the middle of painting on the screen. VBL synching should solve this problem.
--------------------------------------------------------

The problem is that the context owned by a NSOpenGLView is a NSOpenGLContext not an AGLContext. As a result, it doesn't complile if you do something simple minded like this:
GLint swapInt = 1;
AGLSetInteger([self openGLContext], AGL_SWAP_INTERVAL, &swapInt);

I tried:
const long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:AGL_SWAP_INTERVAL];
This compiles but it doesn't work (tears). This is the same:
const long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];


I think the problem is that I am not dealing with the contexts correctly (ie, I am setting the swap interval for one context but drawing into another). Unfortunately, I can't find any documentation that tells me how to do it correctly.

Here is the relevant code:
- (void)drawRect:NSRect)rect
{
int n;
const long swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];

glClearColor(0.0, 0.0, 0.0, 0.0);
for(n = 0; n < 60; n++){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5+(n-30)/70.0, -0.5);
glVertex2f(-0.5+(n-30)/70.0, 0.5);
glVertex2f(0.5+(n-30)/70.0, 0.5);
glVertex2f(0.5+(n-30)/70.0, -0.5);
glEnd();
glFlush();
}
}

     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:57 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2