-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
-
-SHADER_DEFINE( vg_line_shader,
-
- // VERTEX
- "layout (location=0) in vec2 a_co;"
- "layout (location=1) in vec4 a_colour;"
- "uniform mat3 uPv;"
- ""
- "out vec4 s_colour;"
- ""
- "void main()"
- "{"
- " vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
- " s_colour = a_colour;"
- " gl_Position = vert_pos;"
- "}",
-
- // FRAGMENT
- "out vec4 FragColor;"
- ""
- "in vec4 s_colour;"
- ""
- "void main()"
- "{"
- " FragColor = s_colour;"
- "}"
- ,
- UNIFORMS({ "uPv" })
-)
-
-struct
-{
- struct vg_lines_vert
- {
- v2f co;
- u32 colour;
- }
- *buffer;
-
- GLuint vao, vbo;
- u32 draw_idx, cap, buffer_size;
-}
-vg_lines;
-
-static void vg_lines_init(void)
-{
- SHADER_INIT( vg_line_shader );
-
- glGenVertexArrays( 1, &vg_lines.vao );
- glGenBuffers( 1, &vg_lines.vbo );
- glBindVertexArray( vg_lines.vao );
-
- glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
-
- vg_lines.cap = 10000;
- vg_lines.buffer_size = vg_lines.cap * sizeof( struct vg_lines_vert );
-
- glBufferData( GL_ARRAY_BUFFER, vg_lines.buffer_size, NULL, GL_DYNAMIC_DRAW );
- glBindVertexArray( vg_lines.vao );
-
- glVertexAttribPointer(
- 0,
- sizeof( vg_lines.buffer[0].co ) / sizeof(float),
- GL_FLOAT,
- GL_FALSE,
- sizeof( struct vg_lines_vert ),
- (void *)0
- );
- glEnableVertexAttribArray( 0 );
-
- glVertexAttribPointer(
- 1,
- 4,
- GL_UNSIGNED_BYTE,
- GL_TRUE,
- sizeof( struct vg_lines_vert ),
- (void*)(offsetof( struct vg_lines_vert, colour ))
- );
- glEnableVertexAttribArray( 1 );
- vg_lines.buffer = malloc( vg_lines.buffer_size );
-}
-
-static void vg_lines_free(void)
-{
- glDeleteVertexArrays( 1, &vg_lines.vao );
- glDeleteBuffers( 1, &vg_lines.vbo );
-
- free( vg_lines.buffer );
-}
-
-static void vg_lines_drawall(float* projection)
-{
- SHADER_USE( vg_line_shader );
- glUniformMatrix3fv( SHADER_UNIFORM( vg_line_shader, "uPv" ), 1, GL_FALSE, projection );
-
- glBindVertexArray( vg_lines.vao );
- glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
-
- glBufferSubData( GL_ARRAY_BUFFER, 0, vg_lines.draw_idx * sizeof(struct vg_lines_vert), vg_lines.buffer );
- glEnable( GL_BLEND );
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- glBlendEquation( GL_FUNC_ADD );
-
- glDrawArrays( GL_LINES, 0, vg_lines.draw_idx );
-
- glDisable( GL_BLEND );
- vg_lines.draw_idx = 0;
-}
-
-static void vg_line2( v2f from, v2f to, u32 fc, u32 tc )
-{
- struct vg_lines_vert *v = vg_lines.buffer + vg_lines.draw_idx;
- v2_copy( from, v[0].co );
- v2_copy( to, v[1].co );
- v[0].colour = fc;
- v[1].colour = tc;
-
- vg_lines.draw_idx += 2;
-}
-
-static void vg_line( v2f from, v2f to, u32 colour )
-{
- vg_line2( from, to, colour, colour );
-}
-
-static void vg_line_box( v2f min, v2f max, u32 colour )
-{
- vg_line( min, (v2f){min[0],max[1]}, colour );
- vg_line( (v2f){min[0],max[1]}, max, colour );
- vg_line( max, (v2f){max[0],min[1]}, colour );
- vg_line( (v2f){max[0],min[1]}, min, colour );
-}