Board index FlightGear Development Effects and shaders

Geometry shader for cliffs

An exciting "new" option in FlightGear, that includes reflections, lightmaps, the particle system etc.. A lot is yet to be discovered/implemented!

Geometry shader for cliffs

Postby frtps » Thu Oct 17, 2019 8:50 am

I am interested in writing a geometry shader for cliffs. I'm pretty sure I could take an existing FG geometry shader and just tweak it to not use lat/long coords. However, I'm a bit vague as to how a geometry shader works, perhaps one of you could point me at an appropriate geometry shader in FG that I could use as a starting point and study guide? A bit of an explanation wouldn't hurt either.
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby Thorsten » Thu Oct 17, 2019 9:00 am

What exactly do you want to do? There's recently been a terragear/shader combo in development for cliffs which looks pretty useful to me.

Conceptually a geometry shader generates additional triangles - I'd assume a vertex shader which displaces vertices might be better suited to get you a steeper mesh (?) - so perhaps you could supply a bit more info on what you have in mind?
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Re: Geometry shader for cliffs

Postby frtps » Thu Oct 17, 2019 11:19 am

frtps wrote in Thu Oct 17, 2019 8:50 am:I am interested in writing a geometry shader for cliffs. I'm pretty sure I could take an existing FG geometry shader and just tweak it to not use lat/long coords. However, I'm a bit vague as to how a geometry shader works, perhaps one of you could point me at an appropriate geometry shader in FG that I could use as a starting point and study guide? A bit of an explanation wouldn't hurt either.


So what I have in mind is a shader that would give the illusion of an uneven vertical surface, that is, the impression of a ragged rectangular pattern of rock jutting out by varying amounts. I'm not sure what you mean about the cliff shader in development unless it is the slight tweak that I made to one of your standard ALS shaders to use vertical/horizontal coords instead of lat/long.
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby Thorsten » Thu Oct 17, 2019 11:40 am

So what I have in mind is a shader that would give the illusion of an uneven vertical surface, that is, the impression of a ragged rectangular pattern of rock jutting out by varying amounts


Okay, that'd (sort of) work - you're trying to solve a tesselation problem then...

However, I'm a bit vague as to how a geometry shader works, perhaps one of you could point me at an appropriate geometry shader in FG that I could use as a starting point and study guide?


The grass fur shader (Shaders/grass-ALS.geom) might be a good starter - it copies a triangle multiple times and displaces them along the normal axis so that you get a whole stack of triangles instead when it's finished.

Rather than stacking triangles, you'd have to do outward displacements and do proper skirts so that you can't look underneath the new roughness. I actually have a half-baked attempt at a similar problem - I wanted to increase roughness in rocky areas, so I asked a geometry shader to put little random 'pyramids' on each rock triangle. This sort of worked, but it looked like crap, so I haven't pursued this further.
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Re: Geometry shader for cliffs

Postby Icecode GL » Thu Oct 17, 2019 11:51 am

You could try, but you will quickly find that geometry shaders aren't very appropiate for tessellation. FG terrain being a triangulated irregular network doesn't help either. If I were you I would try to fake it using a fragment shader or something like that.
Icecode GL
 
Posts: 622
Joined: Thu Aug 12, 2010 12:17 pm
Location: Spain
Callsign: icecode
Version: GIT
OS: Fedora

Re: Geometry shader for cliffs

Postby Thorsten » Thu Oct 17, 2019 11:53 am

Well, yeah - I'd second that - I'd try a heightmap or so in the fragment shader.
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Re: Geometry shader for cliffs

Postby frtps » Thu Oct 17, 2019 12:31 pm

Thorsten wrote in Thu Oct 17, 2019 11:53 am:Well, yeah - I'd second that - I'd try a heightmap or so in the fragment shader.


Sounds good, happy to fake it (which I thought the urban shader was actually doing). So...any heightmap in fragment shader examples you could point me to?
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby Thorsten » Thu Oct 17, 2019 2:17 pm

Quick introduction I wrote on the various relief thingies:

http://www.science-and-fiction.org/rend ... sl_11.html
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Re: Geometry shader for cliffs

Postby frtps » Sun Mar 29, 2020 6:16 am

OK, so I'm seriously working on this now, by applying a bumpmap across the cliff face. A couple of questions that I hope somebody can answer:

1. I am perturbed by a comment in the urban-ALS.vert shader that world scenery 2.0 generates incorrect binormals, normals and tangents. Is this still the case?
2. If, say, I calculate the binormal and tangent directions in the vertex shader, is it true that these will be averaged for each vertex across *all* triangles having that vertex, or only those triangles that the shader is called with? Obviously I don't want the flat areas at the top of a cliff to influence the normal that is then interpolated across the cliff face.
3. Is there a way to get the plain old normal for the triangle that a fragment is part of rather than some interpolated normal?

thanks!
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby Thorsten » Sun Mar 29, 2020 6:37 am

1. I am perturbed by a comment in the urban-ALS.vert shader that world scenery 2.0 generates incorrect binormals, normals and tangents. Is this still the case?


Since the scenery hasn't changed, I believe yes, but that's hardly relevant for you since I understand the cliffs are in custom scenery rather than WS II urban areas, no?

If, say, I calculate the binormal and tangent directions in the vertex shader, is it true that these will be averaged for each vertex across *all* triangles having that vertex, or only those triangles that the shader is called with?


They're computed for all vertices that shader is run for, but they're not interpolated/used if no fragment shader needs them. But when interpolated the normals always 'leak' into the surrounding terrain (we use the fact to make the sea wavy next to cliffs for instance...) - so you can not exclude edge vertices from the normal computation somehow, all vertices of a triangle contribute, regardless of whether part of them also belongs to triangles that get a different effect.

3. Is there a way to get the plain old normal for the triangle that a fragment is part of rather than some interpolated normal?


There should be a setting of shading to smooth or not - if you do not smooth, every triangle is treated as a flat surface, so yes, that's possible.
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Re: Geometry shader for cliffs

Postby frtps » Sun Mar 29, 2020 7:51 am

Thorsten wrote in Sun Mar 29, 2020 6:37 am:
1. I am perturbed by a comment in the urban-ALS.vert shader that world scenery 2.0 generates incorrect binormals, normals and tangents. Is this still the case?


Since the scenery hasn't changed, I believe yes, but that's hardly relevant for you since I understand the cliffs are in custom scenery rather than WS II urban areas, no?


I am using the same scenery generation chain as WS 2.0 (in particular tg-construct) and the underlying OSG routines in FG are presumably the same so probably this remains the case.

If, say, I calculate the binormal and tangent directions in the vertex shader, is it true that these will be averaged for each vertex across *all* triangles having that vertex, or only those triangles that the shader is called with?


They're computed for all vertices that shader is run for, but they're not interpolated/used if no fragment shader needs them. But when interpolated the normals always 'leak' into the surrounding terrain (we use the fact to make the sea wavy next to cliffs for instance...) - so you can not exclude edge vertices from the normal computation somehow, all vertices of a triangle contribute, regardless of whether part of them also belongs to triangles that get a different effect.

As the cliffs are a single quad top to bottom, split on the diagonal to make 2 triangles, the interpolated normal is almost never pointing straight out as all the vertices have contributions to the normal from the flatter areas at top and bottom. This spoils the lighting equations and bump mapping. So I'm thinking I need to provide a small transition strip of triangles at the top and bottom of the cliffs just to isolate the cliff vertices from pollution by the bordering non-cliff triangles.
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby Thorsten » Sun Mar 29, 2020 8:41 am

The triangle strip would do the trick, yes. If you're willing to do that, I'd try to make the cliffs a bit more random in the mesh - they look very cool in the images, but kind of too regular for my taste.

Otherwise I guess you could try non-smooth shading, but it gives visible hard triangle edges in the shading.

If I remember correctly, the scenery-side problems were pretty specific to urban areas, I can't recall seeing them elsewhere, and I have troble visualizing urban cliffs :mrgreen:
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Re: Geometry shader for cliffs

Postby frtps » Sun Mar 29, 2020 9:53 am

Thorsten wrote in Sun Mar 29, 2020 8:41 am:The triangle strip would do the trick, yes. If you're willing to do that, I'd try to make the cliffs a bit more random in the mesh - they look very cool in the images, but kind of too regular for my taste.


I'm hoping that a bump map as well as all of the other tricks (hires patches, overlay texture, grain) will obviate some of that smooth look.
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby frtps » Fri Apr 10, 2020 12:32 am

So, making progress here.

Image

The picture shows an early morning view with the sun low and to the right (north) of the picture. The point of the picture is that the cliffs are actually illuminated correctly, with patches of light and dark depending on orientation, because I've managed to get the normals etc. generating as per the docs. Previously the cliffs lit up uniformly as the sun went higher, because the default normal provided by OpenGL averaged out to something close to vertical. To answer my questions for the benefit of future generations:

frtps wrote in Sun Mar 29, 2020 6:16 am:
1. I am perturbed by a comment in the urban-ALS.vert shader that world scenery 2.0 generates incorrect binormals, normals and tangents. Is this still the case?


No it is not the case, they are correctly generated. Make sure to follow the docs, and note that the attributes sent to the vertex shader will be vec4.

2. If, say, I calculate the binormal and tangent directions in the vertex shader, is it true that these will be averaged for each vertex across *all* triangles having that vertex, or only those triangles that the shader is called with? Obviously I don't want the flat areas at the top of a cliff to influence the normal that is then interpolated across the cliff face.


The values will only be averaged for the triangles involved in the effect as I think Thorsten explained above. If you use gl_Normal then it appears to be averaged over all triangles.

3. Is there a way to get the plain old normal for the triangle that a fragment is part of rather than some interpolated normal?


That is what the <generate> tag is for in the effect specification. Don't forget (like I did) to actually provide the generated values to the shader by adding <attribute> tags to the <program> section.
frtps
 
Posts: 112
Joined: Sun Aug 05, 2018 9:58 am

Re: Geometry shader for cliffs

Postby Thorsten » Fri Apr 10, 2020 6:49 am

Don't forget (like I did) to actually provide the generated values to the shader by adding <attribute> tags to the <program> section.


Ah, okay... makes sense.
Thorsten
 
Posts: 11698
Joined: Mon Nov 02, 2009 8:33 am

Next

Return to Effects and shaders

Who is online

Users browsing this forum: No registered users and 1 guest