PDA

View Full Version : uuughhh! i really need help with transform feedback



a-sylum
09-11-2015, 01:23 PM
okay so the other day i got transform feedback working in a little driver i wrote. today i tried to use for lighting in a little engine i wrote. so i wrote the first piece of code for lighting where i try to pass the objects geometry through a shader that translates them into world coordinates and it didn't work. it just does'nt give me any feedback. so here is the relevant code for the driver (that works)

if (glewInit() != GLEW_OK)
{
printf("glew could not load");
exit(1);
}

const GLchar *varyings[] = {"colorstfout"};

GLfloat feedback[12];

glClearColor(0, 1, 0, 1);

const char *vshaderambient =
"#version 430 core\n"
//"uniform vec4 colorobject;\n"
//"uniform vec4 colorlight;\n"
//"uniform float intensity;\n"
"out vec4 colorstfout;\n"
"void main()\n"
"{\n"
"colorstfout = vec4(.79, .2, .5, 1);\n"
//"colorstfout = min(colorobject + (colorobject + colorlight * intensity), vec4(1, 1, 1, 1));\n"
"}\n";

GLuint progambient, vambient, tfo, buffercolorout;

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenTransformFeedbacks(1, &tfo);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tfo);

progambient = glCreateProgram();
vambient = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vambient, 1, &vshaderambient, NULL);
glCompileShader(vambient);
glAttachShader(progambient, vambient);

glLinkProgram(progambient);

glTransformFeedbackVaryings(progambient, 1, varyings, GL_SEPARATE_ATTRIBS);

glLinkProgram(progambient);

glUseProgram(progambient);

glGenBuffers(1, &buffercolorout);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, buffercolorout);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, 100, NULL, GL_STREAM_READ);

glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffercolorout); //, 0, sizeof(GLfloat) * 12);

glEnable(GL_RASTERIZER_DISCARD);

glBeginTransformFeedback(GL_TRIANGLES);

glDrawArrays(GL_TRIANGLES, 0, 3);

glEndTransformFeedback();

glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(feedback), feedback);

glFlush();

printf("\nfirst vertex: %f, %f, %f, %f\n", feedback[0], feedback[1], feedback[2], feedback[3]);
printf("\nsecond vertex: %f, %f, %f, %f\n", feedback[4], feedback[5], feedback[6], feedback[7]);
printf("\nthird vertex: %f, %f, %f, %f\n", feedback[8], feedback[9], feedback[10], feedback[11]);

and here is the relevant code for passing an object through my desired transforming shader
shader:
const char *vshadertransform =
"#version 430 core\n"
"layout (location = 0) in vec3 vPosition;\n"
"layout (location = 1) in vec3 vNormal;\n"
"out vec3 verticestfout;\n"
"out vec3 normalstfout;\n"
"uniform vec3 translatemesh;\n"
"uniform vec3 translatecam;\n"
"void main()\n"
"{\n"
//"verticestfout = vPosition - translatecam + translatemesh;\n"
"verticestfout = vec3(1, 1, 1);\n"
//"normalstfout = vec3(rotate * vec4(vPosition, 1));\n"
//"normalstfout = vNormal;\n"
"normalstfout = vec3(2, 2, 2);\n"
"}\n";

code:

GLuint vaobject, tfobject, elementbuffer;

GLuint colorbufferin, vertexbufferin, normalbufferin;
GLuint colorbufferout, vertexbufferout, normalbufferout;
GLuint vtransform;
GLuint progtransform;

GLfloat feedback[12];

glGenVertexArrays(1, &vaobject);
glBindVertexArray(vaobject);
glGenTransformFeedbacks(1, &tfobject);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tfobject);

progtransform = glCreateProgram();
vtransform = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vtransform, 1, &vshadertransform, NULL);
glCompileShader(vtransform);
glAttachShader(progtransform, vtransform);

glLinkProgram(progtransform);

glTransformFeedbackVaryings(progtransform, 2, varyingstransform, GL_SEPARATE_ATTRIBS);

glLinkProgram(progtransform);
glUseProgram(progtransform);

glGenBuffers(1, &vertexbufferout);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, vertexbufferout);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(mesh->MeshVertexList), NULL, GL_STREAM_READ);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vertexbufferout);

glGenBuffers(1, &normalbufferout);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, normalbufferout);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(mesh->MeshNormalList), NULL, GL_STREAM_READ);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 1, normalbufferout);

GLint loctranslatemesh = glGetUniformLocation(progtransform, "translatemesh");
GLint loctranslatecam = glGetUniformLocation(progtransform, "translatecam");

glUniform3fv(loctranslatemesh, 1, mesh->WorldCoords.vec);
glUniform3fv(loctranslatecam, 1, cam->Pos.vec);

glGenBuffers(1, &elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mesh->MeshIndexList), mesh->MeshIndexList, GL_STREAM_DRAW);

glGenBuffers(1, &vertexbufferin);
glBindBuffer(GL_ARRAY_BUFFER, vertexbufferin);
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->MeshVertexList), mesh->MeshVertexList, GL_STREAM_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glGenBuffers(1, &normalbufferin);
glBindBuffer(GL_ARRAY_BUFFER, normalbufferin);
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->MeshNormalList), mesh->MeshNormalList, GL_STREAM_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);

glEnable(GL_RASTERIZER_DISCARD);

glBeginTransformFeedback(GL_TRIANGLES);

//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, mesh->MeshNormalListSize * 3, GL_UNSIGNED_INT, 0);

glEndTransformFeedback();

glDisable(GL_RASTERIZER_DISCARD);

glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(feedback), feedback);

glFinish();

printf("\nfirst vertex: %f, %f, %f, %f\n", feedback[0], feedback[1], feedback[2], feedback[3]);
printf("\nsecond vertex: %f, %f, %f, %f\n", feedback[4], feedback[5], feedback[6], feedback[7]);
printf("\nthird vertex: %f, %f, %f, %f\n", feedback[8], feedback[9], feedback[10], feedback[11]);

glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
glDeleteTransformFeedbacks(1, &tfobject);

glBindVertexArray(0);
glDeleteVertexArrays(1, &vaobject);
notice when their are comments that is just me trying to simplify the program and find the problem.
the info being printed at the end of the code are all zero's

if someone is able to help me get the transform feedback working id be very grateful! :)
thanks

Dark Photon
09-12-2015, 08:53 AM
It could be multiple things, but I see you linking your program twice, one before setting the varyings, once after. IIRC you need to set the varyings before you link. So get rid of the first.

a-sylum
09-12-2015, 09:13 AM
okay i rewrote the code and used different values for the mesh i loaded through assimp. it seems to work now. and by the way i still have the second link and it works fine, you can link multiple times you only have to link after setting the varyings and before calling glBeginTransformFeedback(*). thanks though :)