173 lines
5.6 KiB
C++
173 lines
5.6 KiB
C++
/**
|
|
This file is a part of our_dick
|
|
Copyright (C) 2020-2022 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef OUR_DICK_SCREEN_RENDERER_HPP
|
|
#define OUR_DICK_SCREEN_RENDERER_HPP
|
|
|
|
#include "graphics/vbo.hpp"
|
|
#include "graphics/vao.hpp"
|
|
#include "graphics/texture.hpp"
|
|
#include "math/math.hpp"
|
|
#include "graphics/shader_program.hpp"
|
|
#include "graphics/resource_manager.hpp"
|
|
#include "scene.hpp"
|
|
|
|
#include "engine/font.hpp"
|
|
|
|
static constexpr float pixels_to_screen_space(int dim, int window_dim){
|
|
const float ratio = 2.0f / window_dim;
|
|
return dim * ratio;
|
|
}
|
|
static constexpr float screen_space_to_pixels(int dim, int window_dim){
|
|
const float ratio = window_dim / 2.0f;
|
|
return dim * ratio;
|
|
}
|
|
static constexpr float font_scale_ratio(int height, int target_height){
|
|
return ((float)target_height) / height;
|
|
}
|
|
static constexpr float font_aspect_ratio(const egn::font_character& ch){
|
|
return ((float)ch.size.x()) / ch.size.y();
|
|
}
|
|
|
|
|
|
class text
|
|
{
|
|
private:
|
|
static constexpr int s_size_per_char = sizeof(GLfloat) * 13;
|
|
private:
|
|
egn::font m_font;
|
|
std::string m_text;
|
|
gfx::vbo m_vbo;
|
|
gfx::vao m_vao;
|
|
|
|
float m_scale;
|
|
|
|
public:
|
|
text(egn::font& font, const char* string, int target_px, float target_x, float target_y, int screen_width, int screen_height):
|
|
m_font(font),
|
|
m_text(string),
|
|
m_vbo(s_size_per_char * strlen(string), gfx::vbo::usage::DYNAMIC_DRAW)
|
|
{
|
|
constexpr float tmp_color[] = {1,1,1,1};
|
|
|
|
m_scale = font_scale_ratio(m_font.size().y(), target_px);
|
|
|
|
math::vec3f target_pos = {target_x, target_y, -1};
|
|
size_t offset = 0;
|
|
for(int i = 0;m_text[i] != 0;++i){
|
|
const auto ch = m_font[m_text[i]];
|
|
const math::vec2<int> letter_size = ch.size * m_scale;
|
|
const math::vec2f letter_screen_size = {pixels_to_screen_space(letter_size.x(), screen_width), pixels_to_screen_space(letter_size.y(), screen_height)};
|
|
const math::vec3f position = {target_pos.x() + pixels_to_screen_space(ch.bearing.x() * m_scale, screen_width),
|
|
target_pos.y() - (letter_screen_size.y() - pixels_to_screen_space(ch.bearing.y() * m_scale, screen_height)),
|
|
target_pos.z()};
|
|
|
|
|
|
m_vbo.buffer(ch.texture_coords[1], sizeof(GLfloat) * 2, offset);
|
|
offset += sizeof(GLfloat) * 2;
|
|
m_vbo.buffer(ch.texture_coords[2], sizeof(GLfloat) * 2, offset);
|
|
offset += sizeof(GLfloat) * 2;
|
|
m_vbo.buffer(tmp_color, sizeof(GLfloat) * 4, offset);
|
|
offset += sizeof(GLfloat) * 4;
|
|
m_vbo.buffer(position, sizeof(GLfloat) * 3, offset);
|
|
offset += sizeof(GLfloat) * 3;
|
|
m_vbo.buffer(letter_screen_size, sizeof(GLfloat) * 2, offset);
|
|
offset += sizeof(GLfloat) * 2;
|
|
|
|
target_pos.x() += pixels_to_screen_space(m_scale * ch.advance.x(), screen_width);
|
|
}
|
|
|
|
//attach a vbo to the vao and assign attribute specs
|
|
m_vao.bind_buffer(m_vbo, 0, 0, sizeof(GLfloat) * 13);
|
|
|
|
auto attrib = m_vao.get_attribute(0);
|
|
attrib.set_float_array(4, 0);
|
|
attrib.associate_with(0);
|
|
attrib.enable();
|
|
|
|
attrib = m_vao.get_attribute(1);
|
|
attrib.set_float_array(4, sizeof(GLfloat) * 4);
|
|
attrib.associate_with(0);
|
|
attrib.enable();
|
|
|
|
attrib = m_vao.get_attribute(2);
|
|
attrib.set_float_array(3, sizeof(GLfloat) * 8);
|
|
attrib.associate_with(0);
|
|
attrib.enable();
|
|
|
|
attrib = m_vao.get_attribute(3);
|
|
attrib.set_float_array(2, sizeof(GLfloat) * 11);
|
|
attrib.associate_with(0);
|
|
attrib.enable();
|
|
|
|
}
|
|
void set_position(float x, float y, int screen_width, int screen_height){
|
|
size_t offset = sizeof(GLfloat) * 8;
|
|
math::vec3f target_pos = {x, y, -1};
|
|
for(size_t i = 0;m_text[i] != 0;++i){
|
|
const auto ch = m_font[m_text[i]];
|
|
const math::vec2i letter_size = ch.size * m_scale;
|
|
const math::vec2f letter_screen_size = {pixels_to_screen_space(letter_size.x(), screen_width), pixels_to_screen_space(letter_size.y(), screen_height)};
|
|
const math::vec3f position = {target_pos.x() + pixels_to_screen_space(ch.bearing.x() * m_scale, screen_width),
|
|
target_pos.y() - (letter_screen_size.y() - pixels_to_screen_space(ch.bearing.y() * m_scale, screen_height)),
|
|
target_pos.z()};
|
|
|
|
m_vbo.buffer(position, sizeof(GLfloat) * 3, offset);
|
|
offset += sizeof(GLfloat) * 3 + sizeof(GLfloat) * 8;
|
|
target_pos.x() += pixels_to_screen_space(m_scale * ch.advance.x(), screen_width);
|
|
}
|
|
}
|
|
|
|
void render(gfx::shader_program& sh){
|
|
m_vao.bind();
|
|
sh.set_uniform("texture1", m_font.atlas());
|
|
glDrawArrays(GL_POINTS, 0, m_text.length());
|
|
}
|
|
};
|
|
|
|
class screen_renderer
|
|
{
|
|
private:
|
|
struct vertex{
|
|
math::vec2<GLfloat> pos;
|
|
math::vec2<GLfloat> tex_coord;
|
|
};
|
|
static constexpr vertex s_vertices[] = {
|
|
{{-1.0f, 1.0f}, {0.0f, 1.0f}},
|
|
{{-1.0f, -1.0f}, {0.0f, 0.0f}},
|
|
{{ 1.0f, -1.0f}, {1.0f, 0.0f}},
|
|
{{ 1.0f, -1.0f}, {1.0f, 0.0f}},
|
|
{{ 1.0f, 1.0f}, {1.0f, 1.0f}},
|
|
{{-1.0f, 1.0f}, {0.0f, 1.0f}}
|
|
};
|
|
private:
|
|
gfx::shader_program* m_screen_shader;
|
|
gfx::vbo m_vbo;
|
|
gfx::vao m_vao;
|
|
gfx::texture* m_texture;
|
|
int m_screen_width, m_screen_height;
|
|
|
|
public:
|
|
screen_renderer(gfx::resource_manager& res, int width, int height, gfx::texture& base_tex);
|
|
|
|
void render(scene&);
|
|
void resize_viewport(int width, int height);
|
|
};
|
|
|
|
#endif
|