0
\$\begingroup\$

With a GL_LINE, is there a way to have the vertex (or geometry) shader treat the first and second point differently?

i.e. In a line from p0 -> p1 I'd like to apply a different transform to p0 than to p1.

I know how to do this with a computer shader, but I was wondering if there is a way to do it with a vertex/geometry shader.

\$\endgroup\$
1
  • \$\begingroup\$ I lack experience with geometry shaders, but the vertex shader doesn't know which end of the line a particular vertex is (in fact in the most general case, a single vertex could be the start of multiple different lines AND the end of multiple other lines). You could obviously add markup to the vertices in the form of a texture coordinate or vertex colour to distinguish them, but I gather you want to use the existing topology information instead? \$\endgroup\$ Commented Oct 16, 2017 at 23:48

1 Answer 1

1
\$\begingroup\$

If, in your case, you want to treat all even numbered vertices different from all odd numbered vertices, then you could use gl_VertexID built in GLSL variable.

You could pass in two modelviewprojection matrices, one for type A and one for type B, and apply the one based on the least significant bit of gl_VertexID.

if ( gl_VertexID & 1 ) ...

If there is no fixed pattern for which vertex number gets which treatment, then you just need to add an additional vertex attribute like DMGregory proposed.

If you don't want to do that, and if you are passing 4-float coordinates with x,y,z,w you could even encode the special treatment in 'w', and reset it to '1' after detecting the special case. (Or even hack in a large offset in x to signal this. But that's the ugliest option.)

\$\endgroup\$
4
  • \$\begingroup\$ I tried gl_VertexID in my geometry shader, but I got an error saying it's not available in my current profile. I'm stuck using geometry shaders instead of vertex shaders right now because I'm using multiple viewports with geometry instancing. Anyways, I'm going to try your second suggestion. Thanks! \$\endgroup\$ Commented Oct 17, 2017 at 13:08
  • \$\begingroup\$ @Matt Are you targeting ES2? If so, that lacks gl_VertexId. If you are targeting ES3 or Core Profile, make sure you have a #version specified in your shader source. \$\endgroup\$ Commented Oct 17, 2017 at 16:14
  • \$\begingroup\$ @Bran I'm not 100% sure. I think I'm using the 4.3 core profile. I'm doing this through Qt, so basically I extend from QOpenGLFunctions_4_3_Core. I do begin my shaders with #version #430 I try to keep it pretty basic because I've had issues in the past where after getting stuff working on Linux, I'd move to Windows just to have it say it doesn't support a bunch of stuff. \$\endgroup\$ Commented Oct 17, 2017 at 16:24
  • \$\begingroup\$ @Matt: gl_VertexID is not available in geometry shaders. \$\endgroup\$ Commented Oct 18, 2017 at 1:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.