I'd like to implement shadows and reflective surfaces in my OpenGL app. I already have ideas which I will provide below. But I would like to ask someone more experienced than me before I put too much effort into the implementation. (I'm still very new and so I don't know all options I have on modern graphic cards.)
I would like to know if there are any common improvements or common techniques that would improve the quality and/or performance. (Or even if my idea is completely nonsense)
Shadows
I would render a cube map at the center of each light source in a separate Framebuffer. When rendering the real scene I can lookup the depth information in the the relevant cube map (for each light source) and compare the depth value with xyz-Position of the Fragment. And check if the Fragment is behind an object. Additionally I could also take surrounding depth values into account to render blurred shadows depending on the distance to the object.
For the special case that the light source is stationary I could render all other stationary objects at the beginning. For each frame I still have to render the dynamic objects and reset to the "stationary scene" afterwards.
Reflection
For plane mirrors you could simply mirror the view matrix around the plane and render the scene a second time (with the stencil buffer to clip the rendering process to the mirror surface). Also I should clip the rendering volume with glClipPlane()
.
I found this technique on Google, so it seems to be very common.
But for arbitrary surfaces I had to think about something different. My idea is to render a cube map at the center of every object that has reflection enabled in its material definition. When I am in the real rendering process, I would read out the pixel value in the cube map, depending on the surface normal and the fragment xyz-Position. The problem is that you get an parallax error for near objects because the fragment obviously isn't at the center of the object. I couldn't solve this problem. (Maybe I could render the normals of the mirror object and then use this to make some transformations while rendering the cube map?)
For stationary mirror objects I could do same trick that I described in the Shadows section.
Instead of cube maps I could also use a single map and transform the Cartesian coordinates to spherical coordinates (phi, theta, r). So that the resulting texture has the dimension phi * theta (r is the depth value). But I don't know which method is generally faster. Also a single map would generate different texture qualities depending on theta. I'm not sure how bad this will be in the result.
The main problem that I have is that this all sounds very performance intensive. Maybe there are tricks I don't know?
An advice would be helpful. Thanks in advance.