Hey!
I’ve read http://www.opengl.org/registry/specs/NV/transform_feedback.txt which helped alot I think.
I’ve also studied the latter transform feedback example and tried to do something similar. In the example no ping-ponging takes place, right?
The thing is the program compiles but crashes almost immediately when started, with no error message. I’ve tried to find the problem but with no luck.
Hopefully, a kind soul out there can help me.(maybe someone else than DmitryM who’ve done too much for me already.)
I know it’s really hard to put your mind into new code but I’ll paste some interesting parts of the code below:
//Program defined as:GLuint program;
program = glCreateProgram();
glAttachShader( program, vertexShader );
glAttachShader( program, fragmentShader );
//Set gl_Position to be fedback.Done before linking.
GLchar const * Strings[] = {"gl_Position"};
glTransformFeedbackVaryings(program, 1, Strings, GL_SEPARATE_ATTRIBS);
glLinkProgram( program );
Inilization of the two identical “ping-pong” buffers.
glGenBuffers(1, vTransformParticleBuffer);
glBindBuffer(GL_ARRAY_BUFFER, *vTransformParticleBuffer);
glBufferData(GL_ARRAY_BUFFER, MaxNrOfParticles*3*sizeof(float) , 0, GL_DYNAMIC_COPY);
glGenBuffers(1, vFeedbackParticleBuffer);
glBindBuffer(GL_ARRAY_BUFFER, *vFeedbackParticleBuffer);
glBufferData(GL_ARRAY_BUFFER, MaxNrOfParticles*3*sizeof(float) , 0, GL_DYNAMIC_COPY);
//Check if error occured
GLuint errorID = glGetError();
if (errorID != GL_NO_ERROR)
{
printf("OpenGL error2: %s
", gluErrorString(errorID));
printf("Attempting to proceed anyway. Expect rendering errors or a crash.
");
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
And the code for drawing and ping-ponging. Query code commented because I don’t know if I need it when doing it this way.
glUseProgram( program );
//GLuint Query;
//glGenQueries(1,&Query);
//Pointsprite related code.
//----------------------------------------------------
GLuint pointSize = 20;
glPointSize(pointSize);
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE);
glBindTexture(GL_TEXTURE_2D, texID); // Activate texture
glEnable(GL_POINT_SPRITE_ARB);
glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB,
GL_TRUE);
//----------------------------------------------------
if (ping) //declared as GLboolean ping = GL_TRUE;
{
currentBuffers[0] = vFeedbackParticleBuffer;
currentBuffers[1] = vTransformParticleBuffer;
ping = GL_FALSE; //ping == pong :D
}
else
{
currentBuffers[0] = vTransformParticleBuffer;
currentBuffers[1] = vFeedbackParticleBuffer;
ping = GL_TRUE;
}
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, currentBuffers[0]);
glBindBuffer(GL_ARRAY_BUFFER, currentBuffers[1]);
//glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, Query);
//Spawns 9 particles to the bound buffer only first time.
//Implemented to later be able to spawn particles with a push of a button.
if (spawnParticles)//GLuint spawnParticles = GL_TRUE;
{
//int index =0; declared elsewhere.
glBufferSubData(GL_ARRAY_BUFFER, index, 9*3*sizeof(float), particles2);
index=9*3*sizeof(float);
spawnParticles = GL_FALSE;
*nrOfParticles += 9;
}
glBeginTransformFeedback(GL_POINTS);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, *nrOfParticles);
glDisableClientState(GL_VERTEX_ARRAY);
glEndTransformFeedback();
//glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glBindBuffer( GL_ARRAY_BUFFER, 0);
glDisable(GL_POINT_SPRITE_ARB);
glBindTexture(GL_TEXTURE_2D, 0); // Deactivate the texture again
glDisable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ZERO);
glUseProgram(0);
}
What I think this code should do:
First time it binds BindBufferBase to the feedbackbuffer and then I bind the transFormbuffer to be able to draw its data. Because I set the bindBufferBase to the feedbackbuffer the particle positions,gl_Position, after the vertex shader(no geometry shader), should be saved to the feedbackbuffer. And the second time the draw-function is used the transformbuffer is bound to bindBufferBase and the content of the feedbackBuffer should be drawn into the transformbuffer. And so on and on.
I’ve done some simple std::cout before functions and after functions etc, to find where the program crashes and the “first” crash is at:
glTransformFeedbackVaryings(program, 1, Strings, GL_SEPARATE_ATTRIBS);
Which is the first call to any tranform feedback related function.
I greatly appriciate any answers.
//Englesson