Stencil Buffer Shadows and Object transformations

Started by
6 comments, last by Pudutzki 11 years, 9 months ago
Hi everybody,

long time ago I had some postings here.

SInce a few weeks I've reanimated a recent game engine project. I was able in a relatively short amount of time
to implement stencil buffer shadow volumes. So far so good. Everything works fine and looks good with several
lights and a huge amount of objects.

Now for my question(s) (I hope you'll understand it, since english is not my native language):

The calculations of the shadow silhouette as well the calculations of the shadow volume and the visibility of each triangle
of some mesh is based on the current location and rotation of the mesh. Thus the shadow is only right, if the mesh is not being
rotated by glRotate and translated by glTranslate.

But now how to handle the movement of the mesh and the according shadow calculation, during the game?

Should I iterate over every vertex in the model and multiply it by a transformation matrix,
which contains the right rotation and transformation of the mesh?

IMHO this cannot be the right way, since this would alter the model data, which I do not want.

Is there a way of OpenGL transformation which can handle this issue in some way? My first guess is to move the light
sources accordingly, do the shadow calulations and the move the light source back to its initial position. Does
that make sense?

Any hint to the right direction is highly appreciated.

Greets
Matthias
Advertisement
If you're using fixed function then using a translated light position to generate the shadowvolume should be the best option since the translation has to then be done CPU side (and translating one light position is faster than translating tons of vertices).

If you are using shaders you will have access to the translated vertex data anyway.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
So far so good. But there's still the problem with object rotation. How to handle that?

So far so good. But there's still the problem with object rotation. How to handle that?


basically you want to "rotate the light" around the object, my linear algebra is a bit on the rusty side but i think you can just take the matrix you get if you reverse the objects rotation and translation and multiply that with the lights transformation matrix.

so if your object is rotated 2 radians around the y axis and translated 5 units along the Z axis you can do:

glLoadIdentity()
glRotatef(-2.0f,0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,-5.0f);
glGetFloatv(GL_MODELVIEW_MATIX, neg);
lightpos[0] = light.x;
lightpos[1] = light.y;
lightpos[2] = light.z;
lightpos[3] = light.w; //direction

fakeLightPos = multiplyMatrixAndVector(neg,lightpos);

then use fakeLightPos to get the shadow volume.

(My math might be way off, i don't have my math reference at hand (and can't be arsed to google, sorry) right now but i'm sure someone will point out any mistakes fast enough)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Ok, thank you for your detailed explanations and your time.

Currently I have to steps:

1.) Determine the shadow silhouette edges
2.) Draw the extruded and capped shadow volume based on the calculated silhouette

In each method I am doing:

[source lang="java"]

glLoadIdentity();

glRotatef(-xrot, 1.0f, 0.0f, 0.0f);
glRotatef(-yrot, 0.0f, 1.0f, 0.0f);
glRotatef(-zrot, 0.0f, 0.0f, 1.0f);

glTranslatef(-location.getX(), -location.getY(), -location.getZ());

FloatBuffer neg = BufferUtils.createFloatBuffer(16);
glGetFloat(GL_MODELVIEW_MATRIX,neg);
Vector4f lightPos = new Vector4f(light.x,light.y,light.z,1.0f);

Matrix4f modelView = new Matrix4f();
modelView.load(neg);

Vector4f fakeLightPos = Matrix4f.transform(modelView,lightPos,null);
[/source]

Now the whole shadow of the scene is moving with the camera.

My linear algebra is also not as good as it should, but might it be that the fourth component of the light position (the direction)
is my problem? Or did I forget something else?

Greetings
Matthias
btw.: Just for convenience, I'am using lwjgl.
remember to reset your matrices again after you've gotten your transformed light position. (you should only use that to generate the shadow volume, nothing else)
(Push the matrices before glLoadIdentity and pop them after you've gotten your light position)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Thank you very much for your support! It seems to work now.

(See attachment)

Greetings
Matthias

This topic is closed to new replies.

Advertisement