-
Notifications
You must be signed in to change notification settings - Fork 0
/
framebuffer.cpp
63 lines (54 loc) · 2.3 KB
/
framebuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "framebuffer.h"
#include <iostream>
Framebuffer::Framebuffer(int width, int height) :
texid{},
fb{},
rb{},
width{width},
height{height}
{
// Create framebuffer
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
// create a color attachment texture
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); Can't have this, textures will be weird.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glGenerateMipmap(GL_TEXTURE_2D);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBindTexture(GL_TEXTURE_2D, 0);
// Bind color attatchment to framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texid, 0);
// Create renderbuffer
glGenRenderbuffers(1, &rb);
glBindRenderbuffer(GL_RENDERBUFFER, rb);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); // use a single renderbuffer object for both a depth AND stencil buffer.
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb); // now actually attach it
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
// Unbind
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::bindFBO()
{
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glViewport(0, 0, width, height);
}
void Framebuffer::bindScreenFB()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Hardcoded window size temporary solution, please see main.cpp
glViewport(0, 0, 1080, 800);
}
// Remember to activate correct tex to corresponing texunit beforehand with glActiveTexture(GL_TEXTURE0);
void Framebuffer::bindTex(Shader* shader, std::string texName, int texUnit)
{
shader->useProgram();
shader->uploadInt(texName, texUnit);
glBindTexture(GL_TEXTURE_2D, texid);
}