Fair enough.
Interestingly, your nanoTime comment might be it! I was using it because I wanted accuracy better than currentTimeMillis, but if it’s an issue, maybe that’s a good trade-off (no flicker for worse accuracy). Is there a better timer to use to get millisecond accuracy (not just precision) than nanoTime?
Anyway, here is the basic loop for the image presentation:
:
while( !m_stop_running && stimulus_iterator.hasNext() )
{
TimedStimulus item = stimulus_iterator.next();
// Load image into ByteBuffer
//
loadStimulus( item );
// Determine how long to wait before display
//
// Time to present - (elapsed time since last stimulus) - (typical time to render)
//
sleep_time = item.getTime() +
( m_start - System.nanoTime() ) -
( m_average_image_render_time );
// If we have plenty of time, then sleep until we're ready
//
if( sleep_time > 0 )
{
try
{
Thread.sleep( sleep_time / Constants.NANOSECONDS_PER_MILLISECOND );
}
catch( InterruptedException ex )
{
continue;
}
}
// Skip the image if it's over 5 seconds late
//
else if( sleep_time < -5 * Constants.NANOSECONDS_PER_SECOND )
{
log.warn( "Image display late " + sleep_time + " nsec. Skipping." );
continue;
}
// Present the stimulus
//
try
{
m_gl_canvas.display();
}
catch( GLException glx )
{
log.warn( "Error displaying image: " + glx.getLocalizedMessage() );
}
} // end loop over timed stimulus items in the task
:
The display method looks like this:
:
// Draw to the buffer as RGB bytes
// m_buffer populated in the loadImage() call in main loop
//
GL gl = drawable.getGL();
gl.glPushAttrib( GL.GL_COLOR_BUFFER_BIT );
gl.glDrawPixels( 800, 600, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, m_buffer );
gl.glPopAttrib();
gl.glFlush();
:
Thanks for your help!