add emission gbuffer attachment

This commit is contained in:
MihailRis
2025-07-12 21:09:23 +03:00
parent 8a3306ed41
commit 5ec9f14021
12 changed files with 101 additions and 31 deletions
+53 -9
View File
@@ -8,6 +8,8 @@ using namespace advanced_pipeline;
static debug::Logger logger("gl-gbuffer");
// TODO: REFACTOR
void GBuffer::createColorBuffer() {
glGenTextures(1, &colorBuffer);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
@@ -64,7 +66,7 @@ void GBuffer::createNormalsBuffer() {
width,
height,
0,
GL_RGBA,
GL_RGB,
GL_FLOAT,
nullptr
);
@@ -77,6 +79,29 @@ void GBuffer::createNormalsBuffer() {
);
}
void GBuffer::createEmissionBuffer() {
glGenTextures(1, &emissionBuffer);
glBindTexture(GL_TEXTURE_2D, emissionBuffer);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_R8,
width,
height,
0,
GL_RED,
GL_FLOAT,
nullptr
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, emissionBuffer, 0
);
}
void GBuffer::createDepthBuffer() {
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
@@ -113,16 +138,21 @@ GBuffer::GBuffer(uint width, uint height) : width(width), height(height) {
createColorBuffer();
createPositionsBuffer();
createNormalsBuffer();
createEmissionBuffer();
GLenum attachments[3] = {
GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2
GLenum attachments[4] = {
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3,
};
glDrawBuffers(3, attachments);
glDrawBuffers(4, attachments);
createDepthBuffer();
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
logger.error() << "gbuffer is not complete!";
int status;
if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
logger.error() << "gbuffer is not complete! (" << status << ")";
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
@@ -134,8 +164,10 @@ GBuffer::GBuffer(uint width, uint height) : width(width), height(height) {
);
GLenum ssaoAttachments[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, ssaoAttachments);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
logger.error() << "SSAO framebuffer is not complete!";
if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
logger.error() << "SSAO framebuffer is not complete! (" << status << ")";
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
@@ -144,6 +176,7 @@ GBuffer::~GBuffer() {
glDeleteTextures(1, &colorBuffer);
glDeleteTextures(1, &positionsBuffer);
glDeleteTextures(1, &normalsBuffer);
glDeleteTextures(1, &emissionBuffer);
glDeleteTextures(1, &ssaoBuffer);
glDeleteRenderbuffers(1, &depthBuffer);
glDeleteFramebuffers(1, &fbo);
@@ -164,6 +197,9 @@ void GBuffer::unbind() {
}
void GBuffer::bindBuffers() const {
glActiveTexture(GL_TEXTURE0 + TARGET_EMISSION);
glBindTexture(GL_TEXTURE_2D, emissionBuffer);
glActiveTexture(GL_TEXTURE0 + TARGET_NORMALS);
glBindTexture(GL_TEXTURE_2D, normalsBuffer);
@@ -227,12 +263,20 @@ void GBuffer::resize(uint width, uint height) {
glBindTexture(GL_TEXTURE_2D, normalsBuffer);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, nullptr
GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, normalsBuffer, 0
);
glBindTexture(GL_TEXTURE_2D, emissionBuffer);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, GL_LUMINANCE, GL_FLOAT, nullptr
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, emissionBuffer, 0
);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
+2
View File
@@ -32,6 +32,7 @@ private:
uint colorBuffer;
uint positionsBuffer;
uint normalsBuffer;
uint emissionBuffer;
uint depthBuffer;
uint ssaoFbo;
uint ssaoBuffer;
@@ -39,6 +40,7 @@ private:
void createColorBuffer();
void createPositionsBuffer();
void createNormalsBuffer();
void createEmissionBuffer();
void createDepthBuffer();
void createSSAOBuffer();
};
+1
View File
@@ -102,6 +102,7 @@ void PostProcessing::configureEffect(
if (gbuffer) {
shader.uniform1i("u_position", TARGET_POSITIONS);
shader.uniform1i("u_normal", TARGET_NORMALS);
shader.uniform1i("u_emission", TARGET_EMISSION);
}
shader.uniform1i("u_noise", TARGET_SSAO); // used in SSAO pass
shader.uniform1i("u_ssao", TARGET_SSAO);
+4 -3
View File
@@ -55,9 +55,10 @@ namespace advanced_pipeline {
inline constexpr int TARGET_SKYBOX = 1;
inline constexpr int TARGET_POSITIONS = 2;
inline constexpr int TARGET_NORMALS = 3;
inline constexpr int TARGET_SSAO = 4;
inline constexpr int TARGET_SHADOWS0 = 5;
inline constexpr int TARGET_SHADOWS1 = 6;
inline constexpr int TARGET_EMISSION = 4;
inline constexpr int TARGET_SSAO = 5;
inline constexpr int TARGET_SHADOWS0 = 6;
inline constexpr int TARGET_SHADOWS1 = 7;
}
VC_ENUM_METADATA(CursorShape)
+23 -16
View File
@@ -39,9 +39,13 @@ BlocksRenderer::~BlocksRenderer() {
/// Basic vertex add method
void BlocksRenderer::vertex(
const glm::vec3& coord, float u, float v, const glm::vec4& light, const glm::vec3& normal
const glm::vec3& coord,
float u,
float v,
const glm::vec4& light,
const glm::vec3& normal,
float emission
) {
vertexBuffer[vertexCount].position = coord;
vertexBuffer[vertexCount].uv = {u,v};
@@ -49,11 +53,13 @@ void BlocksRenderer::vertex(
vertexBuffer[vertexCount].normal[0] = static_cast<uint8_t>(normal.r * 127 + 128);
vertexBuffer[vertexCount].normal[1] = static_cast<uint8_t>(normal.g * 127 + 128);
vertexBuffer[vertexCount].normal[2] = static_cast<uint8_t>(normal.b * 127 + 128);
vertexBuffer[vertexCount].normal[3] = static_cast<uint8_t>(emission * 255);
vertexBuffer[vertexCount].color[0] = static_cast<uint8_t>(light.r * 255);
vertexBuffer[vertexCount].color[1] = static_cast<uint8_t>(light.g * 255);
vertexBuffer[vertexCount].color[2] = static_cast<uint8_t>(light.b * 255);
vertexBuffer[vertexCount].color[3] = static_cast<uint8_t>(light.a * 255);
vertexCount++;
}
@@ -86,10 +92,10 @@ void BlocksRenderer::face(
auto Y = axisY * h;
auto Z = axisZ * d;
float s = 0.5f;
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, lights[0] * tint, axisZ);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, lights[1] * tint, axisZ);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, lights[2] * tint, axisZ);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, lights[3] * tint, axisZ);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, lights[0] * tint, axisZ, 0);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, lights[1] * tint, axisZ, 0);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, lights[2] * tint, axisZ, 0);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, lights[3] * tint, axisZ, 0);
index(0, 1, 3, 1, 2, 3);
}
@@ -107,7 +113,7 @@ void BlocksRenderer::vertexAO(
axisX,
axisY
);
vertex(coord, u, v, light * tint, axisZ);
vertex(coord, u, v, light * tint, axisZ, 0.0f);
}
void BlocksRenderer::faceAO(
@@ -140,10 +146,10 @@ void BlocksRenderer::faceAO(
} else {
auto axisZ = glm::normalize(Z);
glm::vec4 tint(1.0f);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisZ);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisZ);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisZ);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisZ);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisZ, 1);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisZ, 1);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisZ, 1);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisZ, 1);
}
index(0, 1, 2, 0, 2, 3);
}
@@ -168,10 +174,10 @@ void BlocksRenderer::face(
d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
tint *= d;
}
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, Z);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, Z);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, Z);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, Z);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, Z, lights ? 0 : 1);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, Z, lights ? 0 : 1);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, Z, lights ? 0 : 1);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, Z, lights ? 0 : 1);
index(0, 1, 2, 0, 2, 3);
}
@@ -345,7 +351,8 @@ void BlocksRenderer::blockCustomModel(
vertex.uv.x,
vertex.uv.y,
glm::vec4(d, d, d, d) * aoColor,
n
n,
0.0f
);
indexBuffer[indexCount++] = vertexOffset++;
}
+8 -1
View File
@@ -44,7 +44,14 @@ class BlocksRenderer {
SortingMeshData sortingMesh;
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light, const glm::vec3& normal);
void vertex(
const glm::vec3& coord,
float u,
float v,
const glm::vec4& light,
const glm::vec3& normal,
float emission
);
void index(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f);
void vertexAO(