Skip to main content
gamedev.net
🔒 Locked

Simple Shadows

Started by Kohaar 25 replies 4.4k views
Original Post
Kohaar
Kohaar
Hi. I would like to create a simple shadow for my character. What I do is just render the character twice where one is transparent. picture1 picture1 As you can see on picture 1, the characters arm is darker than the rest of the body which make sense because the arm is in front of the body. What I would like is to make it look like picture 2, so before I look into stencil buffers and so on I would like to know if there are some blend or depth functions I can use so I get the right effect? Right now I use: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); Any help appreciated
http://vlab.dk
XorMultiPleXus
XorMultiPleXus
hi

1st thing i would try is this:

turn off the depth buffer and then render the shadow. After that turn it on again. (i did not try this)

2nd

1. render the shadow only into stencil buffer and set the values in the stencil to value 1, like this (and don't forget to turn off the depth buffer):

glStencilFunc (GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);

2. now render the shadow to screen with depthbuffer off and use the stencil buffer like this:

glStencilFunc (GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_ZERO, GL_ZERO);

this means that the shadow will be rendered only where stencil is 1 and after that it will be set to 0. So next time when som other polygon is rendered, that causes us trouble, there will be a zero in the stencil buffer. So it should not render and do the trouble. (but i did not try this).

3rd

i am working right now on shadows too. I need it, so if i won't have any problems i would like to finish it in one or two weeks and then i will put in public on my homepage http://xormultiplexus.googlepages.com/home. Maybe this could help you.

4th - trick
if the background on the picture is only a big polygon, then render the shadow to the stencil buffer and then render the shadow on the screen not with the mesh of the shadow but with the big polygon using stencil buffer.

Hope i helped somehow.

XorMultiPleXus
Kohaar
Kohaar
I have tried both to set depth mask to false and disabling depth test, but neither seem to work/change anything. The reason I would like to use this approach with just rendering another model transparent is that it so much easier since the background only consists of a texture :-)
Also I have to finish the project in 2 weeks, and there is a lot to be done besides shadows, so it shouldn’t take to long.

Your fourth idea seems interesting. Could you explain this further, maybe with some pseudo code?

What I do now is:

Draw Background
Enable Light
Draw Character 1
Draw Character 2
Disable Light
Draw Character 1 transparent
Draw Character 2 transparent
...
http://vlab.dk
XorMultiPleXus
XorMultiPleXus
ok

here's the code:

//we enable stencil test
glEnable(GL_STENCIL_TEST);

//now we turn off the depth and color buffer, so we can render only to stencil buffer
glDepthMask(GL_FALSE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

//we set this to render the mesh into the stencil buffer
glStencilFunc (GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);

//RENDER THE SHADOW

//now we turn on the depth and color buffer
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);


//here I render only the shadow
glStencilFunc (GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glColor4f(.5,.5,.5,0.5);
glBegin(GL_TRIANGLES);
glVertex3f(-300, -100, 0);
glVertex3f(300, -100, 0);
glVertex3f(0, 300, 0);
glEnd();

//and here the polygon
glStencilFunc (GL_NOTEQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
glColor4f(.5,.5,.5,1);
glBegin(GL_TRIANGLES);
glVertex3f(-300, -100, 0);
glVertex3f(300, -100, 0);
glVertex3f(0, 300, 0);
glEnd();

glDisable(GL_STENCIL_TEST);

Here's the explanation:
1. render the shadow to the stencil buffer == easy
2. render the polygon to render the shadow with help of stecil buffer
3. render the polygon itself

My advice for you:
0. render background
1. place the shadow mesh in front of the background polygon
1. render the shadow to the stencil buffer == easy
2. render the the shadow with help of stecil buffer

That should work for you.

XorMultiPleXus
Kohaar
Kohaar
^^^ Thats was me :-) I can't get firefox to do automatic login for some reason.
http://vlab.dk
XorMultiPleXus
XorMultiPleXus
well, not.

You have to render the character and it's shadow. The shadow must be rendered into the stencil buffer. Then you render only the background polygon. Now you must render the shadow. Not with the character mesh, but with only one polygon. Take the background polygon. Translate it in front of the background (which is already rendered) and render it with stencil on. That will render the shadow.

Yes it is a little bit computational expensive. But today we have fast computers and even if you play today's games you can see, that they don't bother with effiecienci ;-D.

XorMultiPleXus
Kohaar
Kohaar
^^^^ Have to figure out why gamedev (only site by the way) doesn't remember my login :-)
http://vlab.dk
XorMultiPleXus
XorMultiPleXus
i haven't been working with soft shadows, but that does not means i won't in the future (in one or two months). I know only one way to do soft shadows - througth shaders. Look on www.nvidia.com. I have downloaded their sdk and there is a lot of shaders (in cg and hlsl) to create soft shadows and other kind of shadows and a lot of effects.

XorMultiPleXus
Kohaar
Kohaar
Ok. Is the Nvidia thing better than GLSL or am I missing something?

[Edited by - Kohaar on June 8, 2006 10:54:07 AM]
http://vlab.dk
XorMultiPleXus
XorMultiPleXus
well it's because i have an nvidia card in my computer and they have so many things for free to download. So i've decided to try it. I have also read the spec for OGSL, but i did not find there anything about shader model 3.0. I want to use shader 3 so i have decided to use cg instead of GLSL. But you know i could miss something. You can use whatever you want. Cg is not perfect. But i have a little practive with it so i use it. And HLSL only in the worst case, because i dont wont to use directx. So it depends on what you need and what you like.

XorMultiPleXus
_the_phantom_
_the_phantom_
GLSL's shader level support depends on the card its running on, its perfectly acceptable todo SM3.0 things in GLSL, infact in some regards it goes beyond SM3.0 in what it lets you do in the language.
Kohaar
Kohaar
Just implemented it today, and the shadows work great. Thanks!

Now I just have to draw them in the correct perspective (from the light source).
http://vlab.dk
dawidjoubert
dawidjoubert
Quote:
Original post by Anonymous Poster
As I understand it you actually render the model 3 times.

One for the stencil buffer one for the shadow and one for the character?

The characters are bone animated and some of them have up to 20k polygons. I think this would be a little too expensive in computational power?

Hope you will bear with me if my questions seem extremely stupid :-)


No twice....

Once you rendered into the stencil buffer you can use a quad that covers the entire screen to fill in the shading.
----------------------------http://djoubert.co.uk
dawidjoubert
dawidjoubert
Quote:
Original post by Kohaar
Just implemented it today, and the shadows work great. Thanks!

Now I just have to draw them in the correct perspective (from the light source).



No what you need to do is extrude each edge into infinite from the light's point of view, this gives you volume shadows which are more than simple. Problem with this is the complexity of finding which edges to extrude.

Ofcourse if you plan on rendering the scene into a texture from the light source and then using the technique of depth lookup for shadowing you will be fine. All though kinda doesn't make sense to have stencil buffer then.
----------------------------http://djoubert.co.uk
chawk
chawk
Couldn't you do it in one draw with something like:

// before frameglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);// draw shadowglDisable(GL_DEPTH_TEST);// setup normal blendingglEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// render only where stencil is 0 (all of it, initially), but as the character// renders, pixels that render cause the stencil value to increment, thus // future attempts to render over that pixel failglEnable(GL_STENCIL_TEST);glStencilFunc(GL_EQUAL, 0, 0xFFFFFFFF);glStencilOp(GL_KEEP, GL_INCR, GL_INCR);renderShadow();glDisable(GL_STENCIL_TEST);glDisable(GL_BLEND);glEnable(GL_DEPTH_TEST);


I tested it briefly and it seemed to work, though I stayed up the whole night and I'm really tired, so perhaps I made an error.
Kohaar
Kohaar
Here is an example of the shadows



As you can see the shadows look odd because they don’t match the lighting. Otherwise I'm satisfied with the result. Any idea to how this could be accomplished.

I don’t have the time to start learning about advanced shadowing techniques, but if there are other methods of shadowing that are rather simple, I’m of course open for suggestions.

I would of course prefer that it could be implemented in my current code (using stencils) :-)
http://vlab.dk
XorMultiPleXus
XorMultiPleXus
Is the background still one quad? If yes, then use projected shadows. I you don't have time to play with it i can put on my homepage some simple code (http://xormultiplexus.googlepages.com/home), but i need a few time to put it in one piece. If you want it, then you must decide where to render the shadow. On that mortal kombat picture i would say you want the shadow to be on the ground. So make the ground one quad and the rest is not important. The problem is that, if the ground is not one polygon it wont be possible to do the shadow. One other solution could be to create a quad that would represent the ground. But you will not render that quad, but insted you will use it to make projection on it and to achieve more realistic look for you shadow. This way you don't need to do any changes to the background polygon.

decide ;-D

XorMultiPleXus

btw. that game of yours look good ;-D
Kohaar
Kohaar
Hi. The background in the game is just a texture, so it would be quite easy to create a polygon to represent the ground. A code example would be great!
http://vlab.dk
Kohaar
Kohaar
An image of it.

4.jpg

I would still like the other code because I think it would work better
http://vlab.dk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.