SVG tests are 100% less symlinky
[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                 // FrameBuffer was already Created, destroy it before creating again
11                 Destroy();
12         }
13         m_width = w;
14         m_height = h;
15         glGenTextures(1, &m_render_texture);
16         glGenFramebuffers(1, &m_render_fbo);
17
18         glBindTexture(GL_TEXTURE_2D, m_render_texture);
19         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
20
21         glBindFramebuffer(GL_FRAMEBUFFER, m_render_fbo);
22
23         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_render_texture, 0);
24
25         glBindFramebuffer(GL_FRAMEBUFFER, 0);
26         glBindTexture(GL_TEXTURE_2D, 0);
27 }
28
29 void FrameBuffer::Destroy()
30 {
31         if (!m_render_texture) return; // wasn't Created (or already Destroyed)
32         glDeleteFramebuffers(1, &m_render_fbo);
33         glDeleteTextures(1, &m_render_texture);
34 }
35
36 void FrameBuffer::Bind()
37 {
38         // will draw to this FrameBuffer
39         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_render_fbo);
40 }
41
42 void FrameBuffer::UnBind()
43 {
44         glBindFramebuffer(GL_FRAMEBUFFER, 0); // will both draw to and read pixels from screen
45 }
46
47 void FrameBuffer::Blit()
48 {
49         glBindFramebuffer(GL_READ_FRAMEBUFFER, m_render_fbo); // read pixels from this FrameBuffer
50         glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, m_width, m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST); // draw pixels
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