1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-04 18:29:40 +00:00

Made a first attempt at adding draw-as-a-quad functionality to texture targets.

This commit is contained in:
Thomas Harte 2016-05-01 13:52:35 -04:00
parent 2616d748fe
commit 4ec4cceb84
2 changed files with 109 additions and 1 deletions

View File

@ -10,7 +10,13 @@
using namespace OpenGL;
TextureTarget::TextureTarget(GLsizei width, GLsizei height) : _width(width), _height(height)
TextureTarget::TextureTarget(GLsizei width, GLsizei height) :
_width(width),
_height(height),
_pixel_shader(nullptr),
_drawing_vertex_array(0),
_drawing_array_buffer(0),
_set_aspect_ratio(0.0f)
{
glGenFramebuffers(1, &_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
@ -31,6 +37,8 @@ TextureTarget::~TextureTarget()
{
glDeleteFramebuffers(1, &_framebuffer);
glDeleteTextures(1, &_texture);
if(_drawing_vertex_array) glDeleteVertexArrays(1, &_drawing_vertex_array);
if(_drawing_array_buffer) glDeleteBuffers(1, &_drawing_array_buffer);
}
void TextureTarget::bind_framebuffer()
@ -43,3 +51,92 @@ void TextureTarget::bind_texture()
{
glBindTexture(GL_TEXTURE_2D, _texture);
}
void TextureTarget::draw(float aspect_ratio, GLenum texture_unit)
{
if(!_pixel_shader)
{
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)"
"{"
"fragColour = texture(texID, texCoordVarying);"
"}";
_pixel_shader = std::unique_ptr<Shader>(new Shader(vertex_shader, fragment_shader, nullptr));
glGenVertexArrays(1, &_drawing_vertex_array);
glGenBuffers(1, &_drawing_array_buffer);
glBindVertexArray(_drawing_array_buffer);
glBindBuffer(GL_ARRAY_BUFFER, _drawing_vertex_array);
GLint positionAttribute = _pixel_shader->get_attrib_location("position");
GLint texCoordAttribute = _pixel_shader->get_attrib_location("texCoord");
glEnableVertexAttribArray((GLuint)positionAttribute);
glEnableVertexAttribArray((GLuint)texCoordAttribute);
const GLsizei vertexStride = 12;
glVertexAttribPointer((GLuint)positionAttribute, 2, GL_FLOAT, GL_FALSE, vertexStride, (void *)0);
glVertexAttribPointer((GLuint)texCoordAttribute, 2, GL_BYTE, GL_FALSE, vertexStride, (void *)(2 * sizeof(GLfloat)));
}
if(_set_aspect_ratio != aspect_ratio)
{
aspect_ratio = _set_aspect_ratio;
int8_t buffer[12*4];
// establish texture coordinates
buffer[8] = 0; buffer[9] = 0;
buffer[20] = 0; buffer[21] = 1;
buffer[32] = 1; buffer[33] = 0;
buffer[44] = 1; buffer[45] = 1;
// determine positions
float internal_aspect_ratio = (float)_width / (float)_height;
float aspect_ratio_ratio = internal_aspect_ratio / aspect_ratio;
float *fl_buffer = (float *)buffer;
if(aspect_ratio_ratio >= 1.0f)
{
// output is thinner than we are; letterbox
fl_buffer[0] = -1.0f; fl_buffer[1] = 1.0f / aspect_ratio_ratio;
fl_buffer[3] = -1.0f; fl_buffer[4] = -1.0f / aspect_ratio_ratio;
fl_buffer[6] = 1.0f; fl_buffer[7] = 1.0f / aspect_ratio_ratio;
fl_buffer[9] = 1.0f; fl_buffer[10] = -1.0f / aspect_ratio_ratio;
}
else
{
// output is wider than we are; pillarbox
fl_buffer[0] = -aspect_ratio_ratio; fl_buffer[1] = 1.0f;
fl_buffer[3] = -aspect_ratio_ratio; fl_buffer[4] = -1.0f;
fl_buffer[6] = aspect_ratio_ratio; fl_buffer[7] = 1.0f;
fl_buffer[9] = aspect_ratio_ratio; fl_buffer[10] = -1.0f;
}
// upload buffer
glBindBuffer(GL_ARRAY_BUFFER, _drawing_vertex_array);
glBufferData(GL_ARRAY_BUFFER, sizeof(buffer), buffer, GL_STATIC_DRAW);
}
glBindVertexArray(_drawing_vertex_array);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

View File

@ -10,6 +10,8 @@
#define TextureTarget_hpp
#include "OpenGL.hpp"
#include "Shaders/Shader.hpp"
#include <memory>
namespace OpenGL {
@ -40,6 +42,11 @@ class TextureTarget {
*/
void bind_texture();
/*!
*/
void draw(float aspect_ratio, GLenum texture_unit);
enum {
ErrorFramebufferIncomplete
};
@ -47,6 +54,10 @@ class TextureTarget {
private:
GLuint _framebuffer, _texture;
GLsizei _width, _height;
std::unique_ptr<Shader> _pixel_shader;
GLuint _drawing_vertex_array, _drawing_array_buffer;
float _set_aspect_ratio;
};
}