Animating data from a text file

Hi,

I’m new to jogl and 3d programming. I’m not trained as a programmer, but have taught myself java.

I hope this is the right forum, as I’m not building a game, but here is my query.

I have built an application to visualise atoms sticking to a surface. I need to read in data from a text file which contains the coordinates of the atoms both on the surface and the ones sticking to the surface. The surface atoms don’t move, but the atoms sticking to the surface change from one frame to the next. There are several thousand frames to illustrate for a complete set of reactions and a few tens of atoms (not including atoms on the surface) to illustrate for each frame. I simply use glutSolidSphere to draw a sphere for each atom.

My problem is that I am trying to read data in from a text file, use that data to place spheres at a particular point on the scene, redraw the scene and use the mouse to move around the scene all at the same time, and I’m clearly doing it very badly. Changing from one frame to the next is very slow, and the application often freezes when I use the mouse to move around the screen.

I appreciate you will need more detail on what I have done to help me, so I will happily add any details you may requre!

Thanks in advance,

You should read in the text data one single time at application load and then assign it to variables in RAM. If you are reading it every single frame then naturally that will cause lots and lots of speed problems, as reading from the hard disk is incredibly slow.

You can speed up drawing by not drawing things that are out of view, which is pretty easy to calculate with spheres (one sphere is behind another if, along the camera angle, it is entirely within the bounds of another circle and behind it). However, you should be able to draw thousands of spheres with little problem, so this likely is not the issue.

If the program is freezing, it is probably related to some sort of bug. Does an exception get thrown? Or by freeze do you just mean that it lags?

Are you duplicating any memory? Repeatedly generating textures? Anything of that nature?

Also, are you calling OpenGL calls from the mouse listener? The new version of JOGL (which I’m assuming you’re using) has the ‘feature’ of allowing people to make GL calls from the listeners. However, this can be very slow and the mouse listener is meant to be run very quickly so it doesn’t block other things.

There is a way to disable this behavior, but I won’t explain until I know you’re actually doing the above. Also, do not load the animation files from the mouse listener for the same reason as above, since it will block all pending events and could cause you to experience more than normal lag.

OK, I’ll try to answer these questions as best I can.

I tried to read in the text files up front into RAM - at least using arrays, which I guess is the same thing, but kept on getting out of memory errors. I tried to increase the heap size to 512MB, but still ran into problems with the larger text files. I always thought that was a better way to do it. That’s probably not a question for this forum though!

I don’t get exceptions when it freezes, and usually on my PC the application will come back to life after a little while. However on my bosses slightly older laptop my application has been known to crash his system altogether, which is a little awkward!

I kept all the OpenGL calls in one class, the one which extends the GLJPanel class. I suspect that the display method of this class is doing too much work. Every time this method is called it generates a list containing a set of spheres, the scene is rotated according to the mouse position (found from a mouse listener) and then renders the list using a call to glCallList(list).

Hope this helps.

Yeah, you should be storing a persistent state rather than creating a new one every render. What I mean by that is that the list of spheres and the rotation should be precalculated. The list should never be changed aside from initial creation, and the rotation should be changed via mouse movement.

But either way please give lots more details, like how many spheres you’re trying to draw, how big these arrays are, how many draw calls you’re doing, etc.

A couple of suggestions:

  1. Preprocess your data files so that the positions of the static surface atoms are specified once at the start of the file. Their positions never change, so there’s no point in doing the I/O many times. A few tens of atoms, even for a thousand frames, shouldn’t be a memory issue.

  2. Try compiling only the static atoms into a display list of their own and using that list in all frames.

I second bleb’s suggestions (was writing a reply last night that got a bit long-winded, but I think that more or less covers it).

Also, you might benefit from making a display list containing a call to glutSolidSphere; otherwise you’re working out sphere geometry every time - but that wouldn’t be as bad for performance as what you describe.

GLCanvas would probably be faster than GLJPanel.

It won’t effect performance but normal practice is to implement GLEventListener, rather than extending GLJPanel or GLCanvas; you then add your listener implementation to the drawable (panel / canvas).

It would be useful to know what sort of format your data files are in, and how you’re doing I/O at the moment. If you get your data into an appropriate form, you could probably just use a BufferedReader, iterating with readLine() while parsing and using the results pretty directly - without even bothering to store information in arrays at all…