various bugs
[vg.git] / vg_lines.h
index e2a79fee65efc3540d32300a72c6449e4314840a..7077e737b6719960ccc3a151cffbca990fae0c55 100644 (file)
@@ -8,12 +8,21 @@
 
 typedef v3f line_co;
 
-static struct vg_shader _shader_lines = 
-{
+#define VG__RED   0xff0000ff
+#define VG__GREEN 0xff00ff00
+#define VG__BLUE  0xffff0000
+#define VG__WHITE 0xffffffff
+#define VG__BLACK 0xff000000
+#define VG__CLEAR 0x00ffffff
+#define VG__PINK  0xffff00ff
+#define VG__YELOW 0xff00ffff
+#define VG__CYAN  0xffffff00
+#define VG__NONE  0x00000000
+
+static struct vg_shader _shader_lines = {
    .name = "[vg] lines",
    .link = NULL,
-   .vs = 
-   {
+   .vs = {
       .orig_file = NULL,
       .static_src = 
 
@@ -30,8 +39,7 @@ static struct vg_shader _shader_lines =
        "       gl_Position = vert_pos;"
        "}"
    },
-   .fs = 
-   {
+   .fs = {
       .orig_file = NULL,
       .static_src = 
 
@@ -46,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;
    } 
@@ -63,71 +68,63 @@ struct
 }
 static vg_lines;
 
-VG_STATIC void vg_lines_init(void)
+VG_STATIC void async_vg_lines_init( void *payload, u32 payload_size )
 {
-   vg_info( "vg_lines_init\n" );
-
-   vg_convar_push( (struct vg_convar){
-      .name = "vg_lines",
-      .data = &vg_lines.draw,
-      .data_type = k_convar_dtype_i32,
-      .opt_i32 = { .min=0, .max=1, .clamp=1 },
-      .persistent = 1
-   });
+   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 );
@@ -149,8 +146,8 @@ VG_STATIC void vg_lines_drawall( float* projection )
 
 VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc )
 {
-   if( !vg_lines.allow_input )
-      return;
+   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 );
@@ -167,6 +164,39 @@ 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 )
+{
+   v3f p1, tx, ty, p2, p3;
+   v3_muladds( co, dir, size, p1 );
+   line_tangent_basis( dir, tx, ty );
+
+   v3_muladds( p1, dir, -size * 0.125f, p2 );
+   v3_muladds( p2, ty,  size * 0.125f, p3 );
+   v3_muladds( p2, ty, -size * 0.125f, p2 );
+
+   vg_line( co, p1, colour );
+   vg_line( p1, p2, 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;