JOHN ROMERO! JOHN ROMERO!! JOHN ROMERO!!!

This commit is contained in:
Alec Obradovich 2018-08-31 07:22:12 -05:00
parent 71cc1e55fc
commit c76e9bbade
6 changed files with 59 additions and 44 deletions

View file

@ -10,10 +10,6 @@ void main() {
if(outColor.xyz == vec3(0.0, 0.0, 0.0)) if(outColor.xyz == vec3(0.0, 0.0, 0.0))
discard; discard;
fragColor = vec4( //fragColor = fontColor * vec4(0, 1, 0, 1);
fontColor.x * outColor.x, fragColor = vec4(0, 1, 0, 1);
fontColor.y * outColor.y,
fontColor.z * outColor.z,
1.0
);
} }

View file

@ -8,6 +8,6 @@ uniform mat4 transMatrix;
uniform mat4 orthoMatrix; uniform mat4 orthoMatrix;
void main() { void main() {
gl_Position = orthoMatrix * transMatrix * vec4(aScreenCoords, 0.0, 1.0); gl_Position = orthoMatrix * vec4(aScreenCoords, 0.0, 1.0);
texCoords = aTexCoords; texCoords = aTexCoords;
} }

View file

@ -66,11 +66,16 @@ int main(int argc, char* argv[]) {
); );
ui::font_set_default(&scapeFont); 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; bool running = true;
while(running) { while(running) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1, 1, 1, 1);
text.Render(); text.Render();

View file

@ -47,8 +47,9 @@ void sosc::shdr::Shader::AttachSource
std::ifstream file(fileName); std::ifstream file(fileName);
std::stringstream ss; std::stringstream ss;
ss << file.rdbuf(); 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); glShaderSource(shader, 1, (const GLchar**)&src, nullptr);
glCompileShader(shader); glCompileShader(shader);
@ -80,7 +81,7 @@ void sosc::shdr::Shader::LoadUniforms(std::vector<std::string> names) {
this->locations.clear(); this->locations.clear();
for(const auto& name : names) { for(const auto& name : names) {
if((id = glGetUniformLocation(this->program, name.c_str())) == -1) if((id = glGetUniformLocation(this->program, name.c_str())) == -1)
throw ShaderUniformException(name); 0;//throw ShaderUniformException(name);
this->locations.push_back(id); this->locations.push_back(id);
} }

View file

@ -44,7 +44,7 @@ protected:
this->LinkProgram(); this->LinkProgram();
this->LoadUniforms({ this->LoadUniforms({
"orthoMatrix", "orthoMatrix",
"transMatrix" "transMatrix",
"fontBitmap", "fontBitmap",
"fontColor" "fontColor"
}); });
@ -84,8 +84,9 @@ void sosc::ui::font_deinit_subsystem() {
sosc::ui::Font::Font sosc::ui::Font::Font
(const std::string& bitmapPath, const std::string& dataPath) (const std::string& bitmapPath, const std::string& dataPath)
{ {
this->loaded = false;
if(!this->Load(bitmapPath, dataPath)) if(!this->Load(bitmapPath, dataPath))
throw new FontException(bitmapPath, dataPath); throw FontException(bitmapPath, dataPath);
} }
bool sosc::ui::Font::Load bool sosc::ui::Font::Load
@ -182,6 +183,8 @@ void sosc::ui::Font::Unload() {
sosc::ui::Text::Text() { sosc::ui::Text::Text() {
this->font = _font_ctx.default_font; this->font = _font_ctx.default_font;
this->font_size = 0; this->font_size = 0;
this->vertices = nullptr;
this->tex_coords = nullptr;
glGenVertexArrays(1, &this->vao); glGenVertexArrays(1, &this->vao);
glGenBuffers(2, this->vbos); glGenBuffers(2, this->vbos);
@ -280,12 +283,19 @@ void sosc::ui::Text::Render() {
void sosc::ui::Text::Destroy() { void sosc::ui::Text::Destroy() {
glDeleteBuffers(2, this->vbos); glDeleteBuffers(2, this->vbos);
glDeleteVertexArrays(1, &this->vao); glDeleteVertexArrays(1, &this->vao);
delete[] this->vertices;
delete[] this->tex_coords;
} }
void sosc::ui::Text::Redraw() { void sosc::ui::Text::Redraw() {
this->vertex_count = (GLuint)(6 * this->text.length()); 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; uint32_t line_width = 0, top_x = 0, top_y = 0;
for(int i = 0; i < this->text.length(); ++i) { for(int i = 0; i < this->text.length(); ++i) {
@ -300,62 +310,62 @@ void sosc::ui::Text::Redraw() {
/// TRIANGLE 1 /// /// TRIANGLE 1 ///
// TOP LEFT // TOP LEFT
vertices[i*12] = top_x; this->vertices[i*12] = top_x;
vertices[i*12 + 1] = top_y; this->vertices[i*12 + 1] = top_y;
tex_coords[i*12] = glyph.top_left.x; this->tex_coords[i*12] = glyph.top_left.x;
tex_coords[i*12 + 1] = glyph.top_left.y; this->tex_coords[i*12 + 1] = glyph.top_left.y;
// TOP RIGHT // TOP RIGHT
vertices[i*12 + 2] = top_x + width; this->vertices[i*12 + 2] = top_x + width;
vertices[i*12 + 3] = top_y; this->vertices[i*12 + 3] = top_y;
tex_coords[i*12 + 2] = glyph.top_right.x; this->tex_coords[i*12 + 2] = glyph.top_right.x;
tex_coords[i*12 + 3] = glyph.top_right.y; this->tex_coords[i*12 + 3] = glyph.top_right.y;
// BOTTOM LEFT // BOTTOM LEFT
vertices[i*12 + 4] = top_x; this->vertices[i*12 + 4] = top_x;
vertices[i*12 + 5] = top_y + height; this->vertices[i*12 + 5] = top_y + height;
tex_coords[i*12 + 4] = glyph.bottom_left.x; this->tex_coords[i*12 + 4] = glyph.bottom_left.x;
tex_coords[i*12 + 5] = glyph.bottom_left.y; this->tex_coords[i*12 + 5] = glyph.bottom_left.y;
/// TRIANGLE 2 /// /// TRIANGLE 2 ///
// BOTTOM LEFT // BOTTOM LEFT
vertices[i*12 + 6] = top_x; this->vertices[i*12 + 6] = top_x;
vertices[i*12 + 7] = top_y + height; this->vertices[i*12 + 7] = top_y + height;
tex_coords[i*12 + 6] = glyph.bottom_left.x; this->tex_coords[i*12 + 6] = glyph.bottom_left.x;
tex_coords[i*12 + 7] = glyph.bottom_left.y; this->tex_coords[i*12 + 7] = glyph.bottom_left.y;
// TOP RIGHT // TOP RIGHT
vertices[i*12 + 8] = top_x + width; this->vertices[i*12 + 8] = top_x + width;
vertices[i*12 + 9] = top_y; this->vertices[i*12 + 9] = top_y;
tex_coords[i*12 + 8] = glyph.top_right.x; this->tex_coords[i*12 + 8] = glyph.top_right.x;
tex_coords[i*12 + 9] = glyph.top_right.y; this->tex_coords[i*12 + 9] = glyph.top_right.y;
// BOTTOM RIGHT // BOTTOM RIGHT
vertices[i*12 + 10] = top_x + width; this->vertices[i*12 + 10] = top_x + width;
vertices[i*12 + 11] = top_y + height; this->vertices[i*12 + 11] = top_y + height;
tex_coords[i*12 + 10] = glyph.bottom_right.x; this->tex_coords[i*12 + 10] = glyph.bottom_right.x;
tex_coords[i*12 + 11] = glyph.bottom_right.y; this->tex_coords[i*12 + 11] = glyph.bottom_right.y;
top_x += width; top_x += width;
} }
glBindVertexArray(this->vao); glBindVertexArray(this->vao);
{
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, this->vbos[0]); glBindBuffer(GL_ARRAY_BUFFER, this->vbos[0]);
glBufferData( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
this->vertex_count * 2 * sizeof(float), this->vertex_count * 2 * sizeof(float),
vertices, this->vertices,
GL_STATIC_DRAW GL_STATIC_DRAW
); );
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, this->vbos[1]); glBindBuffer(GL_ARRAY_BUFFER, this->vbos[1]);
glBufferData( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
this->vertex_count * 2 * sizeof(float), this->vertex_count * 2 * sizeof(float),
vertices, this->tex_coords,
GL_STATIC_DRAW GL_STATIC_DRAW
); );
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
}
glBindVertexArray(0); glBindVertexArray(0);
} }

View file

@ -105,7 +105,10 @@ private:
wrap_width; wrap_width;
std::string text; std::string text;
glm::mat4 trans_matrix; glm::mat4 trans_matrix;
GLsizei vertex_count; GLsizei vertex_count;
float* vertices;
float* tex_coords;
GLuint vao, vbos[2]; GLuint vao, vbos[2];
}; };
}} }}