[comp.graphics] Followup, Animated atoms

gfs@abvax.UUCP (Greg F. Shay) (04/13/88)

Some further information on my previous posting of the simple 2-d
particle system program.  The source has come through on comp.sources.misc.

The biggest difference you may run across is the dual frame buffer/page flipping
hardware I was using for smooth animation.  The basic scheme with dual frame
buffers is:
		1) Clear the 'back' frame buffer (the one not visible)
		2) Draw new data in back buffer.
		3) Flip the back and front buffers (This is a hardware switch,
			not a copy from the front to the back.)
		4) goto step 1

The routines are all part of the Masscomp Graphics Library,(MGI) just in case 
you have access to a manual and want to look further.

The frame buffers are numbered 1 and 2. 

If your machine does not have two frame buffers, simply draw the atoms, erase
the screen at the point when mgiclearpln() is called, and draw the next set.
For quicker animation with one frame buffer, keep a list of all the atoms
you draw and when you want to clear the screen, 'undraw' the atoms by drawing
them again with color black.  Depending on how fast your screen clear is
(mine is fairly slow)  for numbers of atoms less than a hundred or so, this
is likely to  be considerably faster than a screen clear.

Be sure to try precomputing the positions with fastmol and saving the 
intermediate file.  On many machines, the floating point computations are the
bottleneck, and the animation can run quite a bit faster.

Routine         Number of arguments

cleargp         0  Clears the current (visible) screen.
mginit          2  Initializes graphics display. (No important function)
mgimodfunc      3  Sets the graphics write mode to overwrite (as opposed to
			anding, oring or xoring new bits in).  Just a simple
			replacement.
mgicm           2  Sets up color map entries.  I use two colors only, black and
			green.
mgihue          1  Sets the current draw color to the given color map entry.
			(I.E. I draw the atoms in green).
mgifb           2  Performs the frame buffer switch.  Note that for b = 1 or 2,
			the function 3 - b switches from 1 to 2 to 1 to 2 etc.
mgiclearpln     3  Fills the indicated planes of the indicated frame buffer to
		   the data given. 0 as data clears the screen completely.
		   -1 clears all planes.  This is called to clear the 'back' 
		   frame buffer.  
mgibox          4  Draws a box at x1,y1,x2,y2.  I use this to draw atoms as
			2x2 pixel blocks since individual pixels are too small.
mgideagp        0  Deassign graphics processor.  Housekeeping call at end of
			program.  No important function.


Further notes:
	I mentioned some modifications to the program.  If you create a data
file of a matrix of atoms about 0.25 apart, they will oscillate like I said, but
may be too small to see the detail.  I changed display.c to multiply the x and y
coordinates to fill the screen with just the range x=0,50 y=0,50 or so.  The
input data points will have to be set in this smaller range so that the atoms
are on screen.   The 'cooling' function is done by adding a multiplier less than
1.0 in the molecule.c program at the point where after the forces are summed, 
the acceleration and velocity is being calculated.

COOLING:  program fastmol.c  changes:

#define COOL 0.97000                   /* Added definition */

/* Now resolve forces */
for(a=0;a<=maxindex;a++)
	for(c=0;c<2;c++)
	{
	atomv[a][c]= COOL*dt*atomf[a][c]/mass+atomv[a][c];   /* Changed line*/
	atompos[a][c]=(dt*atomv[a][c])+atompos[a][c];
	}

Expanding display:

	for(i=0;i<=maxnum;i++)
		{
		/*scanf("%d %d\n",&x,&y);*/
		read(0,idat,4);
		mgibox(idat[0]*4,idat[1]*4,idat[0]*4+2,idat[1]*4+2); /*changed*/
		}

		Greg

	..decvax   !
	..pyramid  !..abvax!gfs
	..mandrill !