-/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
-#ifndef VG_LINES_H
-#define VG_LINES_H
-
-#define VG_GAME
-#include "vg/vg.h"
-
-/*
- * FIXME: The line buffer sometimes overflows. Low priority
- */
+#pragma once
+#include "vg_platform.h"
+#include "vg_engine.h"
typedef v3f line_co;
#define VG__CYAN 0xffffff00
#define VG__NONE 0x00000000
-static struct vg_shader _shader_lines = {
- .name = "[vg] lines",
- .link = NULL,
- .vs = {
- .orig_file = NULL,
- .static_src =
-
- "uniform mat4 uPv;"
- "layout (location=0) in vec3 a_co;"
- "layout (location=1) in vec4 a_colour;"
- ""
- "out vec4 s_colour;"
- ""
- "void main()"
- "{"
- " vec4 vert_pos = uPv * vec4( a_co, 1.0 );"
- " s_colour = a_colour;"
- " gl_Position = vert_pos;"
- "}"
- },
- .fs = {
- .orig_file = NULL,
- .static_src =
-
- "out vec4 FragColor;"
- ""
- "in vec4 s_colour;"
- ""
- "void main()"
- "{"
- " FragColor = s_colour;"
- "}"
- }
-};
-
-struct{
- u32 draw,
- allow_input;
+struct vg_lines
+{
+ u32 enabled,
+ render;
struct vg_lines_vert{
v3f co;
GLuint vao, vbo;
}
-static vg_lines;
-
-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 );
- 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;
-}
-
-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 );
-
-}
-
-static void vg_lines_drawall( void ){
- glUseProgram( _shader_lines.id );
-
- glUniformMatrix4fv( glGetUniformLocation( _shader_lines.id, "uPv" ),
- 1, GL_FALSE, (float *)vg.pv );
-
- 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, 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 )
- glDrawArrays( GL_LINES, 0, bufusage / sizeof(struct vg_lines_vert) );
-
- glDisable( GL_BLEND );
- vg_linear_clear( vg_lines.vertex_buffer );
-}
-
-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;
-
- u32 size = 2 * sizeof(struct vg_lines_vert);
- struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size );
-
- v3_copy( from, v[0].co );
- v3_copy( to, v[1].co );
-
- v[0].colour = fc;
- v[1].colour = tc;
-}
-
-static void vg_line( line_co from, line_co to, u32 colour ){
- vg_line2( from, to, colour, colour );
-}
-
-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 );
- v3_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 );
-}
-
-static void vg_line_box_verts( boxf box, v3f verts[8] ){
- 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];
- }
- }
-}
-
-static void vg_line_mesh( v3f verts[], u32 indices[][2], u32 indice_count,
- u32 colour ){
- 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 ){
- v3f verts[8];
- vg_line_box_verts( box, verts );
- u32 indices[][2] = {{0,1},{1,3},{3,2},{2,0},
- {4,5},{5,7},{7,6},{6,4},
- {4,0},{5,1},{6,2},{7,3}};
-
- vg_line_mesh( verts, indices, vg_list_size(indices), colour );
-}
-
-static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ){
- v3f verts[8];
- vg_line_box_verts( box, verts );
-
- for( u32 i=0; i<8; i++ ){
- m4x3_mulv( m, verts[i], verts[i] );
- }
-
- u32 indices[][2] = {{0,1},{1,3},{3,2},{2,0},
- {4,5},{5,7},{7,6},{6,4},
- {4,0},{5,1},{6,2},{7,3}};
-
- vg_line_mesh( verts, indices, vg_list_size(indices), colour );
-}
-
-static void vg_line_cross(v3f pos,u32 colour, float scale){
- v3f p0, p1;
- v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 );
- v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 );
- vg_line( p0, p1, colour );
- v3_add( (v3f){0.0f, scale,0.0f}, pos, p0 );
- v3_add( (v3f){0.0f,-scale,0.0f}, pos, p1 );
- vg_line( p0, p1, colour );
- v3_add( (v3f){0.0f,0.0f, scale}, pos, p0 );
- v3_add( (v3f){0.0f,0.0f,-scale}, pos, p1 );
- vg_line( p0, p1, colour );
-}
-
-static void vg_line_point( v3f pt, float size, u32 colour ){
- boxf box =
- {
- { pt[0]-size, pt[1]-size, pt[2]-size },
- { pt[0]+size, pt[1]+size, pt[2]+size }
- };
-
- vg_line_boxf( box, colour );
-}
-
-
-static void vg_line_sphere( m4x3f m, float radius, u32 colour ){
- v3f ly = { 0.0f, 0.0f, radius },
- lx = { 0.0f, radius, 0.0f },
- lz = { 0.0f, 0.0f, radius };
-
- for( int i=0; i<16; i++ ){
- float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
- s = sinf(t),
- c = cosf(t);
-
- v3f py = { s*radius, 0.0f, c*radius },
- px = { s*radius, c*radius, 0.0f },
- pz = { 0.0f, s*radius, c*radius };
-
- v3f p0, p1, p2, p3, p4, p5;
- m4x3_mulv( m, py, p0 );
- m4x3_mulv( m, ly, p1 );
- m4x3_mulv( m, px, p2 );
- m4x3_mulv( m, lx, p3 );
- m4x3_mulv( m, pz, p4 );
- m4x3_mulv( m, lz, p5 );
-
- vg_line( p0, p1, colour == 0x00? 0xff00ff00: colour );
- vg_line( p2, p3, colour == 0x00? 0xff0000ff: colour );
- vg_line( p4, p5, colour == 0x00? 0xffff0000: colour );
-
- v3_copy( py, ly );
- v3_copy( px, lx );
- v3_copy( pz, lz );
- }
-}
-
-static void vg_line_capsule( m4x3f m, float radius, float h, u32 colour ){
- v3f ly = { 0.0f, 0.0f, radius },
- lx = { 0.0f, radius, 0.0f },
- lz = { 0.0f, 0.0f, radius };
-
- float s0 = sinf(0.0f)*radius,
- c0 = cosf(0.0f)*radius;
-
- v3f p0, p1, up, right, forward;
- m3x3_mulv( m, (v3f){0.0f,1.0f,0.0f}, up );
- m3x3_mulv( m, (v3f){1.0f,0.0f,0.0f}, right );
- m3x3_mulv( m, (v3f){0.0f,0.0f,-1.0f}, forward );
- v3_muladds( m[3], up, -h*0.5f+radius, p0 );
- v3_muladds( m[3], up, h*0.5f-radius, p1 );
-
- v3f a0, a1, b0, b1;
- v3_muladds( p0, right, radius, a0 );
- v3_muladds( p1, right, radius, a1 );
- v3_muladds( p0, forward, radius, b0 );
- v3_muladds( p1, forward, radius, b1 );
- vg_line( a0, a1, colour );
- vg_line( b0, b1, colour );
-
- v3_muladds( p0, right, -radius, a0 );
- v3_muladds( p1, right, -radius, a1 );
- v3_muladds( p0, forward, -radius, b0 );
- v3_muladds( p1, forward, -radius, b1 );
- vg_line( a0, a1, colour );
- vg_line( b0, b1, colour );
-
- for( int i=0; i<16; i++ ){
- float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
- s1 = sinf(t)*radius,
- c1 = cosf(t)*radius;
-
- v3f e0 = { s0, 0.0f, c0 },
- e1 = { s1, 0.0f, c1 },
- e2 = { s0, c0, 0.0f },
- e3 = { s1, c1, 0.0f },
- e4 = { 0.0f, c0, s0 },
- e5 = { 0.0f, c1, s1 };
-
- m3x3_mulv( m, e0, e0 );
- m3x3_mulv( m, e1, e1 );
- m3x3_mulv( m, e2, e2 );
- m3x3_mulv( m, e3, e3 );
- m3x3_mulv( m, e4, e4 );
- m3x3_mulv( m, e5, e5 );
-
- v3_add( p0, e0, a0 );
- v3_add( p0, e1, a1 );
- v3_add( p1, e0, b0 );
- v3_add( p1, e1, b1 );
-
- vg_line( a0, a1, colour );
- vg_line( b0, b1, colour );
-
- if( c0 < 0.0f ){
- v3_add( p0, e2, a0 );
- v3_add( p0, e3, a1 );
- v3_add( p0, e4, b0 );
- v3_add( p0, e5, b1 );
- }
- else{
- v3_add( p1, e2, a0 );
- v3_add( p1, e3, a1 );
- v3_add( p1, e4, b0 );
- v3_add( p1, e5, b1 );
- }
-
- vg_line( a0, a1, colour );
- vg_line( b0, b1, colour );
-
- s0 = s1;
- c0 = c1;
- }
-}
-
-#endif /* VG_LINES_H */
+extern vg_lines;
+
+void vg_line_capsule( m4x3f m, float radius, float h, u32 colour );
+void vg_line_sphere( m4x3f m, float radius, u32 colour );
+void vg_line_point( v3f pt, float size, u32 colour );
+void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour );
+void vg_line_boxf( boxf box, u32 colour );
+void vg_line_mesh( v3f verts[], u32 indices[][2], u32 indice_count,u32 colour );
+void vg_line_box_verts( boxf box, v3f verts[8] );
+void vg_line_cross(v3f pos,u32 colour, float scale);
+void vg_line_arrow( line_co co, line_co dir, float size, u32 colour );
+void vg_line( line_co from, line_co to, u32 colour );
+void vg_line2( line_co from, line_co to, u32 fc, u32 tc );
+void vg_lines_drawall( void );
+void vg_lines_init(void);