ParticlesTransformFeedback is sample C++ code illustrating the use of Modern OpenGL. It is part of a set of programs introducing the use of Modern OpenGL, which are intended to accompany a possible second edition of the book 3D Computer Graphics: A mathematical approach with OpenGL, Cambridge University Press, 2003.

ParticlesTransformFeedback illustrates the use of a Transform Feedback Buffer to update a large scale particle system. The particles undergo a simple physics simulation based on a gravitational attraction towards the mouse. The image above shows part of a simulation of 4,000,000 particles attracted towards the moving white square.

For information on how this program uses Transform Feedback in a C++ program with GLSL shaders, see the description of how Transform Feedback is implemented in ParticlesTransformFeedback.

The part of the code that handles the Transform Feedback consists of a single C++ source file, ParticlesTransformFeedback.cpp, and one of GLSL shader code, ParticlesTransformFeedback.glsl. It also uses GlShaderMgr.cpp and GlShaderMgr.h; these are part of the GlShaderMgr software package.To download the ParticlesTransformFeedback files:

What is in the ParticlesTransformFeedback program:

  1. When you run the program, you will see a field of red, namely a 2000x2000 array of 4,000,000 red particles. Initially it is frozen.
  2. Hit the space bar to start the physics simulation. After a few seconds, the particles more towards the mouse position. As the particles move in, try moving the mouse around to see how it affects the particles. The results will look better in full screen mode, so the individual particles will become visible. The particles are attracted towards the mouse by a gravitational force.
  3. Repeatedly hitting the space bar starts and stops the simulation. Using the 's' command single-steps the simulation.
  4. Pressing 'r' resets the particles' positions and velocities.
  5. The program has variable N. Change this value to be smaller than 2000 to see a smaller field of particles, namely NxN many particles.
  6. The purpose of the program is to illustrate the simplest usage of a Transform Feedback Buffer to let the GPU handle a large of independent calculations and carry the results from frame to frame without having to transfer the data back to the C++ program.
  7. It uses a call to glDrawTransformFeedback render the correct number of particles from the Transform Feedback Buffer.
  8. The program does not use glEnable(GL_RASTERIZER_DISCARD). Instead it both perform transform feedback, and renders, in a single shader program.
  9. Unrelated to transform feedback, the GLSL code illustrates using the variable gl_PointSize to set the size of points when drawing with GL_POINTS.
  10. For detailed programming explanation, see the description of how Transform Feedback is implemented in ParticlesTransformFeedback.

Version 1.0 of ParticlesTransformFeedback. Last updated, May 22, 2020.