couch/core/Screen.cpp

82 lines
2.5 KiB
C++
Raw Normal View History

2021-01-19 16:01:37 -06:00
#include "Screen.h"
const Vertex vertices[] = {
Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 1.0f),
2021-01-20 12:50:59 -06:00
Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f),
2021-01-19 16:01:37 -06:00
Vertex(1.0f, -1.0f, 0.0f, 1.0f, 0.0f),
2021-01-20 12:50:59 -06:00
2021-01-19 16:01:37 -06:00
Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 1.0f),
Vertex(1.0f, -1.0f, 0.0f, 1.0f, 0.0f),
Vertex(1.0f, 1.0f, 0.0f, 1.0f, 1.0f)
};
Screen::Screen() {
// Setup quad
glGenVertexArrays(1, &quad);
glBindVertexArray(quad);
Id vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
2021-01-21 15:26:39 -06:00
// Vertex Normals
2021-01-19 16:01:37 -06:00
glEnableVertexAttribArray(1);
2021-01-21 15:26:39 -06:00
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float)));
//Vertex UV
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (6 * sizeof(float)));
2021-01-19 16:01:37 -06:00
glBindVertexArray(0);
// Setup frame buffers
2021-03-09 16:40:21 -06:00
framebuffer.clearColor = Vector3(1.0f, 0.0f, 0.0f);
framebuffer.clearFlags = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
framebuffer.depthTest = true;
framebuffer.Bind();
2021-01-19 16:01:37 -06:00
tex.width = width;
tex.height = height;
glGenTextures(1, &tex.id);
glBindTexture(GL_TEXTURE_2D, tex.id);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.id, 0);
// Setup render buffer
glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cerr << "Error setting up screen framebuffer." << std::endl;
exit(1);
}
2021-03-09 16:40:21 -06:00
FramebufferStack::GetStack()->curr->Bind();
2021-01-19 16:01:37 -06:00
}
void Screen::Enable() {
2021-03-09 16:40:21 -06:00
FramebufferStack::GetStack()->Save(&framebuffer);
framebuffer.Enable();
framebuffer.Clear();
}
void Screen::Disable() {
FramebufferStack::GetStack()->Restore();
2021-01-19 16:01:37 -06:00
}
void Screen::Draw() {
glBindVertexArray(quad);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}