Kind of shadow casting.

Hello everyone!

Here i am, working at a new project.
I need some advice about this:

http://img152.imageshack.us/img152/8813/screenshotfrom201302241.png

Is there any way (like shaders) to achive this effect in libGDX? (the shadow having the same shape as a sprite that is casted at 45°)
The way i set up it in above picture is trivial: i draw the shadow in GIMP and then draw it before the sprite.

Thanks.

Quick search turned this up: https://code.google.com/p/box2dlights/
JGO link on dynamic lighting resources: http://www.java-gaming.org/topics/2d-dynamic-lighting/27012/view.html

I already searched and read about dynamic lighting, but this isn’t what i want.
I want static lighting.

If you want static lighting, what is wrong with your current approach, painting the shadows in GIMP?

I think that if i make the shadows by code, it will be easier to me to avoid things like this:

(notice the shadow rendered under the wall and intensifying the wall’s shadow)

Also, it would be easier than drawing and positioning the shadow for every entity i make.

What you are looking for is dynamic shadows.

To achieve dynamic shadows like that for arbitrary shapes (e.g. a tree), you would need 3D representations of your sprites modeled in Blender or Maya. Then you would render a depth pass from the light’s perspective and use the technique here to cast shadows. The amount of work for such a simple feature would be astounding.

If you plan all of your sprites to be very primitive (e.g. boxes), then you can do something like what box2dlights does. This still requires pretty heavy math and probably won’t be easy.

Another idea: don’t blend shadows together. You could do this by rendering all your shadows to an offscreen buffer at 100% opacity, and then blending the shadows atop your scene at 50%. There might also be another way of achieving this without FBOs (for better Android performance).

Oh, so my ideea of getting the sprite’s image, distorting it (flip it vertically and skew (:-??) until it gets 45 degrees) and then making it only black + decreasing it’s opacity (tehniques that i use in photoshop to do the shadow) isn’t so good. Is it practical? :frowning:

That will work if all of your sprites are box-shaped. For arbitrary shapes (e.g. tree branches, character’s limbs, etc) it won’t work accurately.

However, if you are OK with all of your shadows just being boxes, you could apply a transform matrix to your “shadow batch” and use Color.BLACK as your vertex color. You can remove overlapping shadows like I said with an FBO (or perhaps some other solution fit for Android). To “properly” remove the shadows (i.e. based on occluders), you need to get into more complex territory (true dynamic shadows).

I understand. Thank you very much for help! ;D

The basic non-shader trick for rendering planar shadows that you don’t want to “stack” is to use the stencil buffer. Something like this will do it:


glDisable(GL_DEPTH_TEST);
drawBackground();
// Increment the stencil buffer on every draw, don't draw where it's already set.
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_INCR, GL_INCR, GL_INCR);
glClearStencil(0);
glStencilFunc(GL_EQUAL, 0, 1);
glPushMatrix();
// shadowTransform is a simple scale+shear affine transform
glMultMatrix(shadowTransform);
// draw objects in the shadow color (no textures)    
drawObjectsWithShadows();
glPopMatrix();
glDisable(GL_STENCIL_TEST);
glEnable(GL_DEPTH_TEST);
// draw objects normally
drawAllObjects();