I am writing a GLSL program as part of a plugin that runs inside of Maya, a closed-source 3D application. My plugin renders custom geometry into the same image buffer that the application renders it’s default polygonal geometry. The application uses the OpenGL fixed pipeline for it’s lighting and shading but I am using GLSL to render custom geometry and materials.
My problem is, I want to mimic the behavior of the fixed pipeline lights in my shader. The application defines the lights in the gl_LightSource uniform and I want them to have the same direction, intensity etc when assigned to my custom geometry that they have when assigned to the application’s default polygonal geometry.
The gl_LightSource fields are clearly documented but I cannot find definitive documentation of how the fixed-pipeline interprets those fields. There are many examples of how to code point/directional/spot lights in GLSL but they do not seem to exactly mimic the fixed pipeline. For example, how do you determine if a light is a point, directional or spot light if the application defines a mix of them? Can a mix of light types be handled without introducing excessive branching in my shader?
In short, is there any definitive documentation or example of how the fixed function pipeline evaluates gl_LightSource?
Well there’s your first problem, because the “fixed pipeline lights” are implemented in the vertex processor, not per-fragment.
Yes. It’s called “The OpenGL Graphics System: A Specification.” It’s available for download on the OpenGL Registry; you want the compatibility profile. Section 2.13 of the 4.2 compatibility specification covers all of the math used for lighting. Simply translate that into GLSL code.
Note that you’re not going to be able to exactly mimic the fixed-function pipeline. That is, there is no way to guarantee invariance between shader-based and fixed-function lighting.You’ll get something close, but not binary-identical values.
How do you define “excessive”? You’re going to need to branch, because OpenGL’s lighting equations involve conditional logic.
That’s generally part of the reason why people don’t just re-implement GL’s lighting in a shader. You can be much more efficient if you just write GLSL, rather than using a data-driven approach.