title: offscreen rendering opengl 4.5 multisample FBO
body: <p>I'm referencing OpenGL Superbible 6 in my code.
  First I simply wanted to implement object picking in my 3d scene. Eventually I've decided to use framebuffer objects and I have succeeded and then I understood the problem with the need to solve the problem of polygon edge aliasing, so, i've rewritten my code again to make use of GL_TEXTURE_2D_MULTISAMPLE</p>
  
  <p>Here is the initialization code for framebuffer
  void window_glview::init_framebuffer()</p>
  
  <pre><code>{
      //CREATE FRAMEBUFFER OBJECT
      GLenum gl_error=glGetError();
  
      glGenTextures(1,&amp;texture_id_framebuffer_color);
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_color);
      glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,ANTIALIASING_SAMPLES,GL_RGBA8,client_area.right,client_area.bottom,GL_TRUE);
  
      glGenTextures(1,&amp;texture_id_framebuffer_objectid);
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_objectid);
      glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,ANTIALIASING_SAMPLES,GL_RGBA8,client_area.right,client_area.bottom,GL_TRUE);
  
      glGenTextures(1,&amp;texture_id_framebuffer_depth);
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_depth);
      glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,ANTIALIASING_SAMPLES,GL_DEPTH_COMPONENT32,client_area.right,client_area.bottom,GL_TRUE);
      gl_error=glGetError();
  
      glGenFramebuffers(1,&amp;buffer_id_framebuffer);
      glBindFramebuffer(GL_FRAMEBUFFER,buffer_id_framebuffer);
      gl_error=glGetError();
      glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_color,0);
      glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,texture_id_framebuffer_objectid,0);
      glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,texture_id_framebuffer_depth,0);
  
      GLenum draw_buffers[] =
      {
          GL_COLOR_ATTACHMENT0,
          GL_COLOR_ATTACHMENT1
      };
      glDrawBuffers(2,draw_buffers);
      GLenum status=glCheckFramebufferStatus(GL_FRAMEBUFFER);
  
      if(status!=GL_FRAMEBUFFER_COMPLETE)
          MessageBox(0,L"Failed to create framebuffer object",0,0);
      glBindFramebuffer(GL_FRAMEBUFFER,0);
  }
  </code></pre>
  
  <p>It's pretty common to most of the internet listings on the same topic.
  Now here is my drawing code</p>
  
  <pre><code>void window_glview::paint()
  {
      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  
  
          //DRAW TO CUSTOM FRAMEBUFFER
          glBindFramebuffer(GL_FRAMEBUFFER,buffer_id_framebuffer);
          glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  
          glLineWidth(1.0);
          draw_viewport();
          viewport_object_count=0;
  
          draw_lights();
          glLineWidth(1.5);
          for (unsigned short i=0;i&lt;mesh_count;i++)
          {
              draw_mesh(mesh_table[i],GL_TRIANGLES,false);
          }
  
      //DRAW TO DEFAULT
      glBindFramebuffer(GL_FRAMEBUFFER,0);
  
      //USE TEXTURE FROM FRAMEBUFFER COLOR_ATTACHMENT0
      glUseProgram(program_id_screen_render);
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE,texture_id_framebuffer_color);
  
      //HERE IS A QUAD DRAWING PROCESS
      glBindBuffer(GL_ARRAY_BUFFER,buffer_id_screen_quad);
      glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,24,0);
      glEnableVertexAttribArray(0);
  
      glDrawArrays(GL_QUADS,0,4);
      SwapBuffers(hDC);
  }
  </code></pre>
  
  <p>vertex shader is simple</p>
  
  <pre><code>#version 450
  
  layout(location=0) in vec4 _pos;
  void main(void)
  {
      gl_Position=_pos;
  }
  </code></pre>
  
  <p>fragment shader is written with the purpose of iterpreting multisamples</p>
  
  <pre><code>#version 450
  uniform sampler2DMS screen_texture;
  layout(location=0) out vec4 out_color;
  
  void main(void) 
  {
      ivec2 coord=ivec2(gl_FragCoord.xy);
      vec4 result=vec4(0.0);
      int i;
      for (i=0;i&lt;4;i++)
      {
          result=max(result,texelFetch(screen_texture,coord,i));
      }
      out_color=result;
  }
  </code></pre>
  
  <p>I end up with a black screen. If i change out_color to something lice out_color=vec4(1.0,0.0,0.0,1.0) i get red screen.</p>
  
  <ol>
  <li>What could go wrong?</li>
  <li>In my initializer function for framebuffer when i pass GL_DEPTH_COMPONENT to glTexStorage2DMultisample, then i get error. I decided to pass GL_DEPTH_COMPONENT16 and it works. Why is that?</li>
  <li>Should I better use RENDERBUFFER for some perpose and if yes, how can i read it to texture?</li>
  </ol>