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

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