Greetings:
I am trying to write a simple program to understand transform feedback: two spheres move randomly in world space and when they collide some effect happens. I want to use TF for collision detection which is simple in this case: the spheres collide if the distance between their centers <= sum of their radii.
The code to move the spheres works fine. Below is the TF part I’ve got to so far with the idea at each time step of first outputting the two centers to the TF buffer and checking the distance between them, then if there is no sphere collision draw normally, if there is then do effect.
Main:
init(void)
{...
static GLuint TFId, TFBuffer;
...
static const char* varyings[] = {"center0", "center1"};
...
glGenTransformFeedbacks(1, &TFId);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, TFId);
glGenBuffers(1, &TFBuffer);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, TFBuffer);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, 1024, NULL, GL_DYNAMIC_COPY);
glTransformFeedbackVaryings(programId, 2, varyings, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(programId);
glUseProgram(programId);
...}
draw(void)
{...
glUniform1ui(sphereNoLoc, 0);
glMultiDrawElements(GL_TRIANGLE_STRIP... draws sphere 0);
...
glUniform1ui(sphereNoLoc, 1);
glMultiDrawElements(GL_TRIANGLE_STRIP... draws sphere 1);
...}
Vertex shader:
...
out vec3 center0, center1;
...
// The uniform sphereNo is set to 0 before first sphere is drawn, 1 before second.
if (sphereNo == 0) center0 = vec3(modelViewMat * vec4(0.0, 0.0, 0.0, 1.0));
if (sphereNo == 1) center1 = vec3(modelViewMat * vec4(0.0, 0.0, 0.0, 1.0));
Now, I have a bunch of questions:
-
Is the TF part correct so far? How do I complete it? I know there’s bunch of statements missing like glBegin/EndTransformFeedback(), maybe glBindBufferBase(), etc. I would really appreciate being told exactly how to arrange these given the objective of collision detection.
-
Particularly, how do I access the values of center0 and center1 in the TF buffer to find the distance between them? (The red book has an example where they seem to use a texture buffer as TF and texelFetch() to randomly access it but don’t say how to set it all up; in any case, I would rather not use a texture buffer if it can be avoided.)
-
If my understanding of how a vertex buffer works is correct, then there is an obvious inefficiency in the code above: the same value of centeri is going to be written into the TF for every vertex of the current sphere. How to avoid this?
Answer to these questions and any other comments you might have would be highly appreciated.
Thanks a lot,
Sam