X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg_lines.h;h=c44f0713cd9eab045fd92736b5bf96b742f693eb;hb=5e457ededb92af6948a2a7a0d09f09eab2bb697c;hp=9300445a8b35dc21816a76571ba10256e7d55cdd;hpb=0aea2ef68a5ed32fc940673402a1b4b67f38d4d3;p=vg.git diff --git a/vg_lines.h b/vg_lines.h index 9300445..c44f071 100644 --- a/vg_lines.h +++ b/vg_lines.h @@ -19,12 +19,10 @@ typedef v3f line_co; #define VG__CYAN 0xffffff00 #define VG__NONE 0x00000000 -static struct vg_shader _shader_lines = -{ +static struct vg_shader _shader_lines = { .name = "[vg] lines", .link = NULL, - .vs = - { + .vs = { .orig_file = NULL, .static_src = @@ -41,8 +39,7 @@ static struct vg_shader _shader_lines = " gl_Position = vert_pos;" "}" }, - .fs = - { + .fs = { .orig_file = NULL, .static_src = @@ -57,14 +54,11 @@ static struct vg_shader _shader_lines = } }; - -struct -{ +struct{ u32 draw, allow_input; - struct vg_lines_vert - { + struct vg_lines_vert{ v3f co; u32 colour; } @@ -74,71 +68,60 @@ struct } static vg_lines; -VG_STATIC void vg_lines_init(void) -{ - vg_info( "vg_lines_init\n" ); - - vg_var_push( (struct vg_var){ - .name = "vg_lines", - .data = &vg_lines.draw, - .data_type = k_var_dtype_i32, - .opt_i32 = { .min=0, .max=1, .clamp=1 }, - .persistent = 1 - }); +VG_STATIC void async_vg_lines_init( void *payload, u32 payload_size ){ + glGenVertexArrays( 1, &vg_lines.vao ); + glGenBuffers( 1, &vg_lines.vbo ); + glBindVertexArray( vg_lines.vao ); + glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); - vg_shader_register( &_shader_lines ); + u32 size = 50000 * sizeof( struct vg_lines_vert ); - 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.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_lines.allow_input = 1; } -VG_STATIC void vg_lines_drawall( float* projection ) -{ +VG_STATIC void vg_lines_init(void){ + vg_async_call( async_vg_lines_init, NULL, 0 ); + + vg_console_reg_var( "vg_lines", &vg_lines.draw, k_var_dtype_i32, + VG_VAR_CHEAT ); + vg_shader_register( &_shader_lines ); + +} + +VG_STATIC void vg_lines_drawall( void ){ glUseProgram( _shader_lines.id ); - glUniformMatrix4fv - ( glGetUniformLocation( _shader_lines.id, "uPv" ), 1, GL_FALSE, projection ); + glUniformMatrix4fv( glGetUniformLocation( _shader_lines.id, "uPv" ), + 1, GL_FALSE, (float *)vg.pv ); glBindVertexArray( vg_lines.vao ); glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); @@ -158,10 +141,9 @@ VG_STATIC void vg_lines_drawall( float* projection ) vg_linear_clear( vg_lines.vertex_buffer ); } -VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc ) -{ - if( !vg_lines.allow_input ) - return; +VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc ){ + if( !vg_lines.allow_input ) return; + if( !vg_lines.draw ) return; u32 size = 2 * sizeof(struct vg_lines_vert); struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size ); @@ -173,36 +155,14 @@ VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc ) v[1].colour = tc; } -VG_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 ); } -VG_STATIC void line_tangent_basis( v3f n, v3f tx, v3f ty ) -{ - /* Compute tangent basis (box2d) */ - if( fabsf( n[0] ) >= 0.57735027f ) - { - tx[0] = n[1]; - tx[1] = -n[0]; - tx[2] = 0.0f; - } - else - { - tx[0] = 0.0f; - tx[1] = n[2]; - tx[2] = -n[1]; - } - - v3_normalize( tx ); - v3_cross( n, tx, ty ); -} - -VG_STATIC void vg_line_arrow( line_co co, line_co dir, float size, u32 colour ) -{ +VG_STATIC void vg_line_arrow( line_co co, line_co dir, float size, u32 colour ){ v3f p1, tx, ty, p2, p3; v3_muladds( co, dir, size, p1 ); - line_tangent_basis( dir, tx, ty ); + v3_tangent_basis( dir, tx, ty ); v3_muladds( p1, dir, -size * 0.125f, p2 ); v3_muladds( p2, ty, size * 0.125f, p3 ); @@ -213,80 +173,47 @@ VG_STATIC void vg_line_arrow( line_co co, line_co dir, float size, u32 colour ) vg_line( p1, p3, colour ); } -VG_STATIC void vg_line_boxf( boxf box, u32 colour ) -{ - v3f p000, p001, p010, p011, p100, p101, p110, p111; +VG_STATIC void vg_line_box_verts( boxf box, v3f verts[8] ){ + for( u32 i=0; i<8; i++ ){ + for( u32 j=0; j<3; j++ ){ + verts[i][j] = i&(0x1<