summaryrefslogtreecommitdiffstats
path: root/src/Framebuffer.cpp
blob: d81b0e046a88e59ce1cf7ca811ad57896d9a19f3 (plain) (blame)
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
#include "Framebuffer.hpp"
#include "Shader.hpp"
#include <string>
#include "Utility.hpp"
#include "AssetManager.hpp"
#include <optick.h>

const GLuint magic = 316784;
GLuint quadVao = magic, quadVbo = magic;

Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer) : width(width), height(height) {
	OPTICK_EVENT();
	if (quadVao == magic) {
		float quadVertices[] = {
			// positions   // texCoords
			-1.0f,  1.0f,  0.0f, 1.0f,
			-1.0f, -1.0f,  0.0f, 0.0f,
			 1.0f, -1.0f,  1.0f, 0.0f,

			-1.0f,  1.0f,  0.0f, 1.0f,
			 1.0f, -1.0f,  1.0f, 0.0f,
			 1.0f,  1.0f,  1.0f, 1.0f
		};

		glGenVertexArrays(1, &quadVao);
		glGenBuffers(1, &quadVbo);
		glBindVertexArray(quadVao);
		glBindBuffer(GL_ARRAY_BUFFER, quadVbo);
		glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
		AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate();
		AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->SetUniform("inputTexture", 1);
		glActiveTexture(GL_TEXTURE1);
		glCheckError();
	}

	glGenTextures(1, &texColor);
	glBindTexture(GL_TEXTURE_2D, texColor);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glCheckError();

	if (createDepthStencilBuffer) {
		glGenRenderbuffers(1, &rboDepthStencil);
		glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil);
		glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
	}

	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColor, 0);
	if(createDepthStencilBuffer)
		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepthStencil);
	glCheckError();

	GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE)
		throw std::runtime_error("Failed to initialize framebuffer: " + std::to_string(framebufferStatus));	
}

Framebuffer::~Framebuffer() {
	if (rboDepthStencil)
		glDeleteRenderbuffers(1, &rboDepthStencil);
	if (texColor)
		glDeleteTextures(1, &texColor);

	glDeleteFramebuffers(1, &fbo);
}

void Framebuffer::Activate() {
	OPTICK_EVENT();
	glViewport(0, 0, width, height);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
}

void Framebuffer::RenderTo(Framebuffer &target) {
	OPTICK_EVENT();
	glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
	glViewport(0, 0, target.width, target.height);	
	AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate();
	glBindVertexArray(quadVao);
	glBindTexture(GL_TEXTURE_2D, texColor);
	glDrawArrays(GL_TRIANGLES, 0, 6);
}

void Framebuffer::Resize(unsigned int newWidth, unsigned int newHeight) {
	width = newWidth;
	height = newHeight;
	if (texColor) {
		glBindTexture(GL_TEXTURE_2D, texColor);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, newWidth, newHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
	}	
	if (rboDepthStencil) {
		glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil);
		glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
	}
}

Framebuffer &Framebuffer::GetDefault() {
	OPTICK_EVENT();
	static char fboDefaultData[sizeof(Framebuffer)];
	static Framebuffer *fboDefault = nullptr;
	if (fboDefault == nullptr) {
		fboDefault = reinterpret_cast<Framebuffer*>(fboDefaultData);
		fboDefault->fbo = 0;
		fboDefault->width = 1;
		fboDefault->height = 1;
		fboDefault->texColor = 0;
		fboDefault->rboDepthStencil = 0;
	}
	return *fboDefault;
}

void Framebuffer::Clear(bool color, bool depth, bool stencil) {
	OPTICK_EVENT();
	Activate();
	GLbitfield clearBits = 0;
	if (color)
		clearBits |= GL_COLOR_BUFFER_BIT;
	if (depth)
		clearBits |= GL_DEPTH_BUFFER_BIT;
	if (stencil)
		clearBits |= GL_STENCIL_BUFFER_BIT;
	glClear(clearBits);
}