View Full Version : problem with transform feedback for pushing an object through multiple lights

09-01-2015, 08:06 AM
hi, i know that questions where their is alot of code are frowned upon, but ive spent close to three hours trying to debug this and i can't figure out what is wrong with it. what i am trying to do is push an object through multiple light sources using transform feedback. basically i have a shader program designed for translating the objects vertices into world space coordinates. i then also try to capture that data back into some buffers without drawing anything (i use glEnable(GL_RASTERIZER_DISCARD)) i then pass the world coordinates through 1 ambient light source and capture the colors for each vertex. then i pass it through as many point and directional light sources (im gonna add spot when i debug this) as wanted and continue to capture the colors. i then do glDisable(GL_RASTERIZER_DISCARD) and draw the captured world coordinates with the captured colors. their is quite a bit of copying between bufers to. i copy the captured coordinates and colors to other buffers. i wont put all the code in here just one pass through my world coordinates program, one pass through the ambient program, one pass through the program that draws the captured data to the screen and the corresponding shaders.

GLint ibuffer, nbuffer, vbuffer;

GLuint vao[3];

glGenVertexArrays(3, vao);




glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, vertexbufferout);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(struct Type3D) * 3 * mesh->MeshIndexListSize, NULL, GL_DYNAMIC_COPY);
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 7, vertexbufferout, 0, sizeof(struct Type3D) * mesh->MeshIndexListSize);

glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, normalbufferout);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(struct Type3D) * 3 * mesh->MeshIndexListSize, NULL, GL_DYNAMIC_COPY);
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 8, normalbufferout, 0, sizeof(struct Type3D) * mesh->MeshIndexListSize);

glTransformFeedbackVaryings(progtransform, 2, varyingstransform, GL_SEPARATE_ATTRIBS);


glGenBuffers(1, &ibuffer);
glGenBuffers(1, &nbuffer);
glGenBuffers(1, &vbuffer);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(struct TypeVertexIndexList3) * mesh->MeshIndexListSize, mesh->MeshIndexList, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(struct Type3D) * mesh->MeshVertexListSize, mesh->MeshVertexList, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);

glBindBuffer(GL_ARRAY_BUFFER, nbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(struct Type3D) * mesh->MeshNormalListSize, mesh->MeshNormalList, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);

GLint translatemeshloc = glGetUniformLocation(progtransform, "translatemesh");
GLint translatecamloc = glGetUniformLocation(progtransform, "translatecam");

glUniform3fv(translatemeshloc, 1, mesh->WorldCoords.vec);
glUniform3fv(translatecamloc, 1, cam->Pos.vec);

glDrawElements(GL_TRIANGLES, 3 * mesh->MeshIndexListSize, GL_UNSIGNED_INT, (void *)0);

glBindBuffer(GL_COPY_WRITE_BUFFER, vertexbufferin);
glBufferData(GL_COPY_WRITE_BUFFER, sizeof(struct Type3D) * mesh->MeshIndexListSize, NULL, GL_STATIC_DRAW);
glBindBuffer(GL_COPY_READ_BUFFER, vertexbufferout);
//glBindBuffer(GL_COPY_WRITE_BUFFER, vertexbufferin);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, (GLintptr)0, (GLintptr)0, sizeof(struct Type3D) * mesh->MeshIndexListSize);

glBindBuffer(GL_COPY_WRITE_BUFFER, normalbufferin);
glBufferData(GL_COPY_WRITE_BUFFER, sizeof(struct Type3D) * mesh->MeshIndexListSize, NULL, GL_STATIC_DRAW);
glBindBuffer(GL_COPY_READ_BUFFER, normalbufferout);
//glBindBuffer(GL_COPY_WRITE_BUFFER, normalbufferin);

glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, (GLintptr)0, (GLintptr)0, sizeof(struct Type3D) * mesh->MeshIndexListSize);



if (lights->ambient != NULL)

glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, colorbufferout);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(struct Type4D) * 3 * mesh->MeshIndexListSize, NULL, GL_DYNAMIC_COPY);
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 9, colorbufferout, 0, sizeof(struct Type4D) * 3 * mesh->MeshIndexListSize);

glTransformFeedbackVaryings(progambient, 1, varyingsambient, GL_SEPARATE_ATTRIBS);


GLint colorobjectloc = glGetUniformLocation(progambient, "colorobject");
GLint colorlightloc = glGetUniformLocation(progambient, "colorlight");
GLint intensityloc = glGetUniformLocation(progambient, "intensity");

glUniform4fv(colorobjectloc, 1, mesh->RGBA);
glUniform4fv(colorlightloc, 1, lights->ambient->colorlight.vec);
glUniform1f(intensityloc, lights->ambient->intensity);

glDrawArrays(GL_TRIANGLES, 0, 3 * mesh->MeshIndexListSize);

glBindBuffer(GL_COPY_WRITE_BUFFER, colorbufferin);
glBufferData(GL_COPY_WRITE_BUFFER, sizeof(struct Type4D) * 3 * mesh->MeshIndexListSize, NULL, GL_STATIC_DRAW);
glBindBuffer(GL_COPY_READ_BUFFER, colorbufferout);
//glBindBuffer(GL_COPY_WRITE_BUFFER, colorbufferin);
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, (GLintptr)0, (GLintptr)0, sizeof(struct Type4D) * mesh->MeshIndexListSize);
printf("\n##An ambient light is needed\n");





GLint perspectiveloc = glGetUniformLocation(progdraw, "perspective");
GLint uvnloc = glGetUniformLocation(progdraw, "uvn");

glUniformMatrix4fv(perspectiveloc, 1, GL_FALSE, cam->ProjectionMat.marray);
glUniformMatrix3fv(uvnloc, 1, GL_FALSE, cam->UVNMat.marray);

glBindBuffer(GL_ARRAY_BUFFER, colorbufferin);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, 0, (void *)0);

glBindBuffer(GL_ARRAY_BUFFER, vertexbufferin);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);

glDrawArrays(GL_TRIANGLES, 0, 3 * mesh->MeshIndexListSize);

here are the shaders


const char *vshadertransform =
"#version 430 core\n"
"layout (location = 0) in vec3 vPosition;\n"
"layout (location = 1) in vec3 vNormal;\n"
"layout (location = 7) out vec3 verticestfout;\n"
"layout (location = 8) out vec3 normalstfout;\n"
"uniform vec3 translatemesh;\n"
"uniform vec3 translatecam;\n"
"void main()\n"
"verticestfout = vPosition - translatecam + translatemesh;\n"
"normalstfout = vNormal;\n"


const char *vshaderambient =
"#version 430 core\n"
"uniform vec4 colorobject;\n"
"uniform vec4 colorlight;\n"
"uniform float intensity;\n"
"layout (location = 9) out vec4 colorstfout;\n"
"void main()\n"
"colorstfout = min(colorobject + (colorobject + colorlight * intensity), vec4(1, 1, 1, 1));\n"

drawing program

const char *vshaderdraw =
"#version 430 core\n"
"uniform mat4 perspective;\n"
"uniform mat3 uvn;\n"
"layout (location = 4) in vec3 verticestfin;\n"
"layout (location = 6) in vec4 colorstfin;\n"
"out vec4 color;\n"
"void main()\n"
"gl_Position = perspective * vec4(uvn * verticestfin, 1);\n"
"color = colorstfin;\n"

const char *fshaderdraw =
"#version 430 core\n"
"in vec4 color;\n"
"out vec4 colorout;\n"
"void main()\n"
"colorout = color;\n"

thanks alot :)
(btw the screen just shows the color that i cleared it to, it doesnt draw the image.)

09-01-2015, 10:26 AM
Are you actually calling glBeginTransformFeedback()?

09-01-2015, 10:46 AM
yeah i realized that after i posted this and added it but it doesn't seem to change things. i also get inconsistent results. i added a glFlush() and suddenly i got weirdly colored triangles on the screen but after my computer woke up from sleeping it no longer showed them. if noone can help i guess i'll just toss it and begin again with alot of testing before the final product which i didn't do when writing this. any ideas why though?

09-01-2015, 11:45 AM
any ideas why though?
Hard to say with so little information.

Have you tried disabling the GL_RASTERIZER_DISCARD to see what it's actually drawing? Bear in mind that errors can occur because of issues with rasterisation-related state (e.g. framebuffer completeness) even with GL_RASTERIZER_DISCARD in effect.

Have you tried dumping the transform-feedback buffers to a file and inspecting them? This could provide clues as to what's going on. Also, try initialising the buffer contents to a known value so that you can tell which portions are being overwritten.

09-01-2015, 01:24 PM
k ill look if need be, im just going to rewrite it thanks :)