Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLEW_STATIC
- #pragma comment( lib, "opengl32.lib" )
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <array>
- #include <cassert>
- GLuint create_framebuffer_msaa( GLsizei width, GLsizei height, GLsizei samples );
- void render();
- int main( int argc, char * argv[] )
- {
- constexpr GLsizei width = 1800;
- constexpr GLsizei height = 1600;
- constexpr GLsizei samples = 4;
- ::glfwInit();
- ::glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
- // ::glfwWindowHint( GLFW_SAMPLES, samples );
- GLFWwindow * window = ::glfwCreateWindow( width, height, "ogl.window", {}, {} );
- ::glfwMakeContextCurrent( window );
- ::glewInit();
- ::glfwSwapInterval( 0 );
- GLuint draw_framebuffer = create_framebuffer_msaa( width, height, samples );
- GLuint default_framebuffer = 0;
- while ( !::glfwWindowShouldClose( window ) )
- {
- ::glfwPollEvents();
- glBindFramebuffer( GL_FRAMEBUFFER, draw_framebuffer );
- render();
- glBlitNamedFramebuffer( draw_framebuffer,
- default_framebuffer,
- 0,
- 0,
- width,
- height,
- 0,
- 0,
- width,
- height,
- GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
- GL_NEAREST );
- glBindFramebuffer( GL_FRAMEBUFFER, 0 );
- ::glfwSwapBuffers( window );
- }
- }
- GLuint create_framebuffer_msaa( GLsizei width, GLsizei height, GLsizei samples )
- {
- GLuint framebuffer = 0;
- glCreateFramebuffers( 1, &framebuffer );
- GLuint color_texture = {};
- glCreateTextures( GL_TEXTURE_2D_MULTISAMPLE, 1, &color_texture );
- glTextureStorage2DMultisample( color_texture, samples, GL_RGBA8 /*GL_RGB8*/, width, height, false );
- glNamedFramebufferTexture( framebuffer, GL_COLOR_ATTACHMENT0, color_texture, 0 );
- GLuint depth_texture = {};
- glCreateTextures( GL_TEXTURE_2D_MULTISAMPLE, 1, &depth_texture );
- glTextureStorage2DMultisample( depth_texture, samples, GL_DEPTH24_STENCIL8, width, height, false );
- glNamedFramebufferTexture( framebuffer, GL_DEPTH_STENCIL_ATTACHMENT, depth_texture, 0 );
- const bool is_complete = glCheckNamedFramebufferStatus( framebuffer, GL_FRAMEBUFFER ) == GL_FRAMEBUFFER_COMPLETE;
- assert( is_complete );
- return framebuffer;
- }
- void draw_triangle()
- {
- glBegin( GL_TRIANGLES );
- glVertex2f( -0.7f, -0.7f );
- glColor3f( 1.f, 0.f, 0.f );
- glVertex2f( 1.f, 0.f );
- glColor3f( 0.f, 1.f, 0.f );
- glVertex2f( 0.f, 1.f );
- glColor3f( 0.f, 0.f, 1.f );
- glEnd();
- }
- void clear()
- {
- std::array color{0.5f, 1.f, 0.7f, 1.f};
- glClearBufferfv( GL_COLOR, 0, std::data( color ) );
- GLfloat depth = 1;
- glClearBufferfv( GL_DEPTH, 0, &depth );
- }
- void rotate( GLfloat angle )
- {
- glRotatef( angle, 1.f, 1.f, 1.f );
- }
- void render()
- {
- clear();
- rotate( 3.14f );
- draw_triangle();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement