Create animation

Hi guys, I’m new here!
I’m coding with C, and I want to do a gravitational particles simulator.

I want to plot cartesian points and update their coordinates continuously, creating a animation.
I have no idea how to do this, I just know how to plot point with GL_POINTS, and have that:


~include things~

void display (void) { 
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0,10,0,10);
	glFlush();
}

int main (int argc, char **argv) {

        ~code things~

        glutInit(&argc, argv);
	
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(800,600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Particles simulator");
	glutDisplayFunc(display);
	glClearColor(0,0,0,0);
	glutMainLoop();


        return 0;
}

I tried to write each frame of the animation as:


        int i;
	glClear(GL_COLOR_BUFFER_BIT);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0,10,0,10);
	
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity();
	glBegin (GL_POINTS);
		glColor3f(1,1,1);
		for (i=0; i<n; i++) {
			calculateParticle(prtcl);
			glVertex2f(prtcl->coord[0], prtcl->coord[1]);
			prtcl = prtcl->next;
		}
	glEnd();
	
	glFlush();


I hope u guys can help me :smiley:

Use double buffer, this will improve things a bit.

Here is a tutorial about particle systems, but focused on newer OpenGL functionalities.

Here is another one.

And another one involving transform feedback.

Finally, this one is a bit old, but might be interesting for you since it uses glVertex and so on. It also uses textures so that you can make your particles don’t look like only like colored pixels… For this one don’t pay attention on how it sets the windowing, keep using glut.

get familliar with “vertex array object” and “buffer object” if you want a efficient way to render things (like particles)
to update / animate the particle system, there are different ways to do that:
– you update each particle in your c-application, which is not efficient
– you update the particles in your openGL shader code, thats the approach i would recommend

here is an good example:
web.engr.oregonstate.edu/~mjb/cs557/Handouts/compute.shader.1pp.pdf

… I just know how to plot point with GL_POINTS, …

This implies that you can currently open a GL window and display a scene with a bunch of points on the screen. Is that right?

no, updating the particles using the graphics card is much faster than doing it on the CPU
rendering and updating are 2 separate steps:
– use a “compute shader” to calculate the new particle attributes (position/velocity/color/lifetime/etc.)
– use a simple vertex/fragment shader to render colored points

Or if you want to target earlier versions of OpenGL (compute shaders were added in 4.3), you can achieve the same result with a vertex shader and transform feedback.

Or, if you’re animating points along a fixed trajectory, you can just compute the position in the vertex shader during rendering. Transform feedback just allows you to save the updated state for use in the next frame.

Separating update from rendering is useful mainly if you’re updating incrementally and there are relatively complex interactions between particles requiring multiple steps and intermediate data structures.