First jogl/opengl program and it's jittery

This is my first jogl program, and i apologize if the question seems too basic or repeated.

I made a program that is supposed to represent a droplet of liquid diffusing into a glass of liquid. This is very simplistic, it is basically a grid where particles randomly move about, and I can’t imagine it being that cpu intensive. I use a two-dimensional long array to store the amount of particles in that cell. Then based on that I draw a polygon in that cell with a calculated intensity (darker = more particles in cell). I then have to iterate through the array and make the polygons.

the problem is that when the grid gets large, and the amount of arrayPostions needed to check grows proportional to the square of the side, the animation runs smooth and that pauses, almost like it needs to catch up. This jitterying starts when the array is about 100x100, or 10000 cells.

How can I make the animation more smooth?

I don’t see a way to eliminate the need to iterate through all cells. If someone has any idea I would be very appreciative.

could garbage collection be the problem? do you create objects while iterating?

Maybe?? I do create a “long” and a “float” every time. Here is the code for the particles.

If it is the garbage collection, is there a way to find out. Right now I’m running this program in Eclipse if that helps.

thanks again.

	
private void drawInkSquares (GL gl) {
		float spacing = (20.0f)/((float) sideLength);  //square separation on the lattiice grid
		
		for (float lowerLeftRow = -10.0f, row = 0; row < sideLength; lowerLeftRow += spacing, row++) {
			for (float lowerLeftCol = -10.0f, col = 0; col < sideLength; lowerLeftCol += spacing, col++) {
				long particles = particlePositions[(int) row][(int) col];
				
				if (particles == 0) {
					
				} else {
					//particle intensity
					float squareIntensity = ((float) particles)/((float) numOfWalkers);
					
					//To make only fewer shadings
					if ((double) squareIntensity != 0.0) {
						squareIntensity += .6f;
					}
					
					if (tracePath && pathBooleans[(int) row][(int) col]) {
						squareIntensity = 1.0f;
					}
					
					gl.glBegin(GL.GL_QUADS);
						gl.glColor4f(0.025f, 0.325f, 0.95f, squareIntensity);
						gl.glVertex3f(lowerLeftCol, lowerLeftRow, 0.0f);
						gl.glVertex3f(lowerLeftCol, lowerLeftRow + spacing, 0.0f);
						gl.glVertex3f(lowerLeftCol + spacing, lowerLeftRow + spacing, 0.0f);
						gl.glVertex3f(lowerLeftCol + spacing, lowerLeftRow, 0.0f);
					gl.glEnd();
				}
			}
		}
	}

Move glBegin and glEnd outside the loop. You can draw as many quads as you like with a single begin/end block. Something as simple as that can already improve the performance by a factor of 2.5.

Either way… you should use a profiler and determine where the bottleneck really is.

according to my profiler it spends about 12% of its time doing openGL things:

CGLFlushDrawable takes about 70% of the opengl time

and

glVertex3f takes about %20

Is this normal or does this mean that the other processes, outside of gl are just taking too long?

I’ve been messing around with my code, trying to find something that could be the cause of the problem, but to no avail.

Does anyone know why I’m calling CGLFlushDrawable so much and what I might be able to do to solve the problem?

I think it is called automatically to flush all remaining data to the gfxcard and you can do nothing about it (and should not need to). Have you moved the glBegin/glEnd calls? If the you only have 12% glrelated load, what does the other 88% do?

There are basically two steps:

in the update step i go into each cell, and for each particle in that cell i randomly move it in a direction. I do this for every cell, which can be large if the side of the container is large.

Then the actual drawing method, I go to each cell again and draw the quad based on how many particles are in the cell.

You are right that these steps take awhile, but why is it that the animation is smooth for several frames and then pauses for a bit, and then repeats. Is there a way to make it such that the frame rate just slows down to eliminate the pause. I have tried the animators method run as fast a possible method, which didn’t work.

thanks again
kirby

Try to profile memory usage and see if you find a correllation between garbage collector runs and the slowdown. I use jprofiler (commercial), which shows a nice realtime graph of the memory usage. If there is a correllation, you could try to figure out, what objects are created (awt-events?) and how you can optimize this. Also some command line switches to control garbage collection methods could help. But first you have to find out, wheres the problem. It could very well be that’s something completely different.

I’ve never used a java profiler, but I went and found one that apple gives out for free called Shark. It is a bit confusing and I’m having trouble using it. I ran several of the profile tests, but the problem is that not all of the names of the processes show up, there symbols are missing and so I just see a memory address.

???

I understand that you won’t be able to help me with this problem, but I just wanted to update. I’ll try fiddling with it later

thanks

Don’t know Shark. Maybe you should try the Netbeans Profiler: http://profiler.netbeans.org/. I never used it myself, but the “Telemetry Overview” screenshot seems to contain all the info you need to find out, if garbage collection is a problem. Maybe even JConsole which comes with the JDK will do the job: http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

I used the netbeans profiler and from what I could tell, I think the garbage collection is playing a roll. What vm settings would you recommend to help with that.

Thank you so much, so far you’ve been very helpful

I figured it out! ;D

I found a two dimensional array that I was creating that was taking along time to remove during garbage collection. i changed my code and now it works great.

Thanks for helping out,
I feel like ive learned a lot today

kirby