Board index FlightGear Development Effects and shaders

Trying to get shadows...

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

Trying to get shadows...

Postby icecode » Wed Sep 28, 2011 4:14 pm

Hello all!

I've been working in returning shadow volumes to FG via GLSL shaders. It is going OK so far, with stencil working thanks to Gijs for fixing light-cone. But I got in a problem with the vertex shaders. It works perfectly in every shader developing program I try (ShaderMaker, ShaderDesigner, Lumina and others). But it isn't in FG, where it doesn't extrude to infinity as it should be.

Here is the vertex shader:

Code: Select all
void
main(void)
{
   // Compute the light direction to vertex.
   vec3 lightDir = (gl_ModelViewMatrix * gl_Vertex
                    - gl_LightSource[0].position).xyz;

   // If vertex is not illuminated, hold position.
   if (dot (lightDir, gl_NormalMatrix * gl_Normal) <= 0.0) {
      gl_Position = ftransform();
   } else {
     // If it is, extrude in light direction.
      vec4 pos = gl_ProjectionMatrix * (
                 gl_ModelViewMatrix * gl_Vertex
                 + vec4 (normalize (lightDir) * 100000.0, 0.0));
      
     // If the z position is higher than w, make z equal to w.
     // (Similar to extruding to infinity, maybe some correction here helps??)
      if (pos.z > pos.w)
          pos.z = pos.w;

      gl_Position = pos;
   }
}


Any help is appreciated.

Cheers and thanks!
icecode
 
Posts: 709
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Trying to get shadows...

Postby Zan » Wed Sep 28, 2011 7:55 pm

Hi.

First thing I notice is that you calculate a "light direction to vertex" even though FlightGear has a area light, so the light position is just its direction, and it is the same for every vertex. I think using just vec3 lightDir = gl_LightSource[0].position.xyz; would be enough.

Another thing I don't understand is why you make z equal w? Since (iirc) the position is divided by w later, you get z coordinate 1.0, which most likely is not what you want?

There are some other optimizations to be done, but we can look into them if you get this to work. I'd try changing that light direction, and taking out the z=w thing...

Btw, with which kind of object are you trying this? A sphere or something?

Zan
Zan
 
Posts: 123
Joined: Tue Oct 20, 2009 11:28 am

Re: Trying to get shadows...

Postby icecode » Wed Sep 28, 2011 8:27 pm

Hi Zan,

I will try that, I had no idea about FG uses an area light.

About the z equal to w well, I am just guessing here but what I wanted to get is infinity. As I've read it is semi-possible with homogeneous coordinates, and it is working! For the moment it is removed, but the position is finite (1000000), little for the FG world. That's the main problem I see, infinity.

I'm trying with every object, included an aircraft. It works OK with all.

Cheers.
icecode
 
Posts: 709
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Trying to get shadows...

Postby Zan » Wed Sep 28, 2011 8:34 pm

To get infinity I think you need to make the w coordinate 0. Then, when other coordinates are divided by it, they become infinite. Though the graphics card handles that somehow, I have not tested how, but it *should* give sane results.

Cheers, Zan
Zan
 
Posts: 123
Joined: Tue Oct 20, 2009 11:28 am

Re: Trying to get shadows...

Postby icecode » Wed Sep 28, 2011 8:44 pm

Yes, I've tried that, but I get awful results. But doing tests I could get it working in a "beautiful" way with:

Code: Select all
if (gl_Vertex.w == 0.0)
   pos.w = 0.0;


It makes the pos 0 only if the vertex we are working with is 0, otherwise, don't touch out. And it works! Properly extruded to infinity:

Image
(Yeah, I know, not the best screenie ever...)

Here is the new shader if you want to have a look at it:

Code: Select all
void
main(void)
{
   // Light direction
   vec3 lightDir = -gl_LightSource[0].position.xyz;

   // If vertex is not illuminated, hold position.
   if (dot (lightDir, gl_NormalMatrix * gl_Normal) <= 0.0) {
      gl_Position = ftransform();
   } else {
     // If it is, extrude in light direction.
      vec4 pos = gl_ProjectionMatrix * (
                 gl_ModelViewMatrix * gl_Vertex
                 + vec4 (normalize (lightDir) * 100000.0, 0.0));
      
     // Only extrude to infinity if vertex.w is 0.
     if (gl_Vertex.w == 0.0)
        pos.w = 0.0;

      gl_Position = pos;
   }
}


Cheers.
icecode
 
Posts: 709
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Trying to get shadows...

Postby Mossie » Thu Sep 29, 2011 8:10 am

I like what I see a marked visual improvement.

I don't understand how this works, I'm not a programmer. But the look is very nice.

Can I ask when or how this can be implemented into the existing FG set, is there some files that need adding, and object code changed?
Regards, Gerry "Mossie" Mos
--------------------------------------------------------------------------
Mossie 3D CAD, "Prompt and Precise"
http://mossie3dcad.com/

http://ww1-aircraft.info/
Mossie
 
Posts: 111
Joined: Thu Dec 27, 2007 4:22 pm

Re: Trying to get shadows...

Postby icecode » Thu Sep 29, 2011 2:41 pm

Hi Mossie,

Well, I am not sure when this will be finally implemented (I still don't know if I can do it!!) but the hard work is finished. I am learning GLSL slowly, so the work is not going really fast. This shadow volumes are using GLSL shaders, same as the water shader, skydome effect etc. so you just have to add a .vert and .frag in the Shaders/ folder and a .eff in the Effects/ one. No changes in the source code for the moment, but I think some will be made (not sure).

And to answer the question of how it works, well, it simply doesn't move the illuminated vertices and move to infinity the ones that are not illuminated. Then we do like with light-cones, illuminate in ground the shape of the extruded object. I see this is not very complicated, but when I see the urban shader or skydome I say "How???". I don't understand why it wasn't implemented before... Just see the new fog made by Thorsten, yeah, it makes you open your mouth to say Wow, but... maybe spending some time for shadows don't hurt?

Cheers.

EDIT: Bad news, just noticed a BIG framerate drop of 10 fps with shadows enabled... optimizations NEED to be done.
icecode
 
Posts: 709
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Trying to get shadows...

Postby Mossie » Mon Oct 10, 2011 3:02 pm

Thanks for you reply, sounds promising, even if it needs tuning to improve frame rate.
Regards, Gerry "Mossie" Mos
--------------------------------------------------------------------------
Mossie 3D CAD, "Prompt and Precise"
http://mossie3dcad.com/

http://ww1-aircraft.info/
Mossie
 
Posts: 111
Joined: Thu Dec 27, 2007 4:22 pm

Re: Trying to get shadows...

Postby curt » Mon Oct 10, 2011 6:16 pm

Hi Icecode, getting shadows back into the OSG version of FlightGear would be a tremendous improvement. Thanks for exploring and working on this area! One thought from a performance standpoint. While it would be cool to have a complete general shadow implementation that applied to the entire scene (terrain included), in the past we simplified things by only having the main aircraft cast a shadow. Just having it self shade and cast a shadow on the surface was a huge improvement in realism. You don't realize how much we use shadows as a visual cue until you've had them and lost them. :-) External views when taxiing or landing really benefit from the aircraft casting a shadow.

So one idea for starters would be to just draw the primary aircraft when generating your shadow mask. If you can do the entire scene as a separate option, that would still be interesting -- to already have code that supports this in place when the future arrives and we have more powerful computers and video cards to deal with the extra work load.
curt
Administrator
 
Posts: 1168
Joined: Thu Jan 01, 1970 1:00 am
Location: Minneapolis, MN

Re: Trying to get shadows...

Postby icecode » Mon Oct 10, 2011 7:21 pm

Hi curt,

That's a great idea. Since I am working with shaders and the great FlightGear's Effects infrastructure, it wouldn't be so difficult to choose which objects are able to cast shadows and which aren't. Maybe we need some hard coding, maybe not, but I will look on it.

At the moment all objects are casting shadows except terrain. I am dreaming with a c172 flying in a valley, move my head, and find a shadow projected on the valley by a mountain. :) Unfortunatelly as you said, we will have to wait for better hardware. But still a 772 shadow projected in taxiway, is awesome.

Cheers and thanks for help.
icecode
 
Posts: 709
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Trying to get shadows...

Postby curt » Mon Oct 10, 2011 9:47 pm

I never was involved in the technical details, but in the old "plib" based shadow system, the "own aircraft" cast a shadow on everything, but it was the only object that generated a shadow. It also self shaded which was really cool. So in the old plib-based system, you would see your own shadow on the side of the canyon or wherever it was cast.

Here is an old video that showed how cool the self shading is ... it's subtle so you might not think much about it if it isn't there, but it sure adds a lot.

http://www.youtube.com/watch?v=nZIcDDPTDtM

It was actually a nice compromise ... most of the time your focus is on your own aircraft, so if that cast a shadow on the airport or the terrain, and if it self shades, then you almost don't notice (most of the time) that there aren't shadows elsewhere. By the way, watching your shadow follow the terrain was pretty cool.

Just for fun, I made this video a year ago -- based on some math I worked out inside FlightGear and then ported over to my embedded controller:

http://www.youtube.com/watch?v=F15UbgTkz1o

Best regards,

Curt.
curt
Administrator
 
Posts: 1168
Joined: Thu Jan 01, 1970 1:00 am
Location: Minneapolis, MN

Re: Trying to get shadows...

Postby eekpo » Mon Oct 10, 2011 11:38 pm

Hi
found this
viewtopic.php?f=47&t=9972#p102336

Best regards

eekpo
eekpo
Retired
 
Posts: 131
Joined: Tue May 03, 2011 9:23 am

Re: Trying to get shadows...

Postby icecode » Tue Oct 11, 2011 8:45 am

Self shadowing is possible with the current technique I am using. The technique is the same than when we used to have plib, so all the features that were available before, should be possible now.

eekpo, thanks for the link. Shadows were pretty cool. :)
icecode
 
Posts: 709
Joined: Thu Aug 12, 2010 1:17 pm
Location: Spain
Version: next
OS: Fedora

Re: Trying to get shadows...

Postby Thorsten » Tue Oct 11, 2011 8:51 am

Just a comment: It'd probably be good to think early on in the implementation about the problem that light isn't always bright and crisp, but we have environment conditions in which shadows are blurry at best, and others in which they disappear completely.

We have a lot of aircraft with impressive reflection shaders which look a bit silly when they stand in the rain and show the bright reflection of an invisible sun... :D
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Trying to get shadows...

Postby eekpo » Tue Oct 11, 2011 2:55 pm

curt wrote in Mon Oct 10, 2011 6:16 pm:
SNIP

So one idea for starters would be to just draw the primary aircraft when generating your shadow mask. If you can do the entire scene as a separate option, that would still be interesting -- to already have code that supports this in place when the future arrives and we have more powerful computers and video cards to deal with the extra work load.


Hi,

When working on old models xml files , we often find an animation like this
<animation>
<type>noshadow</type>
<object-name>Bezel-pt</object-name>
</animation>

Was it used for that "shadows" system ?
which seems to tell which object is involved or not

Best regards

eekpo
eekpo
Retired
 
Posts: 131
Joined: Tue May 03, 2011 9:23 am

Next

Return to Effects and shaders

Who is online

Users browsing this forum: No registered users and 4 guests