diff options
Diffstat (limited to 'source/main.cpp')
-rw-r--r-- | source/main.cpp | 185 |
1 files changed, 108 insertions, 77 deletions
diff --git a/source/main.cpp b/source/main.cpp index 3458820..ecba4c9 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -3,7 +3,6 @@ #include <SDL2/SDL.h> #include <glad/glad.h> #include <vector> -#include <map> #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" @@ -310,7 +309,6 @@ Vec3 camera_look_around(r32 angle_pitch, r32 angle_yaw) } struct TextChar { - u32 texture_id; Vec2 size; Vec2 bearing; s64 advance; @@ -413,7 +411,7 @@ int main(int argc, char* argv[]) height, SDL_WINDOW_OPENGL); - SDL_SetRelativeMouseMode(SDL_TRUE); + //SDL_SetRelativeMouseMode(SDL_TRUE); // create an opengl context SDL_GLContext context = SDL_GL_CreateContext(window); @@ -431,7 +429,7 @@ int main(int argc, char* argv[]) } // vsync controls: 0 = OFF | 1 = ON (Default) - SDL_GL_SetSwapInterval(1); + SDL_GL_SetSwapInterval(0); // filesystem playground stuff size_t read_count; @@ -447,11 +445,31 @@ int main(int argc, char* argv[]) // ==================== // setup_text // debug only - std::map<char, TextChar> all_text_chars; - - FT_Set_Pixel_Sizes(face, 0, 48); + u32 chunk_size = 32; + Mat4* text_transforms = (Mat4*)malloc(chunk_size*sizeof(Mat4)); + s32* char_indexes = (s32*)malloc(chunk_size*sizeof(s32)); + TextChar* all_text_chars = (TextChar*)malloc(128*sizeof(TextChar)); + FT_Set_Pixel_Sizes(face, 256, 256); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + u32 texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D_ARRAY, texture); + // generate texture + glTexImage3D( + GL_TEXTURE_2D_ARRAY, + 0, + GL_R8, + 256, + 256, + 128, + 0, + GL_RED, + GL_UNSIGNED_BYTE, + 0 + ); + // generate characters for (unsigned char c = 0; c < 128; c++) { @@ -461,43 +479,39 @@ int main(int argc, char* argv[]) } else { - // generate texture - u32 texture; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RED, - face->glyph->bitmap.width, - face->glyph->bitmap.rows, - 0, - GL_RED, - GL_UNSIGNED_BYTE, - face->glyph->bitmap.buffer + glTexSubImage3D( + GL_TEXTURE_2D_ARRAY, + 0, + 0, 0, // x, y offset + int(c), + face->glyph->bitmap.width, + face->glyph->bitmap.rows, + 1, + GL_RED, + GL_UNSIGNED_BYTE, + face->glyph->bitmap.buffer ); // set texture options - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); TextChar tc = { - texture, Vec2{(r32)face->glyph->bitmap.width, (r32)face->glyph->bitmap.rows}, Vec2{(r32)face->glyph->bitmap_left, (r32)face->glyph->bitmap_top}, face->glyph->advance.x }; - all_text_chars.insert(std::pair<char, TextChar>(c, tc)); + all_text_chars[c] = tc; } - } + glBindTexture(GL_TEXTURE_2D_ARRAY, 0); + // @note: this data is used for GL_TRIANGLE_STRIP + // as such the order for vertices for this is AntiCW -> CW -> AntiCW + // that can be seen in this array as it goes from ACW -> CW r32 vertices[] = { - 0.0f, 0.0f, 0.0f, 1.0f, - 1.0f, 1.0f, - 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f @@ -508,11 +522,10 @@ int main(int argc, char* argv[]) glGenBuffers(1, &vbo); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, 6*5*sizeof(r32), NULL, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(r32), 0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(r32), (void*)(3*sizeof(r32))); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); // ==================== END setup_text @@ -537,10 +550,10 @@ int main(int argc, char* argv[]) Mat4 view = camera_create4m(camera_pos, camera_look, preset_up_dir); Mat4 proj = perspective4m((r32)To_Radian(90.0), (r32)width / (r32)height, 0.1f, 1000.0f); - Vec2 camera_res = {1024.0f, 768.0f}; + Vec2 camera_res = {(r32)width, (r32)height}; r32 ortho_cam_height = 2.0f; // CameraOrtho ortho_cam = camera_orthographic(ortho_cam_height, Vec2{camera_res.x, camera_res.y}, 0.1f, 1000.0f); - Mat4 ortho_proj = orthographic_projection4m(0.0f, 1024.0f, 0.0f, 768.0f, 0.1f, 1000.0f); + Mat4 ortho_proj = orthographic_projection4m(0.0f, (r32)width, 0.0f, (r32)height, 0.1f, 1000.0f); // objects Vec3 model_translations[] = { @@ -551,7 +564,7 @@ int main(int argc, char* argv[]) Vec3{ -2.5, 0.0, -0.5}, // 4: square to the left Vec3{ -1.0, 0.0, -1.5}, // 5: random square behind window between squares Vec3{ -1.0, 0.0, -8.0}, // 6: reflective plane - Vec3{ -1.0, 2.0, -8.0}, // 6: refractive "window" + Vec3{ -1.0, 2.0, -8.0}, // 7: refractive "window" }; s32 proj_loc; @@ -564,10 +577,10 @@ int main(int argc, char* argv[]) //glUniformMatrix4fv(tex_quad_shader.proj_loc, 1, GL_TRUE, ortho_cam.projection.buffer); glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + //glEnable(GL_CULL_FACE); + //glCullFace(GL_BACK); + //glEnable(GL_BLEND); + //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); u8 game_running = true; @@ -701,29 +714,33 @@ int main(int argc, char* argv[]) // OUTPUT glClearColor(1.0f, 0.6f, .6f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // glDisable(GL_BLEND); - - //gl2d_draw_tex_quad(tex_quad_shader, tex_quad1, model_translations[1], GL2D_UScale(3.0f), To_Radian(-5.0f)); - //gl2d_draw_tex_quad(tex_quad_shader, tex_quad2, model_translations[0], GL2D_UScale(1.0f), 0.0f); - // gl_text_render(ui_tex_sp, // shader program - // Vec3{startx, starty, zindex}, // coords - // "hello, sailor!\nhow do you do?"); // text to render { - // render ui text + glDisable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glUseProgram(ui_text_sp); s32 text_color_loc = glGetUniformLocation(ui_text_sp, "TextColor"); - glUniform3f(text_color_loc, 0.0f, 0.8f, 0.0f); - glActiveTexture(GL_TEXTURE0); + glUniform3f(text_color_loc, 0.0f, 0.0f, 0.0f); glBindVertexArray(vao); + glBindTexture(GL_TEXTURE_2D_ARRAY, texture); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glActiveTexture(GL_TEXTURE0); + + const char text_to_render[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellentesque fringilla mauris vitae convallis. \nSuspendisse potenti. Morbi at eros nulla. Morbi pellentesque mollis diam, nec eleifend sapien commodo nec. \nAenean elementum, eros non mattis consectetur, odio nulla posuere orci, sed posuere nibh nisl suscipit orci. Donec lectus dolor, rhoncus at vulputate sit amet, pellentesque non velit. \nVivamus vitae lectus lacinia, feugiat elit non, malesuada diam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. \nNulla porttitor cursus nulla, sit amet pulvinar nibh tempus sed. Nulla quis luctus tellus. Suspendisse efficitur tellus vel augue tempor, sed gravida purus consectetur. \nSed congue rutrum elit nec aliquet.\n\n" +"In hac habitasse platea dictumst. Nulla nec pellentesque purus, vitae interdum eros. Pellentesque pretium dui sed erat commodo finibus. \nInteger fringilla tincidunt orci, vel mattis dolor eleifend id. Sed condimentum lorem sit amet enim porttitor dictum nec ut arcu. \nMauris mauris ex, varius in sem eget, semper hendrerit enim. Curabitur vel vehicula metus, vel luctus nunc. \nPraesent et molestie diam, non faucibus ligula. Duis elementum nunc erat, eget dictum mi faucibus id.\n\n" +"Nulla non vehicula erat, vitae tincidunt turpis. In tincidunt dolor vel augue tincidunt, vitae feugiat leo facilisis. Nullam auctor sodales sodales. \nDuis augue nibh, scelerisque id faucibus sit amet, ultricies eget lorem. Integer dapibus ultricies dolor sit amet mollis. \nQuisque eros sem, bibendum non convallis quis, vulputate ac erat. Nam laoreet justo id dui feugiat, condimentum elementum dolor pellentesque. \nVivamus imperdiet metus et mattis laoreet. Ut eget scelerisque neque. Praesent mollis, nisl quis volutpat eleifend, purus sem suscipit ex, \na molestie elit risus vel velit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. \nProin vitae nibh ac leo tempor vehicula quis quis nulla. Donec sodales sapien et nulla varius vulputate. \nPhasellus ac eleifend dui.\n\n" +"Nulla nec nibh ut mauris accumsan tristique. Nam iaculis pellentesque elit, sit amet tristique erat dignissim ac. \nDuis gravida dolor eget sem fringilla, ac consequat ipsum malesuada. In facilisis mattis nibh in bibendum. \nUt congue gravida bibendum. Phasellus egestas consectetur sem, ornare pellentesque mauris. Ut pretium eleifend dui ut feugiat. \nCurabitur ut faucibus nisi, ac laoreet tortor.\n\n" +"Nulla interdum vehicula tempus. Curabitur bibendum nisl eget sem tristique, at aliquam magna rutrum. \nLorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tempus posuere urna et auctor. \nVestibulum lectus risus, aliquet eget placerat nec, iaculis et augue. \nMauris vel nulla eu urna pretium vestibulum in vel elit. Donec sollicitudin dui felis, sed auctor nibh aliquam \nsit amet. Pellentesque eget elementum lacus, suscipit facilisis nisi. Integer tempus velit nulla. \nSed ullamcorper, eros et consequat pharetra, eros ipsum varius odio, vitae commodo lectus libero at libero. \nCurabitur sed elit tincidunt tellus tempor convallis et eu nunc. \nAliquam blandit auctor ipsum, eu mattis leo viverra vel. Integer feugiat dui neque, vitae vulputate lectus congue non. \nFusce molestie vel ligula nec sodales. Integer vestibulum molestie porttitor.\n\n"; - const char text_to_render[] = "hello, sailor!\nhow does your journey go?"; - r32 startx = 512.0f; - r32 starty = 700.0f; + u32 running_index = 0; + r32 startx = 10.0f; + r32 starty = 740.0f; r32 linex = startx; r32 zindex = 3.0f; - r32 scale = 1.0f; + r32 scale = 1.0f * 22.0f / 256.0f; r32 max_size = 0.0f; for (u32 i = 0; i < sizeof(text_to_render) - 1; i++) { @@ -745,37 +762,51 @@ int main(int argc, char* argv[]) continue; } r32 xpos = linex + (scale * rc.bearing.x); - r32 ypos = starty - (rc.size.y - rc.bearing.y) * scale; - - r32 w = scale * rc.size.x; - r32 h = scale * rc.size.y; + r32 ypos = starty - (256.0f - rc.bearing.y) * scale; - // update VBO for each character - float vertices[6][5] = { - // z-index to render on (part of layering) - { xpos, ypos + h, -zindex, 0.0f, 0.0f }, - { xpos, ypos, -zindex, 0.0f, 1.0f }, - { xpos + w, ypos, -zindex, 1.0f, 1.0f }, + r32 w = scale * 256.0f; + r32 h = scale * 256.0f; - { xpos, ypos + h, -zindex, 0.0f, 0.0f }, - { xpos + w, ypos, -zindex, 1.0f, 1.0f }, - { xpos + w, ypos + h, -zindex, 1.0f, 0.0f } - }; + Mat4 tm = translation_matrix4m(xpos, ypos, -zindex); + Mat4 sm = scaling_matrix4m(w, h, 0); + Mat4 model_mat = multiply4m(tm, sm); + text_transforms[running_index] = model_mat; + char_indexes[running_index] = int(c); - glBindTexture(GL_TEXTURE_2D, rc.texture_id); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - glDrawArrays(GL_TRIANGLES, 0, 6); linex += (rc.advance >> 6) * scale; + running_index++; + if (running_index > chunk_size-1) { + { + r32 letter_transforms_loc = glGetUniformLocation(ui_text_sp, "LetterTransforms"); + glUniformMatrix4fv(letter_transforms_loc, chunk_size, GL_TRUE, &text_transforms[0].buffer[0]); + r32 texture_map_loc = glGetUniformLocation(ui_text_sp, "TextureMap"); + glUniform1iv(texture_map_loc, chunk_size, char_indexes); + glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, chunk_size); + running_index = 0; + memset(text_transforms, 0, chunk_size); + memset(char_indexes, 0, chunk_size); + } + } + } + if (running_index > 0) { + r32 letter_transforms_loc = glGetUniformLocation(ui_text_sp, "LetterTransforms"); + glUniformMatrix4fv(letter_transforms_loc, chunk_size, GL_TRUE, &text_transforms[0].buffer[0]); + r32 texture_map_loc = glGetUniformLocation(ui_text_sp, "TextureMap"); + glUniform1iv(texture_map_loc, chunk_size, char_indexes); + glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, chunk_size); } - r32 test = 1; + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D_ARRAY, 0); } SDL_GL_SwapWindow(window); } + free(text_transforms); + free(char_indexes); + free(all_text_chars); // opengl free calls // it is up to the renderer to free everything here // of course I have not bothered to set that up so it does not matter |