Recently I come across the term forward rendering. I'm kind of curious how this could be done in OpenGL. I have done a lot of search on this, majority of the result I get are on theory but not code implementation. May I know is there any code implementation on OpenGL for this rendering technique?
There are two major types of rendering:
- forward rendering
- deferred rendering
In both types you have you objects and lights present in the scene, but the difference is when to calculate lighting.
Render object, and while running its (usually) fragment shader, for each fragment you calculate lighting from all the light sources.
So, you for every object, and all of its fragments, calculate ligthing.
Why is this good and why is it bad?
It's bad because it might happen (and in most cases it does) that fragment is overwritten by the next, and all your lighting calculations are wasted. Also, running lighting calculations on each and every fragment is quite expensive. The cost is const and is equal to
num_fragments * num_lights, and that's a lot.
It's good because it uses hardware enabled AA
Render into buffers the data that is required for calculating lighting later, such as diffuse color, specular power, normals... and after you've done that, run ligthing calculations for every light, but only on the parts of the image that cold possibly be affected by that light.
Cons and pros?
It's good because, once you have the information what is visible in the scene, you run calculations only on those parts. Further more, only on the parts of the image that could be affected by that light. The cost is in worst case scenario
num_lights * num_pixels_of_screen.
It's bad because there's no AA and no support for transparent objects
In short, that's the difference between forward and deferred rendering. So if you put your lighting calculations directly into shading of the object, then it's called forward rendering. That means, almost any tutorial that you might find on the internet is probably implementing forward rendering type.
Hope this helps.
Forward rendering is the classic approach to rendering where the data for a scene is generally used directly to calculate a final pixel color in a single pass (additional post-processing passes can be used as well, but these are usually still considered part of forward rendering). The alternative, deferred rendering, generally encodes much more data (not necessarily actual colors) to an intermediate target, then defers the final render pass that uses the intermediate target as its source.