# Visual Basic > Games and Graphics Programming > Game Demos >  VB: Advanced Lighting and Shadowing (Tutorial)

## Cade

Source + Exe coming as soon as its done.


All lights are realtime and the fps is good (50 fps uncompiled, 8 objects, 2 lights and I havent optimized yet). The lighting is perpixel lighting and the shadows are the only things slowing down the framerate, the perpixel lighting is actually very efficient.

----------


## Cade

Exe is done, I just have to clean up the source a bit before I also post the source.
Link to exe

----------


## wossname

In the first image the box near the centre should be illuminated on three sides but it appears to be only two on the image.  Looks a bit odd.  Also on some other boxes the shadows are actually cancelling out some lit regions.  Or am I reading it wrong?

----------


## Cade

In the first pic, there are only 2 lights. The shadows cancel out the lit areas where the lights meet, where the light is full intensity, the shadow wont cancel out.

----------


## Cade

Source Code + EXE

----------


## Merri

The shadows shouldn't fall on the boxes? I mean... shadow should go on another box only if it is shorter than the box that makes the shadow. Though, I guess this is more of a 2D... but in that case a shadow should never go on top of another box afaik. It just looks weird and unrealistic.

Of course other than that it is great, though I didn't take a look in the source  :Smilie:

----------


## Pino

I think merri is right, the shadows dont look too good.  :Frown:

----------


## Cade

Well I could modify the shadows so they are under all the boxes (that was my old method) but that doesnt look nice, even if it looks more realistic.

----------


## Pino

well lets see it like you said above  :Smilie: 

No one is having a go, the work looks good and we are just trying to help you make it look better  :Smilie: 

Pino

----------


## Cade

Er... I cant put it back without writing a whole new program(just tried), the shadows and lights are both drawn on the same buffer, the objects onto another and they are multiplied, with the output on the backbuffer.

----------


## Merri

Here you have a sample of what is somewhat should look like:



You can clearly see what is the problem when you compare it to the screenshots: the more lights, the more shadows, but the more shadows, the lighter they are: except in the places where there are shadow on shadow. The light strength should also be taken into account, a stronger light surpasses a weaker one and makes shadow to the area of a weaker one.

I'm not an expert in this area, but I've thought about lights and shadows in the dark nights when I've been walking from a light to another in the street  :Smilie:

----------


## wossname

Merri, what you get up to on the streets at night is your own business  :LOL:  j/k.

I don't think the problem lies with shadows being on top of boxes or not, the fact is that if the shadow engine was more realistic then that effect wouldn't occur at all...

I think the first picture would look more like this... (the grey areas would be in light, the black areas in shadow, I haven't bothered with gradients)

Since the middle square is being lit from 3 sides, then only the 4th side can be in shadow, and not as in the original pic.  I omitted a couple of boxes for clarity.

----------


## Cade

If anyone could modify the code I did and show the updated code, that would be really nice. What I do is, for each light, all the shadows are subtracted from the light and then the light is added onto the lightbuffer.

----------


## Cade

By using 2 lights, 1 object and the same code I uploaded (only difference is number of lights & objects), I position the lights at 0,0 and 640,0 and the object at 320,240, I get the following
 
Looks correct?  :Wink:

----------


## Pino

yea that looks good.

Pino

----------


## Merri

Yeah, but you should make it take other objects into account  :Smilie:  At the moment it works somewhat correctly to one object (it seems), but the first screenshots up there were odd. What happens when you include another object near the other one??

----------


## Cade

It gets partly shadowed

----------


## Pino

Lets see another screen?

----------


## Cade



----------


## Merri

Yup, it gets distorted. For example, it seems that it can make a shadow on a light source. There should be no shadow on a light source. The shadows should get weaker when close to light. And as stated already, the shadows should cut out when they go on another object (they do cut out now, but in the wrong edge).


Humm... I played around with a flashlight. I think I got the real problem now: your lights are in the same level as the objects. When I placed a flashlight in the same (ground)level with the object, it didn't make any shadow. So, you should also know how high your lights are compared to the objects. This way you can do more accurate shadow and also calculate where the shadow ends (and where extra light should be made).

----------


## Waxy

What version of D3D and what VB version is this? you peeked my intrest.  
i may tap your brain later when i move on to lighting.  :Big Grin:

----------


## Cade

D3d 8.1, Vb6

----------


## First.Assassin

The reason why it looks strange is because the shadows are extending too close to the light sources.

The shadow generation seems perfect but after the shadows are generated they are not lit up enough by the light source. Look at the picture and see those shadows right close to the lights, they should be much fainter if not invisible.

The red areas show where it is too dark too close to the light

The blue rings show how far the light should extend where no shadows form and from then onwards shadows should get gradually darker.

----------


## Electroman

Anyone else get a automation error when you run it? Haven't had time to look into why its happening but think its not being initalised properly for all cards.


As for the problem its seems from the pics that when you generate a shadow from one light you don't take into account any other lights at that time. Would probably mean taking a different approch, instead of using each light and applying it to each pixel then the next light ect. you could go through each pixel once each and calculate its light value. That sounds like it would be a rather big rewrite so i guess you could go back to how you've done it using each light at a time but when doing each pixel cross referance it with other lights. This might result in still a one pass situation but could be rather processor hungry too  :Smilie: .

----------


## Merri

One way would be to use a midpoint, which would be the highest light. I'll explain this a bit better:

values 0 - 127 = black to white
value 128 - 255 = "too bright to see the brightness", are always considered to be white

Now what does this give to use? You got it: the possibility to have too big values! But why? This would be a small "override" to the problem mentioned by First.Assasin. It would give you the power to control easier what would be the point where shadow turns out to be invisible. The lights would be stronger and not weak as they are now.

----------


## Cade

I tried it, it looks washed out. If someone could edit the code and post the results, that would be good.

----------


## Cade

Update: I am working on a new version with stencil shadow volumes (much more faster) and I am trying to make the light fade using a 1D .bmp (1x256), i'll post it as soon as im done.

----------


## Electroman

Sorry I was meaning to post this but forgot about it  :Blush: ...

I think I figured out why it was looking wrong, the way you are doing it is starting with white and then turning the pixels darker where the shadows are cast....if you go at it the other way by starting with black and lighten the pixels where the lights fall it will give the correct/natural effect  :Big Grin: .

----------


## Cade

That is what Im trying to do but it looks about the same so im trying to use a 1D texture as the light texture. Ill post more once I get it working.

----------


## Merri

Think about this: what happens when a light source goes up and down?

----------


## Cade

Link to EXE+Source

----------

