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>
|
|
|
|
|
2018-11-11 20:11:32 +00:00
|
|
|
using namespace Outputs::Display::OpenGL;
|
2016-02-07 20:42:02 +00:00
|
|
|
|
2018-11-17 20:51:12 +00:00
|
|
|
TextureTarget::TextureTarget(GLsizei width, GLsizei height, GLenum texture_unit, GLint mag_filter, bool has_stencil_buffer) :
|
2017-11-14 03:04:13 +00:00
|
|
|
width_(width),
|
|
|
|
height_(height),
|
|
|
|
texture_unit_(texture_unit) {
|
2018-11-17 20:51:12 +00:00
|
|
|
// Generate and bind a frame buffer.
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenFramebuffers, 1, &framebuffer_);
|
|
|
|
test_gl(glBindFramebuffer, GL_FRAMEBUFFER, framebuffer_);
|
2017-11-14 03:04:13 +00:00
|
|
|
|
2018-11-17 20:51:12 +00:00
|
|
|
// Round the width and height up to the next power of two.
|
2017-11-14 03:04:13 +00:00
|
|
|
expanded_width_ = 1;
|
|
|
|
while(expanded_width_ < width) expanded_width_ <<= 1;
|
|
|
|
expanded_height_ = 1;
|
|
|
|
while(expanded_height_ < height) expanded_height_ <<= 1;
|
|
|
|
|
2018-11-17 20:51:12 +00:00
|
|
|
// Generate a texture and bind it to the nominated texture unit.
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenTextures, 1, &texture_);
|
2018-02-01 12:53:52 +00:00
|
|
|
bind_texture();
|
2017-11-14 03:04:13 +00:00
|
|
|
|
2018-11-19 02:39:11 +00:00
|
|
|
// Set dimensions and set the user-supplied magnification filter.
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glTexImage2D, GL_TEXTURE_2D, 0, GL_RGBA, GLsizei(expanded_width_), GLsizei(expanded_height_), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
test_gl(glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
|
|
|
|
test_gl(glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2016-02-07 20:42:02 +00:00
|
|
|
|
2018-11-17 20:51:12 +00:00
|
|
|
// Set the texture as colour attachment 0 on the frame buffer.
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glFramebufferTexture2D, GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0);
|
2016-02-07 20:42:02 +00:00
|
|
|
|
2018-11-17 20:51:12 +00:00
|
|
|
// Also add a stencil buffer if requested.
|
|
|
|
if(has_stencil_buffer) {
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenRenderbuffers, 1, &renderbuffer_);
|
|
|
|
test_gl(glBindRenderbuffer, GL_RENDERBUFFER, renderbuffer_);
|
|
|
|
test_gl(glRenderbufferStorage, GL_RENDERBUFFER, GL_STENCIL_INDEX1, expanded_width_, expanded_height_);
|
|
|
|
test_gl(glFramebufferRenderbuffer, GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer_);
|
2018-11-17 20:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for successful construction.
|
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_);
|
2018-11-19 03:22:43 +00:00
|
|
|
if(renderbuffer_) glDeleteRenderbuffers(1, &renderbuffer_);
|
2017-11-14 03:04:13 +00:00
|
|
|
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() {
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBindFramebuffer, GL_FRAMEBUFFER, framebuffer_);
|
|
|
|
test_gl(glViewport, 0, 0, width_, height_);
|
2016-02-07 20:42:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 03:33:28 +00:00
|
|
|
void TextureTarget::bind_texture() const {
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glActiveTexture, texture_unit_);
|
|
|
|
test_gl(glBindTexture, GL_TEXTURE_2D, texture_);
|
2016-02-07 20:42:02 +00:00
|
|
|
}
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2018-11-24 03:33:28 +00:00
|
|
|
void TextureTarget::draw(float aspect_ratio, float colour_threshold) const {
|
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;"
|
2018-06-15 00:40:27 +00:00
|
|
|
|
2016-05-01 17:52:35 +00:00
|
|
|
"uniform sampler2D texID;"
|
2018-06-15 00:40:27 +00:00
|
|
|
"uniform float threshold;"
|
|
|
|
|
2016-05-01 17:52:35 +00:00
|
|
|
"out vec4 fragColour;"
|
|
|
|
|
|
|
|
"void main(void)"
|
|
|
|
"{"
|
2018-06-15 00:40:27 +00:00
|
|
|
"fragColour = clamp(texture(texID, texCoordVarying), threshold, 1.0);"
|
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
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenVertexArrays, 1, &drawing_vertex_array_);
|
|
|
|
test_gl(glGenBuffers, 1, &drawing_array_buffer_);
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBindVertexArray, drawing_vertex_array_);
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, drawing_array_buffer_);
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2018-11-24 03:33:28 +00:00
|
|
|
const GLint position_attribute = pixel_shader_->get_attrib_location("position");
|
|
|
|
const GLint tex_coord_attribute = pixel_shader_->get_attrib_location("texCoord");
|
2016-05-01 17:52:35 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glEnableVertexAttribArray, GLuint(position_attribute));
|
|
|
|
test_gl(glEnableVertexAttribArray, 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);
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glVertexAttribPointer, GLuint(position_attribute), 2, GL_FLOAT, GL_FALSE, vertex_stride, (void *)0);
|
|
|
|
test_gl(glVertexAttribPointer, GLuint(tex_coord_attribute), 2, GL_FLOAT, GL_FALSE, vertex_stride, (void *)(2 * sizeof(GLfloat)));
|
2016-05-01 20:17:52 +00:00
|
|
|
|
2018-11-24 03:33:28 +00:00
|
|
|
const GLint texIDUniform = pixel_shader_->get_uniform_location("texID");
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glUniform1i, texIDUniform, GLint(texture_unit_ - GL_TEXTURE0));
|
2018-06-15 00:40:27 +00:00
|
|
|
|
|
|
|
threshold_uniform_ = pixel_shader_->get_uniform_location("threshold");
|
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;
|
2019-02-18 15:29:40 +00:00
|
|
|
buffer[7] = float(height_) / float(expanded_height_);
|
|
|
|
buffer[10] = float(width_) / float(expanded_width_);
|
2017-11-14 03:04:13 +00:00
|
|
|
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
|
2019-02-18 15:29:40 +00:00
|
|
|
float internal_aspect_ratio = float(width_) / 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
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, drawing_array_buffer_);
|
|
|
|
test_gl(glBufferData, GL_ARRAY_BUFFER, sizeof(buffer), buffer, GL_STATIC_DRAW);
|
2016-05-01 17:52:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 03:04:13 +00:00
|
|
|
pixel_shader_->bind();
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glUniform1f, threshold_uniform_, colour_threshold);
|
2018-06-15 00:40:27 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBindVertexArray, drawing_vertex_array_);
|
|
|
|
test_gl(glDrawArrays, GL_TRIANGLE_STRIP, 0, 4);
|
2016-05-01 17:52:35 +00:00
|
|
|
}
|