1

I'm trying to make wide line with width > 10px using this topic as example https://forum.libcinder.org/topic/smooth-thick-lines-using-geometry-shader. But i'm stuck with a problem: trivial perspective division

gl_Position = vec4(gl_PositionIn[0].xy/gl_PositionIn[0].w + width, 0, 1);

gives me undesireable behavior for vertecies in front of near plane (w < 0).

Is it possible to calculate correct screen space coordinates for these vertices? Alternatively coordinates can be calculated as

float w = 1.0;
if(gl_PositionIn[0].w < 0)
    w = - 1.0;
gl_Position = vec4(gl_PositionIn[0].xy/abs(w) + width, 0, w);

However it makes line perspective, but i want line to be constant wide. Can it help to disable perspective interpolation for gl_Position? Is it some way to disable (or compensate) it in #version 120 shader?

BDL
  • 21,052
  • 22
  • 49
  • 55
  • You might want to read [this](http://stackoverflow.com/questions/41085117/why-does-gl-divide-gl-position-by-w-for-you-rather-than-letting-you-do-it-your/41085569#41085569) and [this](http://stackoverflow.com/questions/41952225/why-clipping-should-be-done-in-ccs-not-ndcs) – BDL Apr 05 '17 at 09:33
  • Thank you for links, i'll read it carefully, but main idea is clear: "it won't work". Do you know any algorithm for drawing thick lines as if it drawed glLineWidth? I'd be appreciate. – saul_tarvitz Apr 05 '17 at 10:43
  • Unluckily no, that's why I didn't post the links as an answer. Just wanted to give you the theoretical hint why the initial version doesn't work. – BDL Apr 05 '17 at 10:47
  • Ok, thanks for hints. – saul_tarvitz Apr 05 '17 at 11:20
  • I don't see why you divide by `w` at all for this. Just applying some screen space offset can as well be expressed in homogenous clip space directly (multiply `width` by `w` instead of dividing `gl_Position` by it). It is unclear if the rest of your code does rely on the fact that the perspective is already removed, but this will produce the correct screen space coordinates for the vertices, no matter matter where thee end points lie. – derhass Apr 05 '17 at 18:18
  • I tried to multiply width by w, but in that case line width is not constant. I think the reason for this is that positions are interpolated by perspective interpolation. I can disable it for varying by extension but not for gl_Postion in #version 120 shader – saul_tarvitz Apr 06 '17 at 06:32
  • `gl_Position` isn't interpolated at all (so there is no perspective correction). Since the actual source code is unknown, I cannot tell what exactly is going on, but my suggestion above does actually work in principle. – derhass Apr 06 '17 at 18:35
  • I don't understand if so. We have `gl_FragCoord` position in fragment shader. How was it calculated? I will post some shader code tommorow. Thank you for helping. – saul_tarvitz Apr 06 '17 at 19:06
  • @derhass this is my current geometry shader https://pastebin.com/dUGDqTFr . I have perspective line between points with different `w` (1 and -1). Btw, according to docs, `gl_Position`s are interpolated so, i assume that this values are cliped and transformed to get gl_FragCoord. That was my point. (sorry, my english is not so good for such sentences) – saul_tarvitz Apr 07 '17 at 11:54
  • Not sure what "documentation" you are refering to. But the varyings are interpolated _for_ a specific fragment position, the position itself is not interpolated at all, but generated by the rasterizer by sampling the pixel raster. The only point `gl_Position` could be said to be interpolated is when generating new vertices due to clipping, but there is no perspective correction for that, as _there has not been any perspective distortion_ at that point. Clipping in clip space is perfectly linear (which is one main reason to use it). – derhass Apr 07 '17 at 21:31
  • As far as your shader is concerned, it shouldn't even compile, since it is returning a `vec2` for a function that should return a `vec3`. – derhass Apr 07 '17 at 21:33
  • Hmm, I thought `gl_FragCoord` is used for rasterization. As far i can understand your point it doesn't. But as you mention i meant `gl_Position` is interpolated for generating "not clipped" vertecies (although i dont understand how does this happen). Sorry about shader, it is incorrect. https://pastebin.com/rUvvcJiG new one. But i have tried to multiply by `w` without any division. Same effect. Thank you, i think about what am i doing wrong. – saul_tarvitz Apr 08 '17 at 05:27
  • Now i think, that my normal vector can be incorrect and additional vertecies are wrong. Last question. Can i get "not clipped" vertex by lineary interpolate `gl_Position`s for meet condition -w < x,y,z < w; w > 0? – saul_tarvitz Apr 08 '17 at 05:50

0 Answers0