diff --git a/code/render/2d.cpp b/code/render/2d.cpp index 069f142..dbe3c46 100644 --- a/code/render/2d.cpp +++ b/code/render/2d.cpp @@ -17,10 +17,18 @@ void r_2d_immediate_segment(v2 a, v2 b, v4 a_color, v4 b_color, f32 thickness) glUniform1i(r_render_state.shader_2d.has_texture[0], 0); // Vertex buffer data - GLuint gl_VAO, gl_VBO; - glGenVertexArrays(1, &gl_VAO); + static GLuint gl_VAO, gl_VBO; + static bool init = false; + if(!init) + { + init = true; + + glGenVertexArrays(1, &gl_VAO); + glBindVertexArray(gl_VAO); + glGenBuffers(1, &gl_VBO); + glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); + } glBindVertexArray(gl_VAO); - glGenBuffers(1, &gl_VBO); glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); f32 data[12] = { a.x, a.y, b.x, b.y, a_color.r, a_color.g, a_color.b, a_color.a, b_color.r, b_color.g, b_color.b, b_color.a }; @@ -36,8 +44,8 @@ void r_2d_immediate_segment(v2 a, v2 b, v4 a_color, v4 b_color, f32 thickness) glDrawArrays(GL_LINES, 0, 2); // Deinit - glDeleteBuffers(1, &gl_VBO); - glDeleteVertexArrays(1, &gl_VAO); + // glDeleteBuffers(1, &gl_VBO); + // glDeleteVertexArrays(1, &gl_VAO); glEnable(GL_DEPTH_TEST); } @@ -51,10 +59,18 @@ void r_2d_immediate_polygonal_chain(u64 count, v2 *vertices, v4 color, f32 thick glUniform1i(r_render_state.shader_2d.has_texture[0], 0); // Vertex buffer data - GLuint gl_VAO, gl_VBO; - glGenVertexArrays(1, &gl_VAO); + static GLuint gl_VAO, gl_VBO; + static bool init = false; + if(!init) + { + init = true; + + glGenVertexArrays(1, &gl_VAO); + glBindVertexArray(gl_VAO); + glGenBuffers(1, &gl_VBO); + glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); + } glBindVertexArray(gl_VAO); - glGenBuffers(1, &gl_VBO); glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); glBufferData(GL_ARRAY_BUFFER, count*sizeof(v2), vertices, GL_STATIC_DRAW); @@ -69,8 +85,8 @@ void r_2d_immediate_polygonal_chain(u64 count, v2 *vertices, v4 color, f32 thick glDrawArrays(GL_LINE_STRIP, 0, count); // Deinit - glDeleteBuffers(1, &gl_VBO); - glDeleteVertexArrays(1, &gl_VAO); + // glDeleteBuffers(1, &gl_VBO); + // glDeleteVertexArrays(1, &gl_VAO); glEnable(GL_DEPTH_TEST); } @@ -85,12 +101,21 @@ void r_2d_immediate_triangle(v2 a, v2 b, v2 c, v4 a_color, v4 b_color, v4 c_colo set_texture_in_shader(&r_render_state.shader_2d, texture); // Vertex buffer data - GLuint gl_VAO, gl_VBO; - glGenVertexArrays(1, &gl_VAO); + static GLuint gl_VAO, gl_VBO; + static bool init = false; + if(!init) + { + init = true; + + glGenVertexArrays(1, &gl_VAO); + glBindVertexArray(gl_VAO); + glGenBuffers(1, &gl_VBO); + glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); + } glBindVertexArray(gl_VAO); - glGenBuffers(1, &gl_VBO); glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); + f32 data[24] = { a.x, a.y, b.x, b.y, c.x, c.y, a_color.r, a_color.g, a_color.b, a_color.a, @@ -111,8 +136,8 @@ void r_2d_immediate_triangle(v2 a, v2 b, v2 c, v4 a_color, v4 b_color, v4 c_colo glDrawArrays(GL_TRIANGLES, 0, 3); // Deinit - glDeleteBuffers(1, &gl_VBO); - glDeleteVertexArrays(1, &gl_VAO); + // glDeleteBuffers(1, &gl_VBO); + // glDeleteVertexArrays(1, &gl_VAO); glEnable(GL_DEPTH_TEST); } @@ -127,10 +152,18 @@ void r_2d_immediate_quad(v2 a, v2 b, v2 c, v2 d, v4 color, v2 a_uv, v2 b_uv, v2 set_texture_in_shader(&r_render_state.shader_2d, texture); // Vertex buffer data - GLuint gl_VAO, gl_VBO; - glGenVertexArrays(1, &gl_VAO); + static GLuint gl_VAO, gl_VBO; + static bool init = false; + if(!init) + { + init = true; + + glGenVertexArrays(1, &gl_VAO); + glBindVertexArray(gl_VAO); + glGenBuffers(1, &gl_VBO); + glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); + } glBindVertexArray(gl_VAO); - glGenBuffers(1, &gl_VBO); glBindBuffer(GL_ARRAY_BUFFER, gl_VBO); f32 data[16] = { @@ -150,8 +183,8 @@ void r_2d_immediate_quad(v2 a, v2 b, v2 c, v2 d, v4 color, v2 a_uv, v2 b_uv, v2 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Deinit - glDeleteBuffers(1, &gl_VBO); - glDeleteVertexArrays(1, &gl_VAO); + // glDeleteBuffers(1, &gl_VBO); + // glDeleteVertexArrays(1, &gl_VAO); glEnable(GL_DEPTH_TEST); } @@ -224,12 +257,12 @@ void r_2d_immediate_rounded_rectangle(Rect r, f32 radius, v4 color) }; u32 corner_offset = 30; // Corner semicircles - f32 factor = TAU * .25 / num_of_segments; v2 inner_vertices[4] = {i0, i1, i2, i3}; for(u32 quadrant = 0; quadrant < 4; quadrant++) { for(u32 i = quadrant*num_of_segments; i < (quadrant+1)*num_of_segments; i++) { + f32 factor = TAU * .25 / num_of_segments; v2 inner = inner_vertices[quadrant]; v2 a = inner + radius * v2{cos( i * factor), -sin( i * factor)}; v2 b = inner + radius * v2{cos((i+1) * factor), -sin((i+1) * factor)}; @@ -252,11 +285,18 @@ void r_2d_immediate_mesh(u64 count, v2 *vertices, v4 color, v2 *uvs, r_texture * set_texture_in_shader(&r_render_state.shader_2d, texture); // Vertex buffer data - GLuint gl_VAO, gl_vertices, gl_uvs; - glGenVertexArrays(1, &gl_VAO); + static GLuint gl_VAO, gl_vertices, gl_uvs; + static bool init = false; + if(!init) + { + init = true; + + glGenVertexArrays(1, &gl_VAO); + glBindVertexArray(gl_VAO); + glGenBuffers(1, &gl_vertices); + glGenBuffers(1, &gl_uvs); + } glBindVertexArray(gl_VAO); - glGenBuffers(1, &gl_vertices); - glGenBuffers(1, &gl_uvs); glBindBuffer(GL_ARRAY_BUFFER, gl_vertices); glBufferData(GL_ARRAY_BUFFER, count * sizeof(v2), vertices, GL_STATIC_DRAW); @@ -273,9 +313,9 @@ void r_2d_immediate_mesh(u64 count, v2 *vertices, v4 color, v2 *uvs, r_texture * glDrawArrays(GL_TRIANGLES, 0, count); // Deinitvoid r_2d_draw_mesh(r_2d_mesh *mesh, r_texture *texture); - glDeleteBuffers(1, &gl_vertices); - glDeleteBuffers(1, &gl_uvs); - glDeleteVertexArrays(1, &gl_VAO); + // glDeleteBuffers(1, &gl_vertices); + // glDeleteBuffers(1, &gl_uvs); + // glDeleteVertexArrays(1, &gl_VAO); glEnable(GL_DEPTH_TEST); } @@ -325,17 +365,22 @@ void r_2d_immediate_rounded_rectangle_outline(Rect r, f32 radius, v4 color, f32 u32 v_index = 1; // Corner semicircles - f32 factor = TAU * .25 / num_of_segments; for(u32 quadrant = 0; quadrant < 4; quadrant++) { v2 inner = inner_vertices[quadrant]; - for(u32 i = quadrant*num_of_segments; i < (quadrant+1)*num_of_segments; i++) + v2 offset = {0, 0}; + if(num_of_segments > 0) { - v2 a = inner + radius * v2{cos( i * factor), -sin( i * factor)}; - vertices[v_index] = a; - v_index++; + f32 factor = TAU * .25 / num_of_segments; + for(u32 i = quadrant*num_of_segments; i < (quadrant+1)*num_of_segments; i++) + { + v2 a = inner + radius * v2{cos( i * factor), -sin( i * factor)}; + vertices[v_index] = a; + v_index++; + } + offset = radius * v2{cos( (quadrant+1)*num_of_segments * factor), -sin( (quadrant+1)*num_of_segments * factor)}; } - vertices[v_index] = inner + radius * v2{cos( (quadrant+1)*num_of_segments * factor), -sin( (quadrant+1)*num_of_segments * factor)}; + vertices[v_index] = inner + offset; v_index++; }