Hello,
I’m currently working on a project where users can model a 3D mesh. Right now I’m trying out different methods of marking selected triangles. The method I’m trying right now is creating two different FBOs and in one of them I output the gl_PrimitiveID in the fragmentShader. I then read select pixels from this FBO and if there where any triangles used to render that pixel those triangles are rendered with a different color in the 2nd FBO.
The problem I’m having is that gl_PrimitiveID always is zero in the fragment shader. I have not created or attached a geometry shader to the shader programs im using.
Here is a short example that i wrote outside of the project;
//FBO///////////////////////////////////////////////////////////////////////////////////////////////
GLuint color_tex;
glGenTextures(1, &color_tex);
glBindTexture(GL_TEXTURE_2D, color_tex);
//NULL means reserve texture memory, but texels are undefined
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 256, 256, 0, GL_RGB, GL_FLOAT, NULL);
GLuint fb;
//-------------------------
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
//Attach 2D texture to this FBO
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_tex, 0);
GLuint depth_tex;
//-------------------------
glGenTextures(1, &depth_tex);
glBindTexture(GL_TEXTURE_2D, depth_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 256, 256,
0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
depth_tex, 0);
// Disable reading to avoid problems with older GPUs
glReadBuffer(GL_NONE);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
//------------------------
//Does the GPU support current FBO configuration?
GLenum status;
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch (status)
{
case GL_FRAMEBUFFER_COMPLETE:
cout << "good" << endl;
break;
default:
cout << "bad";
}
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//-------------------------
/////////////////////////////////////////////////////////////////////////////////////////////////
//RENDER LOOP /////////////////////////////////////////////////////////////////////////////////////
while (!glfwWindowShouldClose(window)) {
//FIRST FBO-----------------------------------------------
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, fb);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, 256, 256);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glUseProgram(primitiveShader.programID);
glUniformMatrix4fv(locationPP, 1, GL_FALSE, P);
MVstack.push();
MVstack.translate(box.getPosition());
glUniformMatrix4fv(locationPMV, 1, GL_FALSE, MVstack.getCurrentMatrix());
box.render();
MVstack.pop();
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER, fb);
glReadBuffer(GL_COLOR_ATTACHMENT0);
float pixels[3];
float pixel;
cout << pixels[0] << " " << pixels[1] << " " << pixels[2] << endl;
glReadPixels(128, 128, 1, 1, GL_RGB, GL_FLOAT, &pixels);
glReadPixels(128, 128, 1, 1, GL_BLUE, GL_FLOAT, &pixel);
cout << pixel << endl;
glReadBuffer(GL_NONE);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
glUseProgram(phongShader.programID);
//GLFW WINDOW-----------------------------------------
GLRenderCalls();
setupViewport(window, P);
glUseProgram(phongShader.programID);
glUniformMatrix4fv(locationP, 1, GL_FALSE, P);
MVstack.push();
MVstack.translate(box.getPosition());
glUniformMatrix4fv(locationMV, 1, GL_FALSE, MVstack.getCurrentMatrix());
box.render();
//this is what the render function looks like
//void Box::render() {
// glBindVertexArray(vao);
// glDrawElements(GL_QUADS, nverts, GL_UNSIGNED_INT, (void*)0);
// glBindVertexArray(0);
//}
MVstack.pop();
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
vertex Shader;
#version 330
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
uniform mat4 MV;
uniform mat4 P;
void main ()
{
//! Convert position to clip coordinates and pass along to fragment shader
gl_Position = (P * MV) * vec4(VertexPosition, 1.0);
}
Fragment shader;
#version 330
out vec3 FragColor;
void main () {
FragColor = vec3(0.0f, 1.0f, float(gl_PrimitiveID + 1));
}
Thankful for any help,
Bonepower