问题
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)?
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/11287023/what-is-the-simplest-method-for-rendering-shadows-on-a-scene-in-opengl