summaryrefslogtreecommitdiff
path: root/gfx/gl/sceneRenderer.cpp
blob: 582c28c94a56c3a91e64b342bd1622eb9f3a8a58 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "sceneRenderer.h"
#include "maths.h"
#include "vertexArrayObject.h"
#include <gfx/gl/shaders/fs-directionalLight.h>
#include <gfx/gl/shaders/fs-lighting.h>
#include <gfx/gl/shaders/vs-lighting.h>
#include <glm/gtc/type_ptr.hpp>

static constexpr const std::array<const glm::i8vec4, 4> displayVAOdata {{
		// positions(x,y) texture coords(z,w)
		{-1, 1, 0, 1},
		{-1, -1, 0, 0},
		{1, 1, 1, 1},
		{1, -1, 1, 0},
}};

SceneRenderer::SceneRenderer(ScreenAbsCoord s, GLuint o) :
	camera {{-1250000, -1250000, 35.0F}, quarter_pi, ratio(s), 100, 10000000}, size {s}, output {o},
	lighting {lighting_vs, lighting_fs}, shadowMapper {{2048, 2048}}
{
	shader.setViewPort({0, 0, size.x, size.y});
	VertexArrayObject {displayVAO}.addAttribs<glm::i8vec4>(displayVBO, displayVAOdata);

	glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
	const auto configuregdata
			= [this](const GLuint data, const std::initializer_list<GLint> formats, const GLenum attachment) {
				  glBindTexture(GL_TEXTURE_2D, data);
				  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
				  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				  for (const auto format : formats) {
					  glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGB, GL_BYTE, nullptr);

					  glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, data, 0);
					  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {
						  return format;
					  }
				  }
				  throw std::runtime_error("Framebuffer could not be completed!");
			  };
	configuregdata(gPosition, {GL_RGB32F}, GL_COLOR_ATTACHMENT0);
	configuregdata(gNormal, {GL_RGB8_SNORM, GL_RGB16F}, GL_COLOR_ATTACHMENT1);
	configuregdata(gAlbedoSpec, {GL_RGB8}, GL_COLOR_ATTACHMENT2);
	configuregdata(gIllumination, {GL_RGB8}, GL_COLOR_ATTACHMENT3);

	glBindRenderbuffer(GL_RENDERBUFFER, depth);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size.x, size.y);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);

	glBindFramebuffer(GL_FRAMEBUFFER, output);
}

void
SceneRenderer::render(const SceneProvider & scene) const
{
	shader.setViewProjection(camera.getPosition(), camera.getViewProjection());
	glViewport(0, 0, size.x, size.y);

	// Geometry pass
	glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
	static constexpr std::array<unsigned int, 3> attachments {
			GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
	glDrawBuffers(attachments.size(), attachments.data());
	glEnable(GL_BLEND);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glClearColor(0, 0, 0, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	scene.content(shader);

	// Illumination pass
	glDrawBuffer(GL_COLOR_ATTACHMENT3);
	glBlendFunc(GL_ONE, GL_ONE);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, gPosition);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, gNormal);
	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, shadowMapper);
	scene.environment(shader, *this);
	glDisable(GL_DEPTH_TEST);
	scene.lights(shader);

	// Lighting pass
	glBindFramebuffer(GL_FRAMEBUFFER, output);
	glViewport(0, 0, size.x, size.y);
	glCullFace(GL_BACK);
	glDisable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, gAlbedoSpec);
	glActiveTexture(GL_TEXTURE3);
	glBindTexture(GL_TEXTURE_2D, gIllumination);
	lighting.use();
	renderQuad();
}

void
SceneRenderer::setAmbientLight(const RGB & colour) const
{
	glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
	glClearColor(colour.r, colour.g, colour.b, 1.0F);
	glClear(GL_COLOR_BUFFER_BIT);
}

void
SceneRenderer::setDirectionalLight(const RGB & colour, const Direction3D & direction, const SceneProvider & scene) const
{
	if (colour.r > 0 || colour.g > 0 || colour.b > 0) {
		const auto lvp = shadowMapper.update(scene, direction, camera);
		glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
		glViewport(0, 0, size.x, size.y);
		dirLight.use();
		dirLight.setDirectionalLight(colour, direction, lvp.projections, lvp.regions, lvp.maps);
		renderQuad();
	}
}

void
SceneRenderer::renderQuad() const
{
	glBindVertexArray(displayVAO);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	glBindVertexArray(0);
}

SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() :
	Program {lighting_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"},
	colourLoc {*this, "lightColour"}, lightViewProjectionLoc {*this, "lightViewProjection"},
	lightViewProjectionCountLoc {*this, "lightViewProjectionCount"},
	lightViewShadowMapRegionLoc {*this, "shadowMapRegion"}
{
}

void
SceneRenderer::DirectionalLightProgram::setDirectionalLight(const RGB & c, const Direction3D & d,
		const std::span<const glm::mat4x4> lvp, const std::span<const TextureRelRegion> shadowMapRegions,
		std::size_t maps) const
{
	glUniform3fv(colourLoc, 1, glm::value_ptr(c));
	const auto nd = glm::normalize(d);
	glUniform3fv(directionLoc, 1, glm::value_ptr(nd));
	glUniform1ui(lightViewProjectionCountLoc, static_cast<GLuint>(maps));
	glUniformMatrix4fv(lightViewProjectionLoc, static_cast<GLsizei>(maps), GL_FALSE, glm::value_ptr(lvp.front()));
	glUniform4fv(lightViewShadowMapRegionLoc, static_cast<GLsizei>(maps), glm::value_ptr(shadowMapRegions.front()));
}