X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=src%2Fvg%2Fvg_lines.h;h=672a59e84d663b76fa407310d7ca0bc57e27c2e8;hb=3dd767bb10e6fee9cbffeb185d1a9685810c17b5;hp=e14d3381d3e4bf65942601afcb79a53ee052bb45;hpb=deb49cc59884e298467c497d46518243ddb5a209;p=vg.git diff --git a/src/vg/vg_lines.h b/src/vg/vg_lines.h index e14d338..672a59e 100644 --- a/src/vg/vg_lines.h +++ b/src/vg/vg_lines.h @@ -1,12 +1,11 @@ /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ -static int debug_lines_enable = 1; +#ifndef VG_LINES_H +#define VG_LINES_H -#ifdef VG_3D - typedef v3f line_co; -#else - typedef v2f line_co; -#endif +#include "vg/vg.h" + +typedef v3f line_co; static struct vg_shader _shader_lines = { @@ -17,24 +16,15 @@ static struct vg_shader _shader_lines = .orig_file = NULL, .static_src = -#ifdef VG_3D "uniform mat4 uPv;" "layout (location=0) in vec3 a_co;" -#else - "uniform mat3 uPv;" - "layout (location=0) in vec2 a_co;" -#endif "layout (location=1) in vec4 a_colour;" "" "out vec4 s_colour;" "" "void main()" "{" -#ifdef VG_3D " vec4 vert_pos = uPv * vec4( a_co, 1.0 );" -#else - " vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );" -#endif " s_colour = a_colour;" " gl_Position = vert_pos;" "}" @@ -55,143 +45,128 @@ static struct vg_shader _shader_lines = } }; + struct { - struct vg_lines_vert - { -#ifdef VG_3D + u32 draw, + allow_input; + + struct vg_lines_vert + { v3f co; -#else - v2f co; -#endif + u32 colour; + } + *vertex_buffer; - u32 colour; - } - *buffer; - GLuint vao, vbo; - u32 draw_idx, cap, buffer_size; } -vg_lines; +static vg_lines; -static void vg_lines_init(void) +VG_STATIC void vg_lines_init(void) { + vg_info( "vg_lines_init\n" ); + vg_convar_push( (struct vg_convar){ - .name = "debug_lines", - .data = &debug_lines_enable, + .name = "vg_lines", + .data = &vg_lines.draw, .data_type = k_convar_dtype_i32, .opt_i32 = { .min=0, .max=1, .clamp=1 }, .persistent = 1 }); vg_shader_register( &_shader_lines ); - - glGenVertexArrays( 1, &vg_lines.vao ); - glGenBuffers( 1, &vg_lines.vbo ); - glBindVertexArray( vg_lines.vao ); - - glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); - - vg_lines.cap = 50000; - 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 ); + vg_acquire_thread_sync(); + { + glGenVertexArrays( 1, &vg_lines.vao ); + glGenBuffers( 1, &vg_lines.vbo ); + glBindVertexArray( vg_lines.vao ); + glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); + + u32 size = 50000 * sizeof( struct vg_lines_vert ); + + vg_lines.vertex_buffer = + vg_create_linear_allocator(vg_mem.rtmemory, size, VG_MEMORY_REALTIME); + + glBufferData( GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW ); + glBindVertexArray( vg_lines.vao ); + VG_CHECK_GL_ERR(); + + /* Pointers */ + glVertexAttribPointer( + 0, + 3, + 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_CHECK_GL_ERR(); + vg_success( "done\n" ); + } + + vg_release_thread_sync(); + vg_lines.allow_input = 1; } -static void vg_lines_drawall( float* projection ) +VG_STATIC void vg_lines_drawall( float* projection ) { glUseProgram( _shader_lines.id ); -#ifdef VG_3D glUniformMatrix4fv -#else - glUniformMatrix3fv -#endif ( glGetUniformLocation( _shader_lines.id, "uPv" ), 1, GL_FALSE, projection ); glBindVertexArray( vg_lines.vao ); glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); + + u32 bufusage = vg_linear_get_cur(vg_lines.vertex_buffer); - glBufferSubData( GL_ARRAY_BUFFER, 0, vg_lines.draw_idx * - sizeof(struct vg_lines_vert), vg_lines.buffer ); + glBufferSubData( GL_ARRAY_BUFFER, 0, bufusage, vg_lines.vertex_buffer ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glBlendEquation( GL_FUNC_ADD ); - if( debug_lines_enable ) - glDrawArrays( GL_LINES, 0, vg_lines.draw_idx ); + if( vg_lines.draw ) + glDrawArrays( GL_LINES, 0, bufusage / sizeof(struct vg_lines_vert) ); glDisable( GL_BLEND ); - vg_lines.draw_idx = 0; + vg_linear_clear( vg_lines.vertex_buffer ); } -static void vg_line2( line_co from, line_co to, u32 fc, u32 tc ) +VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc ) { - struct vg_lines_vert *v = &vg_lines.buffer[vg_lines.draw_idx]; + if( !vg_lines.allow_input ) + return; + + u32 size = 2 * sizeof(struct vg_lines_vert); + struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size ); -#ifdef VG_3D v3_copy( from, v[0].co ); v3_copy( to, v[1].co ); -#else - v2_copy( from, v[0].co ); - v2_copy( to, v[1].co ); -#endif v[0].colour = fc; v[1].colour = tc; - - vg_lines.draw_idx += 2; } -static void vg_line( line_co from, line_co to, u32 colour ) +VG_STATIC void vg_line( line_co from, line_co to, u32 colour ) { vg_line2( from, to, colour, colour ); } -static void vg_line_box( line_co min, line_co max, u32 colour ) -{ -#ifdef VG_3D - /* TODO... */ -#else - 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 ); -#endif -} - -static void vg_line_boxf( boxf box, u32 colour ) +VG_STATIC void vg_line_boxf( boxf box, u32 colour ) { v3f p000, p001, p010, p011, p100, p101, p110, p111; @@ -221,7 +196,7 @@ static void vg_line_boxf( boxf box, u32 colour ) vg_line( p111, p011, colour ); } -static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ) +VG_STATIC void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ) { v3f p000, p001, p010, p011, p100, p101, p110, p111; @@ -263,7 +238,21 @@ static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ) vg_line( p100, p010, colour ); } -static void vg_line_pt3( v3f pt, float size, u32 colour ) +VG_STATIC void vg_line_cross(v3f pos,u32 colour, float scale) +{ + v3f p0, p1; + v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 ); + v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 ); + vg_line( p0, p1, colour ); + v3_add( (v3f){0.0f, scale,0.0f}, pos, p0 ); + v3_add( (v3f){0.0f,-scale,0.0f}, pos, p1 ); + vg_line( p0, p1, colour ); + v3_add( (v3f){0.0f,0.0f, scale}, pos, p0 ); + v3_add( (v3f){0.0f,0.0f,-scale}, pos, p1 ); + vg_line( p0, p1, colour ); +} + +VG_STATIC void vg_line_pt3( v3f pt, float size, u32 colour ) { boxf box = { @@ -273,3 +262,5 @@ static void vg_line_pt3( v3f pt, float size, u32 colour ) vg_line_boxf( box, colour ); } + +#endif /* VG_LINES_H */