forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGBuffer.cpp
More file actions
100 lines (85 loc) · 2.2 KB
/
GBuffer.cpp
File metadata and controls
100 lines (85 loc) · 2.2 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
95
96
97
98
99
100
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "GBuffer.h"
#include <GL/glew.h>
#include "Texture.h"
GBuffer::GBuffer()
:mBufferID(0)
{
}
GBuffer::~GBuffer()
{
}
bool GBuffer::Create(int width, int height)
{
// Create the framebuffer object
glGenFramebuffers(1, &mBufferID);
glBindFramebuffer(GL_FRAMEBUFFER, mBufferID);
// Add a depth buffer to this target
GLuint depthBuffer;
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, depthBuffer);
// Create textures for each output in the G-buffer
for (int i = 0; i < NUM_GBUFFER_TEXTURES; i++)
{
Texture* tex = new Texture();
// We want three 32-bit float components for each texture
tex->CreateForRendering(width, height, GL_RGB32F);
mTextures.emplace_back(tex);
// Attach this texture to a color output
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
tex->GetTextureID(), 0);
}
// Create a vector of the color attachments
std::vector<GLenum> attachments;
for (int i = 0; i < NUM_GBUFFER_TEXTURES; i++)
{
attachments.emplace_back(GL_COLOR_ATTACHMENT0 + i);
}
// Set the list of buffers to draw to
glDrawBuffers(static_cast<GLsizei>(attachments.size()),
attachments.data());
// Make sure everything worked
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
Destroy();
return false;
}
return true;
}
void GBuffer::Destroy()
{
glDeleteFramebuffers(1, &mBufferID);
for (Texture* t : mTextures)
{
t->Unload();
delete t;
}
}
Texture* GBuffer::GetTexture(Type type)
{
if (mTextures.size() > 0)
{
return mTextures[type];
}
else
{
return nullptr;
}
}
void GBuffer::SetTexturesActive()
{
for (int i = 0; i < NUM_GBUFFER_TEXTURES; i++)
{
mTextures[i]->SetActive(i);
}
}