Guest User

Untitled

a guest
Jan 31st, 2019
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define GLEW_STATIC
  2. #pragma comment( lib, "opengl32.lib" )
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5.  
  6. #include <array>
  7. #include <cassert>
  8.  
  9. GLuint create_framebuffer_msaa( GLsizei width, GLsizei height, GLsizei samples );
  10. void   render();
  11.  
  12. int main( int argc, char * argv[] )
  13. {
  14.   constexpr GLsizei width   = 1800;
  15.   constexpr GLsizei height  = 1600;
  16.   constexpr GLsizei samples = 4;
  17.  
  18.   ::glfwInit();
  19.   ::glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
  20.  
  21.   // ::glfwWindowHint( GLFW_SAMPLES, samples );
  22.  
  23.   GLFWwindow * window = ::glfwCreateWindow( width, height, "ogl.window", {}, {} );
  24.   ::glfwMakeContextCurrent( window );
  25.   ::glewInit();
  26.   ::glfwSwapInterval( 0 );
  27.  
  28.   GLuint draw_framebuffer    = create_framebuffer_msaa( width, height, samples );
  29.   GLuint default_framebuffer = 0;
  30.  
  31.   while ( !::glfwWindowShouldClose( window ) )
  32.   {
  33.     ::glfwPollEvents();
  34.     glBindFramebuffer( GL_FRAMEBUFFER, draw_framebuffer );
  35.     render();
  36.     glBlitNamedFramebuffer( draw_framebuffer,
  37.                             default_framebuffer,
  38.                             0,
  39.                             0,
  40.                             width,
  41.                             height,
  42.                             0,
  43.                             0,
  44.                             width,
  45.                             height,
  46.                             GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
  47.                             GL_NEAREST );
  48.     glBindFramebuffer( GL_FRAMEBUFFER, 0 );
  49.  
  50.     ::glfwSwapBuffers( window );
  51.   }
  52. }
  53.  
  54. GLuint create_framebuffer_msaa( GLsizei width, GLsizei height, GLsizei samples )
  55. {
  56.   GLuint framebuffer = 0;
  57.   glCreateFramebuffers( 1, &framebuffer );
  58.  
  59.   GLuint color_texture = {};
  60.   glCreateTextures( GL_TEXTURE_2D_MULTISAMPLE, 1, &color_texture );
  61.   glTextureStorage2DMultisample( color_texture, samples, GL_RGBA8 /*GL_RGB8*/, width, height, false );
  62.   glNamedFramebufferTexture( framebuffer, GL_COLOR_ATTACHMENT0, color_texture, 0 );
  63.  
  64.   GLuint depth_texture = {};
  65.   glCreateTextures( GL_TEXTURE_2D_MULTISAMPLE, 1, &depth_texture );
  66.   glTextureStorage2DMultisample( depth_texture, samples, GL_DEPTH24_STENCIL8, width, height, false );
  67.   glNamedFramebufferTexture( framebuffer, GL_DEPTH_STENCIL_ATTACHMENT, depth_texture, 0 );
  68.  
  69.   const bool is_complete = glCheckNamedFramebufferStatus( framebuffer, GL_FRAMEBUFFER ) == GL_FRAMEBUFFER_COMPLETE;
  70.   assert( is_complete );
  71.  
  72.   return framebuffer;
  73. }
  74.  
  75. void draw_triangle()
  76. {
  77.   glBegin( GL_TRIANGLES );
  78.   glVertex2f( -0.7f, -0.7f );
  79.   glColor3f( 1.f, 0.f, 0.f );
  80.   glVertex2f( 1.f, 0.f );
  81.   glColor3f( 0.f, 1.f, 0.f );
  82.   glVertex2f( 0.f, 1.f );
  83.   glColor3f( 0.f, 0.f, 1.f );
  84.   glEnd();
  85. }
  86.  
  87. void clear()
  88. {
  89.   std::array color{0.5f, 1.f, 0.7f, 1.f};
  90.   glClearBufferfv( GL_COLOR, 0, std::data( color ) );
  91.  
  92.   GLfloat depth = 1;
  93.   glClearBufferfv( GL_DEPTH, 0, &depth );
  94. }
  95.  
  96. void rotate( GLfloat angle )
  97. {
  98.   glRotatef( angle, 1.f, 1.f, 1.f );
  99. }
  100.  
  101. void render()
  102. {
  103.   clear();
  104.   rotate( 3.14f );
  105.   draw_triangle();
  106. }
RAW Paste Data