-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathr_gbuffer.cpp
116 lines (90 loc) · 3.78 KB
/
r_gbuffer.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <string.h>
#include "r_gbuffer.h"
#include "m_mat.h"
namespace r {
gBuffer::gBuffer()
: m_fbo(0)
, m_width(0)
, m_height(0)
{
memset(m_textures, 0, sizeof m_textures);
}
gBuffer::~gBuffer() {
destroy();
}
void gBuffer::destroy() {
if (m_fbo)
gl::DeleteFramebuffers(1, &m_fbo);
if (m_textures[0])
gl::DeleteTextures(kMax, m_textures);
}
void gBuffer::update(const m::perspective &p) {
const size_t width = p.width;
const size_t height = p.height;
if (m_width == width && m_height != height)
return;
const GLenum format = gl::has(gl::ARB_texture_rectangle) ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
m_width = width;
m_height = height;
// diffuse + specular
gl::BindTexture(format, m_textures[kColor]);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
// normals + specular
gl::BindTexture(format, m_textures[kNormal]);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
// depth
gl::BindTexture(format, m_textures[kDepth]);
gl::TexImage2D(format, 0, GL_DEPTH24_STENCIL8,
m_width, m_height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
}
bool gBuffer::init(const m::perspective &p) {
m_width = p.width;
m_height = p.height;
gl::GenFramebuffers(1, &m_fbo);
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
gl::GenTextures(kMax, m_textures);
const GLenum format = gl::has(gl::ARB_texture_rectangle) ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
// diffuse + specular
gl::BindTexture(format, m_textures[kColor]);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
gl::TexParameteri(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, format, m_textures[kColor], 0);
// normals + specular
gl::BindTexture(format, m_textures[kNormal]);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
gl::TexParameteri(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, format, m_textures[kNormal], 0);
// depth
gl::BindTexture(format, m_textures[kDepth]);
gl::TexImage2D(format, 0, GL_DEPTH24_STENCIL8,
m_width, m_height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
gl::TexParameteri(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
format, m_textures[kDepth], 0);
static GLenum drawBuffers[] = {
GL_COLOR_ATTACHMENT0, // diffuse + specular intensity
GL_COLOR_ATTACHMENT1 // normal + specular power
};
gl::DrawBuffers(sizeof drawBuffers / sizeof *drawBuffers, drawBuffers);
const GLenum status = gl::CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
return false;
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
return true;
}
void gBuffer::bindWriting() {
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
}
GLuint gBuffer::texture(gBuffer::textureType type) const {
return m_textures[type];
}
}