0
\$\begingroup\$

In my openGL project I draw a lot of quads (composed from 2 triangles) - for sake of example let's say quad 0,1,2 and 3. I process them in single pass. I do transform them with projection view matrix in Vertex shader.

Is there then any way in Fragment shader to tell in which quad (0,1,2,3) I am currently drawing the pixel?

\$\endgroup\$
3
  • \$\begingroup\$ Have you considered using the provoking vertex id for this? \$\endgroup\$
    – DMGregory
    Oct 8, 2021 at 12:28
  • \$\begingroup\$ No I haven't! Reading. \$\endgroup\$ Oct 8, 2021 at 12:29
  • \$\begingroup\$ @DMGregory I have read it trough now x times, but do not understand how this solves my particular problem. Maybe you can try give an answer with some example code? \$\endgroup\$ Oct 9, 2021 at 6:59

2 Answers 2

1
\$\begingroup\$

I have not done this myself, but something in this vein should work:

Define a varying variable to send the vertex ID from the vertex shader to the fragment shader. Use the flat attribute so that it's not interpolated between vertices — instead the value from the provoking vertex will be used for all fragments in the triangle.

flat out int vId;
shader void main() { 
    vId = gl_VertexID;
}

Then in your fragment shader you can read this ID and divide it down (rounding down) to identify the quad:

flat in int vId;
void main() {
    int quadId = vId/4;
    // TODO: do something with quadId
}

Assuming your first quad is made up of vertices 0-3 in your buffer, this will round down to 0 for all fragments in that quad. If your next quad is made up of vertices 4-7, this will round down to 1 for those fragments, and so on.

\$\endgroup\$
1
  • \$\begingroup\$ Will try this ASAP and give feedback. \$\endgroup\$ Oct 10, 2021 at 6:44
1
\$\begingroup\$

You could have a vertex attribute per quad, and send that as a varying to your fragment shader.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .