gbuffer.cpp 4.55 KB
Newer Older
's avatar
committed
1
#include "GBuffer.h" 
2
3
#include <core/state.h>
#include <core/numeric/flags.h>
unknown's avatar
unknown committed
4
#include <util/log.h>
5
#include <util/files.h>
6
7
#include <core/objects/light.h>
#include <core/rendering/light_manager.h>
's avatar
committed
8
9
10
11
12
13
14

namespace glare
{
	namespace core
	{
		GBuffer::GBuffer(unsigned int width, unsigned int height, unsigned samples) : m_width(width), m_height(height)
		{
15
			const std::vector<std::shared_ptr<glare::core::Shader>> shaders = {
16
17
				std::make_shared<glare::core::Shader>(gl::ShaderType::eVertex, files::shader("screenshader/screen_triangle.vert")),
				std::make_shared<glare::core::Shader>(gl::ShaderType::eFragment, files::shader("screenshader/gbuffer/gbuffer.frag"))
's avatar
committed
18
19
20
			};
			m_texture_renderer = std::make_unique<TextureRenderer>(std::make_shared<glare::core::ShaderProgram>(shaders));
			
21
22
23
24
25
26
27
28
29
			m_gbuffer_framebuffer = std::make_shared<Framebuffer<TextureMultisampled>>(width, height, samples);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor0);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor1);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor2);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor3);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor4);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor5);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor6);
			m_gbuffer_framebuffer->attach(gl::Attachment::eDepth);
's avatar
committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
			
			m_lights_buffer = std::make_unique<Buffer<gl::BufferType::eShaderStorage>>();
		}

		GBuffer::~GBuffer()
		{
		}

		void GBuffer::record(std::function<void()> render_function) const
		{
			activate();
			render_function();
			deactivate();
		}

		void GBuffer::activate() const
		{
			m_gbuffer_framebuffer->activate();
			gl::clear(gl::ClearBufferBits::eColor | gl::ClearBufferBits::eDepth);
		}

		void GBuffer::deactivate() const
		{
53
			m_gbuffer_framebuffer->deactivate();
's avatar
committed
54
55
56
57
58
59
60
		}

		void GBuffer::draw() const
		{
			gl::setEnabled(gl::EnableParameter::eDepthTest, false);
			m_texture_renderer->shader().use();

61
			m_lights_buffer->upload(LightManager::getInstance().storage(), gl::BufferUsage::eDynamicCopy);
's avatar
committed
62
63
			m_texture_renderer->shader().updateStorageBuffer("lightsBuffer", m_lights_buffer);
			LightManager::getInstance().clear();
64

65
66
			m_texture_renderer->shader().updateUniformMatrix("u_view", state::camera->getView());
			m_texture_renderer->shader().updateUniformMatrix("u_projection", state::camera->getProjection());
's avatar
committed
67
68
69
70

			m_texture_renderer->shader().updateUniformInt("screen.width", m_width);
			m_texture_renderer->shader().updateUniformInt("screen.height", m_height);

71
72
73
74
75
76
77
78
			m_texture_renderer->shader().updateTexture("u_gbuffer.depth", m_gbuffer_framebuffer->depthAttachment());
			m_texture_renderer->shader().updateTexture("u_gbuffer.diffuse", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor0));
			m_texture_renderer->shader().updateTexture("u_gbuffer.normals", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor1));
			m_texture_renderer->shader().updateTexture("u_gbuffer.specular", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor2));
			m_texture_renderer->shader().updateTexture("u_gbuffer.id", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor3));
			m_texture_renderer->shader().updateTexture("u_gbuffer.position", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor4));
			m_texture_renderer->shader().updateTexture("u_gbuffer.position_raw", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor5));
			m_texture_renderer->shader().updateTexture("u_gbuffer.lighting", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor6));
Johannes Braun's avatar
Johannes Braun committed
79
			
80
			m_texture_renderer->shader().updateUniformUInt("u_samples", m_gbuffer_framebuffer->samples());
's avatar
committed
81
82

			gl::activeTexture(0);
83
			gl::bindTexture(gl::TextureTarget::eCubeMap, m_skybox->id());
's avatar
committed
84
			m_texture_renderer->shader().updateUniformInt("environment", 0);
85
			m_texture_renderer->shader().updateUniformVec3("camera_position", glm::vec3(state::camera->getOwner()->absoluteTransform() * glm::vec4(0,0,0,1)));
's avatar
committed
86
87
88
89
90
91
92
93
94
95
			m_texture_renderer->draw();
			gl::bindTexture(gl::TextureTarget::eCubeMap, 0);
			gl::setEnabled(gl::EnableParameter::eDepthTest, true);
		}

		void GBuffer::setSkybox(std::shared_ptr<Skybox> skybox)
		{
			m_skybox = skybox;
		}

96
		const Framebuffer<TextureMultisampled>& GBuffer::framebuffer() const
's avatar
committed
97
98
99
		{
			return *m_gbuffer_framebuffer;
		}
100
101
102
103
104

		void GBuffer::updateSize(unsigned width, unsigned height)
		{
			m_width = width;
			m_height = height;
105
			m_gbuffer_framebuffer->resize(width, height, m_gbuffer_framebuffer->samples());
106
		}
's avatar
committed
107
108
	}
}