rigidbody view
[vg.git] / vg_lines.h
index c44f0713cd9eab045fd92736b5bf96b742f693eb..8fff226106e7bc2a007611636226a339910ac294 100644 (file)
@@ -6,6 +6,10 @@
 #define VG_GAME
 #include "vg/vg.h"
 
+/*
+ * FIXME: The line buffer sometimes overflows. Low priority
+ */
+
 typedef v3f line_co;
 
 #define VG__RED   0xff0000ff
@@ -55,8 +59,8 @@ static struct vg_shader _shader_lines = {
 };
 
 struct{
-   u32 draw,
-       allow_input;
+   u32 enabled, 
+       render;
        
    struct vg_lines_vert{
       v3f co;
@@ -68,18 +72,15 @@ struct{
 }
 static vg_lines;
 
-VG_STATIC void async_vg_lines_init( void *payload, u32 payload_size ){
+#define VG_LINES_BUFFER_SIZE 50000 * sizeof( struct vg_lines_vert )
+
+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 );
    
-   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 );
+   glBufferData( GL_ARRAY_BUFFER, VG_LINES_BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW );
    glBindVertexArray( vg_lines.vao );
    VG_CHECK_GL_ERR();
 
@@ -105,19 +106,21 @@ VG_STATIC void async_vg_lines_init( void *payload, u32 payload_size ){
    glEnableVertexAttribArray( 1 );
    
    VG_CHECK_GL_ERR();
-   vg_lines.allow_input = 1;
 }
 
-VG_STATIC void vg_lines_init(void){
+static void vg_lines_init(void){
+   vg_lines.vertex_buffer = 
+      vg_create_linear_allocator( vg_mem.rtmemory, 
+                                  VG_LINES_BUFFER_SIZE, VG_MEMORY_REALTIME);
+
    vg_async_call( async_vg_lines_init, NULL, 0 );
 
-   vg_console_reg_var( "vg_lines", &vg_lines.draw, k_var_dtype_i32, 
+   vg_console_reg_var( "vg_lines", &vg_lines.render, k_var_dtype_i32, 
                        VG_VAR_CHEAT );
    vg_shader_register( &_shader_lines );
-
 }
 
-VG_STATIC void vg_lines_drawall( void ){
+static void vg_lines_drawall( void ){
        glUseProgram( _shader_lines.id );
 
    glUniformMatrix4fv( glGetUniformLocation( _shader_lines.id, "uPv" ), 
@@ -127,23 +130,21 @@ VG_STATIC void vg_lines_drawall( void ){
        glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
 
    u32 bufusage = vg_linear_get_cur(vg_lines.vertex_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( vg_lines.draw )
+   if( vg_lines.render )
       glDrawArrays( GL_LINES, 0, bufusage / sizeof(struct vg_lines_vert) );
        
        glDisable( GL_BLEND );
    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;
-   if( !vg_lines.draw ) return;
+static void vg_line2( line_co from, line_co to, u32 fc, u32 tc ){
+   if( !vg_lines.enabled ) return;
 
    u32 size = 2 * sizeof(struct vg_lines_vert);
        struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size );
@@ -155,11 +156,15 @@ 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 ){
+static void vg_line( line_co from, line_co to, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
        vg_line2( from, to, colour, colour );
 }
 
-VG_STATIC void vg_line_arrow( line_co co, line_co dir, float size, u32 colour ){
+static void vg_line_arrow( line_co co, line_co dir, float size, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
    v3f p1, tx, ty, p2, p3;
    v3_muladds( co, dir, size, p1 );
    v3_tangent_basis( dir, tx, ty );
@@ -173,7 +178,9 @@ 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_box_verts( boxf box, v3f verts[8] ){
+static void vg_line_box_verts( boxf box, v3f verts[8] ){
+   if( !vg_lines.enabled ) return;
+
    for( u32 i=0; i<8; i++ ){
       for( u32 j=0; j<3; j++ ){
          verts[i][j] = i&(0x1<<j)? box[1][j]: box[0][j];
@@ -181,14 +188,18 @@ VG_STATIC void vg_line_box_verts( boxf box, v3f verts[8] ){
    }
 }
 
-VG_STATIC void vg_line_mesh( v3f verts[], u32 indices[][2], u32 indice_count,
+static void vg_line_mesh( v3f verts[], u32 indices[][2], u32 indice_count,
                              u32 colour ){
+   if( !vg_lines.enabled ) return;
+
    for( u32 i=0; i<indice_count; i++ ){
       vg_line( verts[indices[i][0]], verts[indices[i][1]], colour );
    }
 }
 
-VG_STATIC void vg_line_boxf( boxf box, u32 colour ){
+static void vg_line_boxf( boxf box, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
    v3f verts[8];
    vg_line_box_verts( box, verts );
    u32 indices[][2] = {{0,1},{1,3},{3,2},{2,0},
@@ -198,7 +209,9 @@ VG_STATIC void vg_line_boxf( boxf box, u32 colour ){
    vg_line_mesh( verts, indices, vg_list_size(indices), colour );
 }
 
-VG_STATIC void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ){
+static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
    v3f verts[8];
    vg_line_box_verts( box, verts );
 
@@ -213,7 +226,9 @@ VG_STATIC void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ){
    vg_line_mesh( verts, indices, vg_list_size(indices), colour );
 }
 
-VG_STATIC void vg_line_cross(v3f pos,u32 colour, float scale){
+static void vg_line_cross(v3f pos,u32 colour, float scale){
+   if( !vg_lines.enabled ) return;
+
    v3f p0, p1;
    v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 );
    v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 );
@@ -226,7 +241,9 @@ VG_STATIC void vg_line_cross(v3f pos,u32 colour, float scale){
    vg_line( p0, p1, colour );
 }
 
-VG_STATIC void vg_line_point( v3f pt, float size, u32 colour ){
+static void vg_line_point( v3f pt, float size, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
    boxf box =
    {
       { pt[0]-size, pt[1]-size, pt[2]-size },
@@ -237,7 +254,9 @@ VG_STATIC void vg_line_point( v3f pt, float size, u32 colour ){
 }
 
 
-VG_STATIC void vg_line_sphere( m4x3f m, float radius, u32 colour ){
+static void vg_line_sphere( m4x3f m, float radius, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
    v3f ly = { 0.0f, 0.0f, radius },
        lx = { 0.0f, radius, 0.0f },
        lz = { 0.0f, 0.0f, radius };
@@ -269,7 +288,9 @@ VG_STATIC void vg_line_sphere( m4x3f m, float radius, u32 colour ){
    }
 }
 
-VG_STATIC void vg_line_capsule( m4x3f m, float radius, float h, u32 colour ){
+static void vg_line_capsule( m4x3f m, float radius, float h, u32 colour ){
+   if( !vg_lines.enabled ) return;
+   
    v3f ly = { 0.0f, 0.0f, radius },
        lx = { 0.0f, radius, 0.0f },
        lz = { 0.0f, 0.0f, radius };