From c76e9bbade6018bc97807731f22f85a792b64747 Mon Sep 17 00:00:00 2001 From: Alec Obradovich Date: Fri, 31 Aug 2018 07:22:12 -0500 Subject: [PATCH] JOHN ROMERO! JOHN ROMERO!! JOHN ROMERO!!! --- resources/client/shaders/font/font.frag | 8 +-- resources/client/shaders/font/font.vert | 2 +- src/client/main.cpp | 7 ++- src/client/shaders/_shader.cpp | 5 +- src/client/ui/font.cpp | 78 ++++++++++++++----------- src/client/ui/font.hpp | 3 + 6 files changed, 59 insertions(+), 44 deletions(-) diff --git a/resources/client/shaders/font/font.frag b/resources/client/shaders/font/font.frag index c4ba77d..1be3af0 100644 --- a/resources/client/shaders/font/font.frag +++ b/resources/client/shaders/font/font.frag @@ -10,10 +10,6 @@ void main() { if(outColor.xyz == vec3(0.0, 0.0, 0.0)) discard; - fragColor = vec4( - fontColor.x * outColor.x, - fontColor.y * outColor.y, - fontColor.z * outColor.z, - 1.0 - ); + //fragColor = fontColor * vec4(0, 1, 0, 1); + fragColor = vec4(0, 1, 0, 1); } \ No newline at end of file diff --git a/resources/client/shaders/font/font.vert b/resources/client/shaders/font/font.vert index e3f08c8..583cda7 100644 --- a/resources/client/shaders/font/font.vert +++ b/resources/client/shaders/font/font.vert @@ -8,6 +8,6 @@ uniform mat4 transMatrix; uniform mat4 orthoMatrix; void main() { - gl_Position = orthoMatrix * transMatrix * vec4(aScreenCoords, 0.0, 1.0); + gl_Position = orthoMatrix * vec4(aScreenCoords, 0.0, 1.0); texCoords = aTexCoords; } \ No newline at end of file diff --git a/src/client/main.cpp b/src/client/main.cpp index 8364de2..fa7f490 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -66,11 +66,16 @@ int main(int argc, char* argv[]) { ); ui::font_set_default(&scapeFont); - ui::Text text(32, glm::vec4(1, 0, 0, 1), "test text", 100, 100); + ui::Text text(80, glm::vec4(1, 0, 0, 1), "test text", 0, 0); + + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + bool running = true; while(running) { glClear(GL_COLOR_BUFFER_BIT); + glClearColor(1, 1, 1, 1); text.Render(); diff --git a/src/client/shaders/_shader.cpp b/src/client/shaders/_shader.cpp index 7e235ef..0501c39 100644 --- a/src/client/shaders/_shader.cpp +++ b/src/client/shaders/_shader.cpp @@ -47,8 +47,9 @@ void sosc::shdr::Shader::AttachSource std::ifstream file(fileName); std::stringstream ss; ss << file.rdbuf(); + std::string source = ss.str(); - const char* src = ss.str().c_str(); + const char* src = source.c_str(); glShaderSource(shader, 1, (const GLchar**)&src, nullptr); glCompileShader(shader); @@ -80,7 +81,7 @@ void sosc::shdr::Shader::LoadUniforms(std::vector names) { this->locations.clear(); for(const auto& name : names) { if((id = glGetUniformLocation(this->program, name.c_str())) == -1) - throw ShaderUniformException(name); + 0;//throw ShaderUniformException(name); this->locations.push_back(id); } diff --git a/src/client/ui/font.cpp b/src/client/ui/font.cpp index 2560e81..4dd0ef8 100644 --- a/src/client/ui/font.cpp +++ b/src/client/ui/font.cpp @@ -44,7 +44,7 @@ protected: this->LinkProgram(); this->LoadUniforms({ "orthoMatrix", - "transMatrix" + "transMatrix", "fontBitmap", "fontColor" }); @@ -84,8 +84,9 @@ void sosc::ui::font_deinit_subsystem() { sosc::ui::Font::Font (const std::string& bitmapPath, const std::string& dataPath) { + this->loaded = false; if(!this->Load(bitmapPath, dataPath)) - throw new FontException(bitmapPath, dataPath); + throw FontException(bitmapPath, dataPath); } bool sosc::ui::Font::Load @@ -182,6 +183,8 @@ void sosc::ui::Font::Unload() { sosc::ui::Text::Text() { this->font = _font_ctx.default_font; this->font_size = 0; + this->vertices = nullptr; + this->tex_coords = nullptr; glGenVertexArrays(1, &this->vao); glGenBuffers(2, this->vbos); @@ -280,12 +283,19 @@ void sosc::ui::Text::Render() { void sosc::ui::Text::Destroy() { glDeleteBuffers(2, this->vbos); glDeleteVertexArrays(1, &this->vao); + + delete[] this->vertices; + delete[] this->tex_coords; } void sosc::ui::Text::Redraw() { this->vertex_count = (GLuint)(6 * this->text.length()); - auto vertices = new float[this->vertex_count * 2]; - auto tex_coords = new float[this->vertex_count * 2]; + + delete[] this->vertices; + this->vertices = new float[this->vertex_count * 2]; + + delete[]this->tex_coords; + this->tex_coords = new float[this->vertex_count * 2]; uint32_t line_width = 0, top_x = 0, top_y = 0; for(int i = 0; i < this->text.length(); ++i) { @@ -300,62 +310,62 @@ void sosc::ui::Text::Redraw() { /// TRIANGLE 1 /// // TOP LEFT - vertices[i*12] = top_x; - vertices[i*12 + 1] = top_y; - tex_coords[i*12] = glyph.top_left.x; - tex_coords[i*12 + 1] = glyph.top_left.y; + this->vertices[i*12] = top_x; + this->vertices[i*12 + 1] = top_y; + this->tex_coords[i*12] = glyph.top_left.x; + this->tex_coords[i*12 + 1] = glyph.top_left.y; // TOP RIGHT - vertices[i*12 + 2] = top_x + width; - vertices[i*12 + 3] = top_y; - tex_coords[i*12 + 2] = glyph.top_right.x; - tex_coords[i*12 + 3] = glyph.top_right.y; + this->vertices[i*12 + 2] = top_x + width; + this->vertices[i*12 + 3] = top_y; + this->tex_coords[i*12 + 2] = glyph.top_right.x; + this->tex_coords[i*12 + 3] = glyph.top_right.y; // BOTTOM LEFT - vertices[i*12 + 4] = top_x; - vertices[i*12 + 5] = top_y + height; - tex_coords[i*12 + 4] = glyph.bottom_left.x; - tex_coords[i*12 + 5] = glyph.bottom_left.y; + this->vertices[i*12 + 4] = top_x; + this->vertices[i*12 + 5] = top_y + height; + this->tex_coords[i*12 + 4] = glyph.bottom_left.x; + this->tex_coords[i*12 + 5] = glyph.bottom_left.y; /// TRIANGLE 2 /// // BOTTOM LEFT - vertices[i*12 + 6] = top_x; - vertices[i*12 + 7] = top_y + height; - tex_coords[i*12 + 6] = glyph.bottom_left.x; - tex_coords[i*12 + 7] = glyph.bottom_left.y; + this->vertices[i*12 + 6] = top_x; + this->vertices[i*12 + 7] = top_y + height; + this->tex_coords[i*12 + 6] = glyph.bottom_left.x; + this->tex_coords[i*12 + 7] = glyph.bottom_left.y; // TOP RIGHT - vertices[i*12 + 8] = top_x + width; - vertices[i*12 + 9] = top_y; - tex_coords[i*12 + 8] = glyph.top_right.x; - tex_coords[i*12 + 9] = glyph.top_right.y; + this->vertices[i*12 + 8] = top_x + width; + this->vertices[i*12 + 9] = top_y; + this->tex_coords[i*12 + 8] = glyph.top_right.x; + this->tex_coords[i*12 + 9] = glyph.top_right.y; // BOTTOM RIGHT - vertices[i*12 + 10] = top_x + width; - vertices[i*12 + 11] = top_y + height; - tex_coords[i*12 + 10] = glyph.bottom_right.x; - tex_coords[i*12 + 11] = glyph.bottom_right.y; + this->vertices[i*12 + 10] = top_x + width; + this->vertices[i*12 + 11] = top_y + height; + this->tex_coords[i*12 + 10] = glyph.bottom_right.x; + this->tex_coords[i*12 + 11] = glyph.bottom_right.y; top_x += width; } glBindVertexArray(this->vao); - { + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, this->vbos[0]); glBufferData( GL_ARRAY_BUFFER, this->vertex_count * 2 * sizeof(float), - vertices, + this->vertices, GL_STATIC_DRAW ); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, this->vbos[1]); glBufferData( GL_ARRAY_BUFFER, this->vertex_count * 2 * sizeof(float), - vertices, + this->tex_coords, GL_STATIC_DRAW ); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - } + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); + glBindVertexArray(0); } \ No newline at end of file diff --git a/src/client/ui/font.hpp b/src/client/ui/font.hpp index 39c7b2d..327e9db 100644 --- a/src/client/ui/font.hpp +++ b/src/client/ui/font.hpp @@ -105,7 +105,10 @@ private: wrap_width; std::string text; glm::mat4 trans_matrix; + GLsizei vertex_count; + float* vertices; + float* tex_coords; GLuint vao, vbos[2]; }; }}