Search code examples
openglframebufferdeferred-rendering

How does rendering to multiple textures work in modern OpenGL?


If I understand FBOs correctly, I can attach several 2D textures to different color attachment points. I'd like to use this capability to write out a few different types of data from a fragment shader (e.g. world position and normals). It looks like in ye olden days, this involved writing to gl_FragData at different indices, but gl_FragData doesn't exist anymore. How is this done now?


Solution

  • You can just add output variables to your fragment shader. Here is an example:

    layout(location=0) out vec4 color;
    layout(location=1) out vec3 normal;
    layout(location=2) out vec3 position;
    
    void main() {
        color = vec4(1,0,0,1);
        normal = vec3(0,1,0);
        position = vec3(1,2,3);
    }