2016-02-07 20:42:02 +00:00
|
|
|
//
|
|
|
|
// TextureTarget.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 07/02/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-02-07 20:42:02 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "TextureTarget.hpp"
|
2017-11-11 20:28:40 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
2017-11-14 03:04:13 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-02-07 20:42:02 +00:00
|
|
|
using namespace OpenGL;
|
|
|
|
|
2017-01-11 03:08:07 +00:00
|
|
|
TextureTarget::TextureTarget(GLsizei width, GLsizei height, GLenum texture_unit, GLint mag_filter) :
|
2017-11-14 03:04:13 +00:00
|
|
|
width_(width),
|
|
|
|
height_(height),
|
|
|
|
texture_unit_(texture_unit) {
|
|
|
|
glGenFramebuffers(1, &framebuffer_);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
|
|
|
|
|
|
|
|
expanded_width_ = 1;
|
|
|
|
while(expanded_width_ < width) expanded_width_ <<= 1;
|
|
|
|
expanded_height_ = 1;
|
|
|
|
while(expanded_height_ < height) expanded_height_ <<= 1;
|
|
|
|
|
|
|
|
glGenTextures(1, &texture_);
|
2018-02-01 12:53:52 +00:00
|
|
|
bind_texture();
|
2017-11-14 03:04:13 +00:00
|
|
|
|
|
|
|
std::vector<uint8_t> blank_buffer(static_cast<size_t>(expanded_width_ * expanded_height_ * 4), 0);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(expanded_width_), static_cast<GLsizei>(expanded_height_), 0, GL_RGBA, GL_UNSIGNED_BYTE, blank_buffer.data());
|
2017-01-11 03:08:07 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
|
2017-01-04 13:06:18 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2016-02-07 20:42:02 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0);
|
2016-02-07 20:42:02 +00:00
|
|
|
|
2016-03-07 23:55:15 +00:00
|
|
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
throw ErrorFramebufferIncomplete;
|
2016-02-07 20:42:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
TextureTarget::~TextureTarget() {
|
2017-11-14 03:04:13 +00:00
|
|
|
glDeleteFramebuffers(1, &framebuffer_);
|
|
|
|
glDeleteTextures(1, &texture_);
|
|
|
|
if(drawing_vertex_array_) glDeleteVertexArrays(1, &drawing_vertex_array_);
|
|
|
|
if(drawing_array_buffer_) glDeleteBuffers(1, &drawing_array_buffer_);
|
2016-02-07 20:42:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void TextureTarget::bind_framebuffer() {
|
2017-11-14 03:04:13 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
|
|
|
|
glViewport(0, 0, width_, height_);
|
2016-02-07 20:42:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void TextureTarget::bind_texture() {
|
2018-02-01 12:53:52 +00:00
|
|
|
glActiveTexture(texture_unit_);
|
2017-11-14 03:04:13 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_);
|
2016-02-07 20:42:02 +00:00
|
|
|
}
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void TextureTarget::draw(float aspect_ratio) {
|
2017-11-14 03:04:13 +00:00
|
|
|
if(!pixel_shader_) {
|
2016-05-01 17:52:35 +00:00
|
|
|
const char *vertex_shader =
|
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 texCoord;"
|
|
|
|
"in vec2 position;"
|
|
|
|
|
|
|
|
"out vec2 texCoordVarying;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
|
|
|
"texCoordVarying = texCoord;"
|
|
|
|
"gl_Position = vec4(position, 0.0, 1.0);"
|
|
|
|
"}";
|
|
|
|
const char *fragment_shader =
|
|
|
|
"#version 150\n"
|
|
|
|
|
|
|
|
"in vec2 texCoordVarying;"
|
|
|
|
"uniform sampler2D texID;"
|
|
|
|
"out vec4 fragColour;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2016-05-01 23:22:24 +00:00
|
|
|
"fragColour = texture(texID, texCoordVarying);"
|
2016-05-01 17:52:35 +00:00
|
|
|
"}";
|
2017-11-24 23:45:24 +00:00
|
|
|
pixel_shader_.reset(new Shader(vertex_shader, fragment_shader));
|
2017-11-14 03:04:13 +00:00
|
|
|
pixel_shader_->bind();
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
glGenVertexArrays(1, &drawing_vertex_array_);
|
|
|
|
glGenBuffers(1, &drawing_array_buffer_);
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
glBindVertexArray(drawing_vertex_array_);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, drawing_array_buffer_);
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
GLint position_attribute = pixel_shader_->get_attrib_location("position");
|
|
|
|
GLint tex_coord_attribute = pixel_shader_->get_attrib_location("texCoord");
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
glEnableVertexAttribArray(static_cast<GLuint>(position_attribute));
|
|
|
|
glEnableVertexAttribArray(static_cast<GLuint>(tex_coord_attribute));
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
const GLsizei vertex_stride = 4 * sizeof(GLfloat);
|
|
|
|
glVertexAttribPointer((GLuint)position_attribute, 2, GL_FLOAT, GL_FALSE, vertex_stride, (void *)0);
|
|
|
|
glVertexAttribPointer((GLuint)tex_coord_attribute, 2, GL_FLOAT, GL_FALSE, vertex_stride, (void *)(2 * sizeof(GLfloat)));
|
2016-05-01 20:17:52 +00:00
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
GLint texIDUniform = pixel_shader_->get_uniform_location("texID");
|
|
|
|
glUniform1i(texIDUniform, static_cast<GLint>(texture_unit_ - GL_TEXTURE0));
|
2016-05-01 17:52:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
if(set_aspect_ratio_ != aspect_ratio) {
|
|
|
|
set_aspect_ratio_ = aspect_ratio;
|
2016-05-02 01:07:27 +00:00
|
|
|
float buffer[4*4];
|
2016-05-01 17:52:35 +00:00
|
|
|
|
|
|
|
// establish texture coordinates
|
2016-05-02 01:07:27 +00:00
|
|
|
buffer[2] = 0.0f;
|
|
|
|
buffer[3] = 0.0f;
|
|
|
|
buffer[6] = 0.0f;
|
2017-11-14 03:04:13 +00:00
|
|
|
buffer[7] = static_cast<float>(height_) / static_cast<float>(expanded_height_);
|
|
|
|
buffer[10] = static_cast<float>(width_) / static_cast<float>(expanded_width_);
|
|
|
|
buffer[11] = 0.0f;
|
2016-05-02 01:07:27 +00:00
|
|
|
buffer[14] = buffer[10];
|
|
|
|
buffer[15] = buffer[7];
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2016-05-02 02:28:33 +00:00
|
|
|
// determine positions; rule is to keep the same height and centre
|
2017-11-14 03:04:13 +00:00
|
|
|
float internal_aspect_ratio = static_cast<float>(width_) / static_cast<float>(height_);
|
2016-05-01 17:52:35 +00:00
|
|
|
float aspect_ratio_ratio = internal_aspect_ratio / aspect_ratio;
|
2016-05-02 02:28:33 +00:00
|
|
|
|
|
|
|
buffer[0] = -aspect_ratio_ratio; buffer[1] = -1.0f;
|
|
|
|
buffer[4] = -aspect_ratio_ratio; buffer[5] = 1.0f;
|
|
|
|
buffer[8] = aspect_ratio_ratio; buffer[9] = -1.0f;
|
|
|
|
buffer[12] = aspect_ratio_ratio; buffer[13] = 1.0f;
|
2016-05-01 17:52:35 +00:00
|
|
|
|
|
|
|
// upload buffer
|
2017-11-14 03:04:13 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, drawing_array_buffer_);
|
2016-05-01 17:52:35 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(buffer), buffer, GL_STATIC_DRAW);
|
|
|
|
}
|
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
pixel_shader_->bind();
|
|
|
|
glBindVertexArray(drawing_vertex_array_);
|
2016-05-01 17:52:35 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
}
|