bfops
bfops

Reputation: 5678

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?

Upvotes: 0

Views: 681

Answers (2)

Aiden Koss
Aiden Koss

Reputation: 111

As dari said, you can use the layout(location=0) specifier.

Another method to assign the location outside of the shader is to use:

glBindFragDataLocation(_program, 0, "color");

Then in the shader:

out vec4 color;

See this for a more thorough discussion of output buffers: https://www.opengl.org/wiki/Fragment_Shader#Output_buffers

Upvotes: 2

dari
dari

Reputation: 2455

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);
}

Upvotes: 4

Related Questions