1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 SHADER_DEFINE( vg_line_shader
,
6 "layout (location=0) in vec2 a_co;"
7 "layout (location=1) in vec4 a_colour;"
14 " vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
15 " s_colour = a_colour;"
16 " gl_Position = vert_pos;"
26 " FragColor = s_colour;"
42 u32 draw_idx
, cap
, buffer_size
;
46 static void vg_lines_init(void)
48 SHADER_INIT( vg_line_shader
);
50 glGenVertexArrays( 1, &vg_lines
.vao
);
51 glGenBuffers( 1, &vg_lines
.vbo
);
52 glBindVertexArray( vg_lines
.vao
);
54 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
57 vg_lines
.buffer_size
= vg_lines
.cap
* sizeof( struct vg_lines_vert
);
59 glBufferData( GL_ARRAY_BUFFER
, vg_lines
.buffer_size
, NULL
, GL_DYNAMIC_DRAW
);
60 glBindVertexArray( vg_lines
.vao
);
62 glVertexAttribPointer(
64 sizeof( vg_lines
.buffer
[0].co
) / sizeof(float),
67 sizeof( struct vg_lines_vert
),
70 glEnableVertexAttribArray( 0 );
72 glVertexAttribPointer(
77 sizeof( struct vg_lines_vert
),
78 (void*)(offsetof( struct vg_lines_vert
, colour
))
80 glEnableVertexAttribArray( 1 );
81 vg_lines
.buffer
= malloc( vg_lines
.buffer_size
);
84 static void vg_lines_free(void)
86 glDeleteVertexArrays( 1, &vg_lines
.vao
);
87 glDeleteBuffers( 1, &vg_lines
.vbo
);
89 free( vg_lines
.buffer
);
92 static void vg_lines_drawall(float* projection
)
94 SHADER_USE( vg_line_shader
);
95 glUniformMatrix3fv( SHADER_UNIFORM( vg_line_shader
, "uPv" ), 1, GL_FALSE
, projection
);
97 glBindVertexArray( vg_lines
.vao
);
98 glBindBuffer( GL_ARRAY_BUFFER
, vg_lines
.vbo
);
100 glBufferSubData( GL_ARRAY_BUFFER
, 0, vg_lines
.draw_idx
* sizeof(struct vg_lines_vert
), vg_lines
.buffer
);
101 glEnable( GL_BLEND
);
102 glBlendFunc( GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
103 glBlendEquation( GL_FUNC_ADD
);
105 glDrawArrays( GL_LINES
, 0, vg_lines
.draw_idx
);
107 glDisable( GL_BLEND
);
108 vg_lines
.draw_idx
= 0;
111 static void vg_line2( v2f from
, v2f to
, u32 fc
, u32 tc
)
113 struct vg_lines_vert
*v
= vg_lines
.buffer
+ vg_lines
.draw_idx
;
114 v2_copy( from
, v
[0].co
);
115 v2_copy( to
, v
[1].co
);
119 vg_lines
.draw_idx
+= 2;
122 static void vg_line( v2f from
, v2f to
, u32 colour
)
124 vg_line2( from
, to
, colour
, colour
);
127 static void vg_line_box( v2f min
, v2f max
, u32 colour
)
129 vg_line( min
, (v2f
){min
[0],max
[1]}, colour
);
130 vg_line( (v2f
){min
[0],max
[1]}, max
, colour
);
131 vg_line( max
, (v2f
){max
[0],min
[1]}, colour
);
132 vg_line( (v2f
){max
[0],min
[1]}, min
, colour
);