add FlatShader; draw a cursor

This commit is contained in:
Josh Holtrop 2016-07-31 23:08:26 -04:00
parent 8beea3461c
commit 5a942a61a9
6 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,11 @@
#version 130
/* Color */
uniform vec4 color;
out vec4 frag_color;
void main(void)
{
frag_color = color;
}

View File

@ -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);
}

View File

@ -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");
}

View File

@ -0,0 +1,40 @@
#ifndef FLATSHADER_H
#define FLATSHADER_H
#include "glcxx.hpp"
#include <memory>
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<glcxx::Program> m_program;
struct
{
GLint viewport_size;
GLint color;
GLint position;
} m_uniforms;
};
#endif

View File

@ -117,6 +117,15 @@ bool Window::create(std::shared_ptr<Buffer> 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<FlatShader>();
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();

View File

@ -3,8 +3,10 @@
#include <SDL.h>
#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<TextShader> text;
std::shared_ptr<FlatShader> flat;
} m_shaders;
Font m_font;
@ -35,6 +38,8 @@ protected:
std::shared_ptr<Buffer> m_buffer;
std::shared_ptr<glcxx::Buffer> m_cursor_buffer;
PieceTable::Piece * m_start_piece;
};