#include "vg/vg.h"
-static int debug_lines_enable = 1;
-
-#ifdef VG_3D
- typedef v3f line_co;
-#else
- typedef v2f line_co;
-#endif
+typedef v3f line_co;
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;"
"}"
}
};
+
struct
{
- struct vg_lines_vert
- {
-#ifdef VG_3D
+ u32 draw,
+ allow_input;
+
+ 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 int 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
});
vg_shader_register( &_shader_lines );
-
- if( vg_acquire_thread_sync(1) )
+ 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 );
- 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, VG_MEMORY_REALTIME);
- 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 );
-
- if( VG_CHECK_GL_ERR() )
- {
- vg_release_thread_sync(1);
- vg_error( "Failed\n" );
- }
+ 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 ),
(void*)(offsetof( struct vg_lines_vert, colour ))
);
glEnableVertexAttribArray( 1 );
-
- if( VG_CHECK_GL_ERR() )
- goto il_delete_buffers_and_fail;
-
- /* Alloc RAM */
- vg_lines.buffer = malloc( vg_lines.buffer_size );
- if( vg_lines.buffer )
- {
- vg_success( "done\n" );
- vg_release_thread_sync(1);
- return 1;
- }
-
-il_delete_buffers_and_fail:
- glDeleteBuffers( 1, &vg_lines.vbo );
- glDeleteVertexArrays( 1, &vg_lines.vao );
- vg_release_thread_sync(1);
- return 0;
+
+ VG_CHECK_GL_ERR();
+ vg_success( "done\n" );
}
- return 0;
+ vg_release_thread_sync();
+ vg_lines.allow_input = 1;
}
-static void vg_lines_free(void *nothing)
-{
- glDeleteVertexArrays( 1, &vg_lines.vao );
- glDeleteBuffers( 1, &vg_lines.vbo );
- 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];
+ if( !vg_lines.allow_input )
+ return;
+
+ 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;
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;
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 );
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 =
{