PDA

View Full Version : Pause after GL_LINE_STRIP



09-20-2006, 08:35 AM
Hi,
I am new to OpenGL. I am working on a project where I need to read data from a input file, store coordinates in an array and then draw the image by using GL_LINE_STRIP and coordinates in the array. I also need to pause after each line is drawn so you can see the image being rendered line by line with about 0.3 second pause after each line.

I have everything finished, the problem I have is with the whole animation of lines. It hangs for a while and then draws the whole image at once, without any delay after each line.


void main(int argc, char** argv)
{
PromptFileName();
CreateArray();
ReadData();

init_window(argc,argv);
other_init();

glutMainLoop();
}
void init_window(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_RGB);
glutInitWindowSize(500,400);
glutInitWindowPosition(0,0);
glutCreateWindow("2A");
}
void other_init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3d(0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2026.0, 2026.0, -2026.0, 2026.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

for (int i=0; i<arraySize; i++){
sleep( (clock_t) (0.3 * CLOCKS_PER_SEC ));

glColor3f(i/10.0, 1.0, 0.0);
//arrayPtr has the coordinates
if (arrayPtr[i][3] == 1) // if this row is marked as jump
{
if (i != 0)
glEnd();
glBegin(GL_LINE_STRIP);
}
// draw a line strip
glVertex3f(arrayPtr[i][0], arrayPtr[i][1], 0.0);

glFlush(); // clear buffers
sleep( (clock_t) (0.3 * CLOCKS_PER_SEC ));

}

glEnd();
glFlush();
}Thank you.
-Tom

Brian Paul
09-20-2006, 12:04 PM
It's illegal to call glFlush between glBegin/glEnd.

You'll need to draw the line segments one at a time with glBegin(GL_LINE_STRIP);glVertex3f();glVertex3f();g lEnd() then sleep().

jide
09-20-2006, 01:09 PM
don't sleep when rendering. sleep before or after. Otherwise it's just like kicking in the air...

k_szczech
09-20-2006, 02:09 PM
If you do not use double buffering, then you will have to use just GL_LINES and draw one line at a time, so glBegin/glVertex/glVertex/glEnd/sleep.

If you use double buffering, then you should draw entire line loop from beginning to current segment and call swapBuffers each time.

RigidBody
09-20-2006, 11:52 PM
i guess the sleep function you are using is the one from unistd.h? in that case, the function takes only integers as arguments, and it is not possible to sleep for a fraction of a second.

you better try usleep. usleep takes an unsigned long argument, which is the number of microseconds. if you want a delay of 0.3 seconds, you need usleep(300000).

plasmonster
09-21-2006, 01:28 PM
Maybe he's using this sleep example from the MSDN
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_clock.asp

plasmonster
09-21-2006, 02:03 PM
There's a gem of a function in OpenGL called glGetError. If you place one of these calls after all significant blocks of GL code, you'll have a real time saver in tracking down problems like this one. The errors themselves are pigeonholed into a handful of enums, but they'll sure help you to confine your search. Look at gluErrorString (in glu.h) for a displayable string version of the enum returned by glGetError.

http://www.mevis.de/opengl/glGetError.html
http://www.mevis.de/opengl/gluErrorString.html

09-21-2006, 02:27 PM
Thank you all for your help.
Actually you are correct, I was using that sleep method. But now I am using the "Sleep(DWORD ms)" method from <windows.h>.

And my problem was trying to use glFlush() inside glBegin(...) and glEnd(). After I moved glFlush() after glEnd(), everything is working fine.

Thanks again.
-Tom