PDA

View Full Version : Create animation



mqzabin
09-15-2016, 09:29 PM
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 :D

Silence
09-16-2016, 02:04 AM
Use double buffer (https://www.cs.arizona.edu/classes/cs433/spring02/opengl/dblbuffer.html), this will improve things a bit.

Here (http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/particles-instancing/) is a tutorial about particle systems, but focused on newer OpenGL functionalities.

Here (http://www.3dgep.com/simulating-particle-effects-using-opengl/) is another one.

And another one (http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=26) involving transform feedback.

Finally, this one (http://nehe.gamedev.net/tutorial/particle_engine_using_triangle_strips/21001/) 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.

john_connor
09-16-2016, 01:36 PM
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

Carmine
09-16-2016, 03:16 PM
... 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?

Cabeceira
09-21-2016, 11:45 AM
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

Ok. you mean by updating the particles in openGL shader code can help in rendering things?

john_connor
09-21-2016, 03:47 PM
Ok. you mean by updating the particles in openGL shader code can help in rendering things?

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

GClements
09-21-2016, 07:21 PM
-- use a "compute shader" to calculate the new particle attributes (position/velocity/color/lifetime/etc.)

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.