2302b6ca5d817fc9b84e5d583fb1da10036c446f
[ipdf/code.git] / src / framebuffer.cpp
1 #include "framebuffer.h"
2 #include "gl_core44.h"
3
4 using namespace IPDF;
5
6 void FrameBuffer::Create(int w, int h)
7 {
8         if (m_render_texture)
9         {
10                 Destroy();
11         }
12         m_width = w;
13         m_height = h;
14         glGenTextures(1, &m_render_texture);
15         glGenFramebuffers(1, &m_render_fbo);
16
17         glBindTexture(GL_TEXTURE_2D, m_render_texture);
18         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
19
20         glBindFramebuffer(GL_FRAMEBUFFER, m_render_fbo);
21
22         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_render_texture, 0);
23
24         glBindFramebuffer(GL_FRAMEBUFFER, 0);
25         glBindTexture(GL_TEXTURE_2D, 0);
26 }
27
28 void FrameBuffer::Destroy()
29 {
30         if (!m_render_texture) return;
31         glDeleteFramebuffers(1, &m_render_fbo);
32         glDeleteTextures(1, &m_render_texture);
33 }
34
35 void FrameBuffer::Bind()
36 {
37         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_render_fbo);
38 }
39
40 void FrameBuffer::UnBind()
41 {
42         glBindFramebuffer(GL_FRAMEBUFFER, 0);
43 }
44
45 void FrameBuffer::Blit()
46 {
47         glBindFramebuffer(GL_READ_FRAMEBUFFER, m_render_fbo);
48         glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, m_width, m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
49 }
50
51 void FrameBuffer::Clear(float r, float g, float b, float a)
52 {
53         Bind();
54         glClearColor(r,g,b,a);
55         glClear(GL_COLOR_BUFFER_BIT);
56 }
57

UCC git Repository :: git.ucc.asn.au