I've been trying to implement a particle simulation engine which computes forces between particles on GPU using CUDA and visualizes it using openGL. I've been stuck for some time on the part where I want to update my vertex buffer without copying it back to CPU. The problem seems to be in the way I'm binding/ passing the vertex buffer.
Here is the fragment of code that is responsible for creating VAO and VBO and drawing it to the screen:
// pointer to array on gpu
float* d_particlePositions;
// Creating a vertex array object
GLuint vertexArrayObject;
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
// Creating a vertex buffer object for N points with 3 floats
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 3 * N * sizeof(float), NULL, GL_DYNAMIC_COPY);
cudaGLRegisterBufferObject(vertexBuffer);
// rendering loop
while(running){
cudaGLMapBufferObject((void **) &d_particlePositions, vertexBuffer);
// call some kernel functions like
update<<<x,y>>>(d_particlePositions, ...);
cudaGLUnmapBufferObject(vertexBuffer);
// bind VAO, VBO and draw to screen
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
glDrawArrays(GL_POINTS, 0, N);
}
I know that my openGL setup is right because i got it working, but in case when every cycle I had to copy it back to CPU and create a new VBO. If you have some resources that can help me solve this I' highly appreciate it.