title: How to use Multisampling with OpenGL FBOs
body: <p>I'm trying to enable mutlisampling and alpha-to-coverage for an FBO. Using the default framebuffer, all I have to do is call <code>glEnable(GL_MULTISAMPLE)</code> and <code>glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE)</code>. However, I am unable to achieve the same effect using my own FBO.</p>
  
  <p><strong>My goal:</strong> Draw the scene to an FBO the same way it would be drawn to the default framebuffer with the above properties. From there I want to be able to use the image as a texture for future passes through a shader.</p>
  
  <p><strong>This works</strong>: Code for making an FBO without multisampling/alpha-to-coverage, 1 color attachment, 1 depth attachment:</p>
  
  <pre><code>// Generate the color attachment
  glGenTextures(1,&amp;defaultColorAttachment0);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D,defaultColorAttachment0);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,screenWidth,screenHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
  
  // Bind the texture to the FBO
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, defaultColorAttachment0,0);
  
  // Generate the depth attachment
  glGenRenderbuffers(1,&amp;defaultDepthBuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, defaultDepthBuffer);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, screenWidth, screenHeight);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, defaultDepthBuffer);
  </code></pre>
  
  <p><strong>This doesn't work</strong>. Code trying to make a multisampled FBO:</p>
  
  <pre><code>// Generate the color attachment
  glGenTextures(1,&amp;defaultColorAttachment0);
  glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, defaultColorAttachment0);
  glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, screenWidth, screenHeight, GL_FALSE);
  glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, defaultColorAttachment0,0);
  
  // Generate the depth attachment
  glGenRenderbuffers(1,&amp;defaultDepthBuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, defaultDepthBuffer);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, screenWidth, screenHeight);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, defaultDepthBuffer);
  </code></pre>
  
  <p>I have tried looking through the OpenGL wiki on this, although the it's incomplete (various unfinished headings make it look unprofessional). <code>glGetError</code> never complains. I've tried messing around with this, but I either get a black screen or a screen full of garbage pixels.</p>
  
  <p><strong>Main Question:</strong> What things do I need to consider/change and where (FBO creation, textures, shaders) in order to get multisampling and alpha-to-coverage to work with an FBO?</p>