Getting ready

On the OpenGL side, we need a buffer for the position of the particles and a buffer for the velocity. Create a buffer containing the initial positions of the particles and a buffer with zeroes for the initial velocities. We'll use four component positions and velocities for this example in order to avoid issues with data layouts. For example, to create the buffer for the positions, we might do something as follows:

vector<GLfloat> initPos; 
 
... // Set initial positions 
 
GLuint bufSize = totalParticles * 4 * sizeof(GLfloat); 
 
GLuint posBuf; 
glGenBuffers(1, &posBuf); 
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, posBuf); 
glBufferData(GL_SHADER_STORAGE_BUFFER, bufSize, &initPos[0], 
               GL_DYNAMIC_DRAW); 

Use a similar process for the ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.