-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframebuffer.hpp
More file actions
94 lines (75 loc) · 3.14 KB
/
framebuffer.hpp
File metadata and controls
94 lines (75 loc) · 3.14 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
framebuffer.hpp
author: Telo PHILIPPE
*/
#ifndef FRAMEBUFFER_HPP
#define FRAMEBUFFER_HPP
#include <iostream>
#include <random>
#include <ctime>
#include "gl_includes.hpp"
#include "mesh.hpp"
class FrameBuffer {
public:
GLuint m_Buffer {};
GLuint m_position {};
GLuint m_normal {};
GLuint m_albedo {};
int m_Width {};
int m_Height {};
std::shared_ptr<Mesh> m_quad {};
public:
FrameBuffer(int width, int height) {
m_Width = width;
m_Height = height;
initFramebuffer();
initMesh();
}
void initFramebuffer() {
glGenFramebuffers(1, &m_Buffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_Buffer);
// - Position color buffer
glGenTextures(1, &m_position);
glBindTexture(GL_TEXTURE_2D, m_position);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_position, 0);
// - Normal color buffer
glGenTextures(1, &m_normal);
glBindTexture(GL_TEXTURE_2D, m_normal);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_normal, 0);
// - Color + Specular color buffer
glGenTextures(1, &m_albedo);
glBindTexture(GL_TEXTURE_2D, m_albedo);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_albedo, 0);
// - Tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
GLuint attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, attachments);
// - Create and attach depth buffer (renderbuffer)
GLuint rboDepth;
glGenRenderbuffers(1, &rboDepth);
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_Width, m_Height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
// - Finally check if framebuffer is complete
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
}
void initMesh() {
m_quad = Mesh::genPlane();
}
~FrameBuffer() {
glDeleteFramebuffers(1, &m_Buffer);
glDeleteTextures(1, &m_position);
glDeleteTextures(1, &m_normal);
glDeleteTextures(1, &m_albedo);
}
};
#endif // FRAMEBUFFER_HPP