0
\$\begingroup\$

Presumably the problem is with the internal formats and my understanding of that, but I've been stuck a while, got this sort of approaching working fine on a more straight forward colour picker, but here I'm picking pos coordinates stored as data at locations.

So I have a fbo, a texture2d attachment in color 0 and essentially I pass my vertices (XYZ) (GLfloat) to the vertex shader in location 0 'position'.

Since my terrain is a square always, 100x100 3000x3000 etc, I only need the X and Z since they will all be unique, so I out a vec2 of the position.XZ to the fragment shader and then in the fragment shader I do this on an if statement

frag
...
layout(location = 0) out vec4 FragColor;
layout(location = 1 )out vec4 FragColor2;

main...
if (terrainEditMode == 1)
{
   
   
   FragColor2 = vec4(vecIDs.x, vecIDs.y, 0.1,0.1);
}

I set up the attachment like this

glGenFramebuffers(1, &terrainPickFBO);
        glBindFramebuffer(GL_FRAMEBUFFER, terrainPickFBO);
        glGenTextures(1, &terrainPickTexture);
        glBindTexture(GL_TEXTURE_2D, terrainPickTexture);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window_width, window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, terrainPickTexture, 0);

and read pixels on the bound fbo

GLuint pickpixelColor[4];
    glReadBuffer(GL_COLOR_ATTACHMENT1);
    //glReadPixels(xpos, ypos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColor);
    glReadPixels(static_cast<GLint>(xpos), static_cast<GLint>(ypos), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pickpixelColor2);

Then render:

glBindFramebuffer(GL_FRAMEBUFFER, terrain.terrainPickFBO);
GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(1, drawBuffers);
terrain.render();
\$\endgroup\$
4
  • 2
    \$\begingroup\$ So .. where's the question, what does not work well in your approach? \$\endgroup\$
    – Kromster
    Jan 27 at 13:02
  • \$\begingroup\$ I am also unsure what the question is; you mention "picking" in the title, are you asking how to see which vertex the mouse is over (something like a raycast?) or are you asking something else? \$\endgroup\$
    – Charanor
    Jan 28 at 9:34
  • \$\begingroup\$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. \$\endgroup\$
    – Charanor
    Jan 28 at 9:34
  • \$\begingroup\$ Thanks. Right now with my above approach the values I get from picking the locations with my mouse don't reflect the pos.xz that I send over from the shader to the fragment out /colour attachment. So I was asking did I miss something obvious. ty \$\endgroup\$
    – Paul
    Jan 28 at 19:05

0

You must log in to answer this question.

Browse other questions tagged .