Render to VBO

Hi everybody,

I would like to render some new positions into a VBO. The output of my fragment shader should be interpreted as new vertices, not as colors. The best way I guess, is to use a geometry shader using transform feedback. Every vertex of the scene should generate a new vertex at another position.

Here is some my geometry shader:


void main()
{	
	vec3 offset = vec3(5.0, 5.0, 5.0);
	
	gl_Position = gl_PositionIn[0];
	EmitVertex();
	EndPrimitive();
	
	gl_Position = vec4(gl_PositionIn[0].xyz + offset, 1.);
	EmitVertex();
	EndPrimitive();
}

My Transform Feedback-Function:


void performFeedback()
{
	primitiveCount = numVertices;
	glBindBufferOffsetNV(GL_TRANSFORM_FEEDBACK_BUFFER_NV, 0, buffers[1-currentBuffer], 0);
	glBeginTransformFeedbackNV(GL_POINTS);
	{
		glEnable(GL_RASTERIZER_DISCARD_NV);
		glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN_NV, query);
		drawBuffer(buffers[currentBuffer], primitiveCount);
		glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN_NV);
		glDisable(GL_RASTERIZER_DISCARD_NV);
	}
	glEndTransformFeedbackNV();
	glGetQueryObjectuiv(query, GL_QUERY_RESULT, &primitiveCount);
	currentBuffer = 1 - currentBuffer;
}

Display-Function:


...
        glPushMatrix();
	glRotatef(yrot, 0.0, 1.0, 0.0);
	drawBuffer(buffers[currentBuffer], primitiveCount);
	glPopMatrix();

	glPushMatrix();
	glRotatef(yrot, 0.0, 1.0, 0.0);
	glUseProgram(programTF);
	performFeedback();
	glUseProgram(0);
	glPopMatrix();
...

I hope anybody can understand my problem and maybe some tipps or a solution of this problem…

Thanks and Greetings,

Thopil

First you should know that the transform feedback records the output of the geometry shader, if none is installed, of the vertex shader and NOT of the fragment shader.
Second you missed to set which varying variables of your shader should be recored into your VBO. I use output varyings I declare with glTransformFeedbackVaryingsNV. It would be also possible to record standard varyings like gl_Position but you would have to work yourself through the docs to get the correct usage pattern for that. Recording self defined varyings is quite easy.

Hi,

in the meantime I solved the problem from the past. But now I have a new problem with transform feedback and the geometry shader.
I am recording triangles with transform feedback, calculate a new position for every vertex, send it to the geometry shader and record the new triangle with transform feedback. But now is the new modell black and I would like to have a new modell illuminated.

How can I record beside gl_Position something like gl_Normal or at minimum the gl_FrontColor. In another post from this forum I found this:


int loc[] =
{
    glGetVaryingLocationNV(programid, "gl_Position"),
    glGetVaryingLocationNV(programid, "gl_FrontColor"),
};

glTransformFeedbackVaryingsNV(programid, 2, loc, GL_SEPARATE_ATTRIBS_NV);

but it doesn’t work. How can I set another VBO to read from transform feedback?

Thanks, thopil

You should write something like


glBindBufferBaseNV(GL_TRANSFORM_FEEDBACK_BUFFER_NV, 0, bufferPosition);
glBindBufferBaseNV(GL_TRANSFORM_FEEDBACK_BUFFER_NV, 1, bufferColor);

just before you begin feedback, provided that you have initialized the two buffers.
Refer to http://www.opengl.org/registry/specs/NV/transform_feedback.txt for more details if you’d like.

If you don’t want to display anything when performing the transform feedback, you could also use GL_RASTERIZER_DISCARD_NV

Thanks for your reply, but I forgot to mention that I have already write this instructions.

This is how I have setup my transform feedback:

void setupTransformFeedback()
{
        //count = number of triangles
	glGenBuffers(1, &tfvbo);
	glBindBuffer(GL_ARRAY_BUFFER, tfvbo);
	glBufferData(GL_ARRAY_BUFFER, count * 3 * 4 * sizeof(GLfloat), 0, GL_DYNAMIC_DRAW);

	glGenBuffers(1, &normalvbo);
	glBindBuffer(GL_COLOR_ARRAY, normalvbo);
	glBufferData(GL_COLOR_ARRAY, count * 3 * 4 * sizeof(GLfloat), 0, GL_DYNAMIC_DRAW);

	glBindBufferBaseNV(GL_TRANSFORM_FEEDBACK_BUFFER_NV, 0, tfvbo);
	glBindBufferBaseNV(GL_TRANSFORM_FEEDBACK_BUFFER_NV, 1, colorvbo);

	int loc[2] =
	{
		glGetVaryingLocationNV(shaderProgram, "gl_Position"),
		glGetVaryingLocationNV(shaderProgram, "gl_FrontColor"),
	};

	glTransformFeedbackVaryingsNV(shaderProgram, 2, loc, GL_SEPARATE_ATTRIBS_NV);
}

This is how I want to record from another VBO:


glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBeginTransformFeedbackNV(GL_TRIANGLES);
{
 glEnable(GL_RASTERIZER_DISCARD_NV);		
 glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN_NV, query);
 glVertexPointer(3, GL_FLOAT, 0, vertices);
 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 45.0f);
 renderModel();
 glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN_NV);
	glDisable(GL_RASTERIZER_DISCARD_NV);
}
glEndTransformFeedbackNV();

And this is my implementation in the render-method:


glPushMatrix();
glBindBuffer(GL_ARRAY_BUFFER, normalvbo);
glColorPointer(4, GL_FLOAT, 0, NULL);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, tfvbo);
glVertexPointer(4, GL_FLOAT, 0, NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, count * 12);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glPopMatrix();

Now I want to record the color (material) of the model with transform feedback to display the recorded VBO of the model with lighting and materials. But the model is always black and some triangles ends in infinity.

This is my simple geometry shader:


#version 120
#extension GL_EXT_geometry_shader4 : enable

void main()
{	
	for(int i = 0; i < gl_VerticesIn; i++) 
    {	
		gl_FrontColor = gl_FrontColorIn[i]; 
		gl_Position = gl_PositionIn[i]; 
		EmitVertex(); 
	} 
	
	EndPrimitive();	
}

I hope my problem seems better to understand.

Thanks, thopil

In setupTransformFeedback() you should use as a target for the normal buffer, GL_ARRAY_BUFFER and not GL_COLOR_ARRAY. Strange if it hasn’t given you any errors there. Try it & send feedback.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.