July 22, 2007
lighting implemented
In addition to the light and shadow generation, I have also implemented multiple textures and four polygon layers (lit non-colliding background, lit colliding foreground, unlit colliding foreground, and unlit non-colliding foreground). I've been able to salvage some code and design from the discontinued PW2 but this map editor is still in the very early stages.
11 comments:
What about sprites, will they be affected by the shadows? Cause if not it will look silly.
Sprites will be lit and shadowed but they won't cast shadows themselves.
The article on GameDev says "To fill the depth buffer we simply enable z-buffer reading and writing, but with colour writing disabled to leave the colour buffer untouched.". How do I do that? Is it glColorMask?
Also the article doesn't explain what depth values each of the component on the scene has, like what is on top of what?
I don't know how to disable the colour buffer in OpenGL, but in managed Direct3D it's this line:
device.SetRenderState(
RenderStates.ColorWriteEnable,
(int)ColorWriteEnable.Alpha
);
From back to front my layers are:
- background polys
- shadows
- foreground polys
- light
Both layers are lit, but only the foreground layer casts shadows onto the background layer.
These are the steps I take to render:
- set render target to a render target texture
- render polys with white as the ambient light colour
- set render target to backbuffer
- render polys using the scene's ambient light colour
- disable colour writing and depth buffer writing
- clear alpha to 0
- render the light (only alpha)
- render the shadows (only alpha)
- turn colour writing back on
- set blend modes for light rendering (dest_alpha and one)
- set texture to the one that was rendered to at the start and draw it using the light's colour as the ambient light colour
The extra render target stuff fixes the problem of overlapping polys blending with each other in the lighting pass.
Thanks a lot. I wrote it down and the alpha channel just didn't do its job (this is a Geforce 6600 card). So I accidentally moved to another computer (with an ATi card) and it worked!
Now I didn't know this was such advanced stuff? I'm worrying that if it doesn't work for some people then it will be cheating.
MM, in DX you can also disable writing to color buffer by just setting blend modes:
_device.RenderState.SourceBlend = Blend.Zero; _device.RenderState.DestinationBlend = Blend.One;
I'm sure in opengl it will be the same.
I don't know what hardware requirements you're aiming for, but it works for me on a GeForce 4. Make sure the backbuffer and texture use the A8R8G8B8 format. The default for me was X8R8G8B8 which doesn't use the alpha channel.
Those shadows look good. One suggestion I have is that the shadows should be soft and more realistic. Like, have them blur more and more as they get farther away from a light source, that would be awesome.
That's what's in the article I got this technique from: Dynamic 2D Soft Shadows
I'm undecided about implementing it. I might just uniformly blur the shadows to get rid of the jaggedness.
agh, uniform gradients?
i read that tutorial on 2d soft shadows. i've had the same idea for ages, (my main curiosity sparked when i wondered why soft shadows varied in softness, depending on size of the light and distance from the shadow casting object from the light source and the surface it casted upon... i wondered all this when i was about eight or nine, so i wasn't smart enough to know why)
the only thing i'm curious about is if that tutorial can work with light sources of different sizes and shapes (ellipses/rectangles, even those of odd proportions).
one more thing: is it realtime raycasting, ie. if you were to ever implement moving geometry, would that cast accurate shadows?
Post a Comment