Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Stencil Buffer Shadows and Object transformations

This topic is 2339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.


Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

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:

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) Edited by SimonForsman

Share this post

Link to post
Share on other sites
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"]


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);
Vector4f lightPos = new Vector4f(light.x,light.y,light.z,1.0f);

Matrix4f modelView = new Matrix4f();

Vector4f fakeLightPos = Matrix4f.transform(modelView,lightPos,null);

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?


Share this post

Link to post
Share on other sites
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) Edited by SimonForsman

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!