8

I can get a shader to read in two textures but for output it seems there is only gl_FragColor. Is there any way to render to two different textures from one shader? I am using Processing and the GLGraphics library btw.

Moss
  • 3,695
  • 6
  • 40
  • 60

3 Answers3

5

Yes, you can write to gl_FragData, which is an array of outputs (size depends on your implementation). Or with newer versions of GL, both gl_FragColor and gl_FragData are deprecated and you declare your own out variables for the fragment shader to write to. Declare multiple such out vars for multiple output buffers.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Thanks. I'd like to see a code example of how this would be done in GLSL and maybe some pseudo code for the host application if that is the right term. – Moss Nov 13 '10 at 23:32
  • 1
    Actually, I figured it out myself. I just had to say "varying out vec4 Color1, Color2" etc. at the top and that seemed to automatically link itself to the array of textures I was already passing in. And then set those variables to something somewhere within void main(). – Moss Nov 15 '10 at 07:38
  • 1
    Is this supported in OpenglES 2.0 ? – Deepak Sharma Oct 07 '14 at 10:23
1

Yes. We can. BUT Notice that gl_FragData is no longer supported in GLSL 4.0. Specify the location as:

glBindFragDataLocation(programHandle, 0, "FragColor");

.....

I hope you could read the book of glsl 4.0 shading language cookbook.

RAS
  • 8,100
  • 16
  • 64
  • 86
ShannonLiu
  • 101
  • 1
  • 1
0

I don't know if this is exactly what you're trying to do, but with Frame Buffer Objects (FBOs) you can draw to multiple color buffers simultaneously.

Within the shader, though, it will still just be as if you're writing one fragment. That is, the shader is unaware of how many attachments the FBO has.

Drew Hall
  • 28,429
  • 12
  • 61
  • 81