openglshaderopengl-3fbomouse-picking

Shader doesn't write anything for picking by id


I am trying to implement picking by an int id, but it looks like my shader doesn't write anything, althought I can read the clear color properly.

vs, I skipped completely any matrix for debugging:

#version 330

#include semantic.glsl

layout (location = POSITION) in vec3 position;

uniform Transform0
{
    mat4 view;
    mat4 proj;
    mat4 viewProj;
};

uniform Transform1
{
    mat4[TRANSFORM1_SIZE] models;
};

uniform Parameters
{
    // x = mesh baseVertex
    // y = selected
    // z = active
    // w = id
    ivec4[INDICES_SIZE] params;
};

out Block
{
    flat int id;
} outBlock;

int getIndex()
{
    int iBegin = 0;
    int iEnd = params.length() - 1;

    int l = iBegin;
    int r = iEnd;

    int i = 0; 

    if(params.length > 1)
    {
        do 
        {
            i = int(((l + r) / 2.0f));

            if (l == (r - 1)) 
                if (l == 0 && gl_VertexID <= params[l].x || gl_VertexID <= params[l].x && gl_VertexID > params[l - 1].x)
                    return l;
                else if(gl_VertexID > params[l].x && gl_VertexID <= params[r].x)
                    return r;
                else 
                    return 0;
            else if (gl_VertexID == params[i].x) 
                return i;
            else if (gl_VertexID < params[i].x) 
                r = i;
            else if (gl_VertexID > params[i].x)
                l = i;
        } while (l < r);
    }    
    return 0;    
}

void main() 
{
    int index = getIndex();

    mat4 model = models[index];

    //gl_Position = proj * (view * (model * vec4(position, 1)));

    gl_Position = vec4(4.0 * float(gl_VertexID % 2) - 1.0, 4.0 * float(gl_VertexID / 2) - 1.0, 0.0, 1.0);

    outBlock.id = params[index].w;
}

fs, hardcoded value for the moment, output is 0 (FRAG_COLOR), tried also 1 but nothing:

#version 330

#include semantic.glsl

// Outgoing final color.
layout (location = FRAG_COLOR) out int outputColor;

in Block
{
    flat int id;
} inBlock;

void main() 
{    
    //outputColor = inBlock.id;
    outputColor = 9;
}

Init phase, I have one fbo, called RESOLVE, with 3 attachements, one depth, one float color on 0 and one integer for picking on 1. Fbo is complete:

gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(Texture.RESOLVE_ID));
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl3.glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, EC.viewer.size.x, EC.viewer.size.y, 0, GL_RED_INTEGER, GL_INT, null);

gl3.glGenFramebuffers(Framebuffer.MAX - (samples == 1 ? 1 : 0), framebufferName);

gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(Framebuffer.RESOLVE));
gl3.glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, textureName.get(Texture.RESOLVE_DEPTH), 0);
gl3.glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureName.get(Texture.RESOLVE_COLOR), 0);
gl3.glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, textureName.get(Texture.RESOLVE_ID), 0);

if (gl3.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    System.err.println("incomplete");
    return false;
}

and render & reading, added also glFlush, glFinish and glPixelStorei but nothing:

gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(Framebuffer.RESOLVE));
gl3.glDrawBuffer(GL_COLOR_ATTACHMENT1);
gl3.glClearBufferiv(GL_COLOR, 0, clearId.put(0, 0)); // we care clearing only red

gl3.glBindVertexArray(EC.meshManager.vertexArrayName.get(0));

gl3.glEnable(GL_DEPTH_TEST);

gl3.glUseProgram(program.name);

gl3.glDrawElements(GL_TRIANGLES, EC_Gl3MeshManager.ELEMENT_OPAQUE_COUNT, GL_UNSIGNED_INT, 0);

gl3.glDisable(GL_DEPTH_TEST);

gl3.glReadBuffer(GL_COLOR_ATTACHMENT1);

glm.vec._2.i.Vec2i window = new glm.vec._2.i.Vec2i(
            EC.inputListener.mousePressed.getX(),
            EC.viewer.size.y - EC.inputListener.mousePressed.getY() - 1);
System.out.println("window (" + window.x + ", " + window.y + ")");

gl3.glFlush();
gl3.glFinish();
gl3.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

//  Get the red coordinate reading the pixel click from the color 1 buffer, we can use the clearId buffer        
gl3.glReadPixels(window.x, window.y, 1, 1, GL_RED_INTEGER, GL_INT, clearId);
gl3.glReadBuffer(GL_COLOR_ATTACHMENT0);

As I said, if I clear with 10, I read 10, so that is working, the only problematic thing is my shader..

Can you spot the error, guys?

Edit: trying to debug, I am checking the attachment type on color 1

gl3.glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
        GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, clearId);
System.out.println("GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE: " + clearId.get(0));
gl3.glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
        GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, clearId);
System.out.println("GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME: " + clearId.get(0)+", tex: "+textureName.get(Texture.RESOLVE_ID));

I get:

GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE: 5890
GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME: 14, tex: 14

5890 is GL_TEXTURE, name looks correct

tried to use glFramebufferTexture2D, but I still get GL_TEXTURE type instead GL_TEXTURE_2D

Edit 2: trying to read the depth component, is always 0


Solution

  • Shame on me, the problem was the depth test, since I forgot to clean the buffer it was never passing