0

I am using the LWJGL package and am able to create a basic scene and draw shapes (with or without textures), move a custom 'Camera' object and rotate it to render the scene accordingly. However, when it comes to creating shadows, I am at a loss.

I can think of the basic algorithm for creating shadows.
1) Render the scene from the camera's view as if in shadow.
2) Render the scene from the light's view, lighting up the visible part of the scene (maybe darken the scene as it becomes farther away from the light source?).
3) Re-render the scene from the camera's view.

However, I do not know the particular methods for OpenGL (particularly LWJGL). Upon research on this topic, I have only come across tutorials that require all the points and planes of the geometry or contain only partial code that I cannot seem to get working for my own project.

Am I thinking about this correctly? If so, what is (are) the method(s) that I need to use for shading the visible parts of the model (or doing something else)?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Stephen Wilkins
  • 125
  • 1
  • 1
  • 8
  • possible duplicate of [Creating OpenGL Shadows](http://stackoverflow.com/questions/8291626/creating-opengl-shadows) – genpfault Jul 02 '12 at 04:53
  • @genpfault I did look through several similar topics on the issue, that topic being one of them. I Googled those search terms, but only one of those methods is applicable to the algorithm I mentioned. I have attempted some of those that use matrix math to determine the shadows, but never have those worked for me. I wasn't asking how to do it exactly, but rather the method calls that I would use to go about the algorithm. The code Hugh posted more or less uses it and I can follow it exactly...but for some unknown reason doesn't work. I do understand your concern with duplicates, though. – Stephen Wilkins Jul 02 '12 at 14:06

2 Answers2

1

The simplest way is, as you've worked out, to render the scene from the viewpoint of the light. Save the contents of the depthmap as a shadowmap texture: each depth is the closest to the light source. Then render the scene again, and calculate for each fragment texture coordinates in a 'world' space relative to the position of the light. Compare the value to the shadowmap: if it's greater, this fragment is shadowed.

Many years ago I wrote some old (pre-shader) OpenGL code to demonstrate this. It's in C for GLUT, but all the OpenGL bits have 1:1 equivalents in LWJGL or JOGL. http://cs.anu.edu.au/~hugh.fisher/3dstuff/shadows.tar

Translating it to a modern shader equivalent shouldn't be hard.

Hope this helps.

Hugh
  • 1,641
  • 1
  • 11
  • 3
  • I read through your code, and it makes great logical sense of how you went about it. However, my attempt to [port the code](http://pastebin.com/u3bjeRV5) yields nothing more than the background. I am at a loss of what I did wrong. I did have to change a few things to work within my context (calling the camera. and light.lookThrough() methods) and change a couple variables and stuff. Buffers are allocated directly through my BufferUtility class. The rendering works with a simple drawPlanes() and camera.lookThrough(), but replacing the method with your display() version doesn't draw anything. – Stephen Wilkins Jul 02 '12 at 13:52
  • Actually, I tracked it down to the GL11.glViewport method called at the end of the renderToShadowMap() method, However, commenting out the line results in a bar on the side not rendering the scene, nor is the scene with lights (the entire scene seems to be in shadow). – Stephen Wilkins Jul 02 '12 at 15:28
  • And again further testing has fixed the the viewport problem (as well as make me realize a couple other mistakes), but the scene is still in complete shadow (even when I don't call the enableShadowing() method; it lights up iff I don't call the renderToShadowMap() method). Sorry to keep bugging you about this. – Stephen Wilkins Jul 02 '12 at 17:29
  • There's some glitches in the program when run on newer systems (the ball doesn't bounce) but the shadows still work. Sorry, I don't have enough expertise with LWJGL to be able to debug the problem. – Hugh Jul 04 '12 at 02:12
0

To implement and understand, I would say ray traced shadows. Basicly it casts rays from the ground plane and checks if it hits anything for each pixel on a texture. But it kills preformance like crazy. So if you really want to, you could do the ray calculations once and save it to a texture, then load it on the start of a level/scean. This isnt at all dynamic but it works.

J Frap
  • 9
  • 4