add a rectangle shader
This commit is contained in:
parent
6d8c8e76a9
commit
c852e9991b
11
share/jes/shaders/rect.f.glsl
Normal file
11
share/jes/shaders/rect.f.glsl
Normal file
@ -0,0 +1,11 @@
|
||||
#version 130
|
||||
|
||||
/* Color */
|
||||
uniform vec4 color;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
frag_color = color;
|
||||
}
|
25
share/jes/shaders/rect.v.glsl
Normal file
25
share/jes/shaders/rect.v.glsl
Normal file
@ -0,0 +1,25 @@
|
||||
#version 130
|
||||
|
||||
/* Viewport width and height */
|
||||
uniform ivec2 viewport_size;
|
||||
/* Position offset */
|
||||
uniform ivec2 position;
|
||||
/* Rectangle size */
|
||||
uniform ivec2 size;
|
||||
|
||||
/* 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 * size), 0, 1);
|
||||
}
|
18
src/gui/Shaders/RectShader.cc
Normal file
18
src/gui/Shaders/RectShader.cc
Normal file
@ -0,0 +1,18 @@
|
||||
#include "RectShader.h"
|
||||
#include "Runtime.h"
|
||||
|
||||
RectShader::RectShader()
|
||||
{
|
||||
std::string v_path = Runtime::find(Runtime::SHADER, "rect.v");
|
||||
std::string f_path = Runtime::find(Runtime::SHADER, "rect.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.color = m_program->get_uniform_location("color");
|
||||
m_uniforms.position = m_program->get_uniform_location("position");
|
||||
m_uniforms.size = m_program->get_uniform_location("size");
|
||||
}
|
46
src/gui/Shaders/RectShader.h
Normal file
46
src/gui/Shaders/RectShader.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef RECTSHADER_H
|
||||
#define RECTSHADER_H
|
||||
|
||||
#include "glcxx.hpp"
|
||||
#include <memory>
|
||||
|
||||
class RectShader
|
||||
{
|
||||
public:
|
||||
RectShader();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void set_size(int x, int y)
|
||||
{
|
||||
glUniform2i(m_uniforms.size, x, y);
|
||||
}
|
||||
|
||||
protected:
|
||||
std::shared_ptr<glcxx::Program> m_program;
|
||||
|
||||
struct
|
||||
{
|
||||
GLint viewport_size;
|
||||
GLint color;
|
||||
GLint position;
|
||||
GLint size;
|
||||
} m_uniforms;
|
||||
};
|
||||
|
||||
#endif
|
@ -118,6 +118,7 @@ bool Window::create(std::shared_ptr<Buffer> buffer)
|
||||
m_shaders.text->set_color(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
m_shaders.flat = std::make_shared<FlatShader>();
|
||||
m_shaders.rect = std::make_shared<RectShader>();
|
||||
|
||||
GLint cursor_bounds[] = {0, 0,
|
||||
m_font.get_advance(), 0,
|
||||
@ -130,6 +131,18 @@ bool Window::create(std::shared_ptr<Buffer> buffer)
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0);
|
||||
|
||||
GLint rect_coords[] = {
|
||||
0, 0,
|
||||
1, 0,
|
||||
1, 1,
|
||||
0, 1};
|
||||
m_rect_array = glcxx::Array::create();
|
||||
m_rect_array->bind();
|
||||
m_rect_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW,
|
||||
rect_coords, sizeof(rect_coords));
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0);
|
||||
|
||||
m_buffer = buffer;
|
||||
m_cursor = m_buffer->piece_table->add_cursor();
|
||||
m_start_piece = m_buffer->piece_table->start_piece->next;
|
||||
@ -430,6 +443,8 @@ void Window::resize()
|
||||
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_shaders.rect->use();
|
||||
m_shaders.rect->set_viewport_size(m_width, m_height);
|
||||
m_columns = m_width / m_font.get_advance();
|
||||
if (m_columns < 1)
|
||||
m_columns = 1;
|
||||
@ -474,3 +489,13 @@ void Window::colrow_to_xy(int col, int row, int * x, int * y)
|
||||
*x = col * m_font.get_advance();
|
||||
*y = m_height - (row + 1) * m_font.get_line_height();
|
||||
}
|
||||
|
||||
void Window::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a)
|
||||
{
|
||||
m_rect_array->bind();
|
||||
m_shaders.rect->use();
|
||||
m_shaders.rect->set_color(r, g, b, a);
|
||||
m_shaders.rect->set_position(x, y);
|
||||
m_shaders.rect->set_size(width, height);
|
||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <SDL.h>
|
||||
#include "TextShader.h"
|
||||
#include "FlatShader.h"
|
||||
#include "RectShader.h"
|
||||
#include "Font.h"
|
||||
#include "Buffer.h"
|
||||
#include "glcxx.hpp"
|
||||
@ -38,6 +39,7 @@ protected:
|
||||
void draw_text();
|
||||
void draw_character(int screen_column, int screen_row, uint32_t character);
|
||||
void update_cursor_row(int cursor_row);
|
||||
void draw_rect(int x, int y, int width, int height, float r, float g, float b, float a);
|
||||
|
||||
SDL_Window * m_window;
|
||||
bool m_exit_requested;
|
||||
@ -47,6 +49,7 @@ protected:
|
||||
{
|
||||
std::shared_ptr<TextShader> text;
|
||||
std::shared_ptr<FlatShader> flat;
|
||||
std::shared_ptr<RectShader> rect;
|
||||
} m_shaders;
|
||||
|
||||
Font m_font;
|
||||
@ -60,6 +63,8 @@ protected:
|
||||
|
||||
std::shared_ptr<glcxx::Array> m_cursor_array;
|
||||
std::shared_ptr<glcxx::Buffer> m_cursor_buffer;
|
||||
std::shared_ptr<glcxx::Array> m_rect_array;
|
||||
std::shared_ptr<glcxx::Buffer> m_rect_buffer;
|
||||
|
||||
std::shared_ptr<PieceTable::Cursor> m_cursor;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user