From 5a942a61a9a096fa16c45128a7d07fade63ddf8d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 31 Jul 2016 23:08:26 -0400 Subject: [PATCH] add FlatShader; draw a cursor --- share/jes/shaders/flat.f.glsl | 11 ++++++++++ share/jes/shaders/flat.v.glsl | 23 ++++++++++++++++++++ src/gui/Shaders/FlatShader.cc | 17 +++++++++++++++ src/gui/Shaders/FlatShader.h | 40 +++++++++++++++++++++++++++++++++++ src/gui/Window.cc | 20 ++++++++++++++++++ src/gui/Window.h | 5 +++++ 6 files changed, 116 insertions(+) create mode 100644 share/jes/shaders/flat.f.glsl create mode 100644 share/jes/shaders/flat.v.glsl create mode 100644 src/gui/Shaders/FlatShader.cc create mode 100644 src/gui/Shaders/FlatShader.h diff --git a/share/jes/shaders/flat.f.glsl b/share/jes/shaders/flat.f.glsl new file mode 100644 index 0000000..f1b3451 --- /dev/null +++ b/share/jes/shaders/flat.f.glsl @@ -0,0 +1,11 @@ +#version 130 + +/* Color */ +uniform vec4 color; + +out vec4 frag_color; + +void main(void) +{ + frag_color = color; +} diff --git a/share/jes/shaders/flat.v.glsl b/share/jes/shaders/flat.v.glsl new file mode 100644 index 0000000..e4de1b9 --- /dev/null +++ b/share/jes/shaders/flat.v.glsl @@ -0,0 +1,23 @@ +#version 130 + +/* Viewport width and height */ +uniform ivec2 viewport_size; +/* Position offset */ +uniform ivec2 position; + +/* Vertex coordinates: x, y */ +in vec2 coords; + +/** + * Map coordinates such that: + * (0 .. viewport_size.[xy]) => (-1.0 .. 1.0) + */ +vec2 map_to_screen(vec2 position) +{ + return 2.0 * position / viewport_size - 1.0; +} + +void main(void) +{ + gl_Position = vec4(map_to_screen(vec2(position) + coords), 0, 1); +} diff --git a/src/gui/Shaders/FlatShader.cc b/src/gui/Shaders/FlatShader.cc new file mode 100644 index 0000000..03c47b3 --- /dev/null +++ b/src/gui/Shaders/FlatShader.cc @@ -0,0 +1,17 @@ +#include "FlatShader.h" +#include "Runtime.h" + +FlatShader::FlatShader() +{ + std::string v_path = Runtime::find(Runtime::SHADER, "flat.v"); + std::string f_path = Runtime::find(Runtime::SHADER, "flat.f"); + + m_program = glcxx::Program::create( + glcxx::Shader::create_from_file(GL_VERTEX_SHADER, v_path.c_str()), + glcxx::Shader::create_from_file(GL_FRAGMENT_SHADER, f_path.c_str()), + "coords", 0); + + m_uniforms.viewport_size = m_program->get_uniform_location("viewport_size"); + m_uniforms.position = m_program->get_uniform_location("position"); + m_uniforms.color = m_program->get_uniform_location("color"); +} diff --git a/src/gui/Shaders/FlatShader.h b/src/gui/Shaders/FlatShader.h new file mode 100644 index 0000000..76817dc --- /dev/null +++ b/src/gui/Shaders/FlatShader.h @@ -0,0 +1,40 @@ +#ifndef FLATSHADER_H +#define FLATSHADER_H + +#include "glcxx.hpp" +#include + +class FlatShader +{ +public: + FlatShader(); + + void use() { m_program->use(); } + + void set_viewport_size(int width, int height) + { + glUniform2i(m_uniforms.viewport_size, width, height); + } + + void set_color(float r, float g, float b, float a) + { + glUniform4f(m_uniforms.color, r, g, b, a); + } + + void set_position(int x, int y) + { + glUniform2i(m_uniforms.position, x, y); + } + +protected: + std::shared_ptr m_program; + + struct + { + GLint viewport_size; + GLint color; + GLint position; + } m_uniforms; +}; + +#endif diff --git a/src/gui/Window.cc b/src/gui/Window.cc index 9d7a6c3..9ed7225 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -117,6 +117,15 @@ bool Window::create(std::shared_ptr buffer) m_shaders.text->set_texture(0); m_shaders.text->set_color(1.0, 1.0, 1.0, 1.0); + m_shaders.flat = std::make_shared(); + + GLint cursor_bounds[] = {0, 0, + m_font.get_advance(), 0, + m_font.get_advance(), m_font.get_line_height(), + 0, m_font.get_line_height()}; + m_cursor_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, + cursor_bounds, sizeof(cursor_bounds)); + m_buffer = buffer; m_start_piece = m_buffer->piece_table->start_piece->next; @@ -253,6 +262,8 @@ void Window::resize() glViewport(0, 0, m_width, m_height); m_shaders.text->use(); m_shaders.text->set_viewport_size(m_width, m_height); + m_shaders.flat->use(); + m_shaders.flat->set_viewport_size(m_width, m_height); m_columns = m_width / m_font.get_advance(); if (m_columns < 1) m_columns = 1; @@ -264,6 +275,15 @@ void Window::redraw() glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); + glBindVertexArray(0); + m_shaders.flat->use(); + m_shaders.flat->set_color(1.0, 0.2, 1.0, 1.0); + m_shaders.flat->set_position(0, m_height - m_font.get_line_height()); + m_cursor_buffer->bind(); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + m_shaders.text->use(); int advance = m_font.get_advance(); diff --git a/src/gui/Window.h b/src/gui/Window.h index eadfbb5..4fdc56a 100644 --- a/src/gui/Window.h +++ b/src/gui/Window.h @@ -3,8 +3,10 @@ #include #include "TextShader.h" +#include "FlatShader.h" #include "Font.h" #include "Buffer.h" +#include "glcxx.hpp" class Window { @@ -27,6 +29,7 @@ protected: struct { std::shared_ptr text; + std::shared_ptr flat; } m_shaders; Font m_font; @@ -35,6 +38,8 @@ protected: std::shared_ptr m_buffer; + std::shared_ptr m_cursor_buffer; + PieceTable::Piece * m_start_piece; };