Problem with dynamically updating display from program output

Hey All!

I am working on visualizing the output of a program I wrote by using OpenGL/GLEW/FreeGlut. I currently have a dual threaded program that calculates some 3D Cartesian coordinates from some data. I also wrote a little OpenGL program that can display coordinates as spheres on a rough 3D axis.

My problem is I cannot get the display to accept input from the two threads performing the computations.

I tried adding my OpenGL display just before I launch both threads and having the threads write their outputs to global variables used by both sides of the program, but I think that the glutMainLoop(); function is not returning control back over to the main program as I am left with just the empty display of the 3D axis and no points. Is there a way to launch the OpenGL display function and have it passively wait for an updated input or simply run and return control back to the program is was launched from?

I just found glutMainLoopEvent();, but I have not found any decent documentation on how to use it. If this is the way I would want to go, then can anyone recommend some examples or docs?

I also thought that maybe I have to put all of my computations inside of the "display" function from glutDisplayFunc(display); but the program is fairly large and complicated with different initializations and thread checks and I am unsure of how it will like being put entirely in the display.

Thanks for looking!

Dave

EDIT: I think it is worth noting that the dual threaded program is also cuda, I am looking into CUDA/OpenGL interop now as well.

I think glutIdlefunc is what you need. Look up definition and examples.

Thank you for your reply!

I am vaguely familiar with glutIdleFunc, and I believe it assigns what the program is to do while it waits for a callback. Since the glutMainLoop() does not return control after it has been launched I am still confused as to how the idle function will help this situation.

Initialize input data and system(Load data, set up buffers)
|
V
Start OpenGL/Freeglut display (read input from shared globals with threads)[Program displays window and empty axis created here but I dont think it is allowing the next steps to occur]
|
V
Start two threads that perform compuations (write output to global variables)
|
V
Run until exit

I currently have glutIdleFunc running with my display function as the input as well. Are you suggesting that I move my entire program inside of glutIdleFunc? I thought about doing this but I am not sure if it is the best option since the program is fairly large and complex.

EDIT: I think my phrasing was confusing, the input I want it to wait for is simply updated coordinates from the code I wrote to perform calculations, not a keyboard or mouse or reshape callback. Sorry!

Here is some code that might show my problem a little more clearly. I have a main program and two functions, one that generates xyz coords and the display function I want to plot the points on. I just can't figure out a method to get the input to the display.

When I read your first post, it sounded like you wanted an animation of balls or points moving as new positions were computed. But your last post seems to indicate that all you need is to show the positions of all the balls after the calculations are complete. This is MUCH easier.

Once you open a GL window with GLUT, you are in an infinite loop. You will never exit the simpleGL routine (unless you terminate the program). That's what your problem is. The 'for' statement in 'main' is never reached. This is a flow problem which is easy to correct.

Try calling 'gen_XYZ_coords' from your 'display' routine. I would put the call at the very beginning, before 'glClearColor'. Glut calls 'display' in response to an input action such as pushing a mouse button. I assume that each time you push a mouse button you want to recompute and redisplay your set of points. This should do it, or least get you much closer to what you want.

BTW - I wouldn't call this a dual threaded program - at least not the example you've posted.

Thank you Carmine! I actually ended up doing a slightly different work around. I have my main thread open a GL window and set all the functions, then that main thread launches two threads that begin generating the XYZ coordinates. ( I left them out of the program for simplicity and to show only the openGL problem, I agree as shown it is not a multithreaded program) After control returns to the main thread I use globals to transfer data to the display function and repeated calls to glutPostRedisplay() and glutMainLoopEvent().

Ideally I would like to have an update call in each of the processing threads so that on every iteration, each thread will update the display, but I found that this could be limiting with the scope of GL and I don't think it is threadsafe, but I may try to make some sort of work around.

Here is the code with my solution implemented in case anyone finds it useful.