now thats a lot of damage!
[vg.git] / src / vg / vg_lines.h
index 40e2886161e97463c26653e274eb5d6b4c7fd24b..58e0e142d84f20e937d4cbc6e0ce8f6365404ec4 100644 (file)
@@ -5,13 +5,7 @@
 
 #include "vg/vg.h"
 
-static int debug_lines_enable = 0;
-
-#ifdef VG_3D
- typedef v3f line_co;
-#else
- typedef v2f line_co;
-#endif
+typedef v3f line_co;
 
 static struct vg_shader _shader_lines = 
 {
@@ -22,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;"
        "}"
@@ -60,32 +45,29 @@ static struct vg_shader _shader_lines =
    }
 };
 
+
 struct
 {
-       struct vg_lines_vert
-       {
-#ifdef VG_3D
+   u32 draw;
+       
+   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
@@ -100,18 +82,19 @@ static void vg_lines_init(void)
       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 );
+      u32 size = 50000 * sizeof( struct vg_lines_vert );
+
+      vg_lines.vertex_buffer = 
+         vg_create_linear_allocator( vg_mem.rtmemory, size );
       
-      glBufferData( GL_ARRAY_BUFFER, vg_lines.buffer_size, 
-                    NULL, GL_DYNAMIC_DRAW );
+      glBufferData( GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
       glBindVertexArray( vg_lines.vao );
       VG_CHECK_GL_ERR();
 
       /* Pointers */
       glVertexAttribPointer( 
          0, 
-         sizeof( vg_lines.buffer[0].co ) / sizeof(float), 
+         3,
          GL_FLOAT, 
          GL_FALSE, 
          sizeof( struct vg_lines_vert ), 
@@ -128,88 +111,57 @@ static void vg_lines_init(void)
          (void*)(offsetof( struct vg_lines_vert, colour ))
       );
       glEnableVertexAttribArray( 1 );
-
+      
       VG_CHECK_GL_ERR();
-
-      /* Alloc RAM */
-      vg_lines.buffer = vg_alloc( vg_lines.buffer_size );
       vg_success( "done\n" );
    }
 
    vg_release_thread_sync();
 }
 
-static void vg_lines_free(void *nothing)
-{
-       glDeleteVertexArrays( 1, &vg_lines.vao );
-       glDeleteBuffers( 1, &vg_lines.vbo );
-       vg_free( vg_lines.buffer );
-}
-
-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];
+   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;
 
@@ -239,7 +191,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;
 
@@ -281,7 +233,7 @@ static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour )
    vg_line( p100, p010, colour );
 }
 
-static void vg_line_cross(v3f pos,u32 colour, float scale)
+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 );
@@ -295,7 +247,7 @@ static void vg_line_cross(v3f pos,u32 colour, float scale)
    vg_line( p0, p1, colour );
 }
 
-static void vg_line_pt3( v3f pt, float size, u32 colour )
+VG_STATIC void vg_line_pt3( v3f pt, float size, u32 colour )
 {
    boxf box =
    {