rigidbody view
[vg.git] / vg_lines.h
index 569ae169739507e88419233708bbe83597227f1a..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;
 
+#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,16 +106,18 @@ static void async_vg_lines_init( void *payload, u32 payload_size ){
    glEnableVertexAttribArray( 1 );
    
    VG_CHECK_GL_ERR();
-   vg_lines.allow_input = 1;
 }
 
 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 );
-
 }
 
 static void vg_lines_drawall( void ){
@@ -127,14 +130,13 @@ 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 );
@@ -142,8 +144,7 @@ static void vg_lines_drawall( void ){
 }
 
 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;
+   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 );
@@ -156,10 +157,14 @@ static void vg_line2( line_co from, line_co to, u32 fc, u32 tc ){
 }
 
 static void vg_line( line_co from, line_co to, u32 colour ){
+   if( !vg_lines.enabled ) return;
+
        vg_line2( from, to, colour, 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 );
@@ -174,6 +179,8 @@ static void vg_line_arrow( line_co co, line_co dir, float size, u32 colour ){
 }
 
 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];
@@ -183,12 +190,16 @@ static void vg_line_box_verts( boxf box, v3f verts[8] ){
 
 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 );
    }
 }
 
 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},
@@ -199,6 +210,8 @@ static void vg_line_boxf( 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 );
 
@@ -214,6 +227,8 @@ static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ){
 }
 
 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 );
@@ -227,6 +242,8 @@ static void vg_line_cross(v3f pos,u32 colour, float scale){
 }
 
 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 },
@@ -238,6 +255,8 @@ static void vg_line_point( v3f pt, float size, 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 };
@@ -270,6 +289,8 @@ static void vg_line_sphere( m4x3f m, float radius, 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 };